2 /******************************************************************************
4 * Module Name: asfile - Main module for the acpi source processor utility
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
119 /* Local prototypes */
123 ACPI_CONVERSION_TABLE *ConversionTable,
131 AsDetectLoneLineFeeds (
135 static ACPI_INLINE int
136 AsMaxInt (int a, int b)
138 return (a > b ? a : b);
142 /******************************************************************************
144 * FUNCTION: AsDoWildcard
146 * DESCRIPTION: Process files via wildcards
148 ******************************************************************************/
152 ACPI_CONVERSION_TABLE *ConversionTable,
163 char RequestedFileType;
166 if (FileType == FILE_TYPE_DIRECTORY)
168 RequestedFileType = REQUEST_DIR_ONLY;
172 RequestedFileType = REQUEST_FILE_ONLY;
175 VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
176 WildcardSpec, SourcePath));
178 /* Open the directory for wildcard search */
180 DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
184 * Get all of the files that match both the
185 * wildcard and the requested file type
187 while ((Filename = AcpiOsGetNextFilename (DirInfo)))
189 /* Looking for directory files, must check file type */
191 switch (RequestedFileType)
193 case REQUEST_DIR_ONLY:
195 /* If we actually have a dir, process the subtree */
197 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
198 &SourceDirPath, &TargetDirPath))
200 VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
202 AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
203 free (SourceDirPath);
204 free (TargetDirPath);
208 case REQUEST_FILE_ONLY:
210 /* Otherwise, this is a file, not a directory */
212 VERBOSE_PRINT (("File: %s\n", Filename));
214 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
215 MaxPathLength, Filename, FileType);
225 AcpiOsCloseDirectory (DirInfo);
230 /******************************************************************************
232 * FUNCTION: AsProcessTree
234 * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
235 * ".H" are processed as the tree is traversed.
237 ******************************************************************************/
241 ACPI_CONVERSION_TABLE *ConversionTable,
248 MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
250 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
252 if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
257 VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
258 if (mkdir (TargetPath))
262 printf ("Could not create target directory\n");
268 /* Do the C source files */
270 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
271 FILE_TYPE_SOURCE, "*.c");
273 /* Do the C header files */
275 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
276 FILE_TYPE_HEADER, "*.h");
278 /* Do the Lex file(s) */
280 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
281 FILE_TYPE_SOURCE, "*.l");
283 /* Do the yacc file(s) */
285 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
286 FILE_TYPE_SOURCE, "*.y");
288 /* Do any ASL files */
290 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
291 FILE_TYPE_HEADER, "*.asl");
293 /* Do any subdirectories */
295 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
296 FILE_TYPE_DIRECTORY, "*");
302 /******************************************************************************
304 * FUNCTION: AsDetectLoneLineFeeds
306 * DESCRIPTION: Find LF without CR.
308 ******************************************************************************/
311 AsDetectLoneLineFeeds (
317 UINT32 LineCount = 0;
327 if (Buffer[i] == 0x0A)
329 if (Buffer[i-1] != 0x0D)
340 if (LineCount == LfCount)
342 if (!Gbl_IgnoreLoneLineFeeds)
344 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
350 printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
359 /******************************************************************************
361 * FUNCTION: AsConvertFile
363 * DESCRIPTION: Perform the requested transforms on the file buffer (as
364 * determined by the ConversionTable and the FileType).
366 ******************************************************************************/
370 ACPI_CONVERSION_TABLE *ConversionTable,
373 ACPI_NATIVE_INT FileType)
377 ACPI_STRING_TABLE *StringTable;
378 ACPI_IDENTIFIER_TABLE *ConditionalTable;
379 ACPI_IDENTIFIER_TABLE *LineTable;
380 ACPI_IDENTIFIER_TABLE *MacroTable;
381 ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
386 case FILE_TYPE_SOURCE:
387 Functions = ConversionTable->SourceFunctions;
388 StringTable = ConversionTable->SourceStringTable;
389 LineTable = ConversionTable->SourceLineTable;
390 ConditionalTable = ConversionTable->SourceConditionalTable;
391 MacroTable = ConversionTable->SourceMacroTable;
392 StructTable = ConversionTable->SourceStructTable;
395 case FILE_TYPE_HEADER:
396 Functions = ConversionTable->HeaderFunctions;
397 StringTable = ConversionTable->HeaderStringTable;
398 LineTable = ConversionTable->HeaderLineTable;
399 ConditionalTable = ConversionTable->HeaderConditionalTable;
400 MacroTable = ConversionTable->HeaderMacroTable;
401 StructTable = ConversionTable->HeaderStructTable;
405 printf ("Unknown file type, cannot process\n");
410 Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
412 VERBOSE_PRINT (("Processing %u bytes\n",
413 (unsigned int) strlen (FileBuffer)));
415 if (ConversionTable->LowerCaseTable)
417 for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
419 AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
424 /* Process all the string replacements */
428 for (i = 0; StringTable[i].Target; i++)
430 AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
431 StringTable[i].Type, FileBuffer);
437 for (i = 0; LineTable[i].Identifier; i++)
439 AsRemoveLine (FileBuffer, LineTable[i].Identifier);
443 if (ConditionalTable)
445 for (i = 0; ConditionalTable[i].Identifier; i++)
447 AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
453 for (i = 0; MacroTable[i].Identifier; i++)
455 AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
461 for (i = 0; StructTable[i].Identifier; i++)
463 AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
467 /* Process the function table */
469 for (i = 0; i < 32; i++)
471 /* Decode the function bitmap */
473 switch ((1 << i) & Functions)
476 /* This function not configured */
482 AsCountTabs (FileBuffer, Filename);
486 case CVT_COUNT_NON_ANSI_COMMENTS:
488 AsCountNonAnsiComments (FileBuffer, Filename);
492 case CVT_CHECK_BRACES:
494 AsCheckForBraces (FileBuffer, Filename);
500 AsTrimLines (FileBuffer, Filename);
504 case CVT_COUNT_LINES:
506 AsCountSourceLines (FileBuffer, Filename);
510 case CVT_BRACES_ON_SAME_LINE:
512 AsBracesOnSameLine (FileBuffer);
516 case CVT_MIXED_CASE_TO_UNDERSCORES:
518 AsMixedCaseToUnderscores (FileBuffer);
522 case CVT_LOWER_CASE_IDENTIFIERS:
524 AsLowerCaseIdentifiers (FileBuffer);
528 case CVT_REMOVE_DEBUG_MACROS:
530 AsRemoveDebugMacros (FileBuffer);
534 case CVT_TRIM_WHITESPACE:
536 AsTrimWhitespace (FileBuffer);
540 case CVT_REMOVE_EMPTY_BLOCKS:
542 AsRemoveEmptyBlocks (FileBuffer, Filename);
546 case CVT_REDUCE_TYPEDEFS:
548 AsReduceTypedefs (FileBuffer, "typedef union");
549 AsReduceTypedefs (FileBuffer, "typedef struct");
553 case CVT_SPACES_TO_TABS4:
555 AsTabify4 (FileBuffer);
559 case CVT_SPACES_TO_TABS8:
561 AsTabify8 (FileBuffer);
564 case CVT_COUNT_SHORTMULTILINE_COMMENTS:
566 #ifdef ACPI_FUTURE_IMPLEMENTATION
567 AsTrimComments (FileBuffer, Filename);
573 printf ("Unknown conversion subfunction opcode\n");
578 if (ConversionTable->NewHeader)
580 AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
585 /******************************************************************************
587 * FUNCTION: AsProcessOneFile
589 * DESCRIPTION: Process one source file. The file is opened, read entirely
590 * into a buffer, converted, then written to a new file.
592 ******************************************************************************/
596 ACPI_CONVERSION_TABLE *ConversionTable,
601 ACPI_NATIVE_INT FileType)
604 char *OutPathname = NULL;
607 /* Allocate a file pathname buffer for both source and target */
609 Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
612 printf ("Could not allocate buffer for file pathnames\n");
616 Gbl_FileType = FileType;
618 /* Generate the source pathname and read the file */
622 strcpy (Pathname, SourcePath);
623 strcat (Pathname, "/");
626 strcat (Pathname, Filename);
628 if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
634 if (strstr (Filename, ".asl"))
636 Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
638 else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
640 Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
642 else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
644 Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
647 /* Process the file in the buffer */
649 Gbl_MadeChanges = FALSE;
650 if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
653 * All lone LFs will be converted to CR/LF
654 * (when file is written, Windows version only)
656 printf ("Converting lone linefeeds\n");
657 Gbl_MadeChanges = TRUE;
660 AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
662 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
664 if (!(Gbl_Overwrite && !Gbl_MadeChanges))
666 /* Generate the target pathname and write the file */
668 OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1);
671 printf ("Could not allocate buffer for file pathnames\n");
675 strcpy (OutPathname, TargetPath);
678 strcat (OutPathname, "/");
679 strcat (OutPathname, Filename);
682 AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
686 free (Gbl_FileBuffer);
697 /******************************************************************************
699 * FUNCTION: AsCheckForDirectory
701 * DESCRIPTION: Check if the current file is a valid directory. If not,
702 * construct the full pathname for the source and target paths.
703 * Checks for the dot and dot-dot files (they are ignored)
705 ******************************************************************************/
708 AsCheckForDirectory (
719 if (!(strcmp (Filename, ".")) ||
720 !(strcmp (Filename, "..")))
725 SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
728 printf ("Could not allocate buffer for directory source pathname\n");
732 TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
735 printf ("Could not allocate buffer for directory target pathname\n");
740 strcpy (SrcPath, SourceDirPath);
741 strcat (SrcPath, "/");
742 strcat (SrcPath, Filename);
744 strcpy (TgtPath, TargetDirPath);
745 strcat (TgtPath, "/");
746 strcat (TgtPath, Filename);
748 *SourcePath = SrcPath;
749 *TargetPath = TgtPath;
754 /******************************************************************************
756 * FUNCTION: AsGetFile
758 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
760 ******************************************************************************/
774 /* Binary mode leaves CR/LF pairs */
776 FileHandle = open (Filename, O_BINARY | O_RDONLY);
779 printf ("Could not open %s\n", Filename);
783 if (fstat (FileHandle, &Gbl_StatBuf))
785 printf ("Could not get file status for %s\n", Filename);
790 * Create a buffer for the entire file
791 * Add plenty extra buffer to accomodate string replacements
793 Size = Gbl_StatBuf.st_size;
794 Gbl_TotalSize += Size;
796 Buffer = calloc (Size * 2, 1);
799 printf ("Could not allocate buffer of size %u\n", Size * 2);
803 /* Read the entire file */
805 Size = read (FileHandle, Buffer, Size);
808 printf ("Could not read the input file %s\n", Filename);
812 Buffer [Size] = 0; /* Null terminate the buffer */
815 /* Check for unix contamination */
817 Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
820 * Convert all CR/LF pairs to LF only. We do this locally so that
821 * this code is portable across operating systems.
823 AsConvertToLineFeeds (Buffer);
825 *FileBuffer = Buffer;
838 /******************************************************************************
840 * FUNCTION: AsPutFile
842 * DESCRIPTION: Create a new output file and write the entire contents of the
843 * buffer to the new file. Buffer must be a zero terminated string
845 ******************************************************************************/
858 /* Create the target file */
860 OpenFlags = O_TRUNC | O_CREAT | O_WRONLY | O_BINARY;
862 if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
864 /* Put back the CR before each LF */
866 AsInsertCarriageReturns (FileBuffer);
869 DestHandle = open (Pathname, OpenFlags, S_IREAD | S_IWRITE);
870 if (DestHandle == -1)
872 perror ("Could not create destination file");
873 printf ("Could not create destination file \"%s\"\n", Pathname);
877 /* Write the buffer to the file */
879 FileSize = strlen (FileBuffer);
880 write (DestHandle, FileBuffer, FileSize);