Merge branch 'vendor/FILE'
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslfiles.c
1 /******************************************************************************
2  *
3  * Module Name: aslfiles - File support functions
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151
152 #include "aslcompiler.h"
153 #include "acapps.h"
154 #include "dtcompiler.h"
155
156 #define _COMPONENT          ACPI_COMPILER
157         ACPI_MODULE_NAME    ("aslfiles")
158
159 /* Local prototypes */
160
161 static FILE *
162 FlOpenIncludeWithPrefix (
163     char                    *PrefixDir,
164     ACPI_PARSE_OBJECT       *Op,
165     char                    *Filename);
166
167 #ifdef ACPI_OBSOLETE_FUNCTIONS
168 ACPI_STATUS
169 FlParseInputPathname (
170     char                    *InputFilename);
171 #endif
172
173
174 /*******************************************************************************
175  *
176  * FUNCTION:    FlSetLineNumber
177  *
178  * PARAMETERS:  Op        - Parse node for the LINE asl statement
179  *
180  * RETURN:      None.
181  *
182  * DESCRIPTION: Set the current line number
183  *
184  ******************************************************************************/
185
186 void
187 FlSetLineNumber (
188     UINT32                  LineNumber)
189 {
190
191     DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
192          LineNumber, Gbl_LogicalLineNumber);
193
194     Gbl_CurrentLineNumber = LineNumber;
195 }
196
197
198 /*******************************************************************************
199  *
200  * FUNCTION:    FlSetFilename
201  *
202  * PARAMETERS:  Op        - Parse node for the LINE asl statement
203  *
204  * RETURN:      None.
205  *
206  * DESCRIPTION: Set the current filename
207  *
208  ******************************************************************************/
209
210 void
211 FlSetFilename (
212     char                    *Filename)
213 {
214
215     DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
216          Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
217
218     /* No need to free any existing filename */
219
220     Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
221 }
222
223
224 /*******************************************************************************
225  *
226  * FUNCTION:    FlAddIncludeDirectory
227  *
228  * PARAMETERS:  Dir             - Directory pathname string
229  *
230  * RETURN:      None
231  *
232  * DESCRIPTION: Add a directory the list of include prefix directories.
233  *
234  ******************************************************************************/
235
236 void
237 FlAddIncludeDirectory (
238     char                    *Dir)
239 {
240     ASL_INCLUDE_DIR         *NewDir;
241     ASL_INCLUDE_DIR         *NextDir;
242     ASL_INCLUDE_DIR         *PrevDir = NULL;
243     UINT32                  NeedsSeparator = 0;
244     size_t                  DirLength;
245
246
247     DirLength = strlen (Dir);
248     if (!DirLength)
249     {
250         return;
251     }
252
253     /* Make sure that the pathname ends with a path separator */
254
255     if ((Dir[DirLength-1] != '/') &&
256         (Dir[DirLength-1] != '\\'))
257     {
258         NeedsSeparator = 1;
259     }
260
261     NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
262     NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
263     strcpy (NewDir->Dir, Dir);
264     if (NeedsSeparator)
265     {
266         strcat (NewDir->Dir, "/");
267     }
268
269     /*
270      * Preserve command line ordering of -I options by adding new elements
271      * at the end of the list
272      */
273     NextDir = Gbl_IncludeDirList;
274     while (NextDir)
275     {
276         PrevDir = NextDir;
277         NextDir = NextDir->Next;
278     }
279
280     if (PrevDir)
281     {
282         PrevDir->Next = NewDir;
283     }
284     else
285     {
286         Gbl_IncludeDirList = NewDir;
287     }
288 }
289
290
291 /*******************************************************************************
292  *
293  * FUNCTION:    FlMergePathnames
294  *
295  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be NULL or
296  *                                a zero length string.
297  *              FilePathname    - The include filename from the source ASL.
298  *
299  * RETURN:      Merged pathname string
300  *
301  * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
302  *              arrive at a minimal length string. Merge can occur if the
303  *              FilePathname is relative to the PrefixDir.
304  *
305  ******************************************************************************/
306
307 char *
308 FlMergePathnames (
309     char                    *PrefixDir,
310     char                    *FilePathname)
311 {
312     char                    *CommonPath;
313     char                    *Pathname;
314     char                    *LastElement;
315
316
317     DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
318         "Include: FilePathname - \"%s\"\n",
319          PrefixDir, FilePathname);
320
321     /*
322      * If there is no prefix directory or if the file pathname is absolute,
323      * just return the original file pathname
324      */
325     if (!PrefixDir || (!*PrefixDir) ||
326         (*FilePathname == '/') ||
327          (FilePathname[1] == ':'))
328     {
329         Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
330         strcpy (Pathname, FilePathname);
331         goto ConvertBackslashes;
332     }
333
334     /* Need a local copy of the prefix directory path */
335
336     CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
337     strcpy (CommonPath, PrefixDir);
338
339     /*
340      * Walk forward through the file path, and simultaneously backward
341      * through the prefix directory path until there are no more
342      * relative references at the start of the file path.
343      */
344     while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
345     {
346         /* Remove last element of the prefix directory path */
347
348         LastElement = strrchr (CommonPath, '/');
349         if (!LastElement)
350         {
351             goto ConcatenatePaths;
352         }
353
354         *LastElement = 0;   /* Terminate CommonPath string */
355         FilePathname += 3;  /* Point to next path element */
356     }
357
358     /*
359      * Remove the last element of the prefix directory path (it is the same as
360      * the first element of the file pathname), and build the final merged
361      * pathname.
362      */
363     LastElement = strrchr (CommonPath, '/');
364     if (LastElement)
365     {
366         *LastElement = 0;
367     }
368
369     /* Build the final merged pathname */
370
371 ConcatenatePaths:
372     Pathname = UtStringCacheCalloc (
373         strlen (CommonPath) + strlen (FilePathname) + 2);
374     if (LastElement && *CommonPath)
375     {
376         strcpy (Pathname, CommonPath);
377         strcat (Pathname, "/");
378     }
379     strcat (Pathname, FilePathname);
380
381     /* Convert all backslashes to normal slashes */
382
383 ConvertBackslashes:
384     UtConvertBackslashes (Pathname);
385
386     DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
387          Pathname);
388     return (Pathname);
389 }
390
391
392 /*******************************************************************************
393  *
394  * FUNCTION:    FlOpenIncludeWithPrefix
395  *
396  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
397  *                                length string.
398  *              Filename        - The include filename from the source ASL.
399  *
400  * RETURN:      Valid file descriptor if successful. Null otherwise.
401  *
402  * DESCRIPTION: Open an include file and push it on the input file stack.
403  *
404  ******************************************************************************/
405
406 static FILE *
407 FlOpenIncludeWithPrefix (
408     char                    *PrefixDir,
409     ACPI_PARSE_OBJECT       *Op,
410     char                    *Filename)
411 {
412     FILE                    *IncludeFile;
413     char                    *Pathname;
414     UINT32                  OriginalLineNumber;
415
416
417     /* Build the full pathname to the file */
418
419     Pathname = FlMergePathnames (PrefixDir, Filename);
420
421     DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
422         Pathname);
423
424     /* Attempt to open the file, push if successful */
425
426     IncludeFile = fopen (Pathname, "r");
427     if (!IncludeFile)
428     {
429         fprintf (stderr, "Could not open include file %s\n", Pathname);
430         ACPI_FREE (Pathname);
431         return (NULL);
432     }
433
434     /*
435      * Check the entire include file for any # preprocessor directives.
436      * This is because there may be some confusion between the #include
437      * preprocessor directive and the ASL Include statement. A file included
438      * by the ASL include cannot contain preprocessor directives because
439      * the preprocessor has already run by the time the ASL include is
440      * recognized (by the compiler, not the preprocessor.)
441      *
442      * Note: DtGetNextLine strips/ignores comments.
443      * Save current line number since DtGetNextLine modifies it.
444      */
445     Gbl_CurrentLineNumber--;
446     OriginalLineNumber = Gbl_CurrentLineNumber;
447
448     while (DtGetNextLine (IncludeFile, DT_ALLOW_MULTILINE_QUOTES) != ASL_EOF)
449     {
450         if (Gbl_CurrentLineBuffer[0] == '#')
451         {
452             AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE,
453                 Op, "use #include instead");
454         }
455     }
456
457     Gbl_CurrentLineNumber = OriginalLineNumber;
458
459     /* Must seek back to the start of the file */
460
461     fseek (IncludeFile, 0, SEEK_SET);
462
463     /* Push the include file on the open input file stack */
464
465     AslPushInputFileStack (IncludeFile, Pathname);
466     return (IncludeFile);
467 }
468
469
470 /*******************************************************************************
471  *
472  * FUNCTION:    FlOpenIncludeFile
473  *
474  * PARAMETERS:  Op        - Parse node for the INCLUDE ASL statement
475  *
476  * RETURN:      None.
477  *
478  * DESCRIPTION: Open an include file and push it on the input file stack.
479  *
480  ******************************************************************************/
481
482 void
483 FlOpenIncludeFile (
484     ACPI_PARSE_OBJECT       *Op)
485 {
486     FILE                    *IncludeFile;
487     ASL_INCLUDE_DIR         *NextDir;
488
489
490     /* Op must be valid */
491
492     if (!Op)
493     {
494         AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
495             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
496             Gbl_InputByteCount, Gbl_CurrentColumn,
497             Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
498
499         return;
500     }
501
502     /*
503      * Flush out the "include ()" statement on this line, start
504      * the actual include file on the next line
505      */
506     AslResetCurrentLineBuffer ();
507     FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
508     Gbl_CurrentLineOffset++;
509
510
511     /* Attempt to open the include file */
512
513     /* If the file specifies an absolute path, just open it */
514
515     if ((Op->Asl.Value.String[0] == '/')  ||
516         (Op->Asl.Value.String[0] == '\\') ||
517         (Op->Asl.Value.String[1] == ':'))
518     {
519         IncludeFile = FlOpenIncludeWithPrefix ("", Op, Op->Asl.Value.String);
520         if (!IncludeFile)
521         {
522             goto ErrorExit;
523         }
524         return;
525     }
526
527     /*
528      * The include filename is not an absolute path.
529      *
530      * First, search for the file within the "local" directory -- meaning
531      * the same directory that contains the source file.
532      *
533      * Construct the file pathname from the global directory name.
534      */
535     IncludeFile = FlOpenIncludeWithPrefix (
536         Gbl_DirectoryPath, Op, Op->Asl.Value.String);
537     if (IncludeFile)
538     {
539         return;
540     }
541
542     /*
543      * Second, search for the file within the (possibly multiple) directories
544      * specified by the -I option on the command line.
545      */
546     NextDir = Gbl_IncludeDirList;
547     while (NextDir)
548     {
549         IncludeFile = FlOpenIncludeWithPrefix (
550             NextDir->Dir, Op, Op->Asl.Value.String);
551         if (IncludeFile)
552         {
553             return;
554         }
555
556         NextDir = NextDir->Next;
557     }
558
559     /* We could not open the include file after trying very hard */
560
561 ErrorExit:
562     sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
563     AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
564 }
565
566
567 /*******************************************************************************
568  *
569  * FUNCTION:    FlOpenInputFile
570  *
571  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
572  *                                    compiled
573  *
574  * RETURN:      Status
575  *
576  * DESCRIPTION: Open the specified input file, and save the directory path to
577  *              the file so that include files can be opened in
578  *              the same directory.
579  *
580  ******************************************************************************/
581
582 ACPI_STATUS
583 FlOpenInputFile (
584     char                    *InputFilename)
585 {
586
587     /* Open the input ASL file, text mode */
588
589     FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
590     AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
591
592     return (AE_OK);
593 }
594
595
596 /*******************************************************************************
597  *
598  * FUNCTION:    FlOpenAmlOutputFile
599  *
600  * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
601  *
602  * RETURN:      Status
603  *
604  * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
605  *              is created in the same directory as the parent input file.
606  *
607  ******************************************************************************/
608
609 ACPI_STATUS
610 FlOpenAmlOutputFile (
611     char                    *FilenamePrefix)
612 {
613     char                    *Filename;
614
615
616     /* Output filename usually comes from the ASL itself */
617
618     Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
619     if (!Filename)
620     {
621         /* Create the output AML filename */
622         if (!Gbl_CaptureComments)
623         {
624             Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
625         }
626         else
627         {
628             Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_CONVERT_AML);
629         }
630         if (!Filename)
631         {
632             AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
633                 0, 0, 0, 0, NULL, NULL);
634             return (AE_ERROR);
635         }
636
637         Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = Filename;
638     }
639
640     /* Open the output AML file in binary mode */
641
642     FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
643     return (AE_OK);
644 }
645
646
647 /*******************************************************************************
648  *
649  * FUNCTION:    FlOpenMiscOutputFiles
650  *
651  * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
652  *
653  * RETURN:      Status
654  *
655  * DESCRIPTION: Create and open the various output files needed, depending on
656  *              the command line options
657  *
658  ******************************************************************************/
659
660 ACPI_STATUS
661 FlOpenMiscOutputFiles (
662     char                    *FilenamePrefix)
663 {
664     char                    *Filename;
665
666
667      /* Create/Open a map file if requested */
668
669     if (Gbl_MapfileFlag)
670     {
671         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_MAP);
672         if (!Filename)
673         {
674             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
675                 0, 0, 0, 0, NULL, NULL);
676             return (AE_ERROR);
677         }
678
679         /* Open the hex file, text mode (closed at compiler exit) */
680
681         FlOpenFile (ASL_FILE_MAP_OUTPUT, Filename, "w+t");
682
683         AslCompilerSignon (ASL_FILE_MAP_OUTPUT);
684         AslCompilerFileHeader (ASL_FILE_MAP_OUTPUT);
685     }
686
687     /* All done for disassembler */
688
689     if (Gbl_FileType == ASL_INPUT_TYPE_BINARY_ACPI_TABLE)
690     {
691         return (AE_OK);
692     }
693
694     /* Create/Open a hex output file if asked */
695
696     if (Gbl_HexOutputFlag)
697     {
698         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
699         if (!Filename)
700         {
701             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
702                 0, 0, 0, 0, NULL, NULL);
703             return (AE_ERROR);
704         }
705
706         /* Open the hex file, text mode */
707
708         FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
709
710         AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
711         AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
712     }
713
714     /* Create/Open a debug output file if asked */
715
716     if (Gbl_DebugFlag)
717     {
718         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
719         if (!Filename)
720         {
721             AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
722                 0, 0, 0, 0, NULL, NULL);
723             return (AE_ERROR);
724         }
725
726         /* Open the debug file as STDERR, text mode */
727
728         Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
729         Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
730             freopen (Filename, "w+t", stderr);
731
732         if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
733         {
734             /*
735              * A problem with freopen is that on error, we no longer
736              * have stderr and cannot emit normal error messages.
737              * Emit error to stdout, close files, and exit.
738              */
739             fprintf (stdout,
740                 "\nCould not open debug output file: %s\n\n", Filename);
741
742             CmCleanupAndExit ();
743             exit (1);
744         }
745
746         AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
747         AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
748     }
749
750     /* Create/Open a cross-reference output file if asked */
751
752     if (Gbl_CrossReferenceOutput)
753     {
754         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_XREF);
755         if (!Filename)
756         {
757             AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
758                 0, 0, 0, 0, NULL, NULL);
759             return (AE_ERROR);
760         }
761
762         FlOpenFile (ASL_FILE_XREF_OUTPUT, Filename, "w+t");
763
764         AslCompilerSignon (ASL_FILE_XREF_OUTPUT);
765         AslCompilerFileHeader (ASL_FILE_XREF_OUTPUT);
766     }
767
768     /* Create/Open a listing output file if asked */
769
770     if (Gbl_ListingFlag)
771     {
772         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
773         if (!Filename)
774         {
775             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
776                 0, 0, 0, 0, NULL, NULL);
777             return (AE_ERROR);
778         }
779
780         /* Open the listing file, text mode */
781
782         FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
783
784         AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
785         AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
786     }
787
788     /* Create the preprocessor output temp file if preprocessor enabled */
789
790     if (Gbl_PreprocessFlag)
791     {
792         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
793         if (!Filename)
794         {
795             AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
796                 0, 0, 0, 0, NULL, NULL);
797             return (AE_ERROR);
798         }
799
800         FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
801     }
802
803     /*
804      * Create the "user" preprocessor output file if -li flag set.
805      * Note, this file contains no embedded #line directives.
806      */
807     if (Gbl_PreprocessorOutputFlag)
808     {
809         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROC_USER);
810         if (!Filename)
811         {
812             AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
813                 0, 0, 0, 0, NULL, NULL);
814             return (AE_ERROR);
815         }
816
817         FlOpenFile (ASL_FILE_PREPROCESSOR_USER, Filename, "w+t");
818     }
819
820     /* All done for data table compiler */
821
822     if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
823     {
824         return (AE_OK);
825     }
826
827     /* Create/Open a combined source output file */
828
829     Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
830     if (!Filename)
831     {
832         AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
833             0, 0, 0, 0, NULL, NULL);
834         return (AE_ERROR);
835     }
836
837     /*
838      * Open the source output file, binary mode (so that LF does not get
839      * expanded to CR/LF on some systems, messing up our seek
840      * calculations.)
841      */
842     FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
843
844 /*
845 // TBD: TEMP
846 //    AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
847 */
848     /* Create/Open a assembly code source output file if asked */
849
850     if (Gbl_AsmOutputFlag)
851     {
852         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
853         if (!Filename)
854         {
855             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
856                 0, 0, 0, 0, NULL, NULL);
857             return (AE_ERROR);
858         }
859
860         /* Open the assembly code source file, text mode */
861
862         FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
863
864         AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
865         AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
866     }
867
868     /* Create/Open a C code source output file if asked */
869
870     if (Gbl_C_OutputFlag)
871     {
872         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
873         if (!Filename)
874         {
875             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
876                 0, 0, 0, 0, NULL, NULL);
877             return (AE_ERROR);
878         }
879
880         /* Open the C code source file, text mode */
881
882         FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
883
884         FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
885         AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
886         AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
887     }
888
889     /* Create/Open a C code source output file for the offset table if asked */
890
891     if (Gbl_C_OffsetTableFlag)
892     {
893         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);
894         if (!Filename)
895         {
896             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
897                 0, 0, 0, 0, NULL, NULL);
898             return (AE_ERROR);
899         }
900
901         /* Open the C code source file, text mode */
902
903         FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");
904
905         FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");
906         AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);
907         AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);
908     }
909
910     /* Create/Open a assembly include output file if asked */
911
912     if (Gbl_AsmIncludeOutputFlag)
913     {
914         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
915         if (!Filename)
916         {
917             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
918                 0, 0, 0, 0, NULL, NULL);
919             return (AE_ERROR);
920         }
921
922         /* Open the assembly include file, text mode */
923
924         FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
925
926         AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
927         AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
928     }
929
930     /* Create/Open a C include output file if asked */
931
932     if (Gbl_C_IncludeOutputFlag)
933     {
934         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
935         if (!Filename)
936         {
937             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
938                 0, 0, 0, 0, NULL, NULL);
939             return (AE_ERROR);
940         }
941
942         /* Open the C include file, text mode */
943
944         FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
945
946         FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
947         AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
948         AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
949     }
950
951     /* Create a namespace output file if asked */
952
953     if (Gbl_NsOutputFlag)
954     {
955         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
956         if (!Filename)
957         {
958             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
959                 0, 0, 0, 0, NULL, NULL);
960             return (AE_ERROR);
961         }
962
963         /* Open the namespace file, text mode */
964
965         FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
966
967         AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
968         AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
969     }
970
971     /* Create a debug file for the converter */
972
973     if (AcpiGbl_DebugAslConversion)
974     {
975         Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_CONVERT_DEBUG);
976         if (!Filename)
977         {
978             AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
979                 0, 0, 0, 0, NULL, NULL);
980             return (AE_ERROR);
981         }
982
983         /* Open the converter debug file, text mode */
984
985         FlOpenFile (ASL_FILE_CONV_DEBUG_OUTPUT, Filename, "w+t");
986
987         AslCompilerSignon (ASL_FILE_CONV_DEBUG_OUTPUT);
988         AslCompilerFileHeader (ASL_FILE_CONV_DEBUG_OUTPUT);
989
990         AcpiGbl_ConvDebugFile = Gbl_Files[ASL_FILE_CONV_DEBUG_OUTPUT].Handle;
991     }
992
993     return (AE_OK);
994 }
995
996
997 #ifdef ACPI_OBSOLETE_FUNCTIONS
998 /*******************************************************************************
999  *
1000  * FUNCTION:    FlParseInputPathname
1001  *
1002  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
1003  *                                    compiled
1004  *
1005  * RETURN:      Status
1006  *
1007  * DESCRIPTION: Split the input path into a directory and filename part
1008  *              1) Directory part used to open include files
1009  *              2) Filename part used to generate output filenames
1010  *
1011  ******************************************************************************/
1012
1013 ACPI_STATUS
1014 FlParseInputPathname (
1015     char                    *InputFilename)
1016 {
1017     char                    *Substring;
1018
1019
1020     if (!InputFilename)
1021     {
1022         return (AE_OK);
1023     }
1024
1025     /* Get the path to the input filename's directory */
1026
1027     Gbl_DirectoryPath = strdup (InputFilename);
1028     if (!Gbl_DirectoryPath)
1029     {
1030         return (AE_NO_MEMORY);
1031     }
1032
1033     Substring = strrchr (Gbl_DirectoryPath, '\\');
1034     if (!Substring)
1035     {
1036         Substring = strrchr (Gbl_DirectoryPath, '/');
1037         if (!Substring)
1038         {
1039             Substring = strrchr (Gbl_DirectoryPath, ':');
1040         }
1041     }
1042
1043     if (!Substring)
1044     {
1045         Gbl_DirectoryPath[0] = 0;
1046         if (Gbl_UseDefaultAmlFilename)
1047         {
1048             Gbl_OutputFilenamePrefix = strdup (InputFilename);
1049         }
1050     }
1051     else
1052     {
1053         if (Gbl_UseDefaultAmlFilename)
1054         {
1055             Gbl_OutputFilenamePrefix = strdup (Substring + 1);
1056         }
1057         *(Substring+1) = 0;
1058     }
1059
1060     UtConvertBackslashes (Gbl_OutputFilenamePrefix);
1061     return (AE_OK);
1062 }
1063 #endif