2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <contrib/dev/acpica/compiler/aslcompiler.h>
46 #include <contrib/dev/acpica/include/acapps.h>
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslfiles")
51 /* Local prototypes */
54 FlOpenIncludeWithPrefix (
59 #ifdef ACPI_OBSOLETE_FUNCTIONS
61 FlParseInputPathname (
66 /*******************************************************************************
74 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
77 ******************************************************************************/
84 AePrintErrorLog (ASL_FILE_STDERR);
87 /* Print error summary to stdout also */
89 AePrintErrorLog (ASL_FILE_STDOUT);
96 /*******************************************************************************
98 * FUNCTION: FlFileError
100 * PARAMETERS: FileId - Index into file info array
101 * ErrorId - Index into error message array
105 * DESCRIPTION: Decode errno to an error message and add the entire error
108 ******************************************************************************/
116 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
118 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
122 /*******************************************************************************
124 * FUNCTION: FlOpenFile
126 * PARAMETERS: FileId - Index into file info array
127 * Filename - file pathname to open
128 * Mode - Open mode for fopen
132 * DESCRIPTION: Open a file.
133 * NOTE: Aborts compiler on any error.
135 ******************************************************************************/
146 File = fopen (Filename, Mode);
148 Gbl_Files[FileId].Filename = Filename;
149 Gbl_Files[FileId].Handle = File;
153 FlFileError (FileId, ASL_MSG_OPEN);
159 /*******************************************************************************
161 * FUNCTION: FlGetFileSize
163 * PARAMETERS: FileId - Index into file info array
167 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
169 ******************************************************************************/
180 fp = Gbl_Files[FileId].Handle;
183 fseek (fp, 0, SEEK_END);
184 FileSize = (UINT32) ftell (fp);
186 /* Restore file pointer */
188 fseek (fp, Offset, SEEK_SET);
193 /*******************************************************************************
195 * FUNCTION: FlReadFile
197 * PARAMETERS: FileId - Index into file info array
198 * Buffer - Where to place the data
199 * Length - Amount to read
201 * RETURN: Status. AE_ERROR indicates EOF.
203 * DESCRIPTION: Read data from an open file.
204 * NOTE: Aborts compiler on any error.
206 ******************************************************************************/
217 /* Read and check for error */
219 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
220 if (Actual != Length)
222 if (feof (Gbl_Files[FileId].Handle))
224 /* End-of-file, just return error */
229 FlFileError (FileId, ASL_MSG_READ);
237 /*******************************************************************************
239 * FUNCTION: FlWriteFile
241 * PARAMETERS: FileId - Index into file info array
242 * Buffer - Data to write
243 * Length - Amount of data to write
247 * DESCRIPTION: Write data to an open file.
248 * NOTE: Aborts compiler on any error.
250 ******************************************************************************/
261 /* Write and check for error */
263 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
264 if (Actual != Length)
266 FlFileError (FileId, ASL_MSG_WRITE);
272 /*******************************************************************************
274 * FUNCTION: FlPrintFile
276 * PARAMETERS: FileId - Index into file info array
277 * Format - Printf format string
278 * ... - Printf arguments
282 * DESCRIPTION: Formatted write to an open file.
283 * NOTE: Aborts compiler on any error.
285 ******************************************************************************/
297 va_start (Args, Format);
299 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
304 FlFileError (FileId, ASL_MSG_WRITE);
310 /*******************************************************************************
312 * FUNCTION: FlSeekFile
314 * PARAMETERS: FileId - Index into file info array
315 * Offset - Absolute byte offset in file
319 * DESCRIPTION: Seek to absolute offset
320 * NOTE: Aborts compiler on any error.
322 ******************************************************************************/
332 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
335 FlFileError (FileId, ASL_MSG_SEEK);
341 /*******************************************************************************
343 * FUNCTION: FlCloseFile
345 * PARAMETERS: FileId - Index into file info array
349 * DESCRIPTION: Close an open file. Aborts compiler on error
351 ******************************************************************************/
360 if (!Gbl_Files[FileId].Handle)
365 Error = fclose (Gbl_Files[FileId].Handle);
368 FlFileError (FileId, ASL_MSG_CLOSE);
372 Gbl_Files[FileId].Handle = NULL;
377 /*******************************************************************************
379 * FUNCTION: FlSetLineNumber
381 * PARAMETERS: Op - Parse node for the LINE asl statement
385 * DESCRIPTION: Set the current line number
387 ******************************************************************************/
394 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
395 LineNumber, Gbl_LogicalLineNumber);
397 Gbl_CurrentLineNumber = LineNumber;
398 Gbl_LogicalLineNumber = LineNumber;
402 /*******************************************************************************
404 * FUNCTION: FlSetFilename
406 * PARAMETERS: Op - Parse node for the LINE asl statement
410 * DESCRIPTION: Set the current filename
412 ******************************************************************************/
419 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
420 Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
422 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
426 /*******************************************************************************
428 * FUNCTION: FlAddIncludeDirectory
430 * PARAMETERS: Dir - Directory pathname string
434 * DESCRIPTION: Add a directory the list of include prefix directories.
436 ******************************************************************************/
439 FlAddIncludeDirectory (
442 ASL_INCLUDE_DIR *NewDir;
443 ASL_INCLUDE_DIR *NextDir;
444 ASL_INCLUDE_DIR *PrevDir = NULL;
445 UINT32 NeedsSeparator = 0;
449 DirLength = strlen (Dir);
455 /* Make sure that the pathname ends with a path separator */
457 if ((Dir[DirLength-1] != '/') &&
458 (Dir[DirLength-1] != '\\'))
463 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
464 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
465 strcpy (NewDir->Dir, Dir);
468 strcat (NewDir->Dir, "/");
472 * Preserve command line ordering of -I options by adding new elements
473 * at the end of the list
475 NextDir = Gbl_IncludeDirList;
479 NextDir = NextDir->Next;
484 PrevDir->Next = NewDir;
488 Gbl_IncludeDirList = NewDir;
493 /*******************************************************************************
495 * FUNCTION: FlOpenIncludeWithPrefix
497 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
499 * Filename - The include filename from the source ASL.
501 * RETURN: Valid file descriptor if successful. Null otherwise.
503 * DESCRIPTION: Open an include file and push it on the input file stack.
505 ******************************************************************************/
508 FlOpenIncludeWithPrefix (
516 /* Build the full pathname to the file */
518 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
520 strcpy (Pathname, PrefixDir);
521 strcat (Pathname, Filename);
523 DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
526 /* Attempt to open the file, push if successful */
528 IncludeFile = fopen (Pathname, "r");
531 /* Push the include file on the open input file stack */
533 AslPushInputFileStack (IncludeFile, Pathname);
534 return (IncludeFile);
537 ACPI_FREE (Pathname);
542 /*******************************************************************************
544 * FUNCTION: FlOpenIncludeFile
546 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
550 * DESCRIPTION: Open an include file and push it on the input file stack.
552 ******************************************************************************/
556 ACPI_PARSE_OBJECT *Op)
559 ASL_INCLUDE_DIR *NextDir;
562 /* Op must be valid */
566 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
567 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
568 Gbl_InputByteCount, Gbl_CurrentColumn,
569 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
575 * Flush out the "include ()" statement on this line, start
576 * the actual include file on the next line
578 AslResetCurrentLineBuffer ();
579 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
580 Gbl_CurrentLineOffset++;
583 /* Attempt to open the include file */
585 /* If the file specifies an absolute path, just open it */
587 if ((Op->Asl.Value.String[0] == '/') ||
588 (Op->Asl.Value.String[0] == '\\') ||
589 (Op->Asl.Value.String[1] == ':'))
591 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
600 * The include filename is not an absolute path.
602 * First, search for the file within the "local" directory -- meaning
603 * the same directory that contains the source file.
605 * Construct the file pathname from the global directory name.
607 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
614 * Second, search for the file within the (possibly multiple) directories
615 * specified by the -I option on the command line.
617 NextDir = Gbl_IncludeDirList;
620 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
626 NextDir = NextDir->Next;
629 /* We could not open the include file after trying very hard */
632 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
633 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
637 /*******************************************************************************
639 * FUNCTION: FlOpenInputFile
641 * PARAMETERS: InputFilename - The user-specified ASL source file to be
646 * DESCRIPTION: Open the specified input file, and save the directory path to
647 * the file so that include files can be opened in
648 * the same directory.
650 ******************************************************************************/
657 /* Open the input ASL file, text mode */
659 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
660 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
666 /*******************************************************************************
668 * FUNCTION: FlOpenAmlOutputFile
670 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
674 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
675 * is created in the same directory as the parent input file.
677 ******************************************************************************/
680 FlOpenAmlOutputFile (
681 char *FilenamePrefix)
686 /* Output filename usually comes from the ASL itself */
688 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
691 /* Create the output AML filename */
693 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
696 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
697 0, 0, 0, 0, NULL, NULL);
702 /* Open the output AML file in binary mode */
704 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
709 /*******************************************************************************
711 * FUNCTION: FlOpenMiscOutputFiles
713 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
717 * DESCRIPTION: Create and open the various output files needed, depending on
718 * the command line options
720 ******************************************************************************/
723 FlOpenMiscOutputFiles (
724 char *FilenamePrefix)
729 /* Create/Open a hex output file if asked */
731 if (Gbl_HexOutputFlag)
733 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
736 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
737 0, 0, 0, 0, NULL, NULL);
741 /* Open the hex file, text mode */
743 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
745 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
746 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
749 /* Create/Open a debug output file if asked */
753 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
756 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
757 0, 0, 0, 0, NULL, NULL);
761 /* Open the debug file as STDERR, text mode */
763 /* TBD: hide this behind a FlReopenFile function */
765 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
766 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
767 freopen (Filename, "w+t", stderr);
769 if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
771 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
772 0, 0, 0, 0, NULL, NULL);
776 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
777 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
780 /* Create/Open a listing output file if asked */
784 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
787 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
788 0, 0, 0, 0, NULL, NULL);
792 /* Open the listing file, text mode */
794 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
796 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
797 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
800 /* Create the preprocessor output file if preprocessor enabled */
802 if (Gbl_PreprocessFlag)
804 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
807 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
808 0, 0, 0, 0, NULL, NULL);
812 FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+b");
815 /* All done for data table compiler */
817 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
822 /* Create/Open a combined source output file */
824 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
827 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
828 0, 0, 0, 0, NULL, NULL);
833 * Open the source output file, binary mode (so that LF does not get
834 * expanded to CR/LF on some systems, messing up our seek
837 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
841 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
843 /* Create/Open a assembly code source output file if asked */
845 if (Gbl_AsmOutputFlag)
847 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
850 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
851 0, 0, 0, 0, NULL, NULL);
855 /* Open the assembly code source file, text mode */
857 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
859 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
860 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
863 /* Create/Open a C code source output file if asked */
865 if (Gbl_C_OutputFlag)
867 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
870 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
871 0, 0, 0, 0, NULL, NULL);
875 /* Open the C code source file, text mode */
877 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
879 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
880 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
881 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
884 /* Create/Open a assembly include output file if asked */
886 if (Gbl_AsmIncludeOutputFlag)
888 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
891 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
892 0, 0, 0, 0, NULL, NULL);
896 /* Open the assembly include file, text mode */
898 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
900 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
901 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
904 /* Create/Open a C include output file if asked */
906 if (Gbl_C_IncludeOutputFlag)
908 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
911 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
912 0, 0, 0, 0, NULL, NULL);
916 /* Open the C include file, text mode */
918 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
920 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
921 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
922 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
925 /* Create a namespace output file if asked */
927 if (Gbl_NsOutputFlag)
929 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
932 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
933 0, 0, 0, 0, NULL, NULL);
937 /* Open the namespace file, text mode */
939 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
941 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
942 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
949 #ifdef ACPI_OBSOLETE_FUNCTIONS
950 /*******************************************************************************
952 * FUNCTION: FlParseInputPathname
954 * PARAMETERS: InputFilename - The user-specified ASL source file to be
959 * DESCRIPTION: Split the input path into a directory and filename part
960 * 1) Directory part used to open include files
961 * 2) Filename part used to generate output filenames
963 ******************************************************************************/
966 FlParseInputPathname (
977 /* Get the path to the input filename's directory */
979 Gbl_DirectoryPath = strdup (InputFilename);
980 if (!Gbl_DirectoryPath)
982 return (AE_NO_MEMORY);
985 Substring = strrchr (Gbl_DirectoryPath, '\\');
988 Substring = strrchr (Gbl_DirectoryPath, '/');
991 Substring = strrchr (Gbl_DirectoryPath, ':');
997 Gbl_DirectoryPath[0] = 0;
998 if (Gbl_UseDefaultAmlFilename)
1000 Gbl_OutputFilenamePrefix = strdup (InputFilename);
1005 if (Gbl_UseDefaultAmlFilename)
1007 Gbl_OutputFilenamePrefix = strdup (Substring + 1);