]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/acpica/lib-acpi/src/acpica/compiler/aslfiles.c
Some minor fixes.
[l4.git] / l4 / pkg / acpica / lib-acpi / src / acpica / compiler / aslfiles.c
1 /******************************************************************************
2  *
3  * Module Name: aslfiles - file I/O suppoert
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2012, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116 #include "aslcompiler.h"
117 #include "acapps.h"
118
119 #define _COMPONENT          ACPI_COMPILER
120         ACPI_MODULE_NAME    ("aslfiles")
121
122 /* Local prototypes */
123
124 FILE *
125 FlOpenIncludeWithPrefix (
126     char                    *PrefixDir,
127     char                    *Filename);
128
129
130 #ifdef ACPI_OBSOLETE_FUNCTIONS
131 ACPI_STATUS
132 FlParseInputPathname (
133     char                    *InputFilename);
134 #endif
135
136
137 /*******************************************************************************
138  *
139  * FUNCTION:    AslAbort
140  *
141  * PARAMETERS:  None
142  *
143  * RETURN:      None
144  *
145  * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
146  *              I/O errors
147  *
148  ******************************************************************************/
149
150 void
151 AslAbort (
152     void)
153 {
154
155     AePrintErrorLog (ASL_FILE_STDERR);
156     if (Gbl_DebugFlag)
157     {
158         /* Print error summary to stdout also */
159
160         AePrintErrorLog (ASL_FILE_STDOUT);
161     }
162
163     exit (1);
164 }
165
166
167 /*******************************************************************************
168  *
169  * FUNCTION:    FlFileError
170  *
171  * PARAMETERS:  FileId              - Index into file info array
172  *              ErrorId             - Index into error message array
173  *
174  * RETURN:      None
175  *
176  * DESCRIPTION: Decode errno to an error message and add the entire error
177  *              to the error log.
178  *
179  ******************************************************************************/
180
181 void
182 FlFileError (
183     UINT32                  FileId,
184     UINT8                   ErrorId)
185 {
186
187     sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
188         strerror (errno));
189     AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
190 }
191
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    FlOpenFile
196  *
197  * PARAMETERS:  FileId              - Index into file info array
198  *              Filename            - file pathname to open
199  *              Mode                - Open mode for fopen
200  *
201  * RETURN:      None
202  *
203  * DESCRIPTION: Open a file.
204  *              NOTE: Aborts compiler on any error.
205  *
206  ******************************************************************************/
207
208 void
209 FlOpenFile (
210     UINT32                  FileId,
211     char                    *Filename,
212     char                    *Mode)
213 {
214     FILE                    *File;
215
216
217     File = fopen (Filename, Mode);
218
219     Gbl_Files[FileId].Filename = Filename;
220     Gbl_Files[FileId].Handle   = File;
221
222     if (!File)
223     {
224         FlFileError (FileId, ASL_MSG_OPEN);
225         AslAbort ();
226     }
227 }
228
229
230 /*******************************************************************************
231  *
232  * FUNCTION:    FlGetFileSize
233  *
234  * PARAMETERS:  FileId              - Index into file info array
235  *
236  * RETURN:      File Size
237  *
238  * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
239  *
240  ******************************************************************************/
241
242 UINT32
243 FlGetFileSize (
244     UINT32                  FileId)
245 {
246     FILE                    *fp;
247     UINT32                  FileSize;
248     long                    Offset;
249
250
251     fp = Gbl_Files[FileId].Handle;
252     Offset = ftell (fp);
253
254     fseek (fp, 0, SEEK_END);
255     FileSize = (UINT32) ftell (fp);
256
257     /* Restore file pointer */
258
259     fseek (fp, Offset, SEEK_SET);
260     return (FileSize);
261 }
262
263
264 /*******************************************************************************
265  *
266  * FUNCTION:    FlReadFile
267  *
268  * PARAMETERS:  FileId              - Index into file info array
269  *              Buffer              - Where to place the data
270  *              Length              - Amount to read
271  *
272  * RETURN:      Status. AE_ERROR indicates EOF.
273  *
274  * DESCRIPTION: Read data from an open file.
275  *              NOTE: Aborts compiler on any error.
276  *
277  ******************************************************************************/
278
279 ACPI_STATUS
280 FlReadFile (
281     UINT32                  FileId,
282     void                    *Buffer,
283     UINT32                  Length)
284 {
285     UINT32                  Actual;
286
287
288     /* Read and check for error */
289
290     Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
291     if (Actual != Length)
292     {
293         if (feof (Gbl_Files[FileId].Handle))
294         {
295             /* End-of-file, just return error */
296
297             return (AE_ERROR);
298         }
299
300         FlFileError (FileId, ASL_MSG_READ);
301         AslAbort ();
302     }
303
304     return (AE_OK);
305 }
306
307
308 /*******************************************************************************
309  *
310  * FUNCTION:    FlWriteFile
311  *
312  * PARAMETERS:  FileId              - Index into file info array
313  *              Buffer              - Data to write
314  *              Length              - Amount of data to write
315  *
316  * RETURN:      None
317  *
318  * DESCRIPTION: Write data to an open file.
319  *              NOTE: Aborts compiler on any error.
320  *
321  ******************************************************************************/
322
323 void
324 FlWriteFile (
325     UINT32                  FileId,
326     void                    *Buffer,
327     UINT32                  Length)
328 {
329     UINT32                  Actual;
330
331
332     /* Write and check for error */
333
334     Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
335     if (Actual != Length)
336     {
337         FlFileError (FileId, ASL_MSG_WRITE);
338         AslAbort ();
339     }
340 }
341
342
343 /*******************************************************************************
344  *
345  * FUNCTION:    FlPrintFile
346  *
347  * PARAMETERS:  FileId              - Index into file info array
348  *              Format              - Printf format string
349  *              ...                 - Printf arguments
350  *
351  * RETURN:      None
352  *
353  * DESCRIPTION: Formatted write to an open file.
354  *              NOTE: Aborts compiler on any error.
355  *
356  ******************************************************************************/
357
358 void
359 FlPrintFile (
360     UINT32                  FileId,
361     char                    *Format,
362     ...)
363 {
364     INT32                   Actual;
365     va_list                 Args;
366
367
368     va_start (Args, Format);
369
370     Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
371     va_end (Args);
372
373     if (Actual == -1)
374     {
375         FlFileError (FileId, ASL_MSG_WRITE);
376         AslAbort ();
377     }
378 }
379
380
381 /*******************************************************************************
382  *
383  * FUNCTION:    FlSeekFile
384  *
385  * PARAMETERS:  FileId              - Index into file info array
386  *              Offset              - Absolute byte offset in file
387  *
388  * RETURN:      None
389  *
390  * DESCRIPTION: Seek to absolute offset
391  *              NOTE: Aborts compiler on any error.
392  *
393  ******************************************************************************/
394
395 void
396 FlSeekFile (
397     UINT32                  FileId,
398     long                    Offset)
399 {
400     int                     Error;
401
402
403     Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
404     if (Error)
405     {
406         FlFileError (FileId, ASL_MSG_SEEK);
407         AslAbort ();
408     }
409 }
410
411
412 /*******************************************************************************
413  *
414  * FUNCTION:    FlCloseFile
415  *
416  * PARAMETERS:  FileId              - Index into file info array
417  *
418  * RETURN:      None
419  *
420  * DESCRIPTION: Close an open file. Aborts compiler on error
421  *
422  ******************************************************************************/
423
424 void
425 FlCloseFile (
426     UINT32                  FileId)
427 {
428     int                     Error;
429
430
431     if (!Gbl_Files[FileId].Handle)
432     {
433         return;
434     }
435
436     Error = fclose (Gbl_Files[FileId].Handle);
437     if (Error)
438     {
439         FlFileError (FileId, ASL_MSG_CLOSE);
440         AslAbort ();
441     }
442
443     Gbl_Files[FileId].Handle = NULL;
444     return;
445 }
446
447
448 /*******************************************************************************
449  *
450  * FUNCTION:    FlDeleteFile
451  *
452  * PARAMETERS:  FileId              - Index into file info array
453  *
454  * RETURN:      None
455  *
456  * DESCRIPTION: Delete a file.
457  *
458  ******************************************************************************/
459
460 void
461 FlDeleteFile (
462     UINT32                  FileId)
463 {
464     ASL_FILE_INFO           *Info = &Gbl_Files[FileId];
465
466
467     if (!Info->Filename)
468     {
469         return;
470     }
471
472     if (remove (Info->Filename))
473     {
474         printf ("%s (%s file) ",
475             Info->Filename, Info->Description);
476         perror ("Could not delete");
477     }
478
479     Info->Filename = NULL;
480     return;
481 }
482
483
484 /*******************************************************************************
485  *
486  * FUNCTION:    FlSetLineNumber
487  *
488  * PARAMETERS:  Op        - Parse node for the LINE asl statement
489  *
490  * RETURN:      None.
491  *
492  * DESCRIPTION: Set the current line number
493  *
494  ******************************************************************************/
495
496 void
497 FlSetLineNumber (
498     UINT32                  LineNumber)
499 {
500
501     DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
502          LineNumber, Gbl_LogicalLineNumber);
503
504     Gbl_CurrentLineNumber = LineNumber;
505     Gbl_LogicalLineNumber = LineNumber;
506 }
507
508
509 /*******************************************************************************
510  *
511  * FUNCTION:    FlSetFilename
512  *
513  * PARAMETERS:  Op        - Parse node for the LINE asl statement
514  *
515  * RETURN:      None.
516  *
517  * DESCRIPTION: Set the current filename
518  *
519  ******************************************************************************/
520
521 void
522 FlSetFilename (
523     char                    *Filename)
524 {
525
526     DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
527          Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
528
529     Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
530 }
531
532
533 /*******************************************************************************
534  *
535  * FUNCTION:    FlAddIncludeDirectory
536  *
537  * PARAMETERS:  Dir             - Directory pathname string
538  *
539  * RETURN:      None
540  *
541  * DESCRIPTION: Add a directory the list of include prefix directories.
542  *
543  ******************************************************************************/
544
545 void
546 FlAddIncludeDirectory (
547     char                    *Dir)
548 {
549     ASL_INCLUDE_DIR         *NewDir;
550     ASL_INCLUDE_DIR         *NextDir;
551     ASL_INCLUDE_DIR         *PrevDir = NULL;
552     UINT32                  NeedsSeparator = 0;
553     size_t                  DirLength;
554
555
556     DirLength = strlen (Dir);
557     if (!DirLength)
558     {
559         return;
560     }
561
562     /* Make sure that the pathname ends with a path separator */
563
564     if ((Dir[DirLength-1] != '/') &&
565         (Dir[DirLength-1] != '\\'))
566     {
567         NeedsSeparator = 1;
568     }
569
570     NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
571     NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
572     strcpy (NewDir->Dir, Dir);
573     if (NeedsSeparator)
574     {
575         strcat (NewDir->Dir, "/");
576     }
577
578     /*
579      * Preserve command line ordering of -I options by adding new elements
580      * at the end of the list
581      */
582     NextDir = Gbl_IncludeDirList;
583     while (NextDir)
584     {
585         PrevDir = NextDir;
586         NextDir = NextDir->Next;
587     }
588
589     if (PrevDir)
590     {
591         PrevDir->Next = NewDir;
592     }
593     else
594     {
595         Gbl_IncludeDirList = NewDir;
596     }
597 }
598
599
600 /*******************************************************************************
601  *
602  * FUNCTION:    FlMergePathnames
603  *
604  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be NULL or
605  *                                a zero length string.
606  *              FilePathname    - The include filename from the source ASL.
607  *
608  * RETURN:      Merged pathname string
609  *
610  * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
611  *              arrive at a minimal length string. Merge can occur if the
612  *              FilePathname is relative to the PrefixDir.
613  *
614  ******************************************************************************/
615
616 char *
617 FlMergePathnames (
618     char                    *PrefixDir,
619     char                    *FilePathname)
620 {
621     char                    *CommonPath;
622     char                    *Pathname;
623     char                    *LastElement;
624
625
626     DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
627         "Include: FilePathname - \"%s\"\n",
628          PrefixDir, FilePathname);
629
630     /*
631      * If there is no prefix directory or if the file pathname is absolute,
632      * just return the original file pathname
633      */
634     if (!PrefixDir || (!*PrefixDir) ||
635         (*FilePathname == '/') ||
636          (FilePathname[1] == ':'))
637     {
638         Pathname = ACPI_ALLOCATE (strlen (FilePathname) + 1);
639         strcpy (Pathname, FilePathname);
640         goto ConvertBackslashes;
641     }
642
643     /* Need a local copy of the prefix directory path */
644
645     CommonPath = ACPI_ALLOCATE (strlen (PrefixDir) + 1);
646     strcpy (CommonPath, PrefixDir);
647
648     /*
649      * Walk forward through the file path, and simultaneously backward
650      * through the prefix directory path until there are no more
651      * relative references at the start of the file path.
652      */
653     while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
654     {
655         /* Remove last element of the prefix directory path */
656
657         LastElement = strrchr (CommonPath, '/');
658         if (!LastElement)
659         {
660             goto ConcatenatePaths;
661         }
662
663         *LastElement = 0;   /* Terminate CommonPath string */
664         FilePathname += 3;  /* Point to next path element */
665     }
666
667     /*
668      * Remove the last element of the prefix directory path (it is the same as
669      * the first element of the file pathname), and build the final merged
670      * pathname.
671      */
672     LastElement = strrchr (CommonPath, '/');
673     if (LastElement)
674     {
675         *LastElement = 0;
676     }
677
678     /* Build the final merged pathname */
679
680 ConcatenatePaths:
681     Pathname = ACPI_ALLOCATE_ZEROED (strlen (CommonPath) + strlen (FilePathname) + 2);
682     if (LastElement && *CommonPath)
683     {
684         strcpy (Pathname, CommonPath);
685         strcat (Pathname, "/");
686     }
687     strcat (Pathname, FilePathname);
688     ACPI_FREE (CommonPath);
689
690     /* Convert all backslashes to normal slashes */
691
692 ConvertBackslashes:
693     UtConvertBackslashes (Pathname);
694
695     DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
696          Pathname);
697     return (Pathname);
698 }
699
700
701 /*******************************************************************************
702  *
703  * FUNCTION:    FlOpenIncludeWithPrefix
704  *
705  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
706  *                                length string.
707  *              Filename        - The include filename from the source ASL.
708  *
709  * RETURN:      Valid file descriptor if successful. Null otherwise.
710  *
711  * DESCRIPTION: Open an include file and push it on the input file stack.
712  *
713  ******************************************************************************/
714
715 FILE *
716 FlOpenIncludeWithPrefix (
717     char                    *PrefixDir,
718     char                    *Filename)
719 {
720     FILE                    *IncludeFile;
721     char                    *Pathname;
722
723
724     /* Build the full pathname to the file */
725
726     Pathname = FlMergePathnames (PrefixDir, Filename);
727
728     DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
729         Pathname);
730
731     /* Attempt to open the file, push if successful */
732
733     IncludeFile = fopen (Pathname, "r");
734     if (IncludeFile)
735     {
736         /* Push the include file on the open input file stack */
737
738         AslPushInputFileStack (IncludeFile, Pathname);
739         return (IncludeFile);
740     }
741
742     ACPI_FREE (Pathname);
743     return (NULL);
744 }
745
746
747 /*******************************************************************************
748  *
749  * FUNCTION:    FlOpenIncludeFile
750  *
751  * PARAMETERS:  Op        - Parse node for the INCLUDE ASL statement
752  *
753  * RETURN:      None.
754  *
755  * DESCRIPTION: Open an include file and push it on the input file stack.
756  *
757  ******************************************************************************/
758
759 void
760 FlOpenIncludeFile (
761     ACPI_PARSE_OBJECT       *Op)
762 {
763     FILE                    *IncludeFile;
764     ASL_INCLUDE_DIR         *NextDir;
765
766
767     /* Op must be valid */
768
769     if (!Op)
770     {
771         AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
772             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
773             Gbl_InputByteCount, Gbl_CurrentColumn,
774             Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
775
776         return;
777     }
778
779     /*
780      * Flush out the "include ()" statement on this line, start
781      * the actual include file on the next line
782      */
783     AslResetCurrentLineBuffer ();
784     FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
785     Gbl_CurrentLineOffset++;
786
787
788     /* Attempt to open the include file */
789
790     /* If the file specifies an absolute path, just open it */
791
792     if ((Op->Asl.Value.String[0] == '/')  ||
793         (Op->Asl.Value.String[0] == '\\') ||
794         (Op->Asl.Value.String[1] == ':'))
795     {
796         IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
797         if (!IncludeFile)
798         {
799             goto ErrorExit;
800         }
801         return;
802     }
803
804     /*
805      * The include filename is not an absolute path.
806      *
807      * First, search for the file within the "local" directory -- meaning
808      * the same directory that contains the source file.
809      *
810      * Construct the file pathname from the global directory name.
811      */
812     IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
813     if (IncludeFile)
814     {
815         return;
816     }
817
818     /*
819      * Second, search for the file within the (possibly multiple) directories
820      * specified by the -I option on the command line.
821      */
822     NextDir = Gbl_IncludeDirList;
823     while (NextDir)
824     {
825         IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
826         if (IncludeFile)
827         {
828             return;
829         }
830
831         NextDir = NextDir->Next;
832     }
833
834     /* We could not open the include file after trying very hard */
835
836 ErrorExit:
837     sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
838     AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
839 }
840
841
842 /*******************************************************************************
843  *
844  * FUNCTION:    FlOpenInputFile
845  *
846  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
847  *                                    compiled
848  *
849  * RETURN:      Status
850  *
851  * DESCRIPTION: Open the specified input file, and save the directory path to
852  *              the file so that include files can be opened in
853  *              the same directory.
854  *
855  ******************************************************************************/
856
857 ACPI_STATUS
858 FlOpenInputFile (
859     char                    *InputFilename)
860 {
861
862     /* Open the input ASL file, text mode */
863
864     FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
865     AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
866
867     return (AE_OK);
868 }
869
870
871 /*******************************************************************************
872  *
873  * FUNCTION:    FlOpenAmlOutputFile
874  *
875  * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
876  *
877  * RETURN:      Status
878  *
879  * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
880  *              is created in the same directory as the parent input file.
881  *
882  ******************************************************************************/
883
884 ACPI_STATUS
885 FlOpenAmlOutputFile (
886     char                    *FilenamePrefix)
887 {
888     char                    *Filename;
889
890
891     /* Output filename usually comes from the ASL itself */
892
893     Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
894     if (!Filename)
895     {
896         /* Create the output AML filename */
897
898         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
899         if (!Filename)
900         {
901             AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
902                 0, 0, 0, 0, NULL, NULL);
903             return (AE_ERROR);
904         }
905     }
906
907     /* Open the output AML file in binary mode */
908
909     FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
910     return (AE_OK);
911 }
912
913
914 /*******************************************************************************
915  *
916  * FUNCTION:    FlOpenMiscOutputFiles
917  *
918  * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
919  *
920  * RETURN:      Status
921  *
922  * DESCRIPTION: Create and open the various output files needed, depending on
923  *              the command line options
924  *
925  ******************************************************************************/
926
927 ACPI_STATUS
928 FlOpenMiscOutputFiles (
929     char                    *FilenamePrefix)
930 {
931     char                    *Filename;
932
933
934     /* Create/Open a hex output file if asked */
935
936     if (Gbl_HexOutputFlag)
937     {
938         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
939         if (!Filename)
940         {
941             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
942                 0, 0, 0, 0, NULL, NULL);
943             return (AE_ERROR);
944         }
945
946         /* Open the hex file, text mode */
947
948         FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
949
950         AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
951         AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
952     }
953
954     /* Create/Open a debug output file if asked */
955
956     if (Gbl_DebugFlag)
957     {
958         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
959         if (!Filename)
960         {
961             AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
962                 0, 0, 0, 0, NULL, NULL);
963             return (AE_ERROR);
964         }
965
966         /* Open the debug file as STDERR, text mode */
967
968         /* TBD: hide this behind a FlReopenFile function */
969
970         Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
971         Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
972             freopen (Filename, "w+t", stderr);
973
974         if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
975         {
976             AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
977                 0, 0, 0, 0, NULL, NULL);
978             return (AE_ERROR);
979         }
980
981         AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
982         AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
983     }
984
985     /* Create/Open a listing output file if asked */
986
987     if (Gbl_ListingFlag)
988     {
989         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
990         if (!Filename)
991         {
992             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
993                 0, 0, 0, 0, NULL, NULL);
994             return (AE_ERROR);
995         }
996
997         /* Open the listing file, text mode */
998
999         FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
1000
1001         AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
1002         AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
1003     }
1004
1005     /* Create the preprocessor output file if preprocessor enabled */
1006
1007     if (Gbl_PreprocessFlag)
1008     {
1009         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
1010         if (!Filename)
1011         {
1012             AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
1013                 0, 0, 0, 0, NULL, NULL);
1014             return (AE_ERROR);
1015         }
1016
1017         FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
1018     }
1019
1020     /* All done for data table compiler */
1021
1022     if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
1023     {
1024         return (AE_OK);
1025     }
1026
1027    /* Create/Open a combined source output file */
1028
1029     Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
1030     if (!Filename)
1031     {
1032         AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1033             0, 0, 0, 0, NULL, NULL);
1034         return (AE_ERROR);
1035     }
1036
1037     /*
1038      * Open the source output file, binary mode (so that LF does not get
1039      * expanded to CR/LF on some systems, messing up our seek
1040      * calculations.)
1041      */
1042     FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
1043
1044 /*
1045 // TBD: TEMP
1046 //    AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
1047 */
1048     /* Create/Open a assembly code source output file if asked */
1049
1050     if (Gbl_AsmOutputFlag)
1051     {
1052         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
1053         if (!Filename)
1054         {
1055             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1056                 0, 0, 0, 0, NULL, NULL);
1057             return (AE_ERROR);
1058         }
1059
1060         /* Open the assembly code source file, text mode */
1061
1062         FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
1063
1064         AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
1065         AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
1066     }
1067
1068     /* Create/Open a C code source output file if asked */
1069
1070     if (Gbl_C_OutputFlag)
1071     {
1072         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
1073         if (!Filename)
1074         {
1075             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1076                 0, 0, 0, 0, NULL, NULL);
1077             return (AE_ERROR);
1078         }
1079
1080         /* Open the C code source file, text mode */
1081
1082         FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
1083
1084         FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
1085         AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
1086         AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
1087     }
1088
1089     /* Create/Open a assembly include output file if asked */
1090
1091     if (Gbl_AsmIncludeOutputFlag)
1092     {
1093         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
1094         if (!Filename)
1095         {
1096             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1097                 0, 0, 0, 0, NULL, NULL);
1098             return (AE_ERROR);
1099         }
1100
1101         /* Open the assembly include file, text mode */
1102
1103         FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
1104
1105         AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
1106         AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
1107     }
1108
1109     /* Create/Open a C include output file if asked */
1110
1111     if (Gbl_C_IncludeOutputFlag)
1112     {
1113         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
1114         if (!Filename)
1115         {
1116             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1117                 0, 0, 0, 0, NULL, NULL);
1118             return (AE_ERROR);
1119         }
1120
1121         /* Open the C include file, text mode */
1122
1123         FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
1124
1125         FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
1126         AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
1127         AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
1128     }
1129
1130     /* Create a namespace output file if asked */
1131
1132     if (Gbl_NsOutputFlag)
1133     {
1134         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
1135         if (!Filename)
1136         {
1137             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
1138                 0, 0, 0, 0, NULL, NULL);
1139             return (AE_ERROR);
1140         }
1141
1142         /* Open the namespace file, text mode */
1143
1144         FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
1145
1146         AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
1147         AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
1148     }
1149
1150     return (AE_OK);
1151 }
1152
1153
1154 #ifdef ACPI_OBSOLETE_FUNCTIONS
1155 /*******************************************************************************
1156  *
1157  * FUNCTION:    FlParseInputPathname
1158  *
1159  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
1160  *                                    compiled
1161  *
1162  * RETURN:      Status
1163  *
1164  * DESCRIPTION: Split the input path into a directory and filename part
1165  *              1) Directory part used to open include files
1166  *              2) Filename part used to generate output filenames
1167  *
1168  ******************************************************************************/
1169
1170 ACPI_STATUS
1171 FlParseInputPathname (
1172     char                    *InputFilename)
1173 {
1174     char                    *Substring;
1175
1176
1177     if (!InputFilename)
1178     {
1179         return (AE_OK);
1180     }
1181
1182     /* Get the path to the input filename's directory */
1183
1184     Gbl_DirectoryPath = strdup (InputFilename);
1185     if (!Gbl_DirectoryPath)
1186     {
1187         return (AE_NO_MEMORY);
1188     }
1189
1190     Substring = strrchr (Gbl_DirectoryPath, '\\');
1191     if (!Substring)
1192     {
1193         Substring = strrchr (Gbl_DirectoryPath, '/');
1194         if (!Substring)
1195         {
1196             Substring = strrchr (Gbl_DirectoryPath, ':');
1197         }
1198     }
1199
1200     if (!Substring)
1201     {
1202         Gbl_DirectoryPath[0] = 0;
1203         if (Gbl_UseDefaultAmlFilename)
1204         {
1205             Gbl_OutputFilenamePrefix = strdup (InputFilename);
1206         }
1207     }
1208     else
1209     {
1210         if (Gbl_UseDefaultAmlFilename)
1211         {
1212             Gbl_OutputFilenamePrefix = strdup (Substring + 1);
1213         }
1214         *(Substring+1) = 0;
1215     }
1216
1217     return (AE_OK);
1218 }
1219 #endif