aslfiles.c revision 151937
1118611Snjl
2118611Snjl/******************************************************************************
3118611Snjl *
4118611Snjl * Module Name: aslfiles - file I/O suppoert
5151937Sjkim *              $Revision: 1.52 $
6118611Snjl *
7118611Snjl *****************************************************************************/
8118611Snjl
9118611Snjl/******************************************************************************
10118611Snjl *
11118611Snjl * 1. Copyright Notice
12118611Snjl *
13151937Sjkim * Some or all of this work - Copyright (c) 1999 - 2005, 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
118151937Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
119151937Sjkim#include <contrib/dev/acpica/acapps.h>
120118611Snjl
121118611Snjl#define _COMPONENT          ACPI_COMPILER
122118611Snjl        ACPI_MODULE_NAME    ("aslfiles")
123118611Snjl
124151937Sjkim/* Local prototypes */
125118611Snjl
126151937Sjkimstatic void
127151937SjkimFlOpenFile (
128151937Sjkim    UINT32                  FileId,
129151937Sjkim    char                    *Filename,
130151937Sjkim    char                    *Mode);
131151937Sjkim
132151937Sjkimstatic FILE *
133151937SjkimFlOpenLocalFile (
134151937Sjkim    char                    *LocalName,
135151937Sjkim    char                    *Mode);
136151937Sjkim
137151937Sjkim#ifdef ACPI_OBSOLETE_FUNCTIONS
138151937SjkimACPI_STATUS
139151937SjkimFlParseInputPathname (
140151937Sjkim    char                    *InputFilename);
141151937Sjkim#endif
142151937Sjkim
143151937Sjkim
144118611Snjl/*******************************************************************************
145118611Snjl *
146118611Snjl * FUNCTION:    AslAbort
147118611Snjl *
148118611Snjl * PARAMETERS:  None
149118611Snjl *
150118611Snjl * RETURN:      None
151118611Snjl *
152118611Snjl * DESCRIPTION: Dump the error log and abort the compiler.  Used for serious
153118611Snjl *              I/O errors
154118611Snjl *
155118611Snjl ******************************************************************************/
156118611Snjl
157118611Snjlvoid
158151937SjkimAslAbort (
159151937Sjkim    void)
160118611Snjl{
161118611Snjl
162118611Snjl    AePrintErrorLog (ASL_FILE_STDOUT);
163118611Snjl    if (Gbl_DebugFlag)
164118611Snjl    {
165118611Snjl        /* Print error summary to the debug file */
166118611Snjl
167118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
168118611Snjl    }
169118611Snjl
170151937Sjkim    exit (1);
171118611Snjl}
172118611Snjl
173118611Snjl
174118611Snjl/*******************************************************************************
175118611Snjl *
176118611Snjl * FUNCTION:    FlOpenLocalFile
177118611Snjl *
178118611Snjl * PARAMETERS:  LocalName           - Single filename (not a pathname)
179118611Snjl *              Mode                - Open mode for fopen
180118611Snjl *
181118611Snjl * RETURN:      File descriptor
182118611Snjl *
183118611Snjl * DESCRIPTION: Build a complete pathname for the input filename and open
184118611Snjl *              the file.
185118611Snjl *
186118611Snjl ******************************************************************************/
187118611Snjl
188151937Sjkimstatic FILE *
189118611SnjlFlOpenLocalFile (
190118611Snjl    char                    *LocalName,
191118611Snjl    char                    *Mode)
192118611Snjl{
193118611Snjl
194118611Snjl    strcpy (StringBuffer, Gbl_DirectoryPath);
195118611Snjl    strcat (StringBuffer, LocalName);
196118611Snjl
197118611Snjl    DbgPrint (ASL_PARSE_OUTPUT, "FlOpenLocalFile: %s\n", StringBuffer);
198118611Snjl    return (fopen (StringBuffer, (const char *) Mode));
199118611Snjl}
200118611Snjl
201118611Snjl
202118611Snjl/*******************************************************************************
203118611Snjl *
204118611Snjl * FUNCTION:    FlFileError
205118611Snjl *
206118611Snjl * PARAMETERS:  FileId              - Index into file info array
207118611Snjl *              ErrorId             - Index into error message array
208118611Snjl *
209118611Snjl * RETURN:      None
210118611Snjl *
211118611Snjl * DESCRIPTION: Decode errno to an error message and add the entire error
212118611Snjl *              to the error log.
213118611Snjl *
214118611Snjl ******************************************************************************/
215118611Snjl
216118611Snjlvoid
217118611SnjlFlFileError (
218118611Snjl    UINT32                  FileId,
219118611Snjl    UINT8                   ErrorId)
220118611Snjl{
221118611Snjl
222151937Sjkim    sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
223151937Sjkim        strerror (errno));
224118611Snjl    AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
225118611Snjl}
226118611Snjl
227118611Snjl
228118611Snjl/*******************************************************************************
229118611Snjl *
230118611Snjl * FUNCTION:    FlOpenFile
231118611Snjl *
232118611Snjl * PARAMETERS:  FileId              - Index into file info array
233118611Snjl *              Filename            - file pathname to open
234118611Snjl *              Mode                - Open mode for fopen
235118611Snjl *
236151937Sjkim * RETURN:      None
237118611Snjl *
238118611Snjl * DESCRIPTION: Open a file.
239118611Snjl *              NOTE: Aborts compiler on any error.
240118611Snjl *
241118611Snjl ******************************************************************************/
242118611Snjl
243151937Sjkimstatic void
244118611SnjlFlOpenFile (
245118611Snjl    UINT32                  FileId,
246118611Snjl    char                    *Filename,
247118611Snjl    char                    *Mode)
248118611Snjl{
249118611Snjl    FILE                    *File;
250118611Snjl
251118611Snjl
252118611Snjl    File = fopen (Filename, Mode);
253118611Snjl
254118611Snjl    Gbl_Files[FileId].Filename = Filename;
255118611Snjl    Gbl_Files[FileId].Handle   = File;
256118611Snjl
257118611Snjl    if (!File)
258118611Snjl    {
259118611Snjl        FlFileError (FileId, ASL_MSG_OPEN);
260118611Snjl        AslAbort ();
261118611Snjl    }
262118611Snjl}
263118611Snjl
264118611Snjl
265118611Snjl/*******************************************************************************
266118611Snjl *
267118611Snjl * FUNCTION:    FlReadFile
268118611Snjl *
269118611Snjl * PARAMETERS:  FileId              - Index into file info array
270118611Snjl *              Buffer              - Where to place the data
271118611Snjl *              Length              - Amount to read
272118611Snjl *
273118611Snjl * RETURN:      Status.  AE_ERROR indicates EOF.
274118611Snjl *
275118611Snjl * DESCRIPTION: Read data from an open file.
276118611Snjl *              NOTE: Aborts compiler on any error.
277118611Snjl *
278118611Snjl ******************************************************************************/
279118611Snjl
280118611SnjlACPI_STATUS
281118611SnjlFlReadFile (
282118611Snjl    UINT32                  FileId,
283118611Snjl    void                    *Buffer,
284118611Snjl    UINT32                  Length)
285118611Snjl{
286118611Snjl    UINT32                  Actual;
287118611Snjl
288118611Snjl
289118611Snjl    /* Read and check for error */
290118611Snjl
291118611Snjl    Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
292118611Snjl    if (Actual != Length)
293118611Snjl    {
294118611Snjl        if (feof (Gbl_Files[FileId].Handle))
295118611Snjl        {
296118611Snjl            /* End-of-file, just return error */
297118611Snjl
298118611Snjl            return (AE_ERROR);
299118611Snjl        }
300118611Snjl
301118611Snjl        FlFileError (FileId, ASL_MSG_READ);
302118611Snjl        AslAbort ();
303118611Snjl    }
304118611Snjl
305118611Snjl    return (AE_OK);
306118611Snjl}
307118611Snjl
308118611Snjl
309118611Snjl/*******************************************************************************
310118611Snjl *
311118611Snjl * FUNCTION:    FlWriteFile
312118611Snjl *
313118611Snjl * PARAMETERS:  FileId              - Index into file info array
314118611Snjl *              Buffer              - Data to write
315118611Snjl *              Length              - Amount of data to write
316118611Snjl *
317151937Sjkim * RETURN:      None
318118611Snjl *
319118611Snjl * DESCRIPTION: Write data to an open file.
320118611Snjl *              NOTE: Aborts compiler on any error.
321118611Snjl *
322118611Snjl ******************************************************************************/
323118611Snjl
324118611Snjlvoid
325118611SnjlFlWriteFile (
326118611Snjl    UINT32                  FileId,
327118611Snjl    void                    *Buffer,
328118611Snjl    UINT32                  Length)
329118611Snjl{
330118611Snjl    UINT32                  Actual;
331118611Snjl
332118611Snjl
333118611Snjl    /* Write and check for error */
334118611Snjl
335118611Snjl    Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
336118611Snjl    if (Actual != Length)
337118611Snjl    {
338118611Snjl        FlFileError (FileId, ASL_MSG_WRITE);
339118611Snjl        AslAbort ();
340118611Snjl    }
341118611Snjl}
342118611Snjl
343118611Snjl
344118611Snjl/*******************************************************************************
345118611Snjl *
346118611Snjl * FUNCTION:    FlPrintFile
347118611Snjl *
348118611Snjl * PARAMETERS:  FileId              - Index into file info array
349118611Snjl *              Format              - Printf format string
350118611Snjl *              ...                 - Printf arguments
351118611Snjl *
352118611Snjl * RETURN:      None
353118611Snjl *
354118611Snjl * DESCRIPTION: Formatted write to an open file.
355118611Snjl *              NOTE: Aborts compiler on any error.
356118611Snjl *
357118611Snjl ******************************************************************************/
358118611Snjl
359118611Snjlvoid
360118611SnjlFlPrintFile (
361118611Snjl    UINT32                  FileId,
362118611Snjl    char                    *Format,
363118611Snjl    ...)
364118611Snjl{
365118611Snjl    INT32                   Actual;
366118611Snjl    va_list                 Args;
367118611Snjl
368118611Snjl
369118611Snjl    va_start (Args, Format);
370118611Snjl
371118611Snjl    Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
372118611Snjl    if (Actual == -1)
373118611Snjl    {
374118611Snjl        FlFileError (FileId, ASL_MSG_WRITE);
375118611Snjl        AslAbort ();
376118611Snjl    }
377118611Snjl}
378118611Snjl
379118611Snjl
380118611Snjl/*******************************************************************************
381118611Snjl *
382118611Snjl * FUNCTION:    FlSeekFile
383118611Snjl *
384118611Snjl * PARAMETERS:  FileId              - Index into file info array
385118611Snjl *              Offset              - Absolute byte offset in file
386118611Snjl *
387151937Sjkim * RETURN:      None
388118611Snjl *
389118611Snjl * DESCRIPTION: Seek to absolute offset
390118611Snjl *              NOTE: Aborts compiler on any error.
391118611Snjl *
392118611Snjl ******************************************************************************/
393118611Snjl
394118611Snjlvoid
395118611SnjlFlSeekFile (
396118611Snjl    UINT32                  FileId,
397118611Snjl    long                    Offset)
398118611Snjl{
399118611Snjl    int                     Error;
400118611Snjl
401118611Snjl
402118611Snjl    Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
403118611Snjl    if (Error)
404118611Snjl    {
405118611Snjl        FlFileError (FileId, ASL_MSG_SEEK);
406118611Snjl        AslAbort ();
407118611Snjl    }
408118611Snjl}
409118611Snjl
410118611Snjl
411118611Snjl/*******************************************************************************
412118611Snjl *
413118611Snjl * FUNCTION:    FlCloseFile
414118611Snjl *
415118611Snjl * PARAMETERS:  FileId              - Index into file info array
416118611Snjl *
417151937Sjkim * RETURN:      None
418118611Snjl *
419118611Snjl * DESCRIPTION: Close an open file.  Aborts compiler on error
420118611Snjl *
421118611Snjl ******************************************************************************/
422118611Snjl
423118611Snjlvoid
424118611SnjlFlCloseFile (
425118611Snjl    UINT32                  FileId)
426118611Snjl{
427118611Snjl    int                     Error;
428118611Snjl
429118611Snjl
430118611Snjl    if (!Gbl_Files[FileId].Handle)
431118611Snjl    {
432118611Snjl        return;
433118611Snjl    }
434118611Snjl
435118611Snjl    Error = fclose (Gbl_Files[FileId].Handle);
436118611Snjl    Gbl_Files[FileId].Handle = NULL;
437118611Snjl
438118611Snjl    if (Error)
439118611Snjl    {
440118611Snjl        FlFileError (FileId, ASL_MSG_CLOSE);
441118611Snjl        AslAbort ();
442118611Snjl    }
443118611Snjl
444118611Snjl    return;
445118611Snjl}
446118611Snjl
447118611Snjl
448118611Snjl/*******************************************************************************
449118611Snjl *
450118611Snjl * FUNCTION:    FlSetLineNumber
451118611Snjl *
452118611Snjl * PARAMETERS:  Op        - Parse node for the LINE asl statement
453118611Snjl *
454118611Snjl * RETURN:      None.
455118611Snjl *
456118611Snjl * DESCRIPTION: Set the current line number
457118611Snjl *
458118611Snjl ******************************************************************************/
459118611Snjl
460118611Snjlvoid
461118611SnjlFlSetLineNumber (
462118611Snjl    ACPI_PARSE_OBJECT       *Op)
463118611Snjl{
464118611Snjl
465118611Snjl    Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
466118611Snjl    Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
467118611Snjl}
468118611Snjl
469118611Snjl
470118611Snjl/*******************************************************************************
471118611Snjl *
472118611Snjl * FUNCTION:    FlOpenIncludeFile
473118611Snjl *
474118611Snjl * PARAMETERS:  Op        - Parse node for the INCLUDE ASL statement
475118611Snjl *
476118611Snjl * RETURN:      None.
477118611Snjl *
478118611Snjl * DESCRIPTION: Open an include file and push it on the input file stack.
479118611Snjl *
480118611Snjl ******************************************************************************/
481118611Snjl
482118611Snjlvoid
483118611SnjlFlOpenIncludeFile (
484118611Snjl    ACPI_PARSE_OBJECT       *Op)
485118611Snjl{
486118611Snjl    FILE                    *IncFile;
487118611Snjl
488118611Snjl
489118611Snjl    /* Op must be valid */
490118611Snjl
491118611Snjl    if (!Op)
492118611Snjl    {
493118611Snjl        AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
494118611Snjl            Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
495118611Snjl            Gbl_InputByteCount, Gbl_CurrentColumn,
496118611Snjl            Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
497118611Snjl
498118611Snjl        return;
499118611Snjl    }
500118611Snjl
501118611Snjl    /*
502118611Snjl     * Flush out the "include ()" statement on this line, start
503118611Snjl     * the actual include file on the next line
504118611Snjl     */
505118611Snjl    ResetCurrentLineBuffer ();
506118611Snjl    FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
507118611Snjl    Gbl_CurrentLineOffset++;
508118611Snjl
509118611Snjl    /* Prepend the directory pathname and open the include file */
510118611Snjl
511151937Sjkim    DbgPrint (ASL_PARSE_OUTPUT, "\nOpen include file: path %s\n\n",
512151937Sjkim        Op->Asl.Value.String);
513118611Snjl    IncFile = FlOpenLocalFile (Op->Asl.Value.String, "r");
514118611Snjl    if (!IncFile)
515118611Snjl    {
516118611Snjl        sprintf (MsgBuffer, "%s (%s)", Op->Asl.Value.String, strerror (errno));
517118611Snjl        AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
518118611Snjl        return;
519118611Snjl    }
520118611Snjl
521118611Snjl    /* Push the include file on the open input file stack */
522118611Snjl
523118611Snjl    AslPushInputFileStack (IncFile, Op->Asl.Value.String);
524118611Snjl}
525118611Snjl
526118611Snjl
527118611Snjl/*******************************************************************************
528118611Snjl *
529118611Snjl * FUNCTION:    FlOpenInputFile
530118611Snjl *
531118611Snjl * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
532118611Snjl *                                    compiled
533118611Snjl *
534118611Snjl * RETURN:      Status
535118611Snjl *
536118611Snjl * DESCRIPTION: Open the specified input file, and save the directory path to
537118611Snjl *              the file so that include files can be opened in
538118611Snjl *              the same directory.
539118611Snjl *
540118611Snjl ******************************************************************************/
541118611Snjl
542118611SnjlACPI_STATUS
543118611SnjlFlOpenInputFile (
544118611Snjl    char                    *InputFilename)
545118611Snjl{
546118611Snjl
547118611Snjl    /* Open the input ASL file, text mode */
548118611Snjl
549118611Snjl    FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
550118611Snjl    AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
551118611Snjl
552118611Snjl    return (AE_OK);
553118611Snjl}
554118611Snjl
555118611Snjl
556118611Snjl/*******************************************************************************
557118611Snjl *
558118611Snjl * FUNCTION:    FlOpenAmlOutputFile
559118611Snjl *
560118611Snjl * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
561118611Snjl *
562118611Snjl * RETURN:      Status
563118611Snjl *
564118611Snjl * DESCRIPTION: Create the output filename (*.AML) and open the file.  The file
565118611Snjl *              is created in the same directory as the parent input file.
566118611Snjl *
567118611Snjl ******************************************************************************/
568118611Snjl
569118611SnjlACPI_STATUS
570118611SnjlFlOpenAmlOutputFile (
571118611Snjl    char                    *FilenamePrefix)
572118611Snjl{
573118611Snjl    char                    *Filename;
574118611Snjl
575118611Snjl
576118611Snjl    /* Output filename usually comes from the ASL itself */
577118611Snjl
578118611Snjl    Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
579118611Snjl    if (!Filename)
580118611Snjl    {
581118611Snjl        /* Create the output AML filename */
582118611Snjl
583118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
584118611Snjl        if (!Filename)
585118611Snjl        {
586151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
587151937Sjkim                0, 0, 0, 0, NULL, NULL);
588118611Snjl            return (AE_ERROR);
589118611Snjl        }
590118611Snjl    }
591118611Snjl
592118611Snjl    /* Open the output AML file in binary mode */
593118611Snjl
594118611Snjl    FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
595118611Snjl    return (AE_OK);
596118611Snjl}
597118611Snjl
598118611Snjl
599118611Snjl/*******************************************************************************
600118611Snjl *
601118611Snjl * FUNCTION:    FlOpenMiscOutputFiles
602118611Snjl *
603118611Snjl * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
604118611Snjl *
605118611Snjl * RETURN:      Status
606118611Snjl *
607118611Snjl * DESCRIPTION: Create and open the various output files needed, depending on
608118611Snjl *              the command line options
609118611Snjl *
610118611Snjl ******************************************************************************/
611118611Snjl
612118611SnjlACPI_STATUS
613118611SnjlFlOpenMiscOutputFiles (
614118611Snjl    char                    *FilenamePrefix)
615118611Snjl{
616118611Snjl    char                    *Filename;
617118611Snjl
618118611Snjl
619118611Snjl    /* Create/Open a combined source output file */
620118611Snjl
621118611Snjl    Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
622118611Snjl    if (!Filename)
623118611Snjl    {
624151937Sjkim        AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
625151937Sjkim            0, 0, 0, 0, NULL, NULL);
626118611Snjl        return (AE_ERROR);
627118611Snjl    }
628118611Snjl
629118611Snjl    /*
630118611Snjl     * Open the source output file, binary mode (so that LF does not get
631118611Snjl     * expanded to CR/LF on some systems, messing up our seek
632118611Snjl     * calculations.)
633118611Snjl     */
634118611Snjl    FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
635118611Snjl
636118611Snjl    /* Create/Open a listing output file if asked */
637118611Snjl
638118611Snjl    if (Gbl_ListingFlag)
639118611Snjl    {
640118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
641118611Snjl        if (!Filename)
642118611Snjl        {
643151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
644151937Sjkim                0, 0, 0, 0, NULL, NULL);
645118611Snjl            return (AE_ERROR);
646118611Snjl        }
647118611Snjl
648118611Snjl        /* Open the listing file, text mode */
649118611Snjl
650118611Snjl        FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
651118611Snjl
652118611Snjl        AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
653118611Snjl        AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
654118611Snjl    }
655118611Snjl
656118611Snjl    /* Create/Open a assembly code source output file if asked */
657118611Snjl
658118611Snjl    if (Gbl_AsmOutputFlag)
659118611Snjl    {
660118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
661118611Snjl        if (!Filename)
662118611Snjl        {
663151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
664151937Sjkim                0, 0, 0, 0, NULL, NULL);
665118611Snjl            return (AE_ERROR);
666118611Snjl        }
667118611Snjl
668118611Snjl        /* Open the assembly code source file, text mode */
669118611Snjl
670118611Snjl        FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
671118611Snjl
672118611Snjl        AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
673118611Snjl        AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
674118611Snjl    }
675118611Snjl
676118611Snjl    /* Create/Open a C code source output file if asked */
677118611Snjl
678118611Snjl    if (Gbl_C_OutputFlag)
679118611Snjl    {
680118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
681118611Snjl        if (!Filename)
682118611Snjl        {
683151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
684151937Sjkim                0, 0, 0, 0, NULL, NULL);
685118611Snjl            return (AE_ERROR);
686118611Snjl        }
687118611Snjl
688118611Snjl        /* Open the C code source file, text mode */
689118611Snjl
690118611Snjl        FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
691118611Snjl
692118611Snjl        FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
693118611Snjl        AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
694118611Snjl        AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
695118611Snjl    }
696118611Snjl
697118611Snjl    /* Create/Open a assembly include output file if asked */
698118611Snjl
699118611Snjl    if (Gbl_AsmIncludeOutputFlag)
700118611Snjl    {
701118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
702118611Snjl        if (!Filename)
703118611Snjl        {
704151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
705151937Sjkim                0, 0, 0, 0, NULL, NULL);
706118611Snjl            return (AE_ERROR);
707118611Snjl        }
708118611Snjl
709118611Snjl        /* Open the assembly include file, text mode */
710118611Snjl
711118611Snjl        FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
712118611Snjl
713118611Snjl        AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
714118611Snjl        AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
715118611Snjl    }
716118611Snjl
717118611Snjl    /* Create/Open a C include output file if asked */
718118611Snjl
719118611Snjl    if (Gbl_C_IncludeOutputFlag)
720118611Snjl    {
721118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
722118611Snjl        if (!Filename)
723118611Snjl        {
724151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
725151937Sjkim                0, 0, 0, 0, NULL, NULL);
726118611Snjl            return (AE_ERROR);
727118611Snjl        }
728118611Snjl
729118611Snjl        /* Open the C include file, text mode */
730118611Snjl
731118611Snjl        FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
732118611Snjl
733118611Snjl        FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
734118611Snjl        AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
735118611Snjl        AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
736118611Snjl    }
737118611Snjl
738118611Snjl    /* Create/Open a hex output file if asked */
739118611Snjl
740118611Snjl    if (Gbl_HexOutputFlag)
741118611Snjl    {
742118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
743118611Snjl        if (!Filename)
744118611Snjl        {
745151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
746151937Sjkim                0, 0, 0, 0, NULL, NULL);
747118611Snjl            return (AE_ERROR);
748118611Snjl        }
749118611Snjl
750118611Snjl        /* Open the hex file, text mode */
751118611Snjl
752118611Snjl        FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
753118611Snjl
754118611Snjl        AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
755118611Snjl        AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
756118611Snjl    }
757118611Snjl
758118611Snjl    /* Create a namespace output file if asked */
759118611Snjl
760118611Snjl    if (Gbl_NsOutputFlag)
761118611Snjl    {
762118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
763118611Snjl        if (!Filename)
764118611Snjl        {
765151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
766151937Sjkim                0, 0, 0, 0, NULL, NULL);
767118611Snjl            return (AE_ERROR);
768118611Snjl        }
769118611Snjl
770118611Snjl        /* Open the namespace file, text mode */
771118611Snjl
772118611Snjl        FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
773118611Snjl
774118611Snjl        AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
775118611Snjl        AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
776118611Snjl    }
777118611Snjl
778118611Snjl    /* Create/Open a debug output file if asked */
779118611Snjl
780118611Snjl    if (Gbl_DebugFlag)
781118611Snjl    {
782118611Snjl        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
783118611Snjl        if (!Filename)
784118611Snjl        {
785151937Sjkim            AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
786151937Sjkim                0, 0, 0, 0, NULL, NULL);
787118611Snjl            return (AE_ERROR);
788118611Snjl        }
789118611Snjl
790118611Snjl        /* Open the debug file as STDERR, text mode */
791118611Snjl
792118611Snjl        /* TBD: hide this behind a FlReopenFile function */
793118611Snjl
794118611Snjl        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
795151937Sjkim        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
796151937Sjkim            freopen (Filename, "w+t", stderr);
797118611Snjl
798118611Snjl        AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
799118611Snjl        AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
800118611Snjl    }
801118611Snjl
802118611Snjl    return (AE_OK);
803118611Snjl}
804118611Snjl
805118611Snjl
806151937Sjkim#ifdef ACPI_OBSOLETE_FUNCTIONS
807151937Sjkim/*******************************************************************************
808151937Sjkim *
809151937Sjkim * FUNCTION:    FlParseInputPathname
810151937Sjkim *
811151937Sjkim * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
812151937Sjkim *                                    compiled
813151937Sjkim *
814151937Sjkim * RETURN:      Status
815151937Sjkim *
816151937Sjkim * DESCRIPTION: Split the input path into a directory and filename part
817151937Sjkim *              1) Directory part used to open include files
818151937Sjkim *              2) Filename part used to generate output filenames
819151937Sjkim *
820151937Sjkim ******************************************************************************/
821151937Sjkim
822151937SjkimACPI_STATUS
823151937SjkimFlParseInputPathname (
824151937Sjkim    char                    *InputFilename)
825151937Sjkim{
826151937Sjkim    char                    *Substring;
827151937Sjkim
828151937Sjkim
829151937Sjkim    if (!InputFilename)
830151937Sjkim    {
831151937Sjkim        return (AE_OK);
832151937Sjkim    }
833151937Sjkim
834151937Sjkim    /* Get the path to the input filename's directory */
835151937Sjkim
836151937Sjkim    Gbl_DirectoryPath = strdup (InputFilename);
837151937Sjkim    if (!Gbl_DirectoryPath)
838151937Sjkim    {
839151937Sjkim        return (AE_NO_MEMORY);
840151937Sjkim    }
841151937Sjkim
842151937Sjkim    Substring = strrchr (Gbl_DirectoryPath, '\\');
843151937Sjkim    if (!Substring)
844151937Sjkim    {
845151937Sjkim        Substring = strrchr (Gbl_DirectoryPath, '/');
846151937Sjkim        if (!Substring)
847151937Sjkim        {
848151937Sjkim            Substring = strrchr (Gbl_DirectoryPath, ':');
849151937Sjkim        }
850151937Sjkim    }
851151937Sjkim
852151937Sjkim    if (!Substring)
853151937Sjkim    {
854151937Sjkim        Gbl_DirectoryPath[0] = 0;
855151937Sjkim        if (Gbl_UseDefaultAmlFilename)
856151937Sjkim        {
857151937Sjkim            Gbl_OutputFilenamePrefix = strdup (InputFilename);
858151937Sjkim        }
859151937Sjkim    }
860151937Sjkim    else
861151937Sjkim    {
862151937Sjkim        if (Gbl_UseDefaultAmlFilename)
863151937Sjkim        {
864151937Sjkim            Gbl_OutputFilenamePrefix = strdup (Substring + 1);
865151937Sjkim        }
866151937Sjkim        *(Substring+1) = 0;
867151937Sjkim    }
868151937Sjkim
869151937Sjkim    return (AE_OK);
870151937Sjkim}
871151937Sjkim#endif
872151937Sjkim
873151937Sjkim
874