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 - 2009, 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 (
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) %d lines\n",
350 printf ("%s: %d 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", strlen (FileBuffer
)));
414 if (ConversionTable
->LowerCaseTable
)
416 for (i
= 0; ConversionTable
->LowerCaseTable
[i
].Identifier
; i
++)
418 AsLowerCaseString (ConversionTable
->LowerCaseTable
[i
].Identifier
,
423 /* Process all the string replacements */
427 for (i
= 0; StringTable
[i
].Target
; i
++)
429 AsReplaceString (StringTable
[i
].Target
, StringTable
[i
].Replacement
,
430 StringTable
[i
].Type
, FileBuffer
);
436 for (i
= 0; LineTable
[i
].Identifier
; i
++)
438 AsRemoveLine (FileBuffer
, LineTable
[i
].Identifier
);
442 if (ConditionalTable
)
444 for (i
= 0; ConditionalTable
[i
].Identifier
; i
++)
446 AsRemoveConditionalCompile (FileBuffer
, ConditionalTable
[i
].Identifier
);
452 for (i
= 0; MacroTable
[i
].Identifier
; i
++)
454 AsRemoveMacro (FileBuffer
, MacroTable
[i
].Identifier
);
460 for (i
= 0; StructTable
[i
].Identifier
; i
++)
462 AsInsertPrefix (FileBuffer
, StructTable
[i
].Identifier
, StructTable
[i
].Type
);
466 /* Process the function table */
468 for (i
= 0; i
< 32; i
++)
470 /* Decode the function bitmap */
472 switch ((1 << i
) & Functions
)
475 /* This function not configured */
481 AsCountTabs (FileBuffer
, Filename
);
485 case CVT_COUNT_NON_ANSI_COMMENTS
:
487 AsCountNonAnsiComments (FileBuffer
, Filename
);
491 case CVT_CHECK_BRACES
:
493 AsCheckForBraces (FileBuffer
, Filename
);
499 AsTrimLines (FileBuffer
, Filename
);
503 case CVT_COUNT_LINES
:
505 AsCountSourceLines (FileBuffer
, Filename
);
509 case CVT_BRACES_ON_SAME_LINE
:
511 AsBracesOnSameLine (FileBuffer
);
515 case CVT_MIXED_CASE_TO_UNDERSCORES
:
517 AsMixedCaseToUnderscores (FileBuffer
);
521 case CVT_LOWER_CASE_IDENTIFIERS
:
523 AsLowerCaseIdentifiers (FileBuffer
);
527 case CVT_REMOVE_DEBUG_MACROS
:
529 AsRemoveDebugMacros (FileBuffer
);
533 case CVT_TRIM_WHITESPACE
:
535 AsTrimWhitespace (FileBuffer
);
539 case CVT_REMOVE_EMPTY_BLOCKS
:
541 AsRemoveEmptyBlocks (FileBuffer
, Filename
);
545 case CVT_REDUCE_TYPEDEFS
:
547 AsReduceTypedefs (FileBuffer
, "typedef union");
548 AsReduceTypedefs (FileBuffer
, "typedef struct");
552 case CVT_SPACES_TO_TABS4
:
554 AsTabify4 (FileBuffer
);
558 case CVT_SPACES_TO_TABS8
:
560 AsTabify8 (FileBuffer
);
563 case CVT_COUNT_SHORTMULTILINE_COMMENTS
:
565 #ifdef ACPI_FUTURE_IMPLEMENTATION
566 AsTrimComments (FileBuffer
, Filename
);
572 printf ("Unknown conversion subfunction opcode\n");
577 if (ConversionTable
->NewHeader
)
579 AsReplaceHeader (FileBuffer
, ConversionTable
->NewHeader
);
584 /******************************************************************************
586 * FUNCTION: AsProcessOneFile
588 * DESCRIPTION: Process one source file. The file is opened, read entirely
589 * into a buffer, converted, then written to a new file.
591 ******************************************************************************/
595 ACPI_CONVERSION_TABLE
*ConversionTable
,
600 ACPI_NATIVE_INT FileType
)
603 char *OutPathname
= NULL
;
606 /* Allocate a file pathname buffer for both source and target */
608 Pathname
= calloc (MaxPathLength
+ strlen (Filename
) + 2, 1);
611 printf ("Could not allocate buffer for file pathnames\n");
615 Gbl_FileType
= FileType
;
617 /* Generate the source pathname and read the file */
621 strcpy (Pathname
, SourcePath
);
622 strcat (Pathname
, "/");
625 strcat (Pathname
, Filename
);
627 if (AsGetFile (Pathname
, &Gbl_FileBuffer
, &Gbl_FileSize
))
633 if (strstr (Filename
, ".asl"))
635 Gbl_HeaderSize
= LINES_IN_ASL_HEADER
; /* Lines in default ASL header */
637 else if (strstr (Gbl_FileBuffer
, LEGAL_HEADER_SIGNATURE
))
639 Gbl_HeaderSize
= LINES_IN_LEGAL_HEADER
; /* Normal C file and H header */
641 else if (strstr (Gbl_FileBuffer
, LINUX_HEADER_SIGNATURE
))
643 Gbl_HeaderSize
= LINES_IN_LINUX_HEADER
; /* Linuxized C file and H header */
646 /* Process the file in the buffer */
648 Gbl_MadeChanges
= FALSE
;
649 if (!Gbl_IgnoreLoneLineFeeds
&& Gbl_HasLoneLineFeeds
)
652 * All lone LFs will be converted to CR/LF
653 * (when file is written, Windows version only)
655 printf ("Converting lone linefeeds\n");
656 Gbl_MadeChanges
= TRUE
;
659 AsConvertFile (ConversionTable
, Gbl_FileBuffer
, Pathname
, FileType
);
661 if (!(ConversionTable
->Flags
& FLG_NO_FILE_OUTPUT
))
663 if (!(Gbl_Overwrite
&& !Gbl_MadeChanges
))
665 /* Generate the target pathname and write the file */
667 OutPathname
= calloc (MaxPathLength
+ strlen (Filename
) + 2 + strlen (TargetPath
), 1);
670 printf ("Could not allocate buffer for file pathnames\n");
674 strcpy (OutPathname
, TargetPath
);
677 strcat (OutPathname
, "/");
678 strcat (OutPathname
, Filename
);
681 AsPutFile (OutPathname
, Gbl_FileBuffer
, ConversionTable
->Flags
);
685 free (Gbl_FileBuffer
);
696 /******************************************************************************
698 * FUNCTION: AsCheckForDirectory
700 * DESCRIPTION: Check if the current file is a valid directory. If not,
701 * construct the full pathname for the source and target paths.
702 * Checks for the dot and dot-dot files (they are ignored)
704 ******************************************************************************/
707 AsCheckForDirectory (
718 if (!(strcmp (Filename
, ".")) ||
719 !(strcmp (Filename
, "..")))
724 SrcPath
= calloc (strlen (SourceDirPath
) + strlen (Filename
) + 2, 1);
727 printf ("Could not allocate buffer for directory source pathname\n");
731 TgtPath
= calloc (strlen (TargetDirPath
) + strlen (Filename
) + 2, 1);
734 printf ("Could not allocate buffer for directory target pathname\n");
739 strcpy (SrcPath
, SourceDirPath
);
740 strcat (SrcPath
, "/");
741 strcat (SrcPath
, Filename
);
743 strcpy (TgtPath
, TargetDirPath
);
744 strcat (TgtPath
, "/");
745 strcat (TgtPath
, Filename
);
747 *SourcePath
= SrcPath
;
748 *TargetPath
= TgtPath
;
753 /******************************************************************************
755 * FUNCTION: AsGetFile
757 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
759 ******************************************************************************/
773 /* Binary mode leaves CR/LF pairs */
775 FileHandle
= open (Filename
, O_BINARY
| O_RDONLY
);
778 printf ("Could not open %s\n", Filename
);
782 if (fstat (FileHandle
, &Gbl_StatBuf
))
784 printf ("Could not get file status for %s\n", Filename
);
789 * Create a buffer for the entire file
790 * Add plenty extra buffer to accomodate string replacements
792 Size
= Gbl_StatBuf
.st_size
;
793 Gbl_TotalSize
+= Size
;
795 Buffer
= calloc (Size
* 2, 1);
798 printf ("Could not allocate buffer of size %d\n", Size
* 2);
802 /* Read the entire file */
804 Size
= read (FileHandle
, Buffer
, Size
);
807 printf ("Could not read the input file %s\n", Filename
);
811 Buffer
[Size
] = 0; /* Null terminate the buffer */
814 /* Check for unix contamination */
816 Gbl_HasLoneLineFeeds
= AsDetectLoneLineFeeds (Filename
, Buffer
);
819 * Convert all CR/LF pairs to LF only. We do this locally so that
820 * this code is portable across operating systems.
822 AsConvertToLineFeeds (Buffer
);
824 *FileBuffer
= Buffer
;
837 /******************************************************************************
839 * FUNCTION: AsPutFile
841 * DESCRIPTION: Create a new output file and write the entire contents of the
842 * buffer to the new file. Buffer must be a zero terminated string
844 ******************************************************************************/
857 /* Create the target file */
859 OpenFlags
= O_TRUNC
| O_CREAT
| O_WRONLY
| O_BINARY
;
861 if (!(SystemFlags
& FLG_NO_CARRIAGE_RETURNS
))
863 /* Put back the CR before each LF */
865 AsInsertCarriageReturns (FileBuffer
);
868 DestHandle
= open (Pathname
, OpenFlags
, S_IREAD
| S_IWRITE
);
869 if (DestHandle
== -1)
871 perror ("Could not create destination file");
872 printf ("Could not create destination file \"%s\"\n", Pathname
);
876 /* Write the buffer to the file */
878 FileSize
= strlen (FileBuffer
);
879 write (DestHandle
, FileBuffer
, FileSize
);