1 /******************************************************************************
3 * Module Name: aslfiles - File support functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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 /* No need to free any existing filename */
112 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
116 /*******************************************************************************
118 * FUNCTION: FlAddIncludeDirectory
120 * PARAMETERS: Dir - Directory pathname string
124 * DESCRIPTION: Add a directory the list of include prefix directories.
126 ******************************************************************************/
129 FlAddIncludeDirectory (
132 ASL_INCLUDE_DIR *NewDir;
133 ASL_INCLUDE_DIR *NextDir;
134 ASL_INCLUDE_DIR *PrevDir = NULL;
135 UINT32 NeedsSeparator = 0;
139 DirLength = strlen (Dir);
145 /* Make sure that the pathname ends with a path separator */
147 if ((Dir[DirLength-1] != '/') &&
148 (Dir[DirLength-1] != '\\'))
153 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
154 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
155 strcpy (NewDir->Dir, Dir);
158 strcat (NewDir->Dir, "/");
162 * Preserve command line ordering of -I options by adding new elements
163 * at the end of the list
165 NextDir = Gbl_IncludeDirList;
169 NextDir = NextDir->Next;
174 PrevDir->Next = NewDir;
178 Gbl_IncludeDirList = NewDir;
183 /*******************************************************************************
185 * FUNCTION: FlMergePathnames
187 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or
188 * a zero length string.
189 * FilePathname - The include filename from the source ASL.
191 * RETURN: Merged pathname string
193 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
194 * arrive at a minimal length string. Merge can occur if the
195 * FilePathname is relative to the PrefixDir.
197 ******************************************************************************/
209 DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
210 "Include: FilePathname - \"%s\"\n",
211 PrefixDir, FilePathname);
214 * If there is no prefix directory or if the file pathname is absolute,
215 * just return the original file pathname
217 if (!PrefixDir || (!*PrefixDir) ||
218 (*FilePathname == '/') ||
219 (FilePathname[1] == ':'))
221 Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
222 strcpy (Pathname, FilePathname);
223 goto ConvertBackslashes;
226 /* Need a local copy of the prefix directory path */
228 CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
229 strcpy (CommonPath, PrefixDir);
232 * Walk forward through the file path, and simultaneously backward
233 * through the prefix directory path until there are no more
234 * relative references at the start of the file path.
236 while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
238 /* Remove last element of the prefix directory path */
240 LastElement = strrchr (CommonPath, '/');
243 goto ConcatenatePaths;
246 *LastElement = 0; /* Terminate CommonPath string */
247 FilePathname += 3; /* Point to next path element */
251 * Remove the last element of the prefix directory path (it is the same as
252 * the first element of the file pathname), and build the final merged
255 LastElement = strrchr (CommonPath, '/');
261 /* Build the final merged pathname */
264 Pathname = UtStringCacheCalloc (strlen (CommonPath) + strlen (FilePathname) + 2);
265 if (LastElement && *CommonPath)
267 strcpy (Pathname, CommonPath);
268 strcat (Pathname, "/");
270 strcat (Pathname, FilePathname);
272 /* Convert all backslashes to normal slashes */
275 UtConvertBackslashes (Pathname);
277 DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
283 /*******************************************************************************
285 * FUNCTION: FlOpenIncludeWithPrefix
287 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
289 * Filename - The include filename from the source ASL.
291 * RETURN: Valid file descriptor if successful. Null otherwise.
293 * DESCRIPTION: Open an include file and push it on the input file stack.
295 ******************************************************************************/
298 FlOpenIncludeWithPrefix (
306 /* Build the full pathname to the file */
308 Pathname = FlMergePathnames (PrefixDir, Filename);
310 DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
313 /* Attempt to open the file, push if successful */
315 IncludeFile = fopen (Pathname, "r");
318 fprintf (stderr, "Could not open include file %s\n", Pathname);
319 ACPI_FREE (Pathname);
323 /* Push the include file on the open input file stack */
325 AslPushInputFileStack (IncludeFile, Pathname);
326 return (IncludeFile);
330 /*******************************************************************************
332 * FUNCTION: FlOpenIncludeFile
334 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
338 * DESCRIPTION: Open an include file and push it on the input file stack.
340 ******************************************************************************/
344 ACPI_PARSE_OBJECT *Op)
347 ASL_INCLUDE_DIR *NextDir;
350 /* Op must be valid */
354 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
355 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
356 Gbl_InputByteCount, Gbl_CurrentColumn,
357 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
363 * Flush out the "include ()" statement on this line, start
364 * the actual include file on the next line
366 AslResetCurrentLineBuffer ();
367 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
368 Gbl_CurrentLineOffset++;
371 /* Attempt to open the include file */
373 /* If the file specifies an absolute path, just open it */
375 if ((Op->Asl.Value.String[0] == '/') ||
376 (Op->Asl.Value.String[0] == '\\') ||
377 (Op->Asl.Value.String[1] == ':'))
379 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
388 * The include filename is not an absolute path.
390 * First, search for the file within the "local" directory -- meaning
391 * the same directory that contains the source file.
393 * Construct the file pathname from the global directory name.
395 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
402 * Second, search for the file within the (possibly multiple) directories
403 * specified by the -I option on the command line.
405 NextDir = Gbl_IncludeDirList;
408 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
414 NextDir = NextDir->Next;
417 /* We could not open the include file after trying very hard */
420 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
421 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
425 /*******************************************************************************
427 * FUNCTION: FlOpenInputFile
429 * PARAMETERS: InputFilename - The user-specified ASL source file to be
434 * DESCRIPTION: Open the specified input file, and save the directory path to
435 * the file so that include files can be opened in
436 * the same directory.
438 ******************************************************************************/
445 /* Open the input ASL file, text mode */
447 FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
448 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
454 /*******************************************************************************
456 * FUNCTION: FlOpenAmlOutputFile
458 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
462 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
463 * is created in the same directory as the parent input file.
465 ******************************************************************************/
468 FlOpenAmlOutputFile (
469 char *FilenamePrefix)
474 /* Output filename usually comes from the ASL itself */
476 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
479 /* Create the output AML filename */
481 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
484 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
485 0, 0, 0, 0, NULL, NULL);
490 /* Open the output AML file in binary mode */
492 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
497 /*******************************************************************************
499 * FUNCTION: FlOpenMiscOutputFiles
501 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
505 * DESCRIPTION: Create and open the various output files needed, depending on
506 * the command line options
508 ******************************************************************************/
511 FlOpenMiscOutputFiles (
512 char *FilenamePrefix)
517 /* All done for disassembler */
519 if (Gbl_FileType == ASL_INPUT_TYPE_ACPI_TABLE)
524 /* Create/Open a hex output file if asked */
526 if (Gbl_HexOutputFlag)
528 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
531 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
532 0, 0, 0, 0, NULL, NULL);
536 /* Open the hex file, text mode */
538 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
540 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
541 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
544 /* Create/Open a debug output file if asked */
548 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
551 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
552 0, 0, 0, 0, NULL, NULL);
556 /* Open the debug file as STDERR, text mode */
558 /* TBD: hide this behind a FlReopenFile function */
560 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
561 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
562 freopen (Filename, "w+t", stderr);
564 if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
566 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
567 0, 0, 0, 0, NULL, NULL);
571 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
572 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
575 /* Create/Open a listing output file if asked */
579 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
582 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
583 0, 0, 0, 0, NULL, NULL);
587 /* Open the listing file, text mode */
589 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
591 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
592 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
595 /* Create the preprocessor output file if preprocessor enabled */
597 if (Gbl_PreprocessFlag)
599 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
602 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
603 0, 0, 0, 0, NULL, NULL);
607 FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
610 /* All done for data table compiler */
612 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
617 /* Create/Open a combined source output file */
619 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
622 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
623 0, 0, 0, 0, NULL, NULL);
628 * Open the source output file, binary mode (so that LF does not get
629 * expanded to CR/LF on some systems, messing up our seek
632 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
636 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
638 /* Create/Open a assembly code source output file if asked */
640 if (Gbl_AsmOutputFlag)
642 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
645 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
646 0, 0, 0, 0, NULL, NULL);
650 /* Open the assembly code source file, text mode */
652 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
654 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
655 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
658 /* Create/Open a C code source output file if asked */
660 if (Gbl_C_OutputFlag)
662 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
665 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
666 0, 0, 0, 0, NULL, NULL);
670 /* Open the C code source file, text mode */
672 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
674 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
675 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
676 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
679 /* Create/Open a C code source output file for the offset table if asked */
681 if (Gbl_C_OffsetTableFlag)
683 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);
686 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
687 0, 0, 0, 0, NULL, NULL);
691 /* Open the C code source file, text mode */
693 FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");
695 FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");
696 AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);
697 AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);
700 /* Create/Open a assembly include output file if asked */
702 if (Gbl_AsmIncludeOutputFlag)
704 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
707 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
708 0, 0, 0, 0, NULL, NULL);
712 /* Open the assembly include file, text mode */
714 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
716 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
717 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
720 /* Create/Open a C include output file if asked */
722 if (Gbl_C_IncludeOutputFlag)
724 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
727 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
728 0, 0, 0, 0, NULL, NULL);
732 /* Open the C include file, text mode */
734 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
736 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
737 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
738 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
741 /* Create a namespace output file if asked */
743 if (Gbl_NsOutputFlag)
745 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
748 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
749 0, 0, 0, 0, NULL, NULL);
753 /* Open the namespace file, text mode */
755 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
757 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
758 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
765 #ifdef ACPI_OBSOLETE_FUNCTIONS
766 /*******************************************************************************
768 * FUNCTION: FlParseInputPathname
770 * PARAMETERS: InputFilename - The user-specified ASL source file to be
775 * DESCRIPTION: Split the input path into a directory and filename part
776 * 1) Directory part used to open include files
777 * 2) Filename part used to generate output filenames
779 ******************************************************************************/
782 FlParseInputPathname (
793 /* Get the path to the input filename's directory */
795 Gbl_DirectoryPath = strdup (InputFilename);
796 if (!Gbl_DirectoryPath)
798 return (AE_NO_MEMORY);
801 Substring = strrchr (Gbl_DirectoryPath, '\\');
804 Substring = strrchr (Gbl_DirectoryPath, '/');
807 Substring = strrchr (Gbl_DirectoryPath, ':');
813 Gbl_DirectoryPath[0] = 0;
814 if (Gbl_UseDefaultAmlFilename)
816 Gbl_OutputFilenamePrefix = strdup (InputFilename);
821 if (Gbl_UseDefaultAmlFilename)
823 Gbl_OutputFilenamePrefix = strdup (Substring + 1);
828 UtConvertBackslashes (Gbl_OutputFilenamePrefix);