2 /******************************************************************************
4 * Module Name: asfile - Main module for the acpi source processor utility
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
74 * 3.4. Intel retains all right, title, and interest in and to the Original
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
121 /******************************************************************************
123 * FUNCTION: AsDoWildcard
125 * DESCRIPTION: Process files via wildcards
127 ******************************************************************************/
131 ACPI_CONVERSION_TABLE
*ConversionTable
,
142 char RequestedFileType
;
145 if (FileType
== FILE_TYPE_DIRECTORY
)
147 RequestedFileType
= REQUEST_DIR_ONLY
;
151 RequestedFileType
= REQUEST_FILE_ONLY
;
154 VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
155 WildcardSpec
, SourcePath
));
157 /* Open the directory for wildcard search */
159 DirInfo
= AcpiOsOpenDirectory (SourcePath
, WildcardSpec
, RequestedFileType
);
163 * Get all of the files that match both the
164 * wildcard and the requested file type
166 while ((Filename
= AcpiOsGetNextFilename (DirInfo
)))
168 /* Looking for directory files, must check file type */
170 switch (RequestedFileType
)
172 case REQUEST_DIR_ONLY
:
174 /* If we actually have a dir, process the subtree */
176 if (!AsCheckForDirectory (SourcePath
, TargetPath
, Filename
,
177 &SourceDirPath
, &TargetDirPath
))
179 VERBOSE_PRINT (("Subdirectory: %s\n", Filename
));
181 AsProcessTree (ConversionTable
, SourceDirPath
, TargetDirPath
);
182 free (SourceDirPath
);
183 free (TargetDirPath
);
187 case REQUEST_FILE_ONLY
:
189 /* Otherwise, this is a file, not a directory */
191 VERBOSE_PRINT (("File: %s\n", Filename
));
193 AsProcessOneFile (ConversionTable
, SourcePath
, TargetPath
,
194 MaxPathLength
, Filename
, FileType
);
201 AcpiOsCloseDirectory (DirInfo
);
206 /******************************************************************************
208 * FUNCTION: AsProcessTree
210 * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
211 * ".H" are processed as the tree is traversed.
213 ******************************************************************************/
217 ACPI_CONVERSION_TABLE
*ConversionTable
,
224 MaxPathLength
= max (strlen (SourcePath
), strlen (TargetPath
));
226 if (!(ConversionTable
->Flags
& FLG_NO_FILE_OUTPUT
))
228 if (ConversionTable
->Flags
& FLG_LOWERCASE_DIRNAMES
)
233 VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath
));
234 if (mkdir (TargetPath
))
238 printf ("Could not create target directory\n");
244 /* Do the C source files */
246 AsDoWildcard (ConversionTable
, SourcePath
, TargetPath
, MaxPathLength
,
247 FILE_TYPE_SOURCE
, "*.c");
249 /* Do the C header files */
251 AsDoWildcard (ConversionTable
, SourcePath
, TargetPath
, MaxPathLength
,
252 FILE_TYPE_HEADER
, "*.h");
254 /* Do the Lex file(s) */
256 AsDoWildcard (ConversionTable
, SourcePath
, TargetPath
, MaxPathLength
,
257 FILE_TYPE_SOURCE
, "*.l");
259 /* Do the yacc file(s) */
261 AsDoWildcard (ConversionTable
, SourcePath
, TargetPath
, MaxPathLength
,
262 FILE_TYPE_SOURCE
, "*.y");
264 /* Do any subdirectories */
266 AsDoWildcard (ConversionTable
, SourcePath
, TargetPath
, MaxPathLength
,
267 FILE_TYPE_DIRECTORY
, "*");
273 /******************************************************************************
275 * FUNCTION: AsDetectLoneLineFeeds
277 * DESCRIPTION: Find LF without CR.
279 ******************************************************************************/
282 AsDetectLoneLineFeeds (
297 if (Buffer
[i
] == 0x0A)
299 if (Buffer
[i
-1] != 0x0D)
310 printf ("****UNIX CONTAMINATION DETECTED****\n");
311 printf ("%d lone linefeeds in file %s\n", LfCount
, Filename
);
319 /******************************************************************************
321 * FUNCTION: AsConvertFile
323 * DESCRIPTION: Perform the requested transforms on the file buffer (as
324 * determined by the ConversionTable and the FileType).
326 ******************************************************************************/
330 ACPI_CONVERSION_TABLE
*ConversionTable
,
333 ACPI_NATIVE_INT FileType
)
337 ACPI_STRING_TABLE
*StringTable
;
338 ACPI_IDENTIFIER_TABLE
*ConditionalTable
;
339 ACPI_IDENTIFIER_TABLE
*LineTable
;
340 ACPI_IDENTIFIER_TABLE
*MacroTable
;
341 ACPI_TYPED_IDENTIFIER_TABLE
*StructTable
;
346 case FILE_TYPE_SOURCE
:
347 Functions
= ConversionTable
->SourceFunctions
;
348 StringTable
= ConversionTable
->SourceStringTable
;
349 LineTable
= ConversionTable
->SourceLineTable
;
350 ConditionalTable
= ConversionTable
->SourceConditionalTable
;
351 MacroTable
= ConversionTable
->SourceMacroTable
;
352 StructTable
= ConversionTable
->SourceStructTable
;
355 case FILE_TYPE_HEADER
:
356 Functions
= ConversionTable
->HeaderFunctions
;
357 StringTable
= ConversionTable
->HeaderStringTable
;
358 LineTable
= ConversionTable
->HeaderLineTable
;
359 ConditionalTable
= ConversionTable
->HeaderConditionalTable
;
360 MacroTable
= ConversionTable
->HeaderMacroTable
;
361 StructTable
= ConversionTable
->HeaderStructTable
;
365 printf ("Unknown file type, cannot process\n");
370 VERBOSE_PRINT (("Processing %d bytes\n", strlen (FileBuffer
)));
372 if (ConversionTable
->LowerCaseTable
)
374 for (i
= 0; ConversionTable
->LowerCaseTable
[i
].Identifier
; i
++)
376 AsLowerCaseString (ConversionTable
->LowerCaseTable
[i
].Identifier
,
381 /* Process all the string replacements */
385 for (i
= 0; StringTable
[i
].Target
; i
++)
387 AsReplaceString (StringTable
[i
].Target
, StringTable
[i
].Replacement
,
388 StringTable
[i
].Type
, FileBuffer
);
394 for (i
= 0; LineTable
[i
].Identifier
; i
++)
396 AsRemoveLine (FileBuffer
, LineTable
[i
].Identifier
);
400 if (ConditionalTable
)
402 for (i
= 0; ConditionalTable
[i
].Identifier
; i
++)
404 AsRemoveConditionalCompile (FileBuffer
, ConditionalTable
[i
].Identifier
);
410 for (i
= 0; MacroTable
[i
].Identifier
; i
++)
412 AsRemoveMacro (FileBuffer
, MacroTable
[i
].Identifier
);
418 for (i
= 0; StructTable
[i
].Identifier
; i
++)
420 AsInsertPrefix (FileBuffer
, StructTable
[i
].Identifier
, StructTable
[i
].Type
);
424 /* Process the function table */
426 for (i
= 0; i
< 32; i
++)
428 /* Decode the function bitmap */
430 switch ((1 << i
) & Functions
)
433 /* This function not configured */
439 AsCountTabs (FileBuffer
, Filename
);
443 case CVT_COUNT_NON_ANSI_COMMENTS
:
445 AsCountNonAnsiComments (FileBuffer
, Filename
);
449 case CVT_CHECK_BRACES
:
451 AsCheckForBraces (FileBuffer
, Filename
);
457 AsTrimLines (FileBuffer
, Filename
);
461 case CVT_COUNT_LINES
:
463 AsCountSourceLines (FileBuffer
, Filename
);
467 case CVT_BRACES_ON_SAME_LINE
:
469 AsBracesOnSameLine (FileBuffer
);
473 case CVT_MIXED_CASE_TO_UNDERSCORES
:
475 AsMixedCaseToUnderscores (FileBuffer
);
479 case CVT_LOWER_CASE_IDENTIFIERS
:
481 AsLowerCaseIdentifiers (FileBuffer
);
485 case CVT_REMOVE_DEBUG_MACROS
:
487 AsRemoveDebugMacros (FileBuffer
);
491 case CVT_TRIM_WHITESPACE
:
493 AsTrimWhitespace (FileBuffer
);
497 case CVT_REMOVE_EMPTY_BLOCKS
:
499 AsRemoveEmptyBlocks (FileBuffer
, Filename
);
503 case CVT_REDUCE_TYPEDEFS
:
505 AsReduceTypedefs (FileBuffer
, "typedef union");
506 AsReduceTypedefs (FileBuffer
, "typedef struct");
510 case CVT_SPACES_TO_TABS4
:
512 AsTabify4 (FileBuffer
);
516 case CVT_SPACES_TO_TABS8
:
518 AsTabify8 (FileBuffer
);
524 printf ("Unknown conversion subfunction opcode\n");
529 if (ConversionTable
->NewHeader
)
531 AsReplaceHeader (FileBuffer
, ConversionTable
->NewHeader
);
536 /******************************************************************************
538 * FUNCTION: AsProcessOneFile
540 * DESCRIPTION: Process one source file. The file is opened, read entirely
541 * into a buffer, converted, then written to a new file.
543 ******************************************************************************/
547 ACPI_CONVERSION_TABLE
*ConversionTable
,
552 ACPI_NATIVE_INT FileType
)
555 char *OutPathname
= NULL
;
558 /* Allocate a file pathname buffer for both source and target */
560 Pathname
= calloc (MaxPathLength
+ strlen (Filename
) + 2, 1);
563 printf ("Could not allocate buffer for file pathnames\n");
567 Gbl_FileType
= FileType
;
569 /* Generate the source pathname and read the file */
573 strcpy (Pathname
, SourcePath
);
574 strcat (Pathname
, "/");
577 strcat (Pathname
, Filename
);
579 if (AsGetFile (Pathname
, &Gbl_FileBuffer
, &Gbl_FileSize
))
584 /* Process the file in the buffer */
586 Gbl_MadeChanges
= FALSE
;
587 AsConvertFile (ConversionTable
, Gbl_FileBuffer
, Pathname
, FileType
);
589 if (!(ConversionTable
->Flags
& FLG_NO_FILE_OUTPUT
))
591 if (!(Gbl_Overwrite
&& !Gbl_MadeChanges
))
593 /* Generate the target pathname and write the file */
595 OutPathname
= calloc (MaxPathLength
+ strlen (Filename
) + 2 + strlen (TargetPath
), 1);
598 printf ("Could not allocate buffer for file pathnames\n");
602 strcpy (OutPathname
, TargetPath
);
605 strcat (OutPathname
, "/");
606 strcat (OutPathname
, Filename
);
609 AsPutFile (OutPathname
, Gbl_FileBuffer
, ConversionTable
->Flags
);
613 free (Gbl_FileBuffer
);
624 /******************************************************************************
626 * FUNCTION: AsCheckForDirectory
628 * DESCRIPTION: Check if the current file is a valid directory. If not,
629 * construct the full pathname for the source and target paths.
630 * Checks for the dot and dot-dot files (they are ignored)
632 ******************************************************************************/
635 AsCheckForDirectory (
646 if (!(strcmp (Filename
, ".")) ||
647 !(strcmp (Filename
, "..")))
652 SrcPath
= calloc (strlen (SourceDirPath
) + strlen (Filename
) + 2, 1);
655 printf ("Could not allocate buffer for directory source pathname\n");
659 TgtPath
= calloc (strlen (TargetDirPath
) + strlen (Filename
) + 2, 1);
662 printf ("Could not allocate buffer for directory target pathname\n");
667 strcpy (SrcPath
, SourceDirPath
);
668 strcat (SrcPath
, "/");
669 strcat (SrcPath
, Filename
);
671 strcpy (TgtPath
, TargetDirPath
);
672 strcat (TgtPath
, "/");
673 strcat (TgtPath
, Filename
);
675 *SourcePath
= SrcPath
;
676 *TargetPath
= TgtPath
;
681 /******************************************************************************
683 * FUNCTION: AsGetFile
685 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
687 ******************************************************************************/
701 /* Binary mode leaves CR/LF pairs */
703 FileHandle
= open (Filename
, O_BINARY
| O_RDONLY
);
706 printf ("Could not open %s\n", Filename
);
710 if (fstat (FileHandle
, &Gbl_StatBuf
))
712 printf ("Could not get file status for %s\n", Filename
);
717 * Create a buffer for the entire file
718 * Add plenty extra buffer to accomodate string replacements
720 Size
= Gbl_StatBuf
.st_size
;
721 Buffer
= calloc (Size
* 2, 1);
724 printf ("Could not allocate buffer of size %d\n", Size
* 2);
728 /* Read the entire file */
730 Size
= read (FileHandle
, Buffer
, Size
);
733 printf ("Could not read the input file %s\n", Filename
);
737 Buffer
[Size
] = 0; /* Null terminate the buffer */
740 /* Check for unix contamination */
742 if (!Gbl_IgnoreLoneLineFeeds
&& AsDetectLoneLineFeeds (Filename
, Buffer
))
748 * Convert all CR/LF pairs to LF only. We do this locally so that
749 * this code is portable across operating systems.
751 AsConvertToLineFeeds (Buffer
);
753 *FileBuffer
= Buffer
;
766 /******************************************************************************
768 * FUNCTION: AsPutFile
770 * DESCRIPTION: Create a new output file and write the entire contents of the
771 * buffer to the new file. Buffer must be a zero terminated string
773 ******************************************************************************/
786 /* Create the target file */
788 OpenFlags
= O_TRUNC
| O_CREAT
| O_WRONLY
| O_BINARY
;
790 if (!(SystemFlags
& FLG_NO_CARRIAGE_RETURNS
))
792 /* Put back the CR before each LF */
794 AsInsertCarriageReturns (FileBuffer
);
797 DestHandle
= open (Pathname
, OpenFlags
, S_IREAD
| S_IWRITE
);
798 if (DestHandle
== -1)
800 perror ("Could not create destination file");
801 printf ("Could not create destination file \"%s\"\n", Pathname
);
805 /* Write the buffer to the file */
807 FileSize
= strlen (FileBuffer
);
808 write (DestHandle
, FileBuffer
, FileSize
);