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.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/acapps.h>
47 #define _COMPONENT ACPI_COMPILER
48 ACPI_MODULE_NAME ("aslfiles")
50 /* Local prototypes */
53 FlOpenIncludeWithPrefix (
55 ACPI_PARSE_OBJECT *Op,
59 #ifdef ACPI_OBSOLETE_FUNCTIONS
61 FlParseInputPathname (
66 /*******************************************************************************
68 * FUNCTION: FlSetLineNumber
70 * PARAMETERS: Op - Parse node for the LINE asl statement
74 * DESCRIPTION: Set the current line number
76 ******************************************************************************/
83 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
84 LineNumber, Gbl_LogicalLineNumber);
86 Gbl_CurrentLineNumber = LineNumber;
87 Gbl_LogicalLineNumber = LineNumber;
91 /*******************************************************************************
93 * FUNCTION: FlSetFilename
95 * PARAMETERS: Op - Parse node for the LINE asl statement
99 * DESCRIPTION: Set the current filename
101 ******************************************************************************/
108 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
109 Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
111 /* No need to free any existing filename */
113 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
117 /*******************************************************************************
119 * FUNCTION: FlAddIncludeDirectory
121 * PARAMETERS: Dir - Directory pathname string
125 * DESCRIPTION: Add a directory the list of include prefix directories.
127 ******************************************************************************/
130 FlAddIncludeDirectory (
133 ASL_INCLUDE_DIR *NewDir;
134 ASL_INCLUDE_DIR *NextDir;
135 ASL_INCLUDE_DIR *PrevDir = NULL;
136 UINT32 NeedsSeparator = 0;
140 DirLength = strlen (Dir);
146 /* Make sure that the pathname ends with a path separator */
148 if ((Dir[DirLength-1] != '/') &&
149 (Dir[DirLength-1] != '\\'))
154 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
155 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
156 strcpy (NewDir->Dir, Dir);
159 strcat (NewDir->Dir, "/");
163 * Preserve command line ordering of -I options by adding new elements
164 * at the end of the list
166 NextDir = Gbl_IncludeDirList;
170 NextDir = NextDir->Next;
175 PrevDir->Next = NewDir;
179 Gbl_IncludeDirList = NewDir;
184 /*******************************************************************************
186 * FUNCTION: FlMergePathnames
188 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or
189 * a zero length string.
190 * FilePathname - The include filename from the source ASL.
192 * RETURN: Merged pathname string
194 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
195 * arrive at a minimal length string. Merge can occur if the
196 * FilePathname is relative to the PrefixDir.
198 ******************************************************************************/
210 DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
211 "Include: FilePathname - \"%s\"\n",
212 PrefixDir, FilePathname);
215 * If there is no prefix directory or if the file pathname is absolute,
216 * just return the original file pathname
218 if (!PrefixDir || (!*PrefixDir) ||
219 (*FilePathname == '/') ||
220 (FilePathname[1] == ':'))
222 Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
223 strcpy (Pathname, FilePathname);
224 goto ConvertBackslashes;
227 /* Need a local copy of the prefix directory path */
229 CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
230 strcpy (CommonPath, PrefixDir);
233 * Walk forward through the file path, and simultaneously backward
234 * through the prefix directory path until there are no more
235 * relative references at the start of the file path.
237 while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
239 /* Remove last element of the prefix directory path */
241 LastElement = strrchr (CommonPath, '/');
244 goto ConcatenatePaths;
247 *LastElement = 0; /* Terminate CommonPath string */
248 FilePathname += 3; /* Point to next path element */
252 * Remove the last element of the prefix directory path (it is the same as
253 * the first element of the file pathname), and build the final merged
256 LastElement = strrchr (CommonPath, '/');
262 /* Build the final merged pathname */
265 Pathname = UtStringCacheCalloc (strlen (CommonPath) + strlen (FilePathname) + 2);
266 if (LastElement && *CommonPath)
268 strcpy (Pathname, CommonPath);
269 strcat (Pathname, "/");
271 strcat (Pathname, FilePathname);
273 /* Convert all backslashes to normal slashes */
276 UtConvertBackslashes (Pathname);
278 DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
284 /*******************************************************************************
286 * FUNCTION: FlOpenIncludeWithPrefix
288 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
290 * Filename - The include filename from the source ASL.
292 * RETURN: Valid file descriptor if successful. Null otherwise.
294 * DESCRIPTION: Open an include file and push it on the input file stack.
296 ******************************************************************************/
299 FlOpenIncludeWithPrefix (
301 ACPI_PARSE_OBJECT *Op,
308 /* Build the full pathname to the file */
310 Pathname = FlMergePathnames (PrefixDir, Filename);
312 DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
315 /* Attempt to open the file, push if successful */
317 IncludeFile = fopen (Pathname, "r");
320 fprintf (stderr, "Could not open include file %s\n", Pathname);
321 ACPI_FREE (Pathname);
325 #ifdef _MUST_HANDLE_COMMENTS
327 * Check entire include file for any # preprocessor directives.
328 * This is because there may be some confusion between the #include
329 * preprocessor directive and the ASL Include statement.
331 while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, IncludeFile))
333 if (Gbl_CurrentLineBuffer[0] == '#')
335 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE,
336 Op, "use #include instead");
341 /* Must seek back to the start of the file */
343 fseek (IncludeFile, 0, SEEK_SET);
345 /* Push the include file on the open input file stack */
347 AslPushInputFileStack (IncludeFile, Pathname);
348 return (IncludeFile);
352 /*******************************************************************************
354 * FUNCTION: FlOpenIncludeFile
356 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
360 * DESCRIPTION: Open an include file and push it on the input file stack.
362 ******************************************************************************/
366 ACPI_PARSE_OBJECT *Op)
369 ASL_INCLUDE_DIR *NextDir;
372 /* Op must be valid */
376 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
377 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
378 Gbl_InputByteCount, Gbl_CurrentColumn,
379 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
385 * Flush out the "include ()" statement on this line, start
386 * the actual include file on the next line
388 AslResetCurrentLineBuffer ();
389 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
390 Gbl_CurrentLineOffset++;
393 /* Attempt to open the include file */
395 /* If the file specifies an absolute path, just open it */
397 if ((Op->Asl.Value.String[0] == '/') ||
398 (Op->Asl.Value.String[0] == '\\') ||
399 (Op->Asl.Value.String[1] == ':'))
401 IncludeFile = FlOpenIncludeWithPrefix ("", Op, Op->Asl.Value.String);
410 * The include filename is not an absolute path.
412 * First, search for the file within the "local" directory -- meaning
413 * the same directory that contains the source file.
415 * Construct the file pathname from the global directory name.
417 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op, Op->Asl.Value.String);
424 * Second, search for the file within the (possibly multiple) directories
425 * specified by the -I option on the command line.
427 NextDir = Gbl_IncludeDirList;
430 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op, Op->Asl.Value.String);
436 NextDir = NextDir->Next;
439 /* We could not open the include file after trying very hard */
442 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
443 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
447 /*******************************************************************************
449 * FUNCTION: FlOpenInputFile
451 * PARAMETERS: InputFilename - The user-specified ASL source file to be
456 * DESCRIPTION: Open the specified input file, and save the directory path to
457 * the file so that include files can be opened in
458 * the same directory.
460 ******************************************************************************/
467 /* Open the input ASL file, text mode */
469 FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
470 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
476 /*******************************************************************************
478 * FUNCTION: FlOpenAmlOutputFile
480 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
484 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
485 * is created in the same directory as the parent input file.
487 ******************************************************************************/
490 FlOpenAmlOutputFile (
491 char *FilenamePrefix)
496 /* Output filename usually comes from the ASL itself */
498 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
501 /* Create the output AML filename */
503 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
506 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
507 0, 0, 0, 0, NULL, NULL);
511 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = Filename;
514 /* Open the output AML file in binary mode */
516 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
521 /*******************************************************************************
523 * FUNCTION: FlOpenMiscOutputFiles
525 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
529 * DESCRIPTION: Create and open the various output files needed, depending on
530 * the command line options
532 ******************************************************************************/
535 FlOpenMiscOutputFiles (
536 char *FilenamePrefix)
541 /* All done for disassembler */
543 if (Gbl_FileType == ASL_INPUT_TYPE_ACPI_TABLE)
548 /* Create/Open a hex output file if asked */
550 if (Gbl_HexOutputFlag)
552 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
555 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
556 0, 0, 0, 0, NULL, NULL);
560 /* Open the hex file, text mode */
562 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
564 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
565 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
568 /* Create/Open a debug output file if asked */
572 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
575 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
576 0, 0, 0, 0, NULL, NULL);
580 /* Open the debug file as STDERR, text mode */
582 /* TBD: hide this behind a FlReopenFile function */
584 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
585 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
586 freopen (Filename, "w+t", stderr);
588 if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
591 * A problem with freopen is that on error,
592 * we no longer have stderr.
594 Gbl_DebugFlag = FALSE;
595 memcpy (stderr, stdout, sizeof (FILE));
596 FlFileError (ASL_FILE_DEBUG_OUTPUT, ASL_MSG_DEBUG_FILENAME);
600 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
601 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
604 /* Create/Open a listing output file if asked */
608 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
611 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
612 0, 0, 0, 0, NULL, NULL);
616 /* Open the listing file, text mode */
618 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
620 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
621 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
624 /* Create the preprocessor output file if preprocessor enabled */
626 if (Gbl_PreprocessFlag)
628 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
631 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
632 0, 0, 0, 0, NULL, NULL);
636 FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
639 /* All done for data table compiler */
641 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
646 /* Create/Open a combined source output file */
648 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
651 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
652 0, 0, 0, 0, NULL, NULL);
657 * Open the source output file, binary mode (so that LF does not get
658 * expanded to CR/LF on some systems, messing up our seek
661 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
665 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
667 /* Create/Open a assembly code source output file if asked */
669 if (Gbl_AsmOutputFlag)
671 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
674 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
675 0, 0, 0, 0, NULL, NULL);
679 /* Open the assembly code source file, text mode */
681 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
683 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
684 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
687 /* Create/Open a C code source output file if asked */
689 if (Gbl_C_OutputFlag)
691 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
694 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
695 0, 0, 0, 0, NULL, NULL);
699 /* Open the C code source file, text mode */
701 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
703 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
704 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
705 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
708 /* Create/Open a C code source output file for the offset table if asked */
710 if (Gbl_C_OffsetTableFlag)
712 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);
715 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
716 0, 0, 0, 0, NULL, NULL);
720 /* Open the C code source file, text mode */
722 FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");
724 FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");
725 AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);
726 AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);
729 /* Create/Open a assembly include output file if asked */
731 if (Gbl_AsmIncludeOutputFlag)
733 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
736 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
737 0, 0, 0, 0, NULL, NULL);
741 /* Open the assembly include file, text mode */
743 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
745 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
746 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
749 /* Create/Open a C include output file if asked */
751 if (Gbl_C_IncludeOutputFlag)
753 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
756 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
757 0, 0, 0, 0, NULL, NULL);
761 /* Open the C include file, text mode */
763 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
765 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
766 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
767 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
770 /* Create a namespace output file if asked */
772 if (Gbl_NsOutputFlag)
774 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
777 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
778 0, 0, 0, 0, NULL, NULL);
782 /* Open the namespace file, text mode */
784 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
786 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
787 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
790 /* Create/Open a map file if requested */
794 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_MAP);
797 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
798 0, 0, 0, 0, NULL, NULL);
802 /* Open the hex file, text mode (closed at compiler exit) */
804 FlOpenFile (ASL_FILE_MAP_OUTPUT, Filename, "w+t");
806 AslCompilerSignon (ASL_FILE_MAP_OUTPUT);
807 AslCompilerFileHeader (ASL_FILE_MAP_OUTPUT);
814 #ifdef ACPI_OBSOLETE_FUNCTIONS
815 /*******************************************************************************
817 * FUNCTION: FlParseInputPathname
819 * PARAMETERS: InputFilename - The user-specified ASL source file to be
824 * DESCRIPTION: Split the input path into a directory and filename part
825 * 1) Directory part used to open include files
826 * 2) Filename part used to generate output filenames
828 ******************************************************************************/
831 FlParseInputPathname (
842 /* Get the path to the input filename's directory */
844 Gbl_DirectoryPath = strdup (InputFilename);
845 if (!Gbl_DirectoryPath)
847 return (AE_NO_MEMORY);
850 Substring = strrchr (Gbl_DirectoryPath, '\\');
853 Substring = strrchr (Gbl_DirectoryPath, '/');
856 Substring = strrchr (Gbl_DirectoryPath, ':');
862 Gbl_DirectoryPath[0] = 0;
863 if (Gbl_UseDefaultAmlFilename)
865 Gbl_OutputFilenamePrefix = strdup (InputFilename);
870 if (Gbl_UseDefaultAmlFilename)
872 Gbl_OutputFilenamePrefix = strdup (Substring + 1);
877 UtConvertBackslashes (Gbl_OutputFilenamePrefix);