1 /******************************************************************************
3 * Module Name: aslfiles - File support functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
44 #include "aslcompiler.h"
47 #define _COMPONENT ACPI_COMPILER
48 ACPI_MODULE_NAME ("aslfiles")
50 /* Local prototypes */
53 FlOpenIncludeWithPrefix (
58 #ifdef ACPI_OBSOLETE_FUNCTIONS
60 FlParseInputPathname (
65 /*******************************************************************************
67 * FUNCTION: FlSetLineNumber
69 * PARAMETERS: Op - Parse node for the LINE asl statement
73 * DESCRIPTION: Set the current line number
75 ******************************************************************************/
82 DbgPrint (ASL_PARSE_OUTPUT
, "\n#line: New line number %u (old %u)\n",
83 LineNumber
, Gbl_LogicalLineNumber
);
85 Gbl_CurrentLineNumber
= LineNumber
;
86 Gbl_LogicalLineNumber
= LineNumber
;
90 /*******************************************************************************
92 * FUNCTION: FlSetFilename
94 * PARAMETERS: Op - Parse node for the LINE asl statement
98 * DESCRIPTION: Set the current filename
100 ******************************************************************************/
107 DbgPrint (ASL_PARSE_OUTPUT
, "\n#line: New filename %s (old %s)\n",
108 Filename
, Gbl_Files
[ASL_FILE_INPUT
].Filename
);
110 Gbl_Files
[ASL_FILE_INPUT
].Filename
= Filename
;
114 /*******************************************************************************
116 * FUNCTION: FlAddIncludeDirectory
118 * PARAMETERS: Dir - Directory pathname string
122 * DESCRIPTION: Add a directory the list of include prefix directories.
124 ******************************************************************************/
127 FlAddIncludeDirectory (
130 ASL_INCLUDE_DIR
*NewDir
;
131 ASL_INCLUDE_DIR
*NextDir
;
132 ASL_INCLUDE_DIR
*PrevDir
= NULL
;
133 UINT32 NeedsSeparator
= 0;
137 DirLength
= strlen (Dir
);
143 /* Make sure that the pathname ends with a path separator */
145 if ((Dir
[DirLength
-1] != '/') &&
146 (Dir
[DirLength
-1] != '\\'))
151 NewDir
= ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR
));
152 NewDir
->Dir
= ACPI_ALLOCATE (DirLength
+ 1 + NeedsSeparator
);
153 strcpy (NewDir
->Dir
, Dir
);
156 strcat (NewDir
->Dir
, "/");
160 * Preserve command line ordering of -I options by adding new elements
161 * at the end of the list
163 NextDir
= Gbl_IncludeDirList
;
167 NextDir
= NextDir
->Next
;
172 PrevDir
->Next
= NewDir
;
176 Gbl_IncludeDirList
= NewDir
;
181 /*******************************************************************************
183 * FUNCTION: FlMergePathnames
185 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or
186 * a zero length string.
187 * FilePathname - The include filename from the source ASL.
189 * RETURN: Merged pathname string
191 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
192 * arrive at a minimal length string. Merge can occur if the
193 * FilePathname is relative to the PrefixDir.
195 ******************************************************************************/
207 DbgPrint (ASL_PARSE_OUTPUT
, "Include: Prefix path - \"%s\"\n"
208 "Include: FilePathname - \"%s\"\n",
209 PrefixDir
, FilePathname
);
212 * If there is no prefix directory or if the file pathname is absolute,
213 * just return the original file pathname
215 if (!PrefixDir
|| (!*PrefixDir
) ||
216 (*FilePathname
== '/') ||
217 (FilePathname
[1] == ':'))
219 Pathname
= ACPI_ALLOCATE (strlen (FilePathname
) + 1);
220 strcpy (Pathname
, FilePathname
);
221 goto ConvertBackslashes
;
224 /* Need a local copy of the prefix directory path */
226 CommonPath
= ACPI_ALLOCATE (strlen (PrefixDir
) + 1);
227 strcpy (CommonPath
, PrefixDir
);
230 * Walk forward through the file path, and simultaneously backward
231 * through the prefix directory path until there are no more
232 * relative references at the start of the file path.
234 while (*FilePathname
&& (!strncmp (FilePathname
, "../", 3)))
236 /* Remove last element of the prefix directory path */
238 LastElement
= strrchr (CommonPath
, '/');
241 goto ConcatenatePaths
;
244 *LastElement
= 0; /* Terminate CommonPath string */
245 FilePathname
+= 3; /* Point to next path element */
249 * Remove the last element of the prefix directory path (it is the same as
250 * the first element of the file pathname), and build the final merged
253 LastElement
= strrchr (CommonPath
, '/');
259 /* Build the final merged pathname */
262 Pathname
= ACPI_ALLOCATE_ZEROED (strlen (CommonPath
) + strlen (FilePathname
) + 2);
263 if (LastElement
&& *CommonPath
)
265 strcpy (Pathname
, CommonPath
);
266 strcat (Pathname
, "/");
268 strcat (Pathname
, FilePathname
);
269 ACPI_FREE (CommonPath
);
271 /* Convert all backslashes to normal slashes */
274 UtConvertBackslashes (Pathname
);
276 DbgPrint (ASL_PARSE_OUTPUT
, "Include: Merged Pathname - \"%s\"\n",
282 /*******************************************************************************
284 * FUNCTION: FlOpenIncludeWithPrefix
286 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
288 * Filename - The include filename from the source ASL.
290 * RETURN: Valid file descriptor if successful. Null otherwise.
292 * DESCRIPTION: Open an include file and push it on the input file stack.
294 ******************************************************************************/
297 FlOpenIncludeWithPrefix (
305 /* Build the full pathname to the file */
307 Pathname
= FlMergePathnames (PrefixDir
, Filename
);
309 DbgPrint (ASL_PARSE_OUTPUT
, "Include: Opening file - \"%s\"\n\n",
312 /* Attempt to open the file, push if successful */
314 IncludeFile
= fopen (Pathname
, "r");
317 fprintf (stderr
, "Could not open include file %s\n", Pathname
);
318 ACPI_FREE (Pathname
);
322 /* Push the include file on the open input file stack */
324 AslPushInputFileStack (IncludeFile
, Pathname
);
325 return (IncludeFile
);
329 /*******************************************************************************
331 * FUNCTION: FlOpenIncludeFile
333 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
337 * DESCRIPTION: Open an include file and push it on the input file stack.
339 ******************************************************************************/
343 ACPI_PARSE_OBJECT
*Op
)
346 ASL_INCLUDE_DIR
*NextDir
;
349 /* Op must be valid */
353 AslCommonError (ASL_ERROR
, ASL_MSG_INCLUDE_FILE_OPEN
,
354 Gbl_CurrentLineNumber
, Gbl_LogicalLineNumber
,
355 Gbl_InputByteCount
, Gbl_CurrentColumn
,
356 Gbl_Files
[ASL_FILE_INPUT
].Filename
, " - Null parse node");
362 * Flush out the "include ()" statement on this line, start
363 * the actual include file on the next line
365 AslResetCurrentLineBuffer ();
366 FlPrintFile (ASL_FILE_SOURCE_OUTPUT
, "\n");
367 Gbl_CurrentLineOffset
++;
370 /* Attempt to open the include file */
372 /* If the file specifies an absolute path, just open it */
374 if ((Op
->Asl
.Value
.String
[0] == '/') ||
375 (Op
->Asl
.Value
.String
[0] == '\\') ||
376 (Op
->Asl
.Value
.String
[1] == ':'))
378 IncludeFile
= FlOpenIncludeWithPrefix ("", Op
->Asl
.Value
.String
);
387 * The include filename is not an absolute path.
389 * First, search for the file within the "local" directory -- meaning
390 * the same directory that contains the source file.
392 * Construct the file pathname from the global directory name.
394 IncludeFile
= FlOpenIncludeWithPrefix (Gbl_DirectoryPath
, Op
->Asl
.Value
.String
);
401 * Second, search for the file within the (possibly multiple) directories
402 * specified by the -I option on the command line.
404 NextDir
= Gbl_IncludeDirList
;
407 IncludeFile
= FlOpenIncludeWithPrefix (NextDir
->Dir
, Op
->Asl
.Value
.String
);
413 NextDir
= NextDir
->Next
;
416 /* We could not open the include file after trying very hard */
419 sprintf (MsgBuffer
, "%s, %s", Op
->Asl
.Value
.String
, strerror (errno
));
420 AslError (ASL_ERROR
, ASL_MSG_INCLUDE_FILE_OPEN
, Op
, MsgBuffer
);
424 /*******************************************************************************
426 * FUNCTION: FlOpenInputFile
428 * PARAMETERS: InputFilename - The user-specified ASL source file to be
433 * DESCRIPTION: Open the specified input file, and save the directory path to
434 * the file so that include files can be opened in
435 * the same directory.
437 ******************************************************************************/
444 /* Open the input ASL file, text mode */
446 FlOpenFile (ASL_FILE_INPUT
, InputFilename
, "rt");
447 AslCompilerin
= Gbl_Files
[ASL_FILE_INPUT
].Handle
;
453 /*******************************************************************************
455 * FUNCTION: FlOpenAmlOutputFile
457 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
461 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
462 * is created in the same directory as the parent input file.
464 ******************************************************************************/
467 FlOpenAmlOutputFile (
468 char *FilenamePrefix
)
473 /* Output filename usually comes from the ASL itself */
475 Filename
= Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
;
478 /* Create the output AML filename */
480 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_AML_CODE
);
483 AslCommonError (ASL_ERROR
, ASL_MSG_OUTPUT_FILENAME
,
484 0, 0, 0, 0, NULL
, NULL
);
489 /* Open the output AML file in binary mode */
491 FlOpenFile (ASL_FILE_AML_OUTPUT
, Filename
, "w+b");
496 /*******************************************************************************
498 * FUNCTION: FlOpenMiscOutputFiles
500 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
504 * DESCRIPTION: Create and open the various output files needed, depending on
505 * the command line options
507 ******************************************************************************/
510 FlOpenMiscOutputFiles (
511 char *FilenamePrefix
)
516 /* All done for disassembler */
518 if (Gbl_FileType
== ASL_INPUT_TYPE_ACPI_TABLE
)
523 /* Create/Open a hex output file if asked */
525 if (Gbl_HexOutputFlag
)
527 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_HEX_DUMP
);
530 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
531 0, 0, 0, 0, NULL
, NULL
);
535 /* Open the hex file, text mode */
537 FlOpenFile (ASL_FILE_HEX_OUTPUT
, Filename
, "w+t");
539 AslCompilerSignon (ASL_FILE_HEX_OUTPUT
);
540 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT
);
543 /* Create/Open a debug output file if asked */
547 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_DEBUG
);
550 AslCommonError (ASL_ERROR
, ASL_MSG_DEBUG_FILENAME
,
551 0, 0, 0, 0, NULL
, NULL
);
555 /* Open the debug file as STDERR, text mode */
557 /* TBD: hide this behind a FlReopenFile function */
559 Gbl_Files
[ASL_FILE_DEBUG_OUTPUT
].Filename
= Filename
;
560 Gbl_Files
[ASL_FILE_DEBUG_OUTPUT
].Handle
=
561 freopen (Filename
, "w+t", stderr
);
563 if (!Gbl_Files
[ASL_FILE_DEBUG_OUTPUT
].Handle
)
565 AslCommonError (ASL_ERROR
, ASL_MSG_DEBUG_FILENAME
,
566 0, 0, 0, 0, NULL
, NULL
);
570 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT
);
571 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT
);
574 /* Create/Open a listing output file if asked */
578 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_LISTING
);
581 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
582 0, 0, 0, 0, NULL
, NULL
);
586 /* Open the listing file, text mode */
588 FlOpenFile (ASL_FILE_LISTING_OUTPUT
, Filename
, "w+t");
590 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT
);
591 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT
);
594 /* Create the preprocessor output file if preprocessor enabled */
596 if (Gbl_PreprocessFlag
)
598 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_PREPROCESSOR
);
601 AslCommonError (ASL_ERROR
, ASL_MSG_PREPROCESSOR_FILENAME
,
602 0, 0, 0, 0, NULL
, NULL
);
606 FlOpenFile (ASL_FILE_PREPROCESSOR
, Filename
, "w+t");
609 /* All done for data table compiler */
611 if (Gbl_FileType
== ASL_INPUT_TYPE_ASCII_DATA
)
616 /* Create/Open a combined source output file */
618 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_SOURCE
);
621 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
622 0, 0, 0, 0, NULL
, NULL
);
627 * Open the source output file, binary mode (so that LF does not get
628 * expanded to CR/LF on some systems, messing up our seek
631 FlOpenFile (ASL_FILE_SOURCE_OUTPUT
, Filename
, "w+b");
635 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
637 /* Create/Open a assembly code source output file if asked */
639 if (Gbl_AsmOutputFlag
)
641 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_ASM_SOURCE
);
644 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
645 0, 0, 0, 0, NULL
, NULL
);
649 /* Open the assembly code source file, text mode */
651 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT
, Filename
, "w+t");
653 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT
);
654 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT
);
657 /* Create/Open a C code source output file if asked */
659 if (Gbl_C_OutputFlag
)
661 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_C_SOURCE
);
664 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
665 0, 0, 0, 0, NULL
, NULL
);
669 /* Open the C code source file, text mode */
671 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT
, Filename
, "w+t");
673 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT
, "/*\n");
674 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT
);
675 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT
);
678 /* Create/Open a C code source output file for the offset table if asked */
680 if (Gbl_C_OffsetTableFlag
)
682 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_C_OFFSET
);
685 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
686 0, 0, 0, 0, NULL
, NULL
);
690 /* Open the C code source file, text mode */
692 FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT
, Filename
, "w+t");
694 FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT
, "/*\n");
695 AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT
);
696 AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT
);
699 /* Create/Open a assembly include output file if asked */
701 if (Gbl_AsmIncludeOutputFlag
)
703 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_ASM_INCLUDE
);
706 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
707 0, 0, 0, 0, NULL
, NULL
);
711 /* Open the assembly include file, text mode */
713 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT
, Filename
, "w+t");
715 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT
);
716 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT
);
719 /* Create/Open a C include output file if asked */
721 if (Gbl_C_IncludeOutputFlag
)
723 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_C_INCLUDE
);
726 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
727 0, 0, 0, 0, NULL
, NULL
);
731 /* Open the C include file, text mode */
733 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT
, Filename
, "w+t");
735 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT
, "/*\n");
736 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT
);
737 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT
);
740 /* Create a namespace output file if asked */
742 if (Gbl_NsOutputFlag
)
744 Filename
= FlGenerateFilename (FilenamePrefix
, FILE_SUFFIX_NAMESPACE
);
747 AslCommonError (ASL_ERROR
, ASL_MSG_LISTING_FILENAME
,
748 0, 0, 0, 0, NULL
, NULL
);
752 /* Open the namespace file, text mode */
754 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT
, Filename
, "w+t");
756 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT
);
757 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT
);
764 #ifdef ACPI_OBSOLETE_FUNCTIONS
765 /*******************************************************************************
767 * FUNCTION: FlParseInputPathname
769 * PARAMETERS: InputFilename - The user-specified ASL source file to be
774 * DESCRIPTION: Split the input path into a directory and filename part
775 * 1) Directory part used to open include files
776 * 2) Filename part used to generate output filenames
778 ******************************************************************************/
781 FlParseInputPathname (
792 /* Get the path to the input filename's directory */
794 Gbl_DirectoryPath
= strdup (InputFilename
);
795 if (!Gbl_DirectoryPath
)
797 return (AE_NO_MEMORY
);
800 Substring
= strrchr (Gbl_DirectoryPath
, '\\');
803 Substring
= strrchr (Gbl_DirectoryPath
, '/');
806 Substring
= strrchr (Gbl_DirectoryPath
, ':');
812 Gbl_DirectoryPath
[0] = 0;
813 if (Gbl_UseDefaultAmlFilename
)
815 Gbl_OutputFilenamePrefix
= strdup (InputFilename
);
820 if (Gbl_UseDefaultAmlFilename
)
822 Gbl_OutputFilenamePrefix
= strdup (Substring
+ 1);