Path: blob/main/sys/contrib/dev/acpica/compiler/aslfiles.c
48373 views
/******************************************************************************1*2* Module Name: aslfiles - File support functions3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/compiler/aslcompiler.h>152#include <contrib/dev/acpica/include/acapps.h>153154#define _COMPONENT ACPI_COMPILER155ACPI_MODULE_NAME ("aslfiles")156157/* Local prototypes */158159static FILE *160FlOpenIncludeWithPrefix (161char *PrefixDir,162ACPI_PARSE_OBJECT *Op,163char *Filename);164165static BOOLEAN166FlInputFileExists (167char *InputFilename);168169#ifdef ACPI_OBSOLETE_FUNCTIONS170ACPI_STATUS171FlParseInputPathname (172char *InputFilename);173#endif174175176/*******************************************************************************177*178* FUNCTION: FlInitOneFile179*180* PARAMETERS: InputFilename - The user-specified ASL source file to be181* compiled182*183* RETURN: Status184*185* DESCRIPTION: Initialize global file structure for one input file. This file186* structure contains references to input, output, debugging, and187* other miscellaneous files that are associated for a single188* input ASL file.189*190******************************************************************************/191192ACPI_STATUS193FlInitOneFile (194char *InputFilename)195{196UINT32 i;197ASL_GLOBAL_FILE_NODE *NewFileNode;198199200if (FlInputFileExists (InputFilename))201{202AslError (ASL_ERROR, ASL_MSG_DUPLICATE_INPUT_FILE, NULL, InputFilename);203return (AE_ALREADY_EXISTS);204}205206NewFileNode = ACPI_CAST_PTR (ASL_GLOBAL_FILE_NODE,207UtLocalCacheCalloc (sizeof (ASL_GLOBAL_FILE_NODE)));208209NewFileNode->ParserErrorDetected = FALSE;210NewFileNode->Next = AslGbl_FilesList;211212AslGbl_FilesList = NewFileNode;213AslGbl_Files = NewFileNode->Files;214215for (i = 0; i < ASL_NUM_FILES; i++)216{217AslGbl_Files[i].Handle = NULL;218AslGbl_Files[i].Filename = NULL;219}220221AslGbl_Files[ASL_FILE_STDOUT].Handle = stdout;222AslGbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";223224if (AslGbl_VerboseErrors)225{226AslGbl_Files[ASL_FILE_STDERR].Handle = stderr;227}228else229{230AslGbl_Files[ASL_FILE_STDERR].Handle = stdout;231}232233AslGbl_Files[ASL_FILE_STDERR].Filename = "STDERR";234return (AE_OK);235}236237238/*******************************************************************************239*240* FUNCTION: FlInputFileExists241*242* PARAMETERS: Filename - File name to be searched243*244* RETURN: Status245*246* DESCRIPTION: Returns true if the file name already exists.247*248******************************************************************************/249250static BOOLEAN251FlInputFileExists (252char *Filename)253{254ASL_GLOBAL_FILE_NODE *Current = AslGbl_FilesList;255256257while (Current)258{259if (!strcmp (Filename, Current->Files[ASL_FILE_INPUT].Filename))260{261return (TRUE);262}263264Current = Current->Next;265}266267return (FALSE);268}269270271/*******************************************************************************272*273* FUNCTION: FlSwitchFileSet274*275* PARAMETERS: Op - Parse node for the LINE asl statement276*277* RETURN: None.278*279* DESCRIPTION: Set the current line number280*281******************************************************************************/282283ASL_FILE_SWITCH_STATUS284FlSwitchFileSet (285char *InputFilename)286{287ASL_GLOBAL_FILE_NODE *Current = AslGbl_FilesList;288char *PrevFilename = Current->Files[ASL_FILE_INPUT].Filename;289290291while (Current)292{293if (!strcmp(Current->Files[ASL_FILE_INPUT].Filename, InputFilename))294{295AslGbl_Files = Current->Files;296AslGbl_TableSignature = Current->TableSignature;297AslGbl_TableId = Current->TableId;298299if (!strcmp (InputFilename, PrevFilename))300{301return (SWITCH_TO_SAME_FILE);302}303else304{305return (SWITCH_TO_DIFFERENT_FILE);306}307}308309Current = Current->Next;310}311312return (FILE_NOT_FOUND);313}314315316/*******************************************************************************317*318* FUNCTION: FlGetFileHandle319*320* PARAMETERS: OutFileId - denotes file type of output handle321* InFileId - denotes file type of the input Filename322* Filename323*324* RETURN: File handle325*326* DESCRIPTION: Get the file handle for a particular filename/FileId. This327* function also allows the caller to specify the file Id of the328* desired type.329*330******************************************************************************/331332FILE *333FlGetFileHandle (334UINT32 OutFileId,335UINT32 InFileId,336char *Filename)337{338ASL_GLOBAL_FILE_NODE *Current = AslGbl_FilesList;339340341if (!Filename)342{343return (NULL);344}345346while (Current)347{348if (!((Current->FileType == ASL_INPUT_TYPE_ASCII_DATA) &&349(InFileId == ASL_FILE_SOURCE_OUTPUT)) &&350!strcmp (Current->Files[InFileId].Filename, Filename))351{352return (Current->Files[OutFileId].Handle);353}354355Current = Current->Next;356}357358return (NULL);359}360361362/*******************************************************************************363*364* FUNCTION: FlGetFileNode365*366* PARAMETERS: FileId - File type (ID) of the input Filename367* Filename - File to search for368*369* RETURN: A global file node370*371* DESCRIPTION: Get the file node for a particular filename/FileId.372*373******************************************************************************/374375ASL_GLOBAL_FILE_NODE *376FlGetFileNode (377UINT32 FileId,378char *Filename)379{380ASL_GLOBAL_FILE_NODE *Current = AslGbl_FilesList;381382383if (!Filename)384{385return (NULL);386}387388while (Current)389{390if (!strcmp (Current->Files[FileId].Filename, Filename))391{392return (Current);393}394395Current = Current->Next;396}397398return (NULL);399}400401402/*******************************************************************************403*404* FUNCTION: FlGetCurrentFileNode405*406* PARAMETERS: None407*408* RETURN: Global file node409*410* DESCRIPTION: Get the current input file node411*412******************************************************************************/413414ASL_GLOBAL_FILE_NODE *415FlGetCurrentFileNode (416void)417{418ASL_GLOBAL_FILE_NODE *FileNode =419FlGetFileNode (ASL_FILE_INPUT,AslGbl_Files[ASL_FILE_INPUT].Filename);420421422if (!FileNode)423{424/*425* If the current file node does not exist after initializing the file426* node structures, something went wrong and this is an unrecoverable427* condition.428*/429FlFileError (ASL_FILE_INPUT, ASL_MSG_COMPILER_INTERNAL);430AslAbort ();431}432433return (FileNode);434}435436437/*******************************************************************************438*439* FUNCTION: FlSetLineNumber440*441* PARAMETERS: Op - Parse node for the LINE asl statement442*443* RETURN: None.444*445* DESCRIPTION: Set the current line number446*447******************************************************************************/448449void450FlSetLineNumber (451UINT32 LineNumber)452{453454DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",455LineNumber, AslGbl_LogicalLineNumber);456457AslGbl_CurrentLineNumber = LineNumber;458}459460461/*******************************************************************************462*463* FUNCTION: FlSetFilename464*465* PARAMETERS: Op - Parse node for the LINE asl statement466*467* RETURN: None.468*469* DESCRIPTION: Set the current filename470*471******************************************************************************/472473void474FlSetFilename (475char *Filename)476{477478DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",479Filename, AslGbl_Files[ASL_FILE_INPUT].Filename);480481/* No need to free any existing filename */482483AslGbl_Files[ASL_FILE_INPUT].Filename = Filename;484}485486487/*******************************************************************************488*489* FUNCTION: FlAddIncludeDirectory490*491* PARAMETERS: Dir - Directory pathname string492*493* RETURN: None494*495* DESCRIPTION: Add a directory the list of include prefix directories.496*497******************************************************************************/498499void500FlAddIncludeDirectory (501char *Dir)502{503ASL_INCLUDE_DIR *NewDir;504ASL_INCLUDE_DIR *NextDir;505ASL_INCLUDE_DIR *PrevDir = NULL;506UINT32 NeedsSeparator = 0;507size_t DirLength;508509510DirLength = strlen (Dir);511if (!DirLength)512{513return;514}515516/* Make sure that the pathname ends with a path separator */517518if ((Dir[DirLength-1] != '/') &&519(Dir[DirLength-1] != '\\'))520{521NeedsSeparator = 1;522}523524NewDir = ACPI_CAST_PTR (ASL_INCLUDE_DIR,525UtLocalCacheCalloc (sizeof (ASL_INCLUDE_DIR)));526NewDir->Dir = UtLocalCacheCalloc (DirLength + 1 + NeedsSeparator);527strcpy (NewDir->Dir, Dir);528if (NeedsSeparator)529{530strcat (NewDir->Dir, "/");531}532533/*534* Preserve command line ordering of -I options by adding new elements535* at the end of the list536*/537NextDir = AslGbl_IncludeDirList;538while (NextDir)539{540PrevDir = NextDir;541NextDir = NextDir->Next;542}543544if (PrevDir)545{546PrevDir->Next = NewDir;547}548else549{550AslGbl_IncludeDirList = NewDir;551}552}553554555/*******************************************************************************556*557* FUNCTION: FlMergePathnames558*559* PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or560* a zero length string.561* FilePathname - The include filename from the source ASL.562*563* RETURN: Merged pathname string564*565* DESCRIPTION: Merge two pathnames that (probably) have common elements, to566* arrive at a minimal length string. Merge can occur if the567* FilePathname is relative to the PrefixDir.568*569******************************************************************************/570571char *572FlMergePathnames (573char *PrefixDir,574char *FilePathname)575{576char *CommonPath;577char *Pathname;578char *LastElement;579580581DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"582"Include: FilePathname - \"%s\"\n",583PrefixDir, FilePathname);584585/*586* If there is no prefix directory or if the file pathname is absolute,587* just return the original file pathname588*/589if (!PrefixDir || (!*PrefixDir) ||590(*FilePathname == '/') ||591(FilePathname[1] == ':'))592{593Pathname = UtLocalCacheCalloc (strlen (FilePathname) + 1);594strcpy (Pathname, FilePathname);595goto ConvertBackslashes;596}597598/* Need a local copy of the prefix directory path */599600CommonPath = UtLocalCacheCalloc (strlen (PrefixDir) + 1);601strcpy (CommonPath, PrefixDir);602603/*604* Walk forward through the file path, and simultaneously backward605* through the prefix directory path until there are no more606* relative references at the start of the file path.607*/608while (*FilePathname && (!strncmp (FilePathname, "../", 3)))609{610/* Remove last element of the prefix directory path */611612LastElement = strrchr (CommonPath, '/');613if (!LastElement)614{615goto ConcatenatePaths;616}617618*LastElement = 0; /* Terminate CommonPath string */619FilePathname += 3; /* Point to next path element */620}621622/*623* Remove the last element of the prefix directory path (it is the same as624* the first element of the file pathname), and build the final merged625* pathname.626*/627LastElement = strrchr (CommonPath, '/');628if (LastElement)629{630*LastElement = 0;631}632633/* Build the final merged pathname */634635ConcatenatePaths:636Pathname = UtLocalCacheCalloc (637strlen (CommonPath) + strlen (FilePathname) + 2);638if (LastElement && *CommonPath)639{640strcpy (Pathname, CommonPath);641strcat (Pathname, "/");642}643strcat (Pathname, FilePathname);644645/* Convert all backslashes to normal slashes */646647ConvertBackslashes:648UtConvertBackslashes (Pathname);649650DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",651Pathname);652return (Pathname);653}654655656/*******************************************************************************657*658* FUNCTION: FlOpenIncludeWithPrefix659*660* PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero661* length string.662* Filename - The include filename from the source ASL.663*664* RETURN: Valid file descriptor if successful. Null otherwise.665*666* DESCRIPTION: Open an include file and push it on the input file stack.667*668******************************************************************************/669670static FILE *671FlOpenIncludeWithPrefix (672char *PrefixDir,673ACPI_PARSE_OBJECT *Op,674char *Filename)675{676FILE *IncludeFile;677char *Pathname;678UINT32 OriginalLineNumber;679680681/* Build the full pathname to the file */682683Pathname = FlMergePathnames (PrefixDir, Filename);684685DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",686Pathname);687688/* Attempt to open the file, push if successful */689690IncludeFile = fopen (Pathname, "r");691if (!IncludeFile)692{693return (NULL);694}695696/*697* Check the entire include file for any # preprocessor directives.698* This is because there may be some confusion between the #include699* preprocessor directive and the ASL Include statement. A file included700* by the ASL include cannot contain preprocessor directives because701* the preprocessor has already run by the time the ASL include is702* recognized (by the compiler, not the preprocessor.)703*704* Note: DtGetNextLine strips/ignores comments.705* Save current line number since DtGetNextLine modifies it.706*/707AslGbl_CurrentLineNumber--;708OriginalLineNumber = AslGbl_CurrentLineNumber;709710while (DtGetNextLine (IncludeFile, DT_ALLOW_MULTILINE_QUOTES) != ASL_EOF)711{712if (AslGbl_CurrentLineBuffer[0] == '#')713{714AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE,715Op, "use #include instead");716}717}718719AslGbl_CurrentLineNumber = OriginalLineNumber;720721/* Must seek back to the start of the file */722723fseek (IncludeFile, 0, SEEK_SET);724725/* Push the include file on the open input file stack */726727AslPushInputFileStack (IncludeFile, Pathname);728return (IncludeFile);729}730731732/*******************************************************************************733*734* FUNCTION: FlOpenIncludeFile735*736* PARAMETERS: Op - Parse node for the INCLUDE ASL statement737*738* RETURN: None.739*740* DESCRIPTION: Open an include file and push it on the input file stack.741*742******************************************************************************/743744void745FlOpenIncludeFile (746ACPI_PARSE_OBJECT *Op)747{748FILE *IncludeFile;749ASL_INCLUDE_DIR *NextDir;750751752/* Op must be valid */753754if (!Op)755{756AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,757AslGbl_CurrentLineNumber, AslGbl_LogicalLineNumber,758AslGbl_InputByteCount, AslGbl_CurrentColumn,759AslGbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");760761return;762}763764/*765* Flush out the "include ()" statement on this line, start766* the actual include file on the next line767*/768AslResetCurrentLineBuffer ();769FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");770AslGbl_CurrentLineOffset++;771772773/* Attempt to open the include file */774775/* If the file specifies an absolute path, just open it */776777if ((Op->Asl.Value.String[0] == '/') ||778(Op->Asl.Value.String[0] == '\\') ||779(Op->Asl.Value.String[1] == ':'))780{781IncludeFile = FlOpenIncludeWithPrefix ("", Op, Op->Asl.Value.String);782if (!IncludeFile)783{784goto ErrorExit;785}786return;787}788789/*790* The include filename is not an absolute path.791*792* First, search for the file within the "local" directory -- meaning793* the same directory that contains the source file.794*795* Construct the file pathname from the global directory name.796*/797IncludeFile = FlOpenIncludeWithPrefix (798AslGbl_DirectoryPath, Op, Op->Asl.Value.String);799if (IncludeFile)800{801return;802}803804/*805* Second, search for the file within the (possibly multiple) directories806* specified by the -I option on the command line.807*/808NextDir = AslGbl_IncludeDirList;809while (NextDir)810{811IncludeFile = FlOpenIncludeWithPrefix (812NextDir->Dir, Op, Op->Asl.Value.String);813if (IncludeFile)814{815return;816}817818NextDir = NextDir->Next;819}820821/* We could not open the include file after trying very hard */822823ErrorExit:824sprintf (AslGbl_MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));825AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, AslGbl_MsgBuffer);826}827828829/*******************************************************************************830*831* FUNCTION: FlOpenInputFile832*833* PARAMETERS: InputFilename - The user-specified ASL source file to be834* compiled835*836* RETURN: Status837*838* DESCRIPTION: Open the specified input file, and save the directory path to839* the file so that include files can be opened in the same840* directory. NOTE: File is opened in text mode.841*842******************************************************************************/843844ACPI_STATUS845FlOpenInputFile (846char *InputFilename)847{848849/* Open the input ASL file, text mode */850851FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");852AslCompilerin = AslGbl_Files[ASL_FILE_INPUT].Handle;853854return (AE_OK);855}856857858/*******************************************************************************859*860* FUNCTION: FlOpenAmlOutputFile861*862* PARAMETERS: FilenamePrefix - The user-specified ASL source file863*864* RETURN: Status865*866* DESCRIPTION: Create the output filename (*.AML) and open the file. The file867* is created in the same directory as the parent input file.868*869******************************************************************************/870871ACPI_STATUS872FlOpenAmlOutputFile (873char *FilenamePrefix)874{875char *Filename;876877878/* Output filename usually comes from the ASL itself */879880Filename = AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename;881if (!Filename)882{883/* Create the output AML filename */884if (!AcpiGbl_CaptureComments)885{886Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);887}888else889{890Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_CONVERT_AML);891}892if (!Filename)893{894AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,8950, 0, 0, 0, NULL, NULL);896return (AE_ERROR);897}898899AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename = Filename;900}901902/* Open the output AML file in binary mode */903904FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");905return (AE_OK);906}907908909/*******************************************************************************910*911* FUNCTION: FlOpenMiscOutputFiles912*913* PARAMETERS: FilenamePrefix - The user-specified ASL source file914*915* RETURN: Status916*917* DESCRIPTION: Create and open the various output files needed, depending on918* the command line options919*920******************************************************************************/921922ACPI_STATUS923FlOpenMiscOutputFiles (924char *FilenamePrefix)925{926char *Filename;927928929/* Create/Open a map file if requested */930931if (AslGbl_MapfileFlag)932{933Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_MAP);934if (!Filename)935{936AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,9370, 0, 0, 0, NULL, NULL);938return (AE_ERROR);939}940941/* Open the hex file, text mode (closed at compiler exit) */942943FlOpenFile (ASL_FILE_MAP_OUTPUT, Filename, "w+t");944945AslCompilerSignon (ASL_FILE_MAP_OUTPUT);946AslCompilerFileHeader (ASL_FILE_MAP_OUTPUT);947}948949/* All done for disassembler */950951if (AslGbl_FileType == ASL_INPUT_TYPE_BINARY_ACPI_TABLE)952{953return (AE_OK);954}955956/* Create/Open a hex output file if asked */957958if (AslGbl_HexOutputFlag)959{960Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);961if (!Filename)962{963AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,9640, 0, 0, 0, NULL, NULL);965return (AE_ERROR);966}967968/* Open the hex file, text mode */969970FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");971972AslCompilerSignon (ASL_FILE_HEX_OUTPUT);973AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);974}975976/* Create/Open a debug output file if asked */977978if (AslGbl_DebugFlag)979{980Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);981if (!Filename)982{983AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,9840, 0, 0, 0, NULL, NULL);985return (AE_ERROR);986}987988/* Open the debug file as STDERR, text mode */989990AslGbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;991AslGbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =992freopen (Filename, "w+t", stderr);993994if (!AslGbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)995{996/*997* A problem with freopen is that on error, we no longer998* have stderr and cannot emit normal error messages.999* Emit error to stdout, close files, and exit.1000*/1001fprintf (stdout,1002"\nCould not open debug output file: %s\n\n", Filename);10031004CmCleanupAndExit ();1005exit (1);1006}10071008AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);1009AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);1010}10111012/* Create/Open a cross-reference output file if asked */10131014if (AslGbl_CrossReferenceOutput)1015{1016Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_XREF);1017if (!Filename)1018{1019AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,10200, 0, 0, 0, NULL, NULL);1021return (AE_ERROR);1022}10231024FlOpenFile (ASL_FILE_XREF_OUTPUT, Filename, "w+t");10251026AslCompilerSignon (ASL_FILE_XREF_OUTPUT);1027AslCompilerFileHeader (ASL_FILE_XREF_OUTPUT);1028}10291030/* Create/Open a listing output file if asked */10311032if (AslGbl_ListingFlag)1033{1034Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);1035if (!Filename)1036{1037AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,10380, 0, 0, 0, NULL, NULL);1039return (AE_ERROR);1040}10411042/* Open the listing file, text mode */10431044FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");10451046AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);1047AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);1048}10491050/* Create the preprocessor output temp file if preprocessor enabled */10511052if (AslGbl_PreprocessFlag)1053{1054Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);1055if (!Filename)1056{1057AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,10580, 0, 0, 0, NULL, NULL);1059return (AE_ERROR);1060}10611062FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");1063}10641065/*1066* Create the "user" preprocessor output file if -li flag set.1067* Note, this file contains no embedded #line directives.1068*/1069if (AslGbl_PreprocessorOutputFlag)1070{1071Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROC_USER);1072if (!Filename)1073{1074AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,10750, 0, 0, 0, NULL, NULL);1076return (AE_ERROR);1077}10781079FlOpenFile (ASL_FILE_PREPROCESSOR_USER, Filename, "w+t");1080}10811082/* All done for data table compiler */10831084if (AslGbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)1085{1086return (AE_OK);1087}10881089/* Create/Open a combined source output file */10901091Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);1092if (!Filename)1093{1094AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,10950, 0, 0, 0, NULL, NULL);1096return (AE_ERROR);1097}10981099/*1100* Open the source output file, binary mode (so that LF does not get1101* expanded to CR/LF on some systems, messing up our seek1102* calculations.)1103*/1104FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");11051106/*1107// TBD: TEMP1108// AslCompilerin = AslGbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;1109*/1110/* Create/Open a assembly code source output file if asked */11111112if (AslGbl_AsmOutputFlag)1113{1114Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);1115if (!Filename)1116{1117AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,11180, 0, 0, 0, NULL, NULL);1119return (AE_ERROR);1120}11211122/* Open the assembly code source file, text mode */11231124FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");11251126AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);1127AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);1128}11291130/* Create/Open a C code source output file if asked */11311132if (AslGbl_C_OutputFlag)1133{1134Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);1135if (!Filename)1136{1137AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,11380, 0, 0, 0, NULL, NULL);1139return (AE_ERROR);1140}11411142/* Open the C code source file, text mode */11431144FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");11451146FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");1147AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);1148AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);1149}11501151/* Create/Open a C code source output file for the offset table if asked */11521153if (AslGbl_C_OffsetTableFlag)1154{1155Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);1156if (!Filename)1157{1158AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,11590, 0, 0, 0, NULL, NULL);1160return (AE_ERROR);1161}11621163/* Open the C code source file, text mode */11641165FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");11661167FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");1168AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);1169AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);1170}11711172/* Create/Open a assembly include output file if asked */11731174if (AslGbl_AsmIncludeOutputFlag)1175{1176Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);1177if (!Filename)1178{1179AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,11800, 0, 0, 0, NULL, NULL);1181return (AE_ERROR);1182}11831184/* Open the assembly include file, text mode */11851186FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");11871188AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);1189AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);1190}11911192/* Create/Open a C include output file if asked */11931194if (AslGbl_C_IncludeOutputFlag)1195{1196Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);1197if (!Filename)1198{1199AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,12000, 0, 0, 0, NULL, NULL);1201return (AE_ERROR);1202}12031204/* Open the C include file, text mode */12051206FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");12071208FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");1209AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);1210AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);1211}12121213/* Create a namespace output file if asked */12141215if (AslGbl_NsOutputFlag)1216{1217Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);1218if (!Filename)1219{1220AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,12210, 0, 0, 0, NULL, NULL);1222return (AE_ERROR);1223}12241225/* Open the namespace file, text mode */12261227FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");12281229AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);1230AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);1231}12321233/* Create a debug file for the converter */12341235if (AcpiGbl_DebugAslConversion)1236{1237Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_CONVERT_DEBUG);1238if (!Filename)1239{1240AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,12410, 0, 0, 0, NULL, NULL);1242return (AE_ERROR);1243}12441245/* Open the converter debug file, text mode */12461247FlOpenFile (ASL_FILE_CONV_DEBUG_OUTPUT, Filename, "w+t");12481249AslCompilerSignon (ASL_FILE_CONV_DEBUG_OUTPUT);1250AslCompilerFileHeader (ASL_FILE_CONV_DEBUG_OUTPUT);12511252AcpiGbl_ConvDebugFile = AslGbl_Files[ASL_FILE_CONV_DEBUG_OUTPUT].Handle;1253}12541255return (AE_OK);1256}125712581259#ifdef ACPI_OBSOLETE_FUNCTIONS1260/*******************************************************************************1261*1262* FUNCTION: FlParseInputPathname1263*1264* PARAMETERS: InputFilename - The user-specified ASL source file to be1265* compiled1266*1267* RETURN: Status1268*1269* DESCRIPTION: Split the input path into a directory and filename part1270* 1) Directory part used to open include files1271* 2) Filename part used to generate output filenames1272*1273******************************************************************************/12741275ACPI_STATUS1276FlParseInputPathname (1277char *InputFilename)1278{1279char *Substring;128012811282if (!InputFilename)1283{1284return (AE_OK);1285}12861287/* Get the path to the input filename's directory */12881289AslGbl_DirectoryPath = strdup (InputFilename);1290if (!AslGbl_DirectoryPath)1291{1292return (AE_NO_MEMORY);1293}12941295Substring = strrchr (AslGbl_DirectoryPath, '\\');1296if (!Substring)1297{1298Substring = strrchr (AslGbl_DirectoryPath, '/');1299if (!Substring)1300{1301Substring = strrchr (AslGbl_DirectoryPath, ':');1302}1303}13041305if (!Substring)1306{1307AslGbl_DirectoryPath[0] = 0;1308if (AslGbl_UseDefaultAmlFilename)1309{1310AslGbl_OutputFilenamePrefix = strdup (InputFilename);1311}1312}1313else1314{1315if (AslGbl_UseDefaultAmlFilename)1316{1317AslGbl_OutputFilenamePrefix = strdup (Substring + 1);1318}1319*(Substring+1) = 0;1320}13211322UtConvertBackslashes (AslGbl_OutputFilenamePrefix);1323return (AE_OK);1324}1325#endif132613271328