1 /******************************************************************************
3 * Module Name: aslfiles - File support functions
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.
48 #include "aslcompiler.h"
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslfiles")
54 /* Local prototypes */
57 FlOpenIncludeWithPrefix (
62 #ifdef ACPI_OBSOLETE_FUNCTIONS
64 FlParseInputPathname (
69 /*******************************************************************************
71 * FUNCTION: FlSetLineNumber
73 * PARAMETERS: Op - Parse node for the LINE asl statement
77 * DESCRIPTION: Set the current line number
79 ******************************************************************************/
86 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
87 LineNumber, Gbl_LogicalLineNumber);
89 Gbl_CurrentLineNumber = LineNumber;
90 Gbl_LogicalLineNumber = LineNumber;
94 /*******************************************************************************
96 * FUNCTION: FlSetFilename
98 * PARAMETERS: Op - Parse node for the LINE asl statement
102 * DESCRIPTION: Set the current filename
104 ******************************************************************************/
111 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
112 Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
114 /* No need to free any existing filename */
116 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
120 /*******************************************************************************
122 * FUNCTION: FlAddIncludeDirectory
124 * PARAMETERS: Dir - Directory pathname string
128 * DESCRIPTION: Add a directory the list of include prefix directories.
130 ******************************************************************************/
133 FlAddIncludeDirectory (
136 ASL_INCLUDE_DIR *NewDir;
137 ASL_INCLUDE_DIR *NextDir;
138 ASL_INCLUDE_DIR *PrevDir = NULL;
139 UINT32 NeedsSeparator = 0;
143 DirLength = strlen (Dir);
149 /* Make sure that the pathname ends with a path separator */
151 if ((Dir[DirLength-1] != '/') &&
152 (Dir[DirLength-1] != '\\'))
157 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
158 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
159 strcpy (NewDir->Dir, Dir);
162 strcat (NewDir->Dir, "/");
166 * Preserve command line ordering of -I options by adding new elements
167 * at the end of the list
169 NextDir = Gbl_IncludeDirList;
173 NextDir = NextDir->Next;
178 PrevDir->Next = NewDir;
182 Gbl_IncludeDirList = NewDir;
187 /*******************************************************************************
189 * FUNCTION: FlMergePathnames
191 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or
192 * a zero length string.
193 * FilePathname - The include filename from the source ASL.
195 * RETURN: Merged pathname string
197 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
198 * arrive at a minimal length string. Merge can occur if the
199 * FilePathname is relative to the PrefixDir.
201 ******************************************************************************/
213 DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
214 "Include: FilePathname - \"%s\"\n",
215 PrefixDir, FilePathname);
218 * If there is no prefix directory or if the file pathname is absolute,
219 * just return the original file pathname
221 if (!PrefixDir || (!*PrefixDir) ||
222 (*FilePathname == '/') ||
223 (FilePathname[1] == ':'))
225 Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
226 strcpy (Pathname, FilePathname);
227 goto ConvertBackslashes;
230 /* Need a local copy of the prefix directory path */
232 CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
233 strcpy (CommonPath, PrefixDir);
236 * Walk forward through the file path, and simultaneously backward
237 * through the prefix directory path until there are no more
238 * relative references at the start of the file path.
240 while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
242 /* Remove last element of the prefix directory path */
244 LastElement = strrchr (CommonPath, '/');
247 goto ConcatenatePaths;
250 *LastElement = 0; /* Terminate CommonPath string */
251 FilePathname += 3; /* Point to next path element */
255 * Remove the last element of the prefix directory path (it is the same as
256 * the first element of the file pathname), and build the final merged
259 LastElement = strrchr (CommonPath, '/');
265 /* Build the final merged pathname */
268 Pathname = UtStringCacheCalloc (strlen (CommonPath) + strlen (FilePathname) + 2);
269 if (LastElement && *CommonPath)
271 strcpy (Pathname, CommonPath);
272 strcat (Pathname, "/");
274 strcat (Pathname, FilePathname);
276 /* Convert all backslashes to normal slashes */
279 UtConvertBackslashes (Pathname);
281 DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
287 /*******************************************************************************
289 * FUNCTION: FlOpenIncludeWithPrefix
291 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
293 * Filename - The include filename from the source ASL.
295 * RETURN: Valid file descriptor if successful. Null otherwise.
297 * DESCRIPTION: Open an include file and push it on the input file stack.
299 ******************************************************************************/
302 FlOpenIncludeWithPrefix (
310 /* Build the full pathname to the file */
312 Pathname = FlMergePathnames (PrefixDir, Filename);
314 DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
317 /* Attempt to open the file, push if successful */
319 IncludeFile = fopen (Pathname, "r");
322 fprintf (stderr, "Could not open include file %s\n", Pathname);
323 ACPI_FREE (Pathname);
327 /* Push the include file on the open input file stack */
329 AslPushInputFileStack (IncludeFile, Pathname);
330 return (IncludeFile);
334 /*******************************************************************************
336 * FUNCTION: FlOpenIncludeFile
338 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
342 * DESCRIPTION: Open an include file and push it on the input file stack.
344 ******************************************************************************/
348 ACPI_PARSE_OBJECT *Op)
351 ASL_INCLUDE_DIR *NextDir;
354 /* Op must be valid */
358 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
359 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
360 Gbl_InputByteCount, Gbl_CurrentColumn,
361 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
367 * Flush out the "include ()" statement on this line, start
368 * the actual include file on the next line
370 AslResetCurrentLineBuffer ();
371 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
372 Gbl_CurrentLineOffset++;
375 /* Attempt to open the include file */
377 /* If the file specifies an absolute path, just open it */
379 if ((Op->Asl.Value.String[0] == '/') ||
380 (Op->Asl.Value.String[0] == '\\') ||
381 (Op->Asl.Value.String[1] == ':'))
383 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
392 * The include filename is not an absolute path.
394 * First, search for the file within the "local" directory -- meaning
395 * the same directory that contains the source file.
397 * Construct the file pathname from the global directory name.
399 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
406 * Second, search for the file within the (possibly multiple) directories
407 * specified by the -I option on the command line.
409 NextDir = Gbl_IncludeDirList;
412 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
418 NextDir = NextDir->Next;
421 /* We could not open the include file after trying very hard */
424 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
425 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
429 /*******************************************************************************
431 * FUNCTION: FlOpenInputFile
433 * PARAMETERS: InputFilename - The user-specified ASL source file to be
438 * DESCRIPTION: Open the specified input file, and save the directory path to
439 * the file so that include files can be opened in
440 * the same directory.
442 ******************************************************************************/
449 /* Open the input ASL file, text mode */
451 FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
452 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
458 /*******************************************************************************
460 * FUNCTION: FlOpenAmlOutputFile
462 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
466 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
467 * is created in the same directory as the parent input file.
469 ******************************************************************************/
472 FlOpenAmlOutputFile (
473 char *FilenamePrefix)
478 /* Output filename usually comes from the ASL itself */
480 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
483 /* Create the output AML filename */
485 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
488 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
489 0, 0, 0, 0, NULL, NULL);
493 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = Filename;
496 /* Open the output AML file in binary mode */
498 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
503 /*******************************************************************************
505 * FUNCTION: FlOpenMiscOutputFiles
507 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
511 * DESCRIPTION: Create and open the various output files needed, depending on
512 * the command line options
514 ******************************************************************************/
517 FlOpenMiscOutputFiles (
518 char *FilenamePrefix)
523 /* All done for disassembler */
525 if (Gbl_FileType == ASL_INPUT_TYPE_ACPI_TABLE)
530 /* Create/Open a hex output file if asked */
532 if (Gbl_HexOutputFlag)
534 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
537 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
538 0, 0, 0, 0, NULL, NULL);
542 /* Open the hex file, text mode */
544 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
546 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
547 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
550 /* Create/Open a debug output file if asked */
554 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
557 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
558 0, 0, 0, 0, NULL, NULL);
562 /* Open the debug file as STDERR, text mode */
564 /* TBD: hide this behind a FlReopenFile function */
566 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
567 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
568 freopen (Filename, "w+t", stderr);
570 if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
577 * A problem with freopen is that on error,
578 * we no longer have stderr.
580 Gbl_DebugFlag = FALSE;
585 stderr = fdopen(temp_fd, "w");
587 setvbuf(stderr, NULL, _IONBF, 0);
591 if (stderr == NULL) {
592 /* This is wrong, but better than nothing */
597 memcpy (stderr, stdout, sizeof (FILE));
599 FlFileError (ASL_FILE_DEBUG_OUTPUT, ASL_MSG_DEBUG_FILENAME);
603 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
604 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
607 /* Create/Open a listing output file if asked */
611 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
614 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
615 0, 0, 0, 0, NULL, NULL);
619 /* Open the listing file, text mode */
621 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
623 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
624 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
627 /* Create the preprocessor output file if preprocessor enabled */
629 if (Gbl_PreprocessFlag)
631 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
634 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
635 0, 0, 0, 0, NULL, NULL);
639 FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
642 /* All done for data table compiler */
644 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
649 /* Create/Open a combined source output file */
651 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
654 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
655 0, 0, 0, 0, NULL, NULL);
660 * Open the source output file, binary mode (so that LF does not get
661 * expanded to CR/LF on some systems, messing up our seek
664 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
668 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
670 /* Create/Open a assembly code source output file if asked */
672 if (Gbl_AsmOutputFlag)
674 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
677 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
678 0, 0, 0, 0, NULL, NULL);
682 /* Open the assembly code source file, text mode */
684 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
686 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
687 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
690 /* Create/Open a C code source output file if asked */
692 if (Gbl_C_OutputFlag)
694 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
697 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
698 0, 0, 0, 0, NULL, NULL);
702 /* Open the C code source file, text mode */
704 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
706 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
707 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
708 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
711 /* Create/Open a C code source output file for the offset table if asked */
713 if (Gbl_C_OffsetTableFlag)
715 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);
718 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
719 0, 0, 0, 0, NULL, NULL);
723 /* Open the C code source file, text mode */
725 FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");
727 FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");
728 AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);
729 AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);
732 /* Create/Open a assembly include output file if asked */
734 if (Gbl_AsmIncludeOutputFlag)
736 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
739 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
740 0, 0, 0, 0, NULL, NULL);
744 /* Open the assembly include file, text mode */
746 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
748 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
749 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
752 /* Create/Open a C include output file if asked */
754 if (Gbl_C_IncludeOutputFlag)
756 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
759 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
760 0, 0, 0, 0, NULL, NULL);
764 /* Open the C include file, text mode */
766 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
768 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
769 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
770 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
773 /* Create a namespace output file if asked */
775 if (Gbl_NsOutputFlag)
777 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
780 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
781 0, 0, 0, 0, NULL, NULL);
785 /* Open the namespace file, text mode */
787 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
789 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
790 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
793 /* Create/Open a map file if requested */
797 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_MAP);
800 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
801 0, 0, 0, 0, NULL, NULL);
805 /* Open the hex file, text mode (closed at compiler exit) */
807 FlOpenFile (ASL_FILE_MAP_OUTPUT, Filename, "w+t");
809 AslCompilerSignon (ASL_FILE_MAP_OUTPUT);
810 AslCompilerFileHeader (ASL_FILE_MAP_OUTPUT);
817 #ifdef ACPI_OBSOLETE_FUNCTIONS
818 /*******************************************************************************
820 * FUNCTION: FlParseInputPathname
822 * PARAMETERS: InputFilename - The user-specified ASL source file to be
827 * DESCRIPTION: Split the input path into a directory and filename part
828 * 1) Directory part used to open include files
829 * 2) Filename part used to generate output filenames
831 ******************************************************************************/
834 FlParseInputPathname (
845 /* Get the path to the input filename's directory */
847 Gbl_DirectoryPath = strdup (InputFilename);
848 if (!Gbl_DirectoryPath)
850 return (AE_NO_MEMORY);
853 Substring = strrchr (Gbl_DirectoryPath, '\\');
856 Substring = strrchr (Gbl_DirectoryPath, '/');
859 Substring = strrchr (Gbl_DirectoryPath, ':');
865 Gbl_DirectoryPath[0] = 0;
866 if (Gbl_UseDefaultAmlFilename)
868 Gbl_OutputFilenamePrefix = strdup (InputFilename);
873 if (Gbl_UseDefaultAmlFilename)
875 Gbl_OutputFilenamePrefix = strdup (Substring + 1);
880 UtConvertBackslashes (Gbl_OutputFilenamePrefix);