aslfiles.c revision 118611
1118611Snjl
2118611Snjl/******************************************************************************
3118611Snjl *
4118611Snjl * Module Name: aslfiles - file I/O suppoert
5118611Snjl *              $Revision: 46 $
6118611Snjl *
7118611Snjl *****************************************************************************/
8118611Snjl
9118611Snjl/******************************************************************************
10118611Snjl *
11118611Snjl * 1. Copyright Notice
12118611Snjl *
13118611Snjl * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
14118611Snjl * All rights reserved.
15118611Snjl *
16118611Snjl * 2. License
17118611Snjl *
18118611Snjl * 2.1. This is your license from Intel Corp. under its intellectual property
19118611Snjl * rights.  You may have additional license terms from the party that provided
20118611Snjl * you this software, covering your right to use that party's intellectual
21118611Snjl * property rights.
22118611Snjl *
23118611Snjl * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24118611Snjl * copy of the source code appearing in this file ("Covered Code") an
25118611Snjl * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26118611Snjl * base code distributed originally by Intel ("Original Intel Code") to copy,
27118611Snjl * make derivatives, distribute, use and display any portion of the Covered
28118611Snjl * Code in any form, with the right to sublicense such rights; and
29118611Snjl *
30118611Snjl * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31118611Snjl * license (with the right to sublicense), under only those claims of Intel
32118611Snjl * patents that are infringed by the Original Intel Code, to make, use, sell,
33118611Snjl * offer to sell, and import the Covered Code and derivative works thereof
34118611Snjl * solely to the minimum extent necessary to exercise the above copyright
35118611Snjl * license, and in no event shall the patent license extend to any additions
36118611Snjl * to or modifications of the Original Intel Code.  No other license or right
37118611Snjl * is granted directly or by implication, estoppel or otherwise;
38118611Snjl *
39118611Snjl * The above copyright and patent license is granted only if the following
40118611Snjl * conditions are met:
41118611Snjl *
42118611Snjl * 3. Conditions
43118611Snjl *
44118611Snjl * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45118611Snjl * Redistribution of source code of any substantial portion of the Covered
46118611Snjl * Code or modification with rights to further distribute source must include
47118611Snjl * the above Copyright Notice, the above License, this list of Conditions,
48118611Snjl * and the following Disclaimer and Export Compliance provision.  In addition,
49118611Snjl * Licensee must cause all Covered Code to which Licensee contributes to
50118611Snjl * contain a file documenting the changes Licensee made to create that Covered
51118611Snjl * Code and the date of any change.  Licensee must include in that file the
52118611Snjl * documentation of any changes made by any predecessor Licensee.  Licensee
53118611Snjl * must include a prominent statement that the modification is derived,
54118611Snjl * directly or indirectly, from Original Intel Code.
55118611Snjl *
56118611Snjl * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57118611Snjl * Redistribution of source code of any substantial portion of the Covered
58118611Snjl * Code or modification without rights to further distribute source must
59118611Snjl * include the following Disclaimer and Export Compliance provision in the
60118611Snjl * documentation and/or other materials provided with distribution.  In
61118611Snjl * addition, Licensee may not authorize further sublicense of source of any
62118611Snjl * portion of the Covered Code, and must include terms to the effect that the
63118611Snjl * license from Licensee to its licensee is limited to the intellectual
64118611Snjl * property embodied in the software Licensee provides to its licensee, and
65118611Snjl * not to intellectual property embodied in modifications its licensee may
66118611Snjl * make.
67118611Snjl *
68118611Snjl * 3.3. Redistribution of Executable. Redistribution in executable form of any
69118611Snjl * substantial portion of the Covered Code or modification must reproduce the
70118611Snjl * above Copyright Notice, and the following Disclaimer and Export Compliance
71118611Snjl * provision in the documentation and/or other materials provided with the
72118611Snjl * distribution.
73118611Snjl *
74118611Snjl * 3.4. Intel retains all right, title, and interest in and to the Original
75118611Snjl * Intel Code.
76118611Snjl *
77118611Snjl * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78118611Snjl * Intel shall be used in advertising or otherwise to promote the sale, use or
79118611Snjl * other dealings in products derived from or relating to the Covered Code
80118611Snjl * without prior written authorization from Intel.
81118611Snjl *
82118611Snjl * 4. Disclaimer and Export Compliance
83118611Snjl *
84118611Snjl * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85118611Snjl * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86118611Snjl * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87118611Snjl * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88118611Snjl * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89118611Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90118611Snjl * PARTICULAR PURPOSE.
91118611Snjl *
92118611Snjl * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93118611Snjl * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94118611Snjl * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95118611Snjl * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96118611Snjl * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97118611Snjl * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98118611Snjl * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99118611Snjl * LIMITED REMEDY.
100118611Snjl *
101118611Snjl * 4.3. Licensee shall not export, either directly or indirectly, any of this
102118611Snjl * software or system incorporating such software without first obtaining any
103118611Snjl * required license or other approval from the U. S. Department of Commerce or
104118611Snjl * any other agency or department of the United States Government.  In the
105118611Snjl * event Licensee exports any such software from the United States or
106118611Snjl * re-exports any such software from a foreign destination, Licensee shall
107118611Snjl * ensure that the distribution and export/re-export of the software is in
108118611Snjl * compliance with all laws, regulations, orders, or other restrictions of the
109118611Snjl * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110118611Snjl * any of its subsidiaries will export/re-export any technical data, process,
111118611Snjl * software, or service, directly or indirectly, to any country for which the
112118611Snjl * United States government or any agency thereof requires an export license,
113118611Snjl * other governmental approval, or letter of assurance, without first obtaining
114118611Snjl * such license, approval or letter.
115118611Snjl *
116118611Snjl *****************************************************************************/
117118611Snjl
118118611Snjl#include "aslcompiler.h"
119118611Snjl#include "acapps.h"
120118611Snjl
121118611Snjl#define _COMPONENT          ACPI_COMPILER
122118611Snjl        ACPI_MODULE_NAME    ("aslfiles")
123118611Snjl
124118611Snjl
125118611Snjl/*******************************************************************************
126118611Snjl *
127118611Snjl * FUNCTION:    AslAbort
128118611Snjl *
129118611Snjl * PARAMETERS:  None
130118611Snjl *
131118611Snjl * RETURN:      None
132118611Snjl *
133118611Snjl * DESCRIPTION: Dump the error log and abort the compiler.  Used for serious
134118611Snjl *              I/O errors
135118611Snjl *
136118611Snjl ******************************************************************************/
137118611Snjl
138118611Snjlvoid
139118611SnjlAslAbort (void)
140118611Snjl{
141118611Snjl
142118611Snjl    AePrintErrorLog (ASL_FILE_STDOUT);
143118611Snjl    if (Gbl_DebugFlag)
144118611Snjl    {
145118611Snjl        /* Print error summary to the debug file */
146118611Snjl
147118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
148118611Snjl    }
149118611Snjl
150118611Snjl    exit (0);
151118611Snjl}
152118611Snjl
153118611Snjl
154118611Snjl/*******************************************************************************
155118611Snjl *
156118611Snjl * FUNCTION:    FlOpenLocalFile
157118611Snjl *
158118611Snjl * PARAMETERS:  LocalName           - Single filename (not a pathname)
159118611Snjl *              Mode                - Open mode for fopen
160118611Snjl *
161118611Snjl * RETURN:      File descriptor
162118611Snjl *
163118611Snjl * DESCRIPTION: Build a complete pathname for the input filename and open
164118611Snjl *              the file.
165118611Snjl *
166118611Snjl ******************************************************************************/
167118611Snjl
168118611SnjlFILE *
169118611SnjlFlOpenLocalFile (
170118611Snjl    char                    *LocalName,
171118611Snjl    char                    *Mode)
172118611Snjl{
173118611Snjl
174118611Snjl    strcpy (StringBuffer, Gbl_DirectoryPath);
175118611Snjl    strcat (StringBuffer, LocalName);
176118611Snjl
177118611Snjl    DbgPrint (ASL_PARSE_OUTPUT, "FlOpenLocalFile: %s\n", StringBuffer);
178118611Snjl    return (fopen (StringBuffer, (const char *) Mode));
179118611Snjl}
180118611Snjl
181118611Snjl
182118611Snjl/*******************************************************************************
183118611Snjl *
184118611Snjl * FUNCTION:    FlFileError
185118611Snjl *
186118611Snjl * PARAMETERS:  FileId              - Index into file info array
187118611Snjl *              ErrorId             - Index into error message array
188118611Snjl *
189118611Snjl * RETURN:      None
190118611Snjl *
191118611Snjl * DESCRIPTION: Decode errno to an error message and add the entire error
192118611Snjl *              to the error log.
193118611Snjl *
194118611Snjl ******************************************************************************/
195118611Snjl
196118611Snjlvoid
197118611SnjlFlFileError (
198118611Snjl    UINT32                  FileId,
199118611Snjl    UINT8                   ErrorId)
200118611Snjl{
201118611Snjl
202118611Snjl    sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename, strerror (errno));
203118611Snjl    AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
204118611Snjl}
205118611Snjl
206118611Snjl
207118611Snjl/*******************************************************************************
208118611Snjl *
209118611Snjl * FUNCTION:    FlOpenFile
210118611Snjl *
211118611Snjl * PARAMETERS:  FileId              - Index into file info array
212118611Snjl *              Filename            - file pathname to open
213118611Snjl *              Mode                - Open mode for fopen
214118611Snjl *
215118611Snjl * RETURN:      File descriptor
216118611Snjl *
217118611Snjl * DESCRIPTION: Open a file.
218118611Snjl *              NOTE: Aborts compiler on any error.
219118611Snjl *
220118611Snjl ******************************************************************************/
221118611Snjl
222118611Snjlvoid
223118611SnjlFlOpenFile (
224118611Snjl    UINT32                  FileId,
225118611Snjl    char                    *Filename,
226118611Snjl    char                    *Mode)
227118611Snjl{
228118611Snjl    FILE                    *File;
229118611Snjl
230118611Snjl
231118611Snjl    File = fopen (Filename, Mode);
232118611Snjl
233118611Snjl    Gbl_Files[FileId].Filename = Filename;
234118611Snjl    Gbl_Files[FileId].Handle   = File;
235118611Snjl
236118611Snjl    if (!File)
237118611Snjl    {
238118611Snjl        FlFileError (FileId, ASL_MSG_OPEN);
239118611Snjl        AslAbort ();
240118611Snjl    }
241118611Snjl}
242118611Snjl
243118611Snjl
244118611Snjl/*******************************************************************************
245118611Snjl *
246118611Snjl * FUNCTION:    FlReadFile
247118611Snjl *
248118611Snjl * PARAMETERS:  FileId              - Index into file info array
249118611Snjl *              Buffer              - Where to place the data
250118611Snjl *              Length              - Amount to read
251118611Snjl *
252118611Snjl * RETURN:      Status.  AE_ERROR indicates EOF.
253118611Snjl *
254118611Snjl * DESCRIPTION: Read data from an open file.
255118611Snjl *              NOTE: Aborts compiler on any error.
256118611Snjl *
257118611Snjl ******************************************************************************/
258118611Snjl
259118611SnjlACPI_STATUS
260118611SnjlFlReadFile (
261118611Snjl    UINT32                  FileId,
262118611Snjl    void                    *Buffer,
263118611Snjl    UINT32                  Length)
264118611Snjl{
265118611Snjl    UINT32                  Actual;
266118611Snjl
267118611Snjl
268118611Snjl    /* Read and check for error */
269118611Snjl
270118611Snjl    Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
271118611Snjl    if (Actual != Length)
272118611Snjl    {
273118611Snjl        if (feof (Gbl_Files[FileId].Handle))
274118611Snjl        {
275118611Snjl            /* End-of-file, just return error */
276118611Snjl
277118611Snjl            return (AE_ERROR);
278118611Snjl        }
279118611Snjl
280118611Snjl        FlFileError (FileId, ASL_MSG_READ);
281118611Snjl        AslAbort ();
282118611Snjl    }
283118611Snjl
284118611Snjl    return (AE_OK);
285118611Snjl}
286118611Snjl
287118611Snjl
288118611Snjl/*******************************************************************************
289118611Snjl *
290118611Snjl * FUNCTION:    FlWriteFile
291118611Snjl *
292118611Snjl * PARAMETERS:  FileId              - Index into file info array
293118611Snjl *              Buffer              - Data to write
294118611Snjl *              Length              - Amount of data to write
295118611Snjl *
296118611Snjl * RETURN:      Status
297118611Snjl *
298118611Snjl * DESCRIPTION: Write data to an open file.
299118611Snjl *              NOTE: Aborts compiler on any error.
300118611Snjl *
301118611Snjl ******************************************************************************/
302118611Snjl
303118611Snjlvoid
304118611SnjlFlWriteFile (
305118611Snjl    UINT32                  FileId,
306118611Snjl    void                    *Buffer,
307118611Snjl    UINT32                  Length)
308118611Snjl{
309118611Snjl    UINT32                  Actual;
310118611Snjl
311118611Snjl
312118611Snjl    /* Write and check for error */
313118611Snjl
314118611Snjl    Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
315118611Snjl    if (Actual != Length)
316118611Snjl    {
317118611Snjl        FlFileError (FileId, ASL_MSG_WRITE);
318118611Snjl        AslAbort ();
319118611Snjl    }
320118611Snjl}
321118611Snjl
322118611Snjl
323118611Snjl/*******************************************************************************
324118611Snjl *
325118611Snjl * FUNCTION:    FlPrintFile
326118611Snjl *
327118611Snjl * PARAMETERS:  FileId              - Index into file info array
328118611Snjl *              Format              - Printf format string
329118611Snjl *              ...                 - Printf arguments
330118611Snjl *
331118611Snjl * RETURN:      None
332118611Snjl *
333118611Snjl * DESCRIPTION: Formatted write to an open file.
334118611Snjl *              NOTE: Aborts compiler on any error.
335118611Snjl *
336118611Snjl ******************************************************************************/
337118611Snjl
338118611Snjlvoid
339118611SnjlFlPrintFile (
340118611Snjl    UINT32                  FileId,
341118611Snjl    char                    *Format,
342118611Snjl    ...)
343118611Snjl{
344118611Snjl    INT32                   Actual;
345118611Snjl    va_list                 Args;
346118611Snjl
347118611Snjl
348118611Snjl    va_start (Args, Format);
349118611Snjl
350118611Snjl    Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
351118611Snjl    if (Actual == -1)
352118611Snjl    {
353118611Snjl        FlFileError (FileId, ASL_MSG_WRITE);
354118611Snjl        AslAbort ();
355118611Snjl    }
356118611Snjl}
357118611Snjl
358118611Snjl
359118611Snjl/*******************************************************************************
360118611Snjl *
361118611Snjl * FUNCTION:    FlSeekFile
362118611Snjl *
363118611Snjl * PARAMETERS:  FileId              - Index into file info array
364118611Snjl *              Offset              - Absolute byte offset in file
365118611Snjl *
366118611Snjl * RETURN:      Status
367118611Snjl *
368118611Snjl * DESCRIPTION: Seek to absolute offset
369118611Snjl *              NOTE: Aborts compiler on any error.
370118611Snjl *
371118611Snjl ******************************************************************************/
372118611Snjl
373118611Snjlvoid
374118611SnjlFlSeekFile (
375118611Snjl    UINT32                  FileId,
376118611Snjl    long                    Offset)
377118611Snjl{
378118611Snjl    int                     Error;
379118611Snjl
380118611Snjl
381118611Snjl    Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
382118611Snjl    if (Error)
383118611Snjl    {
384118611Snjl        FlFileError (FileId, ASL_MSG_SEEK);
385118611Snjl        AslAbort ();
386118611Snjl    }
387118611Snjl}
388118611Snjl
389118611Snjl
390118611Snjl/*******************************************************************************
391118611Snjl *
392118611Snjl * FUNCTION:    FlCloseFile
393118611Snjl *
394118611Snjl * PARAMETERS:  FileId              - Index into file info array
395118611Snjl *
396118611Snjl * RETURN:      Status
397118611Snjl *
398118611Snjl * DESCRIPTION: Close an open file.  Aborts compiler on error
399118611Snjl *
400118611Snjl ******************************************************************************/
401118611Snjl
402118611Snjlvoid
403118611SnjlFlCloseFile (
404118611Snjl    UINT32                  FileId)
405118611Snjl{
406118611Snjl    int                     Error;
407118611Snjl
408118611Snjl
409118611Snjl    if (!Gbl_Files[FileId].Handle)
410118611Snjl    {
411118611Snjl        return;
412118611Snjl    }
413118611Snjl
414118611Snjl    Error = fclose (Gbl_Files[FileId].Handle);
415118611Snjl    Gbl_Files[FileId].Handle = NULL;
416118611Snjl
417118611Snjl    if (Error)
418118611Snjl    {
419118611Snjl        FlFileError (FileId, ASL_MSG_CLOSE);
420118611Snjl        AslAbort ();
421118611Snjl    }
422118611Snjl
423118611Snjl    return;
424118611Snjl}
425118611Snjl
426118611Snjl
427118611Snjl/*******************************************************************************
428118611Snjl *
429118611Snjl * FUNCTION:    FlSetLineNumber
430118611Snjl *
431118611Snjl * PARAMETERS:  Op        - Parse node for the LINE asl statement
432118611Snjl *
433118611Snjl * RETURN:      None.
434118611Snjl *
435118611Snjl * DESCRIPTION: Set the current line number
436118611Snjl *
437118611Snjl ******************************************************************************/
438118611Snjl
439118611Snjlvoid
440118611SnjlFlSetLineNumber (
441118611Snjl    ACPI_PARSE_OBJECT       *Op)
442118611Snjl{
443118611Snjl
444118611Snjl    Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
445118611Snjl    Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
446118611Snjl}
447118611Snjl
448118611Snjl
449118611Snjl/*******************************************************************************
450118611Snjl *
451118611Snjl * FUNCTION:    FlOpenIncludeFile
452118611Snjl *
453118611Snjl * PARAMETERS:  Op        - Parse node for the INCLUDE ASL statement
454118611Snjl *
455118611Snjl * RETURN:      None.
456118611Snjl *
457118611Snjl * DESCRIPTION: Open an include file and push it on the input file stack.
458118611Snjl *
459118611Snjl ******************************************************************************/
460118611Snjl
461118611Snjlvoid
462118611SnjlFlOpenIncludeFile (
463118611Snjl    ACPI_PARSE_OBJECT       *Op)
464118611Snjl{
465118611Snjl    FILE                    *IncFile;
466118611Snjl
467118611Snjl
468118611Snjl    /* Op must be valid */
469118611Snjl
470118611Snjl    if (!Op)
471118611Snjl    {
472118611Snjl        AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
473118611Snjl            Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
474118611Snjl            Gbl_InputByteCount, Gbl_CurrentColumn,
475118611Snjl            Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
476118611Snjl
477118611Snjl        return;
478118611Snjl    }
479118611Snjl
480118611Snjl    /*
481118611Snjl     * Flush out the "include ()" statement on this line, start
482118611Snjl     * the actual include file on the next line
483118611Snjl     */
484118611Snjl    ResetCurrentLineBuffer ();
485118611Snjl    FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
486118611Snjl    Gbl_CurrentLineOffset++;
487118611Snjl
488118611Snjl    /* Prepend the directory pathname and open the include file */
489118611Snjl
490118611Snjl    DbgPrint (ASL_PARSE_OUTPUT, "\nOpen include file: path %s\n\n", Op->Asl.Value.String);
491118611Snjl    IncFile = FlOpenLocalFile (Op->Asl.Value.String, "r");
492118611Snjl    if (!IncFile)
493118611Snjl    {
494118611Snjl        sprintf (MsgBuffer, "%s (%s)", Op->Asl.Value.String, strerror (errno));
495118611Snjl        AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
496118611Snjl        return;
497118611Snjl    }
498118611Snjl
499118611Snjl    /* Push the include file on the open input file stack */
500118611Snjl
501118611Snjl    AslPushInputFileStack (IncFile, Op->Asl.Value.String);
502118611Snjl}
503118611Snjl
504118611Snjl
505118611Snjl/*******************************************************************************
506118611Snjl *
507118611Snjl * FUNCTION:    FlParseInputPathname
508118611Snjl *
509118611Snjl * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
510118611Snjl *                                    compiled
511118611Snjl *
512118611Snjl * RETURN:      Status
513118611Snjl *
514118611Snjl * DESCRIPTION: Split the input path into a directory and filename part
515118611Snjl *              1) Directory part used to open include files
516118611Snjl *              2) Filename part used to generate output filenames
517118611Snjl *
518118611Snjl ******************************************************************************/
519118611Snjl
520118611SnjlACPI_STATUS
521118611SnjlFlParseInputPathname (
522118611Snjl    char                    *InputFilename)
523118611Snjl{
524118611Snjl    char                    *Substring;
525118611Snjl
526118611Snjl
527118611Snjl    if (!InputFilename)
528118611Snjl    {
529118611Snjl        return (AE_OK);
530118611Snjl    }
531118611Snjl
532118611Snjl    /* Get the path to the input filename's directory */
533118611Snjl
534118611Snjl    Gbl_DirectoryPath = strdup (InputFilename);
535118611Snjl    if (!Gbl_DirectoryPath)
536118611Snjl    {
537118611Snjl        return (AE_NO_MEMORY);
538118611Snjl    }
539118611Snjl
540118611Snjl    Substring = strrchr (Gbl_DirectoryPath, '\\');
541118611Snjl    if (!Substring)
542118611Snjl    {
543118611Snjl        Substring = strrchr (Gbl_DirectoryPath, '/');
544118611Snjl        if (!Substring)
545118611Snjl        {
546118611Snjl            Substring = strrchr (Gbl_DirectoryPath, ':');
547118611Snjl        }
548118611Snjl    }
549118611Snjl
550118611Snjl    if (!Substring)
551118611Snjl    {
552118611Snjl        Gbl_DirectoryPath[0] = 0;
553118611Snjl        if (Gbl_UseDefaultAmlFilename)
554118611Snjl        {
555118611Snjl            Gbl_OutputFilenamePrefix = strdup (InputFilename);
556118611Snjl        }
557118611Snjl    }
558118611Snjl    else
559118611Snjl    {
560118611Snjl        if (Gbl_UseDefaultAmlFilename)
561118611Snjl        {
562118611Snjl            Gbl_OutputFilenamePrefix = strdup (Substring + 1);
563118611Snjl        }
564118611Snjl        *(Substring+1) = 0;
565118611Snjl    }
566118611Snjl
567118611Snjl    return (AE_OK);
568118611Snjl}
569118611Snjl
570118611Snjl
571118611Snjl/*******************************************************************************
572118611Snjl *
573118611Snjl * FUNCTION:    FlOpenInputFile
574118611Snjl *
575118611Snjl * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
576118611Snjl *                                    compiled
577118611Snjl *
578118611Snjl * RETURN:      Status
579118611Snjl *
580118611Snjl * DESCRIPTION: Open the specified input file, and save the directory path to
581118611Snjl *              the file so that include files can be opened in
582118611Snjl *              the same directory.
583118611Snjl *
584118611Snjl ******************************************************************************/
585118611Snjl
586118611SnjlACPI_STATUS
587118611SnjlFlOpenInputFile (
588118611Snjl    char                    *InputFilename)
589118611Snjl{
590118611Snjl
591118611Snjl
592118611Snjl    /* Open the input ASL file, text mode */
593118611Snjl
594118611Snjl    FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
595118611Snjl    AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
596118611Snjl
597118611Snjl    return (AE_OK);
598118611Snjl}
599118611Snjl
600118611Snjl
601118611Snjl/*******************************************************************************
602118611Snjl *
603118611Snjl * FUNCTION:    FlOpenAmlOutputFile
604118611Snjl *
605118611Snjl * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
606118611Snjl *
607118611Snjl * RETURN:      Status
608118611Snjl *
609118611Snjl * DESCRIPTION: Create the output filename (*.AML) and open the file.  The file
610118611Snjl *              is created in the same directory as the parent input file.
611118611Snjl *
612118611Snjl ******************************************************************************/
613118611Snjl
614118611SnjlACPI_STATUS
615118611SnjlFlOpenAmlOutputFile (
616118611Snjl    char                    *FilenamePrefix)
617118611Snjl{
618118611Snjl    char                    *Filename;
619118611Snjl
620118611Snjl
621118611Snjl    /* Output filename usually comes from the ASL itself */
622118611Snjl
623118611Snjl    Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
624118611Snjl    if (!Filename)
625118611Snjl    {
626118611Snjl        /* Create the output AML filename */
627118611Snjl
628118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
629118611Snjl        if (!Filename)
630118611Snjl        {
631118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME, 0, 0, 0, 0, NULL, NULL);
632118611Snjl            return (AE_ERROR);
633118611Snjl        }
634118611Snjl    }
635118611Snjl
636118611Snjl    /* Open the output AML file in binary mode */
637118611Snjl
638118611Snjl    FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
639118611Snjl    return (AE_OK);
640118611Snjl}
641118611Snjl
642118611Snjl
643118611Snjl/*******************************************************************************
644118611Snjl *
645118611Snjl * FUNCTION:    FlOpenMiscOutputFiles
646118611Snjl *
647118611Snjl * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
648118611Snjl *
649118611Snjl * RETURN:      Status
650118611Snjl *
651118611Snjl * DESCRIPTION: Create and open the various output files needed, depending on
652118611Snjl *              the command line options
653118611Snjl *
654118611Snjl ******************************************************************************/
655118611Snjl
656118611SnjlACPI_STATUS
657118611SnjlFlOpenMiscOutputFiles (
658118611Snjl    char                    *FilenamePrefix)
659118611Snjl{
660118611Snjl    char                    *Filename;
661118611Snjl
662118611Snjl
663118611Snjl    /* Create/Open a combined source output file */
664118611Snjl
665118611Snjl    Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
666118611Snjl    if (!Filename)
667118611Snjl    {
668118611Snjl        AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
669118611Snjl        return (AE_ERROR);
670118611Snjl    }
671118611Snjl
672118611Snjl    /*
673118611Snjl     * Open the source output file, binary mode (so that LF does not get
674118611Snjl     * expanded to CR/LF on some systems, messing up our seek
675118611Snjl     * calculations.)
676118611Snjl     */
677118611Snjl    FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
678118611Snjl
679118611Snjl    /* Create/Open a listing output file if asked */
680118611Snjl
681118611Snjl    if (Gbl_ListingFlag)
682118611Snjl    {
683118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
684118611Snjl        if (!Filename)
685118611Snjl        {
686118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
687118611Snjl            return (AE_ERROR);
688118611Snjl        }
689118611Snjl
690118611Snjl        /* Open the listing file, text mode */
691118611Snjl
692118611Snjl        FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
693118611Snjl
694118611Snjl        AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
695118611Snjl        AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
696118611Snjl    }
697118611Snjl
698118611Snjl    /* Create/Open a assembly code source output file if asked */
699118611Snjl
700118611Snjl    if (Gbl_AsmOutputFlag)
701118611Snjl    {
702118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
703118611Snjl        if (!Filename)
704118611Snjl        {
705118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
706118611Snjl            return (AE_ERROR);
707118611Snjl        }
708118611Snjl
709118611Snjl        /* Open the assembly code source file, text mode */
710118611Snjl
711118611Snjl        FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
712118611Snjl
713118611Snjl        AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
714118611Snjl        AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
715118611Snjl    }
716118611Snjl
717118611Snjl    /* Create/Open a C code source output file if asked */
718118611Snjl
719118611Snjl    if (Gbl_C_OutputFlag)
720118611Snjl    {
721118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
722118611Snjl        if (!Filename)
723118611Snjl        {
724118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
725118611Snjl            return (AE_ERROR);
726118611Snjl        }
727118611Snjl
728118611Snjl        /* Open the C code source file, text mode */
729118611Snjl
730118611Snjl        FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
731118611Snjl
732118611Snjl        FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
733118611Snjl        AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
734118611Snjl        AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
735118611Snjl    }
736118611Snjl
737118611Snjl    /* Create/Open a assembly include output file if asked */
738118611Snjl
739118611Snjl    if (Gbl_AsmIncludeOutputFlag)
740118611Snjl    {
741118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
742118611Snjl        if (!Filename)
743118611Snjl        {
744118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
745118611Snjl            return (AE_ERROR);
746118611Snjl        }
747118611Snjl
748118611Snjl        /* Open the assembly include file, text mode */
749118611Snjl
750118611Snjl        FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
751118611Snjl
752118611Snjl        AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
753118611Snjl        AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
754118611Snjl    }
755118611Snjl
756118611Snjl    /* Create/Open a C include output file if asked */
757118611Snjl
758118611Snjl    if (Gbl_C_IncludeOutputFlag)
759118611Snjl    {
760118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
761118611Snjl        if (!Filename)
762118611Snjl        {
763118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
764118611Snjl            return (AE_ERROR);
765118611Snjl        }
766118611Snjl
767118611Snjl        /* Open the C include file, text mode */
768118611Snjl
769118611Snjl        FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
770118611Snjl
771118611Snjl        FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
772118611Snjl        AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
773118611Snjl        AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
774118611Snjl    }
775118611Snjl
776118611Snjl    /* Create/Open a hex output file if asked */
777118611Snjl
778118611Snjl    if (Gbl_HexOutputFlag)
779118611Snjl    {
780118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
781118611Snjl        if (!Filename)
782118611Snjl        {
783118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
784118611Snjl            return (AE_ERROR);
785118611Snjl        }
786118611Snjl
787118611Snjl        /* Open the hex file, text mode */
788118611Snjl
789118611Snjl        FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
790118611Snjl
791118611Snjl        AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
792118611Snjl        AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
793118611Snjl    }
794118611Snjl
795118611Snjl    /* Create a namespace output file if asked */
796118611Snjl
797118611Snjl    if (Gbl_NsOutputFlag)
798118611Snjl    {
799118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
800118611Snjl        if (!Filename)
801118611Snjl        {
802118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
803118611Snjl            return (AE_ERROR);
804118611Snjl        }
805118611Snjl
806118611Snjl        /* Open the namespace file, text mode */
807118611Snjl
808118611Snjl        FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
809118611Snjl
810118611Snjl        AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
811118611Snjl        AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
812118611Snjl    }
813118611Snjl
814118611Snjl    /* Create/Open a debug output file if asked */
815118611Snjl
816118611Snjl    if (Gbl_DebugFlag)
817118611Snjl    {
818118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
819118611Snjl        if (!Filename)
820118611Snjl        {
821118611Snjl            AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME, 0, 0, 0, 0, NULL, NULL);
822118611Snjl            return (AE_ERROR);
823118611Snjl        }
824118611Snjl
825118611Snjl        /* Open the debug file as STDERR, text mode */
826118611Snjl
827118611Snjl        /* TBD: hide this behind a FlReopenFile function */
828118611Snjl
829118611Snjl        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
830118611Snjl        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle   = freopen (Filename, "w+t", stderr);
831118611Snjl
832118611Snjl        AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
833118611Snjl        AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
834118611Snjl    }
835118611Snjl
836118611Snjl    return (AE_OK);
837118611Snjl}
838118611Snjl
839118611Snjl
840