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