aslfiles.c revision 233250
11901Swollman
21901Swollman/******************************************************************************
31901Swollman *
41901Swollman * Module Name: aslfiles - file I/O suppoert
51901Swollman *
61901Swollman *****************************************************************************/
71901Swollman
88870Srgrimes/*
91901Swollman * Copyright (C) 2000 - 2012, Intel Corp.
101901Swollman * All rights reserved.
111901Swollman *
128870Srgrimes * Redistribution and use in source and binary forms, with or without
131901Swollman * modification, are permitted provided that the following conditions
141901Swollman * are met:
151901Swollman * 1. Redistributions of source code must retain the above copyright
168870Srgrimes *    notice, this list of conditions, and the following disclaimer,
171901Swollman *    without modification.
181901Swollman * 2. Redistributions in binary form must reproduce at minimum a disclaimer
191901Swollman *    substantially similar to the "NO WARRANTY" disclaimer below
208870Srgrimes *    ("Disclaimer") and any redistribution must be conditioned upon
211901Swollman *    including a substantially similar Disclaimer requirement for further
221901Swollman *    binary redistribution.
231901Swollman * 3. Neither the names of the above-listed copyright holders nor the names
248870Srgrimes *    of any contributors may be used to endorse or promote products derived
251901Swollman *    from this software without specific prior written permission.
261901Swollman *
271901Swollman * Alternatively, this software may be distributed under the terms of the
281901Swollman * GNU General Public License ("GPL") version 2 as published by the Free
291901Swollman * Software Foundation.
301901Swollman *
311901Swollman * NO WARRANTY
321901Swollman * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3311666Sphk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
341901Swollman * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
351901Swollman * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
361901Swollman * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
371901Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
381901Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
391901Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
401901Swollman * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
411901Swollman * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
421901Swollman * POSSIBILITY OF SUCH DAMAGES.
431901Swollman */
441901Swollman
451901Swollman#include <contrib/dev/acpica/compiler/aslcompiler.h>
461901Swollman#include <contrib/dev/acpica/include/acapps.h>
471901Swollman
481901Swollman#define _COMPONENT          ACPI_COMPILER
491901Swollman        ACPI_MODULE_NAME    ("aslfiles")
5011666Sphk
511901Swollman/* Local prototypes */
5211666Sphk
531901SwollmanFILE *
541901SwollmanFlOpenIncludeWithPrefix (
551901Swollman    char                    *PrefixDir,
561901Swollman    char                    *Filename);
571901Swollman
581901Swollman
591901Swollman#ifdef ACPI_OBSOLETE_FUNCTIONS
601901SwollmanACPI_STATUS
611901SwollmanFlParseInputPathname (
621901Swollman    char                    *InputFilename);
631901Swollman#endif
641901Swollman
651901Swollman
661901Swollman/*******************************************************************************
671901Swollman *
681901Swollman * FUNCTION:    AslAbort
691901Swollman *
701901Swollman * PARAMETERS:  None
711901Swollman *
721901Swollman * RETURN:      None
731901Swollman *
741901Swollman * DESCRIPTION: Dump the error log and abort the compiler.  Used for serious
751901Swollman *              I/O errors
761901Swollman *
771901Swollman ******************************************************************************/
781901Swollman
791901Swollmanvoid
801901SwollmanAslAbort (
811901Swollman    void)
821901Swollman{
831901Swollman
841901Swollman    AePrintErrorLog (ASL_FILE_STDOUT);
851901Swollman    if (Gbl_DebugFlag)
861901Swollman    {
871901Swollman        /* Print error summary to the debug file */
881901Swollman
891901Swollman        AePrintErrorLog (ASL_FILE_STDERR);
901901Swollman    }
911901Swollman
921901Swollman    exit (1);
931901Swollman}
941901Swollman
951901Swollman
961901Swollman/*******************************************************************************
971901Swollman *
981901Swollman * FUNCTION:    FlFileError
991901Swollman *
1001901Swollman * PARAMETERS:  FileId              - Index into file info array
1011901Swollman *              ErrorId             - Index into error message array
1021901Swollman *
1031901Swollman * RETURN:      None
1041901Swollman *
1051901Swollman * DESCRIPTION: Decode errno to an error message and add the entire error
1061901Swollman *              to the error log.
1071901Swollman *
1081901Swollman ******************************************************************************/
1091901Swollman
1101901Swollmanvoid
1111901SwollmanFlFileError (
1121901Swollman    UINT32                  FileId,
1131901Swollman    UINT8                   ErrorId)
1141901Swollman{
1151901Swollman
1161901Swollman    sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
1171901Swollman        strerror (errno));
1181901Swollman    AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
1191901Swollman}
1201901Swollman
1211901Swollman
1221901Swollman/*******************************************************************************
1231901Swollman *
1241901Swollman * FUNCTION:    FlOpenFile
1251901Swollman *
1261901Swollman * PARAMETERS:  FileId              - Index into file info array
1271901Swollman *              Filename            - file pathname to open
1281901Swollman *              Mode                - Open mode for fopen
1291901Swollman *
1301901Swollman * RETURN:      None
1311901Swollman *
1321901Swollman * DESCRIPTION: Open a file.
1331901Swollman *              NOTE: Aborts compiler on any error.
1341901Swollman *
1351901Swollman ******************************************************************************/
1361901Swollman
1371901Swollmanvoid
1381901SwollmanFlOpenFile (
1391901Swollman    UINT32                  FileId,
1401901Swollman    char                    *Filename,
1411901Swollman    char                    *Mode)
1421901Swollman{
1431901Swollman    FILE                    *File;
1441901Swollman
1451901Swollman
1461901Swollman    File = fopen (Filename, Mode);
1471901Swollman
1481901Swollman    Gbl_Files[FileId].Filename = Filename;
1491901Swollman    Gbl_Files[FileId].Handle   = File;
1501901Swollman
1511901Swollman    if (!File)
1521901Swollman    {
1531901Swollman        FlFileError (FileId, ASL_MSG_OPEN);
1541901Swollman        AslAbort ();
1551901Swollman    }
1561901Swollman}
1571901Swollman
1581901Swollman
1591901Swollman/*******************************************************************************
1601901Swollman *
1618870Srgrimes * FUNCTION:    FlGetFileSize
1621901Swollman *
1631901Swollman * PARAMETERS:  FileId              - Index into file info array
1641901Swollman *
1651901Swollman * RETURN:      File Size
1661901Swollman *
1671901Swollman * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
1681901Swollman *
1691901Swollman ******************************************************************************/
1701901Swollman
1711901SwollmanUINT32
1721901SwollmanFlGetFileSize (
1731901Swollman    UINT32                  FileId)
1741901Swollman{
1751901Swollman    FILE                    *fp;
1761901Swollman    UINT32                  FileSize;
1771901Swollman    long                    Offset;
1781901Swollman
1791901Swollman
1801901Swollman    fp = Gbl_Files[FileId].Handle;
1811901Swollman    Offset = ftell (fp);
1821901Swollman
1831901Swollman    fseek (fp, 0, SEEK_END);
1841901Swollman    FileSize = (UINT32) ftell (fp);
1851901Swollman
1861901Swollman    /* Restore file pointer */
1871901Swollman
1881901Swollman    fseek (fp, Offset, SEEK_SET);
1891901Swollman    return (FileSize);
1901901Swollman}
1911901Swollman
1921901Swollman
1931901Swollman/*******************************************************************************
1941901Swollman *
1951901Swollman * FUNCTION:    FlReadFile
1961901Swollman *
1971901Swollman * PARAMETERS:  FileId              - Index into file info array
1981901Swollman *              Buffer              - Where to place the data
1991901Swollman *              Length              - Amount to read
2001901Swollman *
2011901Swollman * RETURN:      Status.  AE_ERROR indicates EOF.
2021901Swollman *
2031901Swollman * DESCRIPTION: Read data from an open file.
2041901Swollman *              NOTE: Aborts compiler on any error.
2051901Swollman *
2061901Swollman ******************************************************************************/
2071901Swollman
2081901SwollmanACPI_STATUS
2091901SwollmanFlReadFile (
2101901Swollman    UINT32                  FileId,
2111901Swollman    void                    *Buffer,
2121901Swollman    UINT32                  Length)
2131901Swollman{
2141901Swollman    UINT32                  Actual;
2151901Swollman
2161901Swollman
2171901Swollman    /* Read and check for error */
2181901Swollman
2198870Srgrimes    Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
2201901Swollman    if (Actual != Length)
2211901Swollman    {
2221901Swollman        if (feof (Gbl_Files[FileId].Handle))
2231901Swollman        {
2241901Swollman            /* End-of-file, just return error */
2251901Swollman
2261901Swollman            return (AE_ERROR);
2271901Swollman        }
2281901Swollman
2291901Swollman        FlFileError (FileId, ASL_MSG_READ);
2301901Swollman        AslAbort ();
2311901Swollman    }
2321901Swollman
2331901Swollman    return (AE_OK);
2341901Swollman}
2351901Swollman
2361901Swollman
2371901Swollman/*******************************************************************************
2381901Swollman *
2391901Swollman * FUNCTION:    FlWriteFile
2401901Swollman *
2411901Swollman * PARAMETERS:  FileId              - Index into file info array
2421901Swollman *              Buffer              - Data to write
2431901Swollman *              Length              - Amount of data to write
2441901Swollman *
2451901Swollman * RETURN:      None
2461901Swollman *
2471901Swollman * DESCRIPTION: Write data to an open file.
2481901Swollman *              NOTE: Aborts compiler on any error.
2491901Swollman *
2501901Swollman ******************************************************************************/
2511901Swollman
2521901Swollmanvoid
2538870SrgrimesFlWriteFile (
2541901Swollman    UINT32                  FileId,
2551901Swollman    void                    *Buffer,
2561901Swollman    UINT32                  Length)
2571901Swollman{
2581901Swollman    UINT32                  Actual;
2591901Swollman
2601901Swollman
2611901Swollman    /* Write and check for error */
2621901Swollman
2631901Swollman    Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
2641901Swollman    if (Actual != Length)
2651901Swollman    {
2661901Swollman        FlFileError (FileId, ASL_MSG_WRITE);
2671901Swollman        AslAbort ();
2681901Swollman    }
2691901Swollman}
2701901Swollman
2711901Swollman
2721901Swollman/*******************************************************************************
2731901Swollman *
2741901Swollman * FUNCTION:    FlPrintFile
2751901Swollman *
2761901Swollman * PARAMETERS:  FileId              - Index into file info array
2771901Swollman *              Format              - Printf format string
2781901Swollman *              ...                 - Printf arguments
2791901Swollman *
2801901Swollman * RETURN:      None
2811901Swollman *
2821901Swollman * DESCRIPTION: Formatted write to an open file.
2831901Swollman *              NOTE: Aborts compiler on any error.
2841901Swollman *
2851901Swollman ******************************************************************************/
2861901Swollman
2871901Swollmanvoid
2881901SwollmanFlPrintFile (
2891901Swollman    UINT32                  FileId,
2901901Swollman    char                    *Format,
2911901Swollman    ...)
2921901Swollman{
2931901Swollman    INT32                   Actual;
2941901Swollman    va_list                 Args;
2951901Swollman
2961901Swollman
2971901Swollman    va_start (Args, Format);
2981901Swollman
2991901Swollman    Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
3001901Swollman    va_end (Args);
3011901Swollman
3021901Swollman    if (Actual == -1)
3031901Swollman    {
3041901Swollman        FlFileError (FileId, ASL_MSG_WRITE);
3051901Swollman        AslAbort ();
3061901Swollman    }
3071901Swollman}
3081901Swollman
3091901Swollman
3101901Swollman/*******************************************************************************
3111901Swollman *
3121901Swollman * FUNCTION:    FlSeekFile
3131901Swollman *
3141901Swollman * PARAMETERS:  FileId              - Index into file info array
3151901Swollman *              Offset              - Absolute byte offset in file
3161901Swollman *
3171901Swollman * RETURN:      None
3181901Swollman *
3191901Swollman * DESCRIPTION: Seek to absolute offset
3201901Swollman *              NOTE: Aborts compiler on any error.
3211901Swollman *
3221901Swollman ******************************************************************************/
3231901Swollman
3241901Swollmanvoid
3251901SwollmanFlSeekFile (
3261901Swollman    UINT32                  FileId,
3271901Swollman    long                    Offset)
3281901Swollman{
3291901Swollman    int                     Error;
3301901Swollman
3311901Swollman
3321901Swollman    Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
3338870Srgrimes    if (Error)
3341901Swollman    {
3351901Swollman        FlFileError (FileId, ASL_MSG_SEEK);
3361901Swollman        AslAbort ();
3371901Swollman    }
3381901Swollman}
3391901Swollman
3401901Swollman
3411901Swollman/*******************************************************************************
3421901Swollman *
3431901Swollman * FUNCTION:    FlCloseFile
3441901Swollman *
3451901Swollman * PARAMETERS:  FileId              - Index into file info array
3461901Swollman *
3471901Swollman * RETURN:      None
3481901Swollman *
3491901Swollman * DESCRIPTION: Close an open file.  Aborts compiler on error
3501901Swollman *
3511901Swollman ******************************************************************************/
3521901Swollman
3531901Swollmanvoid
3541901SwollmanFlCloseFile (
3551901Swollman    UINT32                  FileId)
3561901Swollman{
3571901Swollman    int                     Error;
3581901Swollman
3591901Swollman
3601901Swollman    if (!Gbl_Files[FileId].Handle)
3611901Swollman    {
3621901Swollman        return;
3631901Swollman    }
3641901Swollman
3651901Swollman    Error = fclose (Gbl_Files[FileId].Handle);
3661901Swollman    if (Error)
3671901Swollman    {
3681901Swollman        FlFileError (FileId, ASL_MSG_CLOSE);
3691901Swollman        AslAbort ();
3701901Swollman    }
3711901Swollman
3721901Swollman    Gbl_Files[FileId].Handle = NULL;
3731901Swollman    return;
3741901Swollman}
3751901Swollman
3761901Swollman
3771901Swollman/*******************************************************************************
3781901Swollman *
3791901Swollman * FUNCTION:    FlSetLineNumber
3801901Swollman *
3811901Swollman * PARAMETERS:  Op        - Parse node for the LINE asl statement
3821901Swollman *
3831901Swollman * RETURN:      None.
3841901Swollman *
3851901Swollman * DESCRIPTION: Set the current line number
3861901Swollman *
3871901Swollman ******************************************************************************/
3881901Swollman
3891901Swollmanvoid
3901901SwollmanFlSetLineNumber (
3911901Swollman    ACPI_PARSE_OBJECT       *Op)
3921901Swollman{
3931901Swollman
3941901Swollman    Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
3951901Swollman    Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
3961901Swollman}
3971901Swollman
3981901Swollman
3991901Swollman/*******************************************************************************
400 *
401 * FUNCTION:    FlAddIncludeDirectory
402 *
403 * PARAMETERS:  Dir             - Directory pathname string
404 *
405 * RETURN:      None
406 *
407 * DESCRIPTION: Add a directory the list of include prefix directories.
408 *
409 ******************************************************************************/
410
411void
412FlAddIncludeDirectory (
413    char                    *Dir)
414{
415    ASL_INCLUDE_DIR         *NewDir;
416    ASL_INCLUDE_DIR         *NextDir;
417    ASL_INCLUDE_DIR         *PrevDir = NULL;
418    UINT32                  NeedsSeparator = 0;
419    size_t                  DirLength;
420
421
422    DirLength = strlen (Dir);
423    if (!DirLength)
424    {
425        return;
426    }
427
428    /* Make sure that the pathname ends with a path separator */
429
430    if ((Dir[DirLength-1] != '/') &&
431        (Dir[DirLength-1] != '\\'))
432    {
433        NeedsSeparator = 1;
434    }
435
436    NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
437    NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
438    strcpy (NewDir->Dir, Dir);
439    if (NeedsSeparator)
440    {
441        strcat (NewDir->Dir, "/");
442    }
443
444    /*
445     * Preserve command line ordering of -I options by adding new elements
446     * at the end of the list
447     */
448    NextDir = Gbl_IncludeDirList;
449    while (NextDir)
450    {
451        PrevDir = NextDir;
452        NextDir = NextDir->Next;
453    }
454
455    if (PrevDir)
456    {
457        PrevDir->Next = NewDir;
458    }
459    else
460    {
461        Gbl_IncludeDirList = NewDir;
462    }
463}
464
465
466/*******************************************************************************
467 *
468 * FUNCTION:    FlOpenIncludeWithPrefix
469 *
470 * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
471 *                                length string.
472 *              Filename        - The include filename from the source ASL.
473 *
474 * RETURN:      Valid file descriptor if successful. Null otherwise.
475 *
476 * DESCRIPTION: Open an include file and push it on the input file stack.
477 *
478 ******************************************************************************/
479
480FILE *
481FlOpenIncludeWithPrefix (
482    char                    *PrefixDir,
483    char                    *Filename)
484{
485    FILE                    *IncludeFile;
486    char                    *Pathname;
487
488
489    /* Build the full pathname to the file */
490
491    Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
492
493    strcpy (Pathname, PrefixDir);
494    strcat (Pathname, Filename);
495
496    DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
497        Pathname);
498
499    /* Attempt to open the file, push if successful */
500
501    IncludeFile = fopen (Pathname, "r");
502    if (IncludeFile)
503    {
504        /* Push the include file on the open input file stack */
505
506        AslPushInputFileStack (IncludeFile, Pathname);
507        return (IncludeFile);
508    }
509
510    ACPI_FREE (Pathname);
511    return (NULL);
512}
513
514
515/*******************************************************************************
516 *
517 * FUNCTION:    FlOpenIncludeFile
518 *
519 * PARAMETERS:  Op        - Parse node for the INCLUDE ASL statement
520 *
521 * RETURN:      None.
522 *
523 * DESCRIPTION: Open an include file and push it on the input file stack.
524 *
525 ******************************************************************************/
526
527void
528FlOpenIncludeFile (
529    ACPI_PARSE_OBJECT       *Op)
530{
531    FILE                    *IncludeFile;
532    ASL_INCLUDE_DIR         *NextDir;
533
534
535    /* Op must be valid */
536
537    if (!Op)
538    {
539        AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
540            Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
541            Gbl_InputByteCount, Gbl_CurrentColumn,
542            Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
543
544        return;
545    }
546
547    /*
548     * Flush out the "include ()" statement on this line, start
549     * the actual include file on the next line
550     */
551    ResetCurrentLineBuffer ();
552    FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
553    Gbl_CurrentLineOffset++;
554
555
556    /* Attempt to open the include file */
557
558    /* If the file specifies an absolute path, just open it */
559
560    if ((Op->Asl.Value.String[0] == '/')  ||
561        (Op->Asl.Value.String[0] == '\\') ||
562        (Op->Asl.Value.String[1] == ':'))
563    {
564        IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
565        if (!IncludeFile)
566        {
567            goto ErrorExit;
568        }
569        return;
570    }
571
572    /*
573     * The include filename is not an absolute path.
574     *
575     * First, search for the file within the "local" directory -- meaning
576     * the same directory that contains the source file.
577     *
578     * Construct the file pathname from the global directory name.
579     */
580    IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
581    if (IncludeFile)
582    {
583        return;
584    }
585
586    /*
587     * Second, search for the file within the (possibly multiple) directories
588     * specified by the -I option on the command line.
589     */
590    NextDir = Gbl_IncludeDirList;
591    while (NextDir)
592    {
593        IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
594        if (IncludeFile)
595        {
596            return;
597        }
598
599        NextDir = NextDir->Next;
600    }
601
602    /* We could not open the include file after trying very hard */
603
604ErrorExit:
605    sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
606    AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
607}
608
609
610/*******************************************************************************
611 *
612 * FUNCTION:    FlOpenInputFile
613 *
614 * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
615 *                                    compiled
616 *
617 * RETURN:      Status
618 *
619 * DESCRIPTION: Open the specified input file, and save the directory path to
620 *              the file so that include files can be opened in
621 *              the same directory.
622 *
623 ******************************************************************************/
624
625ACPI_STATUS
626FlOpenInputFile (
627    char                    *InputFilename)
628{
629
630    /* Open the input ASL file, text mode */
631
632    FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
633    AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
634
635    return (AE_OK);
636}
637
638
639/*******************************************************************************
640 *
641 * FUNCTION:    FlOpenAmlOutputFile
642 *
643 * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
644 *
645 * RETURN:      Status
646 *
647 * DESCRIPTION: Create the output filename (*.AML) and open the file.  The file
648 *              is created in the same directory as the parent input file.
649 *
650 ******************************************************************************/
651
652ACPI_STATUS
653FlOpenAmlOutputFile (
654    char                    *FilenamePrefix)
655{
656    char                    *Filename;
657
658
659    /* Output filename usually comes from the ASL itself */
660
661    Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
662    if (!Filename)
663    {
664        /* Create the output AML filename */
665
666        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
667        if (!Filename)
668        {
669            AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
670                0, 0, 0, 0, NULL, NULL);
671            return (AE_ERROR);
672        }
673    }
674
675    /* Open the output AML file in binary mode */
676
677    FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
678    return (AE_OK);
679}
680
681
682/*******************************************************************************
683 *
684 * FUNCTION:    FlOpenMiscOutputFiles
685 *
686 * PARAMETERS:  FilenamePrefix       - The user-specified ASL source file
687 *
688 * RETURN:      Status
689 *
690 * DESCRIPTION: Create and open the various output files needed, depending on
691 *              the command line options
692 *
693 ******************************************************************************/
694
695ACPI_STATUS
696FlOpenMiscOutputFiles (
697    char                    *FilenamePrefix)
698{
699    char                    *Filename;
700
701
702    /* Create/Open a hex output file if asked */
703
704    if (Gbl_HexOutputFlag)
705    {
706        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
707        if (!Filename)
708        {
709            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
710                0, 0, 0, 0, NULL, NULL);
711            return (AE_ERROR);
712        }
713
714        /* Open the hex file, text mode */
715
716        FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
717
718        AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
719        AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
720    }
721
722    /* Create/Open a debug output file if asked */
723
724    if (Gbl_DebugFlag)
725    {
726        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
727        if (!Filename)
728        {
729            AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
730                0, 0, 0, 0, NULL, NULL);
731            return (AE_ERROR);
732        }
733
734        /* Open the debug file as STDERR, text mode */
735
736        /* TBD: hide this behind a FlReopenFile function */
737
738        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
739        Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
740            freopen (Filename, "w+t", stderr);
741
742        if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
743        {
744            AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
745                0, 0, 0, 0, NULL, NULL);
746            return (AE_ERROR);
747        }
748
749        AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
750        AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
751    }
752
753    /* Create/Open a listing output file if asked */
754
755    if (Gbl_ListingFlag)
756    {
757        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
758        if (!Filename)
759        {
760            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
761                0, 0, 0, 0, NULL, NULL);
762            return (AE_ERROR);
763        }
764
765        /* Open the listing file, text mode */
766
767        FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
768
769        AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
770        AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
771    }
772
773    /* Create the preprocessor output file */
774
775    Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
776    if (!Filename)
777    {
778        AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
779            0, 0, 0, 0, NULL, NULL);
780        return (AE_ERROR);
781    }
782
783    FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+b");
784
785    /* All done for data table compiler */
786
787    if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
788    {
789        return (AE_OK);
790    }
791
792   /* Create/Open a combined source output file */
793
794    Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
795    if (!Filename)
796    {
797        AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
798            0, 0, 0, 0, NULL, NULL);
799        return (AE_ERROR);
800    }
801
802    /*
803     * Open the source output file, binary mode (so that LF does not get
804     * expanded to CR/LF on some systems, messing up our seek
805     * calculations.)
806     */
807    FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
808
809/*
810// TBD: TEMP
811//    AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
812*/
813    /* Create/Open a assembly code source output file if asked */
814
815    if (Gbl_AsmOutputFlag)
816    {
817        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
818        if (!Filename)
819        {
820            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
821                0, 0, 0, 0, NULL, NULL);
822            return (AE_ERROR);
823        }
824
825        /* Open the assembly code source file, text mode */
826
827        FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
828
829        AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
830        AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
831    }
832
833    /* Create/Open a C code source output file if asked */
834
835    if (Gbl_C_OutputFlag)
836    {
837        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
838        if (!Filename)
839        {
840            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
841                0, 0, 0, 0, NULL, NULL);
842            return (AE_ERROR);
843        }
844
845        /* Open the C code source file, text mode */
846
847        FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
848
849        FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
850        AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
851        AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
852    }
853
854    /* Create/Open a assembly include output file if asked */
855
856    if (Gbl_AsmIncludeOutputFlag)
857    {
858        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
859        if (!Filename)
860        {
861            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
862                0, 0, 0, 0, NULL, NULL);
863            return (AE_ERROR);
864        }
865
866        /* Open the assembly include file, text mode */
867
868        FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
869
870        AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
871        AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
872    }
873
874    /* Create/Open a C include output file if asked */
875
876    if (Gbl_C_IncludeOutputFlag)
877    {
878        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
879        if (!Filename)
880        {
881            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
882                0, 0, 0, 0, NULL, NULL);
883            return (AE_ERROR);
884        }
885
886        /* Open the C include file, text mode */
887
888        FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
889
890        FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
891        AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
892        AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
893    }
894
895    /* Create a namespace output file if asked */
896
897    if (Gbl_NsOutputFlag)
898    {
899        Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
900        if (!Filename)
901        {
902            AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
903                0, 0, 0, 0, NULL, NULL);
904            return (AE_ERROR);
905        }
906
907        /* Open the namespace file, text mode */
908
909        FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
910
911        AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
912        AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
913    }
914
915    return (AE_OK);
916}
917
918
919#ifdef ACPI_OBSOLETE_FUNCTIONS
920/*******************************************************************************
921 *
922 * FUNCTION:    FlParseInputPathname
923 *
924 * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
925 *                                    compiled
926 *
927 * RETURN:      Status
928 *
929 * DESCRIPTION: Split the input path into a directory and filename part
930 *              1) Directory part used to open include files
931 *              2) Filename part used to generate output filenames
932 *
933 ******************************************************************************/
934
935ACPI_STATUS
936FlParseInputPathname (
937    char                    *InputFilename)
938{
939    char                    *Substring;
940
941
942    if (!InputFilename)
943    {
944        return (AE_OK);
945    }
946
947    /* Get the path to the input filename's directory */
948
949    Gbl_DirectoryPath = strdup (InputFilename);
950    if (!Gbl_DirectoryPath)
951    {
952        return (AE_NO_MEMORY);
953    }
954
955    Substring = strrchr (Gbl_DirectoryPath, '\\');
956    if (!Substring)
957    {
958        Substring = strrchr (Gbl_DirectoryPath, '/');
959        if (!Substring)
960        {
961            Substring = strrchr (Gbl_DirectoryPath, ':');
962        }
963    }
964
965    if (!Substring)
966    {
967        Gbl_DirectoryPath[0] = 0;
968        if (Gbl_UseDefaultAmlFilename)
969        {
970            Gbl_OutputFilenamePrefix = strdup (InputFilename);
971        }
972    }
973    else
974    {
975        if (Gbl_UseDefaultAmlFilename)
976        {
977            Gbl_OutputFilenamePrefix = strdup (Substring + 1);
978        }
979        *(Substring+1) = 0;
980    }
981
982    return (AE_OK);
983}
984#endif
985
986
987