2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2006, 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 *****************************************************************************/
118 #include "aslcompiler.h"
121 #define _COMPONENT ACPI_COMPILER
122 ACPI_MODULE_NAME ("aslfiles")
124 /* Local prototypes */
137 #ifdef ACPI_OBSOLETE_FUNCTIONS
139 FlParseInputPathname (
140 char *InputFilename
);
144 /*******************************************************************************
152 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
155 ******************************************************************************/
162 AePrintErrorLog (ASL_FILE_STDOUT
);
165 /* Print error summary to the debug file */
167 AePrintErrorLog (ASL_FILE_STDERR
);
174 /*******************************************************************************
176 * FUNCTION: FlOpenLocalFile
178 * PARAMETERS: LocalName - Single filename (not a pathname)
179 * Mode - Open mode for fopen
181 * RETURN: File descriptor
183 * DESCRIPTION: Build a complete pathname for the input filename and open
186 ******************************************************************************/
194 strcpy (StringBuffer
, Gbl_DirectoryPath
);
195 strcat (StringBuffer
, LocalName
);
197 DbgPrint (ASL_PARSE_OUTPUT
, "FlOpenLocalFile: %s\n", StringBuffer
);
198 return (fopen (StringBuffer
, (const char *) Mode
));
202 /*******************************************************************************
204 * FUNCTION: FlFileError
206 * PARAMETERS: FileId - Index into file info array
207 * ErrorId - Index into error message array
211 * DESCRIPTION: Decode errno to an error message and add the entire error
214 ******************************************************************************/
222 sprintf (MsgBuffer
, "\"%s\" (%s)", Gbl_Files
[FileId
].Filename
,
224 AslCommonError (ASL_ERROR
, ErrorId
, 0, 0, 0, 0, NULL
, MsgBuffer
);
228 /*******************************************************************************
230 * FUNCTION: FlOpenFile
232 * PARAMETERS: FileId - Index into file info array
233 * Filename - file pathname to open
234 * Mode - Open mode for fopen
238 * DESCRIPTION: Open a file.
239 * NOTE: Aborts compiler on any error.
241 ******************************************************************************/
252 File
= fopen (Filename
, Mode
);
254 Gbl_Files
[FileId
].Filename
= Filename
;
255 Gbl_Files
[FileId
].Handle
= File
;
259 FlFileError (FileId
, ASL_MSG_OPEN
);
265 /*******************************************************************************
267 * FUNCTION: FlReadFile
269 * PARAMETERS: FileId - Index into file info array
270 * Buffer - Where to place the data
271 * Length - Amount to read
273 * RETURN: Status. AE_ERROR indicates EOF.
275 * DESCRIPTION: Read data from an open file.
276 * NOTE: Aborts compiler on any error.
278 ******************************************************************************/
289 /* Read and check for error */
291 Actual
= fread (Buffer
, 1, Length
, Gbl_Files
[FileId
].Handle
);
292 if (Actual
!= Length
)
294 if (feof (Gbl_Files
[FileId
].Handle
))
296 /* End-of-file, just return error */
301 FlFileError (FileId
, ASL_MSG_READ
);
309 /*******************************************************************************
311 * FUNCTION: FlWriteFile
313 * PARAMETERS: FileId - Index into file info array
314 * Buffer - Data to write
315 * Length - Amount of data to write
319 * DESCRIPTION: Write data to an open file.
320 * NOTE: Aborts compiler on any error.
322 ******************************************************************************/
333 /* Write and check for error */
335 Actual
= fwrite ((char *) Buffer
, 1, Length
, Gbl_Files
[FileId
].Handle
);
336 if (Actual
!= Length
)
338 FlFileError (FileId
, ASL_MSG_WRITE
);
344 /*******************************************************************************
346 * FUNCTION: FlPrintFile
348 * PARAMETERS: FileId - Index into file info array
349 * Format - Printf format string
350 * ... - Printf arguments
354 * DESCRIPTION: Formatted write to an open file.
355 * NOTE: Aborts compiler on any error.
357 ******************************************************************************/
369 va_start (Args
, Format
);
371 Actual
= vfprintf (Gbl_Files
[FileId
].Handle
, Format
, Args
);
374 FlFileError (FileId
, ASL_MSG_WRITE
);
380 /*******************************************************************************
382 * FUNCTION: FlSeekFile
384 * PARAMETERS: FileId - Index into file info array
385 * Offset - Absolute byte offset in file
389 * DESCRIPTION: Seek to absolute offset
390 * NOTE: Aborts compiler on any error.
392 ******************************************************************************/
402 Error
= fseek (Gbl_Files
[FileId
].Handle
, Offset
, SEEK_SET
);
405 FlFileError (FileId
, ASL_MSG_SEEK
);
411 /*******************************************************************************
413 * FUNCTION: FlCloseFile
415 * PARAMETERS: FileId - Index into file info array
419 * DESCRIPTION: Close an open file. Aborts compiler on error
421 ******************************************************************************/
430 if (!Gbl_Files
[FileId
].Handle
)
435 Error
= fclose (Gbl_Files
[FileId
].Handle
);
436 Gbl_Files
[FileId
].Handle
= NULL
;
440 FlFileError (FileId
, ASL_MSG_CLOSE
);
448 /*******************************************************************************
450 * FUNCTION: FlSetLineNumber
452 * PARAMETERS: Op - Parse node for the LINE asl statement
456 * DESCRIPTION: Set the current line number
458 ******************************************************************************/
462 ACPI_PARSE_OBJECT
*Op
)
465 Gbl_CurrentLineNumber
= (UINT32
) Op
->Asl
.Value
.Integer
;
466 Gbl_LogicalLineNumber
= (UINT32
) Op
->Asl
.Value
.Integer
;
470 /*******************************************************************************
472 * FUNCTION: FlOpenIncludeFile
474 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
478 * DESCRIPTION: Open an include file and push it on the input file stack.
480 ******************************************************************************/
484 ACPI_PARSE_OBJECT
*Op
)
489 /* Op must be valid */
493 AslCommonError (ASL_ERROR
, ASL_MSG_INCLUDE_FILE_OPEN
,
494 Gbl_CurrentLineNumber
, Gbl_LogicalLineNumber
,
495 Gbl_InputByteCount
, Gbl_CurrentColumn
,
496 Gbl_Files
[ASL_FILE_INPUT
].Filename
, " - Null parse node");
502 * Flush out the "include ()" statement on this line, start
503 * the actual include file on the next line
505 ResetCurrentLineBuffer ();
506 FlPrintFile (ASL_FILE_SOURCE_OUTPUT
, "\n");
507 Gbl_CurrentLineOffset
++;
509 /* Prepend the directory pathname and open the include file */
511 DbgPrint (ASL_PARSE_OUTPUT
, "\nOpen include file: path %s\n\n",
512 Op
->Asl
.Value
.String
);
513 IncFile
= FlOpenLocalFile (Op
->Asl
.Value
.String
, "r");
516 sprintf (MsgBuffer
, "%s (%s)", Op
->Asl
.Value
.String
, strerror (errno
));
517 AslError (ASL_ERROR
, ASL_MSG_INCLUDE_FILE_OPEN
, Op
, MsgBuffer
);
521 /* Push the include file on the open input file stack */
523 AslPushInputFileStack (IncFile
, Op
->Asl
.Value
.String
);
527 /*******************************************************************************
529 * FUNCTION: FlOpenInputFile
531 * PARAMETERS: InputFilename - The user-specified ASL source file to be
536 * DESCRIPTION: Open the specified input file, and save the directory path to
537 * the file so that include files can be opened in
538 * the same directory.
540 ******************************************************************************/
547 /* Open the input ASL file, text mode */
549 FlOpenFile (ASL_FILE_INPUT
, InputFilename
, "r");
550 AslCompilerin
= Gbl_Files
[ASL_FILE_INPUT
].Handle
;
556 /*******************************************************************************
558 * FUNCTION: FlOpenAmlOutputFile
560 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
564 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
565 * is created in the same directory as the parent input file.
567 ******************************************************************************/
570 FlOpenAmlOutputFile (
571 char *FilenamePrefix
)
576 /* Output filename usually comes from the ASL itself */
578 Filename
= Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
;
581 /* Create the output AML filename */
583 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_AML_CODE
);
586 AslCommonError (ASL_ERROR
, ASL_MSG_OUTPUT_FILENAME
,
587 0, 0, 0, 0, NULL
, NULL
);
592 /* Open the output AML file in binary mode */
594 FlOpenFile (ASL_FILE_AML_OUTPUT
, Filename
, "w+b");
599 /*******************************************************************************
601 * FUNCTION: FlOpenMiscOutputFiles
603 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
607 * DESCRIPTION: Create and open the various output files needed, depending on
608 * the command line options
610 ******************************************************************************/
613 FlOpenMiscOutputFiles (
614 char *FilenamePrefix
)
619 /* Create/Open a combined source output file */
621 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_SOURCE
);
624 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
625 0, 0, 0, 0, NULL
, NULL
);
630 * Open the source output file, binary mode (so that LF does not get
631 * expanded to CR/LF on some systems, messing up our seek
634 FlOpenFile (ASL_FILE_SOURCE_OUTPUT
, Filename
, "w+b");
636 /* Create/Open a listing output file if asked */
640 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_LISTING
);
643 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
644 0, 0, 0, 0, NULL
, NULL
);
648 /* Open the listing file, text mode */
650 FlOpenFile (ASL_FILE_LISTING_OUTPUT
, Filename
, "w+");
652 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT
);
653 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT
);
656 /* Create/Open a assembly code source output file if asked */
658 if (Gbl_AsmOutputFlag
)
660 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_ASM_SOURCE
);
663 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
664 0, 0, 0, 0, NULL
, NULL
);
668 /* Open the assembly code source file, text mode */
670 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT
, Filename
, "w+");
672 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT
);
673 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT
);
676 /* Create/Open a C code source output file if asked */
678 if (Gbl_C_OutputFlag
)
680 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_C_SOURCE
);
683 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
684 0, 0, 0, 0, NULL
, NULL
);
688 /* Open the C code source file, text mode */
690 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT
, Filename
, "w+");
692 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT
, "/*\n");
693 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT
);
694 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT
);
697 /* Create/Open a assembly include output file if asked */
699 if (Gbl_AsmIncludeOutputFlag
)
701 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_ASM_INCLUDE
);
704 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
705 0, 0, 0, 0, NULL
, NULL
);
709 /* Open the assembly include file, text mode */
711 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT
, Filename
, "w+");
713 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT
);
714 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT
);
717 /* Create/Open a C include output file if asked */
719 if (Gbl_C_IncludeOutputFlag
)
721 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_C_INCLUDE
);
724 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
725 0, 0, 0, 0, NULL
, NULL
);
729 /* Open the C include file, text mode */
731 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT
, Filename
, "w+");
733 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT
, "/*\n");
734 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT
);
735 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT
);
738 /* Create/Open a hex output file if asked */
740 if (Gbl_HexOutputFlag
)
742 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_HEX_DUMP
);
745 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
746 0, 0, 0, 0, NULL
, NULL
);
750 /* Open the hex file, text mode */
752 FlOpenFile (ASL_FILE_HEX_OUTPUT
, Filename
, "w+");
754 AslCompilerSignon (ASL_FILE_HEX_OUTPUT
);
755 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT
);
758 /* Create a namespace output file if asked */
760 if (Gbl_NsOutputFlag
)
762 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_NAMESPACE
);
765 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
766 0, 0, 0, 0, NULL
, NULL
);
770 /* Open the namespace file, text mode */
772 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT
, Filename
, "w+");
774 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT
);
775 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT
);
778 /* Create/Open a debug output file if asked */
782 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_DEBUG
);
785 AslCommonError (ASL_ERROR
, ASL_MSG_DEBUG_FILENAME
,
786 0, 0, 0, 0, NULL
, NULL
);
790 /* Open the debug file as STDERR, text mode */
792 /* TBD: hide this behind a FlReopenFile function */
794 Gbl_Files
[ASL_FILE_DEBUG_OUTPUT
].Filename
= Filename
;
795 Gbl_Files
[ASL_FILE_DEBUG_OUTPUT
].Handle
=
796 freopen (Filename
, "w+t", stderr
);
798 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT
);
799 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT
);
806 #ifdef ACPI_OBSOLETE_FUNCTIONS
807 /*******************************************************************************
809 * FUNCTION: FlParseInputPathname
811 * PARAMETERS: InputFilename - The user-specified ASL source file to be
816 * DESCRIPTION: Split the input path into a directory and filename part
817 * 1) Directory part used to open include files
818 * 2) Filename part used to generate output filenames
820 ******************************************************************************/
823 FlParseInputPathname (
834 /* Get the path to the input filename's directory */
836 Gbl_DirectoryPath
= strdup (InputFilename
);
837 if (!Gbl_DirectoryPath
)
839 return (AE_NO_MEMORY
);
842 Substring
= strrchr (Gbl_DirectoryPath
, '\\');
845 Substring
= strrchr (Gbl_DirectoryPath
, '/');
848 Substring
= strrchr (Gbl_DirectoryPath
, ':');
854 Gbl_DirectoryPath
[0] = 0;
855 if (Gbl_UseDefaultAmlFilename
)
857 Gbl_OutputFilenamePrefix
= strdup (InputFilename
);
862 if (Gbl_UseDefaultAmlFilename
)
864 Gbl_OutputFilenamePrefix
= strdup (Substring
+ 1);