1 /******************************************************************************
3 * Module Name: asfile - Main module for the acpi source processor utility
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
47 /* Local prototypes */
51 ACPI_CONVERSION_TABLE *ConversionTable,
59 AsDetectLoneLineFeeds (
63 static ACPI_INLINE int
64 AsMaxInt (int a, int b)
66 return (a > b ? a : b);
70 /******************************************************************************
72 * FUNCTION: AsDoWildcard
74 * DESCRIPTION: Process files via wildcards
76 ******************************************************************************/
80 ACPI_CONVERSION_TABLE *ConversionTable,
91 char RequestedFileType;
94 if (FileType == FILE_TYPE_DIRECTORY)
96 RequestedFileType = REQUEST_DIR_ONLY;
100 RequestedFileType = REQUEST_FILE_ONLY;
103 VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
104 WildcardSpec, SourcePath));
106 /* Open the directory for wildcard search */
108 DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
112 * Get all of the files that match both the
113 * wildcard and the requested file type
115 while ((Filename = AcpiOsGetNextFilename (DirInfo)))
117 /* Looking for directory files, must check file type */
119 switch (RequestedFileType)
121 case REQUEST_DIR_ONLY:
123 /* If we actually have a dir, process the subtree */
125 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
126 &SourceDirPath, &TargetDirPath))
128 VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
130 AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
131 free (SourceDirPath);
132 free (TargetDirPath);
136 case REQUEST_FILE_ONLY:
138 /* Otherwise, this is a file, not a directory */
140 VERBOSE_PRINT (("File: %s\n", Filename));
142 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
143 MaxPathLength, Filename, FileType);
154 AcpiOsCloseDirectory (DirInfo);
159 /******************************************************************************
161 * FUNCTION: AsProcessTree
163 * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
164 * ".H" are processed as the tree is traversed.
166 ******************************************************************************/
170 ACPI_CONVERSION_TABLE *ConversionTable,
177 MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
179 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
181 if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
183 AcpiUtStrlwr (TargetPath);
186 VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
187 if (mkdir (TargetPath))
191 printf ("Could not create target directory\n");
197 /* Do the C source files */
199 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
200 FILE_TYPE_SOURCE, "*.c");
202 /* Do the C header files */
204 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
205 FILE_TYPE_HEADER, "*.h");
207 /* Do the Lex file(s) */
209 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
210 FILE_TYPE_SOURCE, "*.l");
212 /* Do the yacc file(s) */
214 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
215 FILE_TYPE_SOURCE, "*.y");
217 /* Do any ASL files */
219 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
220 FILE_TYPE_HEADER, "*.asl");
222 /* Do any subdirectories */
224 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
225 FILE_TYPE_DIRECTORY, "*");
231 /******************************************************************************
233 * FUNCTION: AsDetectLoneLineFeeds
235 * DESCRIPTION: Find LF without CR.
237 ******************************************************************************/
240 AsDetectLoneLineFeeds (
246 UINT32 LineCount = 0;
256 if (Buffer[i] == 0x0A)
258 if (Buffer[i-1] != 0x0D)
270 if (LineCount == LfCount)
272 if (!Gbl_IgnoreLoneLineFeeds)
274 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
280 printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
290 /******************************************************************************
292 * FUNCTION: AsConvertFile
294 * DESCRIPTION: Perform the requested transforms on the file buffer (as
295 * determined by the ConversionTable and the FileType).
297 ******************************************************************************/
301 ACPI_CONVERSION_TABLE *ConversionTable,
304 ACPI_NATIVE_INT FileType)
308 ACPI_STRING_TABLE *StringTable;
309 ACPI_IDENTIFIER_TABLE *ConditionalTable;
310 ACPI_IDENTIFIER_TABLE *LineTable;
311 ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
312 ACPI_IDENTIFIER_TABLE *SpecialMacroTable;
317 case FILE_TYPE_SOURCE:
319 Functions = ConversionTable->SourceFunctions;
320 StringTable = ConversionTable->SourceStringTable;
321 LineTable = ConversionTable->SourceLineTable;
322 ConditionalTable = ConversionTable->SourceConditionalTable;
323 StructTable = ConversionTable->SourceStructTable;
324 SpecialMacroTable = ConversionTable->SourceSpecialMacroTable;
327 case FILE_TYPE_HEADER:
329 Functions = ConversionTable->HeaderFunctions;
330 StringTable = ConversionTable->HeaderStringTable;
331 LineTable = ConversionTable->HeaderLineTable;
332 ConditionalTable = ConversionTable->HeaderConditionalTable;
333 StructTable = ConversionTable->HeaderStructTable;
334 SpecialMacroTable = ConversionTable->HeaderSpecialMacroTable;
337 case FILE_TYPE_PATCH:
339 Functions = ConversionTable->PatchFunctions;
340 StringTable = ConversionTable->PatchStringTable;
341 LineTable = ConversionTable->PatchLineTable;
342 ConditionalTable = ConversionTable->PatchConditionalTable;
343 StructTable = ConversionTable->PatchStructTable;
344 SpecialMacroTable = ConversionTable->PatchSpecialMacroTable;
349 printf ("Unknown file type, cannot process\n");
354 Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
356 VERBOSE_PRINT (("Processing %u bytes\n",
357 (unsigned int) strlen (FileBuffer)));
361 AsRemoveExtraLines (FileBuffer, Filename);
362 AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
365 if (ConversionTable->LowerCaseTable)
367 for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
369 AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
374 /* Process all the string replacements */
378 for (i = 0; StringTable[i].Target; i++)
380 AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
381 StringTable[i].Type, FileBuffer);
387 for (i = 0; LineTable[i].Identifier; i++)
389 AsRemoveLine (FileBuffer, LineTable[i].Identifier);
393 if (ConditionalTable)
395 for (i = 0; ConditionalTable[i].Identifier; i++)
397 AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
401 #ifdef _OBSOLETE_FUNCTIONS
404 for (i = 0; MacroTable[i].Identifier; i++)
406 AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
413 for (i = 0; StructTable[i].Identifier; i++)
415 AsInsertPrefix (FileBuffer, StructTable[i].Identifier,
416 StructTable[i].Type);
420 if (SpecialMacroTable)
422 for (i = 0; SpecialMacroTable[i].Identifier; i++)
424 AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
428 /* Process the function table */
430 for (i = 0; i < 32; i++)
432 /* Decode the function bitmap */
434 switch ((1 << i) & Functions)
438 /* This function not configured */
443 AsCountTabs (FileBuffer, Filename);
446 case CVT_COUNT_NON_ANSI_COMMENTS:
448 AsCountNonAnsiComments (FileBuffer, Filename);
451 case CVT_CHECK_BRACES:
453 AsCheckForBraces (FileBuffer, Filename);
458 AsTrimLines (FileBuffer, Filename);
461 case CVT_COUNT_LINES:
463 AsCountSourceLines (FileBuffer, Filename);
466 case CVT_BRACES_ON_SAME_LINE:
468 AsBracesOnSameLine (FileBuffer);
471 case CVT_MIXED_CASE_TO_UNDERSCORES:
473 AsMixedCaseToUnderscores (FileBuffer, Filename);
476 case CVT_LOWER_CASE_IDENTIFIERS:
478 AsLowerCaseIdentifiers (FileBuffer);
481 case CVT_REMOVE_DEBUG_MACROS:
483 AsRemoveDebugMacros (FileBuffer);
486 case CVT_TRIM_WHITESPACE:
488 AsTrimWhitespace (FileBuffer);
491 case CVT_REMOVE_EMPTY_BLOCKS:
493 AsRemoveEmptyBlocks (FileBuffer, Filename);
496 case CVT_REDUCE_TYPEDEFS:
498 AsReduceTypedefs (FileBuffer, "typedef union");
499 AsReduceTypedefs (FileBuffer, "typedef struct");
502 case CVT_SPACES_TO_TABS4:
504 AsTabify4 (FileBuffer);
507 case CVT_SPACES_TO_TABS8:
509 AsTabify8 (FileBuffer);
512 case CVT_COUNT_SHORTMULTILINE_COMMENTS:
514 #ifdef ACPI_FUTURE_IMPLEMENTATION
515 AsTrimComments (FileBuffer, Filename);
521 printf ("Unknown conversion subfunction opcode\n");
526 if (ConversionTable->NewHeader)
528 AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
533 /******************************************************************************
535 * FUNCTION: AsProcessOneFile
537 * DESCRIPTION: Process one source file. The file is opened, read entirely
538 * into a buffer, converted, then written to a new file.
540 ******************************************************************************/
544 ACPI_CONVERSION_TABLE *ConversionTable,
549 ACPI_NATIVE_INT FileType)
552 char *OutPathname = NULL;
555 /* Allocate a file pathname buffer for both source and target */
557 Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
560 printf ("Could not allocate buffer for file pathnames\n");
564 Gbl_FileType = FileType;
566 /* Generate the source pathname and read the file */
570 strcpy (Pathname, SourcePath);
571 strcat (Pathname, "/");
574 strcat (Pathname, Filename);
576 if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
582 if (strstr (Filename, ".asl"))
584 Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
586 else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
588 Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
590 else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
592 Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
595 /* Process the file in the buffer */
597 Gbl_MadeChanges = FALSE;
598 if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
601 * All lone LFs will be converted to CR/LF
602 * (when file is written, Windows version only)
604 printf ("Converting lone linefeeds\n");
605 Gbl_MadeChanges = TRUE;
608 AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
610 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
612 if (!(Gbl_Overwrite && !Gbl_MadeChanges))
614 /* Generate the target pathname and write the file */
616 OutPathname = calloc (MaxPathLength +
617 strlen (Filename) + 2 + strlen (TargetPath), 1);
620 printf ("Could not allocate buffer for file pathnames\n");
624 strcpy (OutPathname, TargetPath);
627 strcat (OutPathname, "/");
628 strcat (OutPathname, Filename);
631 AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
635 free (Gbl_FileBuffer);
646 /******************************************************************************
648 * FUNCTION: AsCheckForDirectory
650 * DESCRIPTION: Check if the current file is a valid directory. If not,
651 * construct the full pathname for the source and target paths.
652 * Checks for the dot and dot-dot files (they are ignored)
654 ******************************************************************************/
657 AsCheckForDirectory (
668 if (!(strcmp (Filename, ".")) ||
669 !(strcmp (Filename, "..")))
674 SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
677 printf ("Could not allocate buffer for directory source pathname\n");
681 TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
684 printf ("Could not allocate buffer for directory target pathname\n");
689 strcpy (SrcPath, SourceDirPath);
690 strcat (SrcPath, "/");
691 strcat (SrcPath, Filename);
693 strcpy (TgtPath, TargetDirPath);
694 strcat (TgtPath, "/");
695 strcat (TgtPath, Filename);
697 *SourcePath = SrcPath;
698 *TargetPath = TgtPath;
703 /******************************************************************************
705 * FUNCTION: AsGetFile
707 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
709 ******************************************************************************/
723 /* Binary mode leaves CR/LF pairs */
725 File = fopen (Filename, "rb");
728 printf ("Could not open file %s\n", Filename);
732 /* Need file size to allocate a buffer */
734 Size = CmGetFileSize (File);
735 if (Size == ACPI_UINT32_MAX)
737 printf ("Could not get file size for %s\n", Filename);
742 * Create a buffer for the entire file
743 * Add plenty extra buffer to accommodate string replacements
745 Gbl_TotalSize += Size;
747 Buffer = calloc (Size * 2, 1);
750 printf ("Could not allocate buffer of size %u\n", Size * 2);
754 /* Read the entire file */
756 Actual = fread (Buffer, 1, Size, File);
759 printf ("Could not read the input file %s (%u bytes)\n",
764 Buffer [Size] = 0; /* Null terminate the buffer */
767 /* Check for unix contamination */
769 Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
772 * Convert all CR/LF pairs to LF only. We do this locally so that
773 * this code is portable across operating systems.
775 AsConvertToLineFeeds (Buffer);
777 *FileBuffer = Buffer;
789 /******************************************************************************
791 * FUNCTION: AsPutFile
793 * DESCRIPTION: Create a new output file and write the entire contents of the
794 * buffer to the new file. Buffer must be a zero terminated string
796 ******************************************************************************/
810 /* Create the target file */
812 if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
814 /* Put back the CR before each LF */
816 AsInsertCarriageReturns (FileBuffer);
819 File = fopen (Pathname, "w+b");
822 perror ("Could not create destination file");
823 printf ("Could not create destination file \"%s\"\n", Pathname);
827 /* Write the buffer to the file */
829 FileSize = strlen (FileBuffer);
830 Actual = fwrite (FileBuffer, 1, FileSize, File);
831 if (Actual != FileSize)
833 printf ("Error writing output file \"%s\"\n", Pathname);