2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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_STDOUT);
87 /* Print error summary to the debug file */
89 AePrintErrorLog (ASL_FILE_STDERR);
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 ******************************************************************************/
179 fp = Gbl_Files[FileId].Handle;
181 fseek (fp, 0, SEEK_END);
182 FileSize = (UINT32) ftell (fp);
183 fseek (fp, 0, SEEK_SET);
189 /*******************************************************************************
191 * FUNCTION: FlReadFile
193 * PARAMETERS: FileId - Index into file info array
194 * Buffer - Where to place the data
195 * Length - Amount to read
197 * RETURN: Status. AE_ERROR indicates EOF.
199 * DESCRIPTION: Read data from an open file.
200 * NOTE: Aborts compiler on any error.
202 ******************************************************************************/
213 /* Read and check for error */
215 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
216 if (Actual != Length)
218 if (feof (Gbl_Files[FileId].Handle))
220 /* End-of-file, just return error */
225 FlFileError (FileId, ASL_MSG_READ);
233 /*******************************************************************************
235 * FUNCTION: FlWriteFile
237 * PARAMETERS: FileId - Index into file info array
238 * Buffer - Data to write
239 * Length - Amount of data to write
243 * DESCRIPTION: Write data to an open file.
244 * NOTE: Aborts compiler on any error.
246 ******************************************************************************/
257 /* Write and check for error */
259 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
260 if (Actual != Length)
262 FlFileError (FileId, ASL_MSG_WRITE);
268 /*******************************************************************************
270 * FUNCTION: FlPrintFile
272 * PARAMETERS: FileId - Index into file info array
273 * Format - Printf format string
274 * ... - Printf arguments
278 * DESCRIPTION: Formatted write to an open file.
279 * NOTE: Aborts compiler on any error.
281 ******************************************************************************/
293 va_start (Args, Format);
295 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
300 FlFileError (FileId, ASL_MSG_WRITE);
306 /*******************************************************************************
308 * FUNCTION: FlSeekFile
310 * PARAMETERS: FileId - Index into file info array
311 * Offset - Absolute byte offset in file
315 * DESCRIPTION: Seek to absolute offset
316 * NOTE: Aborts compiler on any error.
318 ******************************************************************************/
328 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
331 FlFileError (FileId, ASL_MSG_SEEK);
337 /*******************************************************************************
339 * FUNCTION: FlCloseFile
341 * PARAMETERS: FileId - Index into file info array
345 * DESCRIPTION: Close an open file. Aborts compiler on error
347 ******************************************************************************/
356 if (!Gbl_Files[FileId].Handle)
361 Error = fclose (Gbl_Files[FileId].Handle);
362 Gbl_Files[FileId].Handle = NULL;
366 FlFileError (FileId, ASL_MSG_CLOSE);
374 /*******************************************************************************
376 * FUNCTION: FlSetLineNumber
378 * PARAMETERS: Op - Parse node for the LINE asl statement
382 * DESCRIPTION: Set the current line number
384 ******************************************************************************/
388 ACPI_PARSE_OBJECT *Op)
391 Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
392 Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
396 /*******************************************************************************
398 * FUNCTION: FlAddIncludeDirectory
400 * PARAMETERS: Dir - Directory pathname string
404 * DESCRIPTION: Add a directory the list of include prefix directories.
406 ******************************************************************************/
409 FlAddIncludeDirectory (
412 ASL_INCLUDE_DIR *NewDir;
413 ASL_INCLUDE_DIR *NextDir;
414 ASL_INCLUDE_DIR *PrevDir = NULL;
415 UINT32 NeedsSeparator = 0;
419 DirLength = strlen (Dir);
425 /* Make sure that the pathname ends with a path separator */
427 if ((Dir[DirLength-1] != '/') &&
428 (Dir[DirLength-1] != '\\'))
433 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
434 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
435 strcpy (NewDir->Dir, Dir);
438 strcat (NewDir->Dir, "/");
442 * Preserve command line ordering of -I options by adding new elements
443 * at the end of the list
445 NextDir = Gbl_IncludeDirList;
449 NextDir = NextDir->Next;
454 PrevDir->Next = NewDir;
458 Gbl_IncludeDirList = NewDir;
463 /*******************************************************************************
465 * FUNCTION: FlOpenIncludeWithPrefix
467 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
469 * Filename - The include filename from the source ASL.
471 * RETURN: Valid file descriptor if successful. Null otherwise.
473 * DESCRIPTION: Open an include file and push it on the input file stack.
475 ******************************************************************************/
478 FlOpenIncludeWithPrefix (
486 /* Build the full pathname to the file */
488 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
490 strcpy (Pathname, PrefixDir);
491 strcat (Pathname, Filename);
493 DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
496 /* Attempt to open the file, push if successful */
498 IncludeFile = fopen (Pathname, "r");
501 /* Push the include file on the open input file stack */
503 AslPushInputFileStack (IncludeFile, Pathname);
504 return (IncludeFile);
507 ACPI_FREE (Pathname);
512 /*******************************************************************************
514 * FUNCTION: FlOpenIncludeFile
516 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
520 * DESCRIPTION: Open an include file and push it on the input file stack.
522 ******************************************************************************/
526 ACPI_PARSE_OBJECT *Op)
529 ASL_INCLUDE_DIR *NextDir;
532 /* Op must be valid */
536 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
537 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
538 Gbl_InputByteCount, Gbl_CurrentColumn,
539 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
545 * Flush out the "include ()" statement on this line, start
546 * the actual include file on the next line
548 ResetCurrentLineBuffer ();
549 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
550 Gbl_CurrentLineOffset++;
553 /* Attempt to open the include file */
555 /* If the file specifies an absolute path, just open it */
557 if ((Op->Asl.Value.String[0] == '/') ||
558 (Op->Asl.Value.String[0] == '\\') ||
559 (Op->Asl.Value.String[1] == ':'))
561 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
570 * The include filename is not an absolute path.
572 * First, search for the file within the "local" directory -- meaning
573 * the same directory that contains the source file.
575 * Construct the file pathname from the global directory name.
577 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
584 * Second, search for the file within the (possibly multiple) directories
585 * specified by the -I option on the command line.
587 NextDir = Gbl_IncludeDirList;
590 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
596 NextDir = NextDir->Next;
599 /* We could not open the include file after trying very hard */
602 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
603 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
607 /*******************************************************************************
609 * FUNCTION: FlOpenInputFile
611 * PARAMETERS: InputFilename - The user-specified ASL source file to be
616 * DESCRIPTION: Open the specified input file, and save the directory path to
617 * the file so that include files can be opened in
618 * the same directory.
620 ******************************************************************************/
627 /* Open the input ASL file, text mode */
629 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
630 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
636 /*******************************************************************************
638 * FUNCTION: FlOpenAmlOutputFile
640 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
644 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
645 * is created in the same directory as the parent input file.
647 ******************************************************************************/
650 FlOpenAmlOutputFile (
651 char *FilenamePrefix)
656 /* Output filename usually comes from the ASL itself */
658 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
661 /* Create the output AML filename */
663 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
666 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
667 0, 0, 0, 0, NULL, NULL);
672 /* Open the output AML file in binary mode */
674 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
679 /*******************************************************************************
681 * FUNCTION: FlOpenMiscOutputFiles
683 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
687 * DESCRIPTION: Create and open the various output files needed, depending on
688 * the command line options
690 ******************************************************************************/
693 FlOpenMiscOutputFiles (
694 char *FilenamePrefix)
699 /* Create/Open a hex output file if asked */
701 if (Gbl_HexOutputFlag)
703 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
706 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
707 0, 0, 0, 0, NULL, NULL);
711 /* Open the hex file, text mode */
713 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
715 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
716 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
719 /* Create/Open a debug output file if asked */
723 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
726 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
727 0, 0, 0, 0, NULL, NULL);
731 /* Open the debug file as STDERR, text mode */
733 /* TBD: hide this behind a FlReopenFile function */
735 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
736 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
737 freopen (Filename, "w+t", stderr);
739 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
740 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
743 /* Create/Open a listing output file if asked */
747 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
750 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
751 0, 0, 0, 0, NULL, NULL);
755 /* Open the listing file, text mode */
757 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
759 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
760 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
763 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
768 /* Create/Open a combined source output file */
770 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
773 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
774 0, 0, 0, 0, NULL, NULL);
779 * Open the source output file, binary mode (so that LF does not get
780 * expanded to CR/LF on some systems, messing up our seek
783 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
785 /* Create/Open a assembly code source output file if asked */
787 if (Gbl_AsmOutputFlag)
789 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
792 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
793 0, 0, 0, 0, NULL, NULL);
797 /* Open the assembly code source file, text mode */
799 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
801 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
802 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
805 /* Create/Open a C code source output file if asked */
807 if (Gbl_C_OutputFlag)
809 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
812 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
813 0, 0, 0, 0, NULL, NULL);
817 /* Open the C code source file, text mode */
819 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
821 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
822 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
823 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
826 /* Create/Open a assembly include output file if asked */
828 if (Gbl_AsmIncludeOutputFlag)
830 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
833 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
834 0, 0, 0, 0, NULL, NULL);
838 /* Open the assembly include file, text mode */
840 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
842 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
843 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
846 /* Create/Open a C include output file if asked */
848 if (Gbl_C_IncludeOutputFlag)
850 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
853 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
854 0, 0, 0, 0, NULL, NULL);
858 /* Open the C include file, text mode */
860 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
862 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
863 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
864 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
867 /* Create a namespace output file if asked */
869 if (Gbl_NsOutputFlag)
871 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
874 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
875 0, 0, 0, 0, NULL, NULL);
879 /* Open the namespace file, text mode */
881 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
883 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
884 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
891 #ifdef ACPI_OBSOLETE_FUNCTIONS
892 /*******************************************************************************
894 * FUNCTION: FlParseInputPathname
896 * PARAMETERS: InputFilename - The user-specified ASL source file to be
901 * DESCRIPTION: Split the input path into a directory and filename part
902 * 1) Directory part used to open include files
903 * 2) Filename part used to generate output filenames
905 ******************************************************************************/
908 FlParseInputPathname (
919 /* Get the path to the input filename's directory */
921 Gbl_DirectoryPath = strdup (InputFilename);
922 if (!Gbl_DirectoryPath)
924 return (AE_NO_MEMORY);
927 Substring = strrchr (Gbl_DirectoryPath, '\\');
930 Substring = strrchr (Gbl_DirectoryPath, '/');
933 Substring = strrchr (Gbl_DirectoryPath, ':');
939 Gbl_DirectoryPath[0] = 0;
940 if (Gbl_UseDefaultAmlFilename)
942 Gbl_OutputFilenamePrefix = strdup (InputFilename);
947 if (Gbl_UseDefaultAmlFilename)
949 Gbl_OutputFilenamePrefix = strdup (Substring + 1);