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 AsStrlwr (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)
269 if (LineCount == LfCount)
271 if (!Gbl_IgnoreLoneLineFeeds)
273 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
279 printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
288 /******************************************************************************
290 * FUNCTION: AsConvertFile
292 * DESCRIPTION: Perform the requested transforms on the file buffer (as
293 * determined by the ConversionTable and the FileType).
295 ******************************************************************************/
299 ACPI_CONVERSION_TABLE *ConversionTable,
302 ACPI_NATIVE_INT FileType)
306 ACPI_STRING_TABLE *StringTable;
307 ACPI_IDENTIFIER_TABLE *ConditionalTable;
308 ACPI_IDENTIFIER_TABLE *LineTable;
309 ACPI_IDENTIFIER_TABLE *MacroTable;
310 ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
311 ACPI_IDENTIFIER_TABLE *SpecialMacroTable;
316 case FILE_TYPE_SOURCE:
318 Functions = ConversionTable->SourceFunctions;
319 StringTable = ConversionTable->SourceStringTable;
320 LineTable = ConversionTable->SourceLineTable;
321 ConditionalTable = ConversionTable->SourceConditionalTable;
322 MacroTable = ConversionTable->SourceMacroTable;
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 MacroTable = ConversionTable->HeaderMacroTable;
334 StructTable = ConversionTable->HeaderStructTable;
335 SpecialMacroTable = ConversionTable->HeaderSpecialMacroTable;
338 case FILE_TYPE_PATCH:
340 Functions = ConversionTable->PatchFunctions;
341 StringTable = ConversionTable->PatchStringTable;
342 LineTable = ConversionTable->PatchLineTable;
343 ConditionalTable = ConversionTable->PatchConditionalTable;
344 MacroTable = ConversionTable->PatchMacroTable;
345 StructTable = ConversionTable->PatchStructTable;
346 SpecialMacroTable = ConversionTable->PatchSpecialMacroTable;
351 printf ("Unknown file type, cannot process\n");
356 Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
358 VERBOSE_PRINT (("Processing %u bytes\n",
359 (unsigned int) strlen (FileBuffer)));
363 AsRemoveExtraLines (FileBuffer, Filename);
364 AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
367 if (ConversionTable->LowerCaseTable)
369 for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
371 AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
376 /* Process all the string replacements */
380 for (i = 0; StringTable[i].Target; i++)
382 AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
383 StringTable[i].Type, FileBuffer);
389 for (i = 0; LineTable[i].Identifier; i++)
391 AsRemoveLine (FileBuffer, LineTable[i].Identifier);
395 if (ConditionalTable)
397 for (i = 0; ConditionalTable[i].Identifier; i++)
399 AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
405 for (i = 0; MacroTable[i].Identifier; i++)
407 AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
413 for (i = 0; StructTable[i].Identifier; i++)
415 AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
419 if (SpecialMacroTable)
421 for (i = 0; SpecialMacroTable[i].Identifier; i++)
423 AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
427 /* Process the function table */
429 for (i = 0; i < 32; i++)
431 /* Decode the function bitmap */
433 switch ((1 << i) & Functions)
437 /* This function not configured */
442 AsCountTabs (FileBuffer, Filename);
445 case CVT_COUNT_NON_ANSI_COMMENTS:
447 AsCountNonAnsiComments (FileBuffer, Filename);
450 case CVT_CHECK_BRACES:
452 AsCheckForBraces (FileBuffer, Filename);
457 AsTrimLines (FileBuffer, Filename);
460 case CVT_COUNT_LINES:
462 AsCountSourceLines (FileBuffer, Filename);
465 case CVT_BRACES_ON_SAME_LINE:
467 AsBracesOnSameLine (FileBuffer);
470 case CVT_MIXED_CASE_TO_UNDERSCORES:
472 AsMixedCaseToUnderscores (FileBuffer, Filename);
475 case CVT_LOWER_CASE_IDENTIFIERS:
477 AsLowerCaseIdentifiers (FileBuffer);
480 case CVT_REMOVE_DEBUG_MACROS:
482 AsRemoveDebugMacros (FileBuffer);
485 case CVT_TRIM_WHITESPACE:
487 AsTrimWhitespace (FileBuffer);
490 case CVT_REMOVE_EMPTY_BLOCKS:
492 AsRemoveEmptyBlocks (FileBuffer, Filename);
495 case CVT_REDUCE_TYPEDEFS:
497 AsReduceTypedefs (FileBuffer, "typedef union");
498 AsReduceTypedefs (FileBuffer, "typedef struct");
501 case CVT_SPACES_TO_TABS4:
503 AsTabify4 (FileBuffer);
506 case CVT_SPACES_TO_TABS8:
508 AsTabify8 (FileBuffer);
511 case CVT_COUNT_SHORTMULTILINE_COMMENTS:
513 #ifdef ACPI_FUTURE_IMPLEMENTATION
514 AsTrimComments (FileBuffer, Filename);
520 printf ("Unknown conversion subfunction opcode\n");
525 if (ConversionTable->NewHeader)
527 AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
532 /******************************************************************************
534 * FUNCTION: AsProcessOneFile
536 * DESCRIPTION: Process one source file. The file is opened, read entirely
537 * into a buffer, converted, then written to a new file.
539 ******************************************************************************/
543 ACPI_CONVERSION_TABLE *ConversionTable,
548 ACPI_NATIVE_INT FileType)
551 char *OutPathname = NULL;
554 /* Allocate a file pathname buffer for both source and target */
556 Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
559 printf ("Could not allocate buffer for file pathnames\n");
563 Gbl_FileType = FileType;
565 /* Generate the source pathname and read the file */
569 strcpy (Pathname, SourcePath);
570 strcat (Pathname, "/");
573 strcat (Pathname, Filename);
575 if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
581 if (strstr (Filename, ".asl"))
583 Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
585 else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
587 Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
589 else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
591 Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
594 /* Process the file in the buffer */
596 Gbl_MadeChanges = FALSE;
597 if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
600 * All lone LFs will be converted to CR/LF
601 * (when file is written, Windows version only)
603 printf ("Converting lone linefeeds\n");
604 Gbl_MadeChanges = TRUE;
607 AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
609 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
611 if (!(Gbl_Overwrite && !Gbl_MadeChanges))
613 /* Generate the target pathname and write the file */
615 OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1);
618 printf ("Could not allocate buffer for file pathnames\n");
622 strcpy (OutPathname, TargetPath);
625 strcat (OutPathname, "/");
626 strcat (OutPathname, Filename);
629 AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
633 free (Gbl_FileBuffer);
644 /******************************************************************************
646 * FUNCTION: AsCheckForDirectory
648 * DESCRIPTION: Check if the current file is a valid directory. If not,
649 * construct the full pathname for the source and target paths.
650 * Checks for the dot and dot-dot files (they are ignored)
652 ******************************************************************************/
655 AsCheckForDirectory (
666 if (!(strcmp (Filename, ".")) ||
667 !(strcmp (Filename, "..")))
672 SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
675 printf ("Could not allocate buffer for directory source pathname\n");
679 TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
682 printf ("Could not allocate buffer for directory target pathname\n");
687 strcpy (SrcPath, SourceDirPath);
688 strcat (SrcPath, "/");
689 strcat (SrcPath, Filename);
691 strcpy (TgtPath, TargetDirPath);
692 strcat (TgtPath, "/");
693 strcat (TgtPath, Filename);
695 *SourcePath = SrcPath;
696 *TargetPath = TgtPath;
701 /******************************************************************************
703 * FUNCTION: AsGetFile
705 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
707 ******************************************************************************/
721 /* Binary mode leaves CR/LF pairs */
723 File = fopen (Filename, "rb");
726 printf ("Could not open file %s\n", Filename);
730 /* Need file size to allocate a buffer */
732 Size = CmGetFileSize (File);
733 if (Size == ACPI_UINT32_MAX)
735 printf ("Could not get file size for %s\n", Filename);
740 * Create a buffer for the entire file
741 * Add plenty extra buffer to accommodate string replacements
743 Gbl_TotalSize += Size;
745 Buffer = calloc (Size * 2, 1);
748 printf ("Could not allocate buffer of size %u\n", Size * 2);
752 /* Read the entire file */
754 Actual = fread (Buffer, 1, Size, File);
757 printf ("Could not read the input file %s (%u bytes)\n",
762 Buffer [Size] = 0; /* Null terminate the buffer */
765 /* Check for unix contamination */
767 Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
770 * Convert all CR/LF pairs to LF only. We do this locally so that
771 * this code is portable across operating systems.
773 AsConvertToLineFeeds (Buffer);
775 *FileBuffer = Buffer;
787 /******************************************************************************
789 * FUNCTION: AsPutFile
791 * DESCRIPTION: Create a new output file and write the entire contents of the
792 * buffer to the new file. Buffer must be a zero terminated string
794 ******************************************************************************/
808 /* Create the target file */
810 if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
812 /* Put back the CR before each LF */
814 AsInsertCarriageReturns (FileBuffer);
817 File = fopen (Pathname, "w+b");
820 perror ("Could not create destination file");
821 printf ("Could not create destination file \"%s\"\n", Pathname);
825 /* Write the buffer to the file */
827 FileSize = strlen (FileBuffer);
828 Actual = fwrite (FileBuffer, 1, FileSize, File);
829 if (Actual != FileSize)
831 printf ("Error writing output file \"%s\"\n", Pathname);