Home | History | Annotate | Line # | Download | only in compiler
      1 /******************************************************************************
      2  *
      3  * Module Name: prutils - Preprocessor utilities
      4  *
      5  *****************************************************************************/
      6 
      7 /******************************************************************************
      8  *
      9  * 1. Copyright Notice
     10  *
     11  * Some or all of this work - Copyright (c) 1999 - 2025, 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 
    154 #define _COMPONENT          ASL_PREPROCESSOR
    155         ACPI_MODULE_NAME    ("prutils")
    156 
    157 
    158 /******************************************************************************
    159  *
    160  * FUNCTION:    PrGetNextToken
    161  *
    162  * PARAMETERS:  Buffer              - Current line buffer
    163  *              MatchString         - String with valid token delimiters
    164  *              Next                - Set to next possible token in buffer
    165  *
    166  * RETURN:      Next token (null-terminated). Modifies the input line.
    167  *              Remainder of line is stored in *Next.
    168  *
    169  * DESCRIPTION: Local implementation of strtok() with local storage for the
    170  *              next pointer. Not only thread-safe, but allows multiple
    171  *              parsing of substrings such as expressions.
    172  *
    173  *****************************************************************************/
    174 
    175 char *
    176 PrGetNextToken (
    177     char                    *Buffer,
    178     char                    *MatchString,
    179     char                    **Next)
    180 {
    181     char                    *TokenStart;
    182 
    183 
    184     if (!Buffer)
    185     {
    186         /* Use Next if it is valid */
    187 
    188         Buffer = *Next;
    189         if (!(*Next))
    190         {
    191             return (NULL);
    192         }
    193     }
    194 
    195     /* Skip any leading delimiters */
    196 
    197     while (*Buffer)
    198     {
    199         if (strchr (MatchString, *Buffer))
    200         {
    201             Buffer++;
    202         }
    203         else
    204         {
    205             break;
    206         }
    207     }
    208 
    209     /* Anything left on the line? */
    210 
    211     if (!(*Buffer))
    212     {
    213         *Next = NULL;
    214         return (NULL);
    215     }
    216 
    217     TokenStart = Buffer;
    218 
    219     /* Find the end of this token */
    220 
    221     while (*Buffer)
    222     {
    223         if (strchr (MatchString, *Buffer))
    224         {
    225             *Buffer = 0;
    226             *Next = Buffer+1;
    227             if (!**Next)
    228             {
    229                 *Next = NULL;
    230             }
    231 
    232             return (TokenStart);
    233         }
    234 
    235         Buffer++;
    236     }
    237 
    238     *Next = NULL;
    239     return (TokenStart);
    240 }
    241 
    242 
    243 /*******************************************************************************
    244  *
    245  * FUNCTION:    PrError
    246  *
    247  * PARAMETERS:  Level               - Seriousness (Warning/error, etc.)
    248  *              MessageId           - Index into global message buffer
    249  *              Column              - Column in current line
    250  *
    251  * RETURN:      None
    252  *
    253  * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
    254  *
    255  ******************************************************************************/
    256 
    257 void
    258 PrError (
    259     UINT8                   Level,
    260     UINT16                  MessageId,
    261     UINT32                  Column)
    262 {
    263 #if 0
    264     AcpiOsPrintf ("%s (%u) : %s", AslGbl_Files[ASL_FILE_INPUT].Filename,
    265         AslGbl_CurrentLineNumber, AslGbl_CurrentLineBuffer);
    266 #endif
    267 
    268 
    269     if (Column > 120)
    270     {
    271         Column = 0;
    272     }
    273 
    274     /* TBD: Need Logical line number? */
    275 
    276     AslCommonError2 (Level, MessageId,
    277         AslGbl_CurrentLineNumber, Column,
    278         AslGbl_CurrentLineBuffer,
    279         AslGbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
    280 
    281     AslGbl_PreprocessorError = TRUE;
    282 }
    283 
    284 
    285 /*******************************************************************************
    286  *
    287  * FUNCTION:    PrReplaceResizeSubstring
    288  *
    289  * PARAMETERS:  Args                - Struct containing name, offset & usecount
    290  *              Diff1               - Difference in lengths when new < old
    291  *              Diff2               - Difference in lengths when new > old
    292 *               i                   - The curr. no. of iteration of replacement
    293  *              Token               - Substring that replaces Args->Name
    294  *
    295  * RETURN:      None
    296  *
    297  * DESCRIPTION: Advanced substring replacement in a string using resized buffer.
    298  *
    299  ******************************************************************************/
    300 
    301 void
    302 PrReplaceResizeSubstring(
    303     PR_MACRO_ARG            *Args,
    304     UINT32                  Diff1,
    305     UINT32                  Diff2,
    306     UINT32                  i,
    307     char                    *Token)
    308 {
    309     UINT32                  b, PrevOffset;
    310     char                    *temp;
    311     char                    macro_sep[64];
    312 
    313 
    314     AslGbl_MacroTokenReplaceBuffer = (char *) realloc (AslGbl_MacroTokenReplaceBuffer,
    315         (2 * (strlen (AslGbl_MacroTokenBuffer))));
    316 
    317     strcpy (macro_sep, "~,() {}!*/%+-<>=&^|\"\t\n");
    318 
    319     /*
    320      * When the replacement argument (during invocation) length
    321      * < replaced parameter (in the macro function definition
    322      * and its expansion) length
    323      */
    324     if (Diff1 != 0)
    325     {
    326         /*
    327          * We save the offset value to reset it after replacing each
    328          * instance of each arg and setting the offset value to
    329          * the start of the arg to be replaced since it changes
    330          * with each iteration when arg length != token length
    331          */
    332         PrevOffset = Args->Offset[i];
    333         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
    334         if (temp == NULL)
    335         {
    336             return;
    337         }
    338 
    339 ResetHere1:
    340         temp = strstr (temp, Args->Name);
    341         if (temp == NULL)
    342         {
    343             return;
    344         }
    345         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
    346             strlen (temp);
    347         if (Args->Offset[i] == 0)
    348         {
    349             goto JumpHere1;
    350         }
    351         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
    352             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
    353         {
    354             Args->Offset[i] += 0;
    355         }
    356         else
    357         {
    358             temp += strlen (Args->Name);
    359             goto ResetHere1;
    360         }
    361 
    362         /*
    363          * For now, we simply set the extra char positions (generated
    364          * due to longer name replaced by shorter name) to whitespace
    365          * chars so it will be ignored during compilation
    366          */
    367 JumpHere1:
    368         b = strlen (Token) + Args->Offset[i];
    369         memset (&AslGbl_MacroTokenBuffer[b], ' ', Diff1);
    370 
    371 # if 0
    372 
    373     /* Work in progress as of 03/08/2023 - experimental 'if' block
    374      * to test code for removing extra whitespaces from the macro
    375      * replacement when replacement arg < replaced param
    376      */
    377         char Buff[8192];
    378         /* char* Replace; */
    379         /* Replace = Buff; */
    380 
    381         for (j = 0; j < strlen (AslGbl_MacroTokenBuffer); j++)
    382         {
    383             Buff[j] = AslGbl_MacroTokenBuffer[j];
    384         }
    385         Buff[strlen (AslGbl_MacroTokenBuffer)] = '\0';
    386         /* fprintf(stderr, "Buff: %s\n", Buff); */
    387 
    388         UINT32 len = strlen (Buff);
    389 
    390         for (j = 0; j < len; j++)
    391         {
    392             if (Buff[0] == ' ')
    393             {
    394                 for (j = 0; j < (len - 1); j++)
    395                 {
    396                     Buff[j] = Buff[j + 1];
    397                 }
    398                 Buff[j] = '\0';
    399                 len--;
    400                 j = -1;
    401                 continue;
    402             }
    403 
    404             if (Buff[j] == ' ' && Buff[j + 1] == ' ')
    405             {
    406                 for (k = 0; k < (len - 1); k++)
    407                 {
    408                     Buff[j] = Buff[j + 1];
    409                 }
    410                 Buff[j] = '\0';
    411                 len--;
    412                 j--;
    413             }
    414         }
    415         /* fprintf(stderr, "Buff: %s\n", Buff); */
    416 
    417         for (k = 0; k < strlen (Buff); k++)
    418         {
    419             AslGbl_MacroTokenBuffer[k] = Buff[k];
    420         }
    421 #endif
    422 
    423         PrReplaceData (
    424             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
    425             strlen (Token), Token, strlen (Token));
    426 
    427         temp = NULL;
    428         Args->Offset[i] = PrevOffset;
    429     }
    430 
    431     /*
    432      * When the replacement argument (during invocation) length
    433      * > replaced parameter (in the macro function definition
    434      * and its expansion) length
    435      */
    436     else if (Diff2 != 0)
    437     {
    438         /* Doing the same thing with offset value as for prev case */
    439 
    440         PrevOffset = Args->Offset[i];
    441         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
    442         if (temp == NULL)
    443         {
    444             return;
    445         }
    446 
    447 ResetHere2:
    448         temp = strstr (temp, Args->Name);
    449         if (temp == NULL)
    450         {
    451             return;
    452         }
    453         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
    454             strlen (temp);
    455         if (Args->Offset[i] == 0)
    456         {
    457             goto JumpHere2;
    458         }
    459         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
    460             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
    461         {
    462             Args->Offset[i] += 0;
    463         }
    464         else
    465         {
    466             temp+= strlen (Args->Name);
    467             goto ResetHere2;
    468         }
    469 
    470         /*
    471          * We will need to allocate some extra space in our buffer to
    472          * accommodate the increase in the replacement string length
    473          * over the shorter outgoing arg string and do the replacement
    474          * at the correct offset value which is resetted every iteration
    475          */
    476 JumpHere2:
    477         memcpy (AslGbl_MacroTokenReplaceBuffer, AslGbl_MacroTokenBuffer, Args->Offset[i]);
    478         strcat (AslGbl_MacroTokenReplaceBuffer, Token);
    479         strcat (AslGbl_MacroTokenReplaceBuffer, (AslGbl_MacroTokenBuffer +
    480             (Args->Offset[i] + strlen (Args->Name))));
    481 
    482         strcpy (AslGbl_MacroTokenBuffer, AslGbl_MacroTokenReplaceBuffer);
    483 
    484         temp = NULL;
    485         Args->Offset[i] = PrevOffset;
    486     }
    487 
    488     /*
    489      * When the replacement argument (during invocation) length =
    490      * replaced parameter (in the macro function definition and
    491      * its expansion) length
    492      */
    493     else
    494     {
    495 
    496         /*
    497          * We still need to reset the offset for each iteration even when
    498          * arg and param lengths are same since any macro func invocation
    499          * could use various cases for each separate arg-param pair
    500          */
    501         PrevOffset = Args->Offset[i];
    502         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
    503         if (temp == NULL)
    504         {
    505             return;
    506         }
    507 
    508 ResetHere3:
    509         temp = strstr (temp, Args->Name);
    510         if (temp == NULL)
    511         {
    512             return;
    513         }
    514         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
    515             strlen (temp);
    516         if (Args->Offset[i] == 0)
    517         {
    518             goto JumpHere3;
    519         }
    520         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
    521             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
    522         {
    523             Args->Offset[i] += 0;
    524         }
    525         else
    526         {
    527             temp += strlen (Args->Name);
    528             goto ResetHere3;
    529         }
    530 
    531 JumpHere3:
    532         PrReplaceData (
    533             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
    534             strlen (Args->Name), Token, strlen (Token));
    535         temp = NULL;
    536         Args->Offset[i] = PrevOffset;
    537     }
    538 }
    539 
    540 
    541 /*******************************************************************************
    542  *
    543  * FUNCTION:    PrReplaceData
    544  *
    545  * PARAMETERS:  Buffer              - Original(target) buffer pointer
    546  *              LengthToRemove      - Length to be removed from target buffer
    547  *              BufferToAdd         - Data to be inserted into target buffer
    548  *              LengthToAdd         - Length of BufferToAdd
    549  *
    550  * RETURN:      Pointer to where the buffer is replaced with data
    551  *
    552  * DESCRIPTION: Generic buffer data replacement.
    553  *
    554  ******************************************************************************/
    555 
    556 char *
    557 PrReplaceData (
    558     char                    *Buffer,
    559     UINT32                  LengthToRemove,
    560     char                    *BufferToAdd,
    561     UINT32                  LengthToAdd)
    562 {
    563     UINT32                  BufferLength;
    564 
    565 
    566     /* Buffer is a string, so the length must include the terminating zero */
    567 
    568     BufferLength = strlen (Buffer) + 1;
    569 
    570     if (LengthToRemove != LengthToAdd)
    571     {
    572         /*
    573          * Move some of the existing data
    574          * 1) If adding more bytes than removing, make room for the new data
    575          * 2) if removing more bytes than adding, delete the extra space
    576          */
    577         if (LengthToRemove > 0)
    578         {
    579             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
    580                 (BufferLength - LengthToRemove));
    581         }
    582     }
    583 
    584 
    585     /* Now we can move in the new data */
    586 
    587     if (LengthToAdd > 0)
    588     {
    589         memmove (Buffer, BufferToAdd, LengthToAdd);
    590     }
    591     return (Buffer + LengthToAdd);
    592 }
    593 
    594 
    595 /*******************************************************************************
    596  *
    597  * FUNCTION:    PrOpenIncludeFile
    598  *
    599  * PARAMETERS:  Filename            - Filename or pathname for include file
    600  *
    601  * RETURN:      None.
    602  *
    603  * DESCRIPTION: Open an include file and push it on the input file stack.
    604  *
    605  ******************************************************************************/
    606 
    607 FILE *
    608 PrOpenIncludeFile (
    609     char                    *Filename,
    610     char                    *OpenMode,
    611     char                    **FullPathname)
    612 {
    613     FILE                    *IncludeFile;
    614     ASL_INCLUDE_DIR         *NextDir;
    615 
    616 
    617     /* Start the actual include file on the next line */
    618 
    619     AslGbl_CurrentLineOffset++;
    620 
    621     /* Attempt to open the include file */
    622     /* If the file specifies an absolute path, just open it */
    623 
    624     if ((Filename[0] == '/')  ||
    625         (Filename[0] == '\\') ||
    626         (Filename[1] == ':'))
    627     {
    628         IncludeFile = PrOpenIncludeWithPrefix (
    629             "", Filename, OpenMode, FullPathname);
    630         if (!IncludeFile)
    631         {
    632             goto ErrorExit;
    633         }
    634         return (IncludeFile);
    635     }
    636 
    637     /*
    638      * The include filename is not an absolute path.
    639      *
    640      * First, search for the file within the "local" directory -- meaning
    641      * the same directory that contains the source file.
    642      *
    643      * Construct the file pathname from the global directory name.
    644      */
    645     IncludeFile = PrOpenIncludeWithPrefix (
    646         AslGbl_DirectoryPath, Filename, OpenMode, FullPathname);
    647     if (IncludeFile)
    648     {
    649         return (IncludeFile);
    650     }
    651 
    652     /*
    653      * Second, search for the file within the (possibly multiple)
    654      * directories specified by the -I option on the command line.
    655      */
    656     NextDir = AslGbl_IncludeDirList;
    657     while (NextDir)
    658     {
    659         IncludeFile = PrOpenIncludeWithPrefix (
    660             NextDir->Dir, Filename, OpenMode, FullPathname);
    661         if (IncludeFile)
    662         {
    663             return (IncludeFile);
    664         }
    665 
    666         NextDir = NextDir->Next;
    667     }
    668 
    669     /* We could not open the include file after trying very hard */
    670 
    671 ErrorExit:
    672     snprintf (AslGbl_MainTokenBuffer, ASL_DEFAULT_LINE_BUFFER_SIZE, "%s, %s",
    673       Filename, strerror (errno));
    674     PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
    675     return (NULL);
    676 }
    677 
    678 
    679 /*******************************************************************************
    680  *
    681  * FUNCTION:    FlOpenIncludeWithPrefix
    682  *
    683  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
    684  *                                length string.
    685  *              Filename        - The include filename from the source ASL.
    686  *
    687  * RETURN:      Valid file descriptor if successful. Null otherwise.
    688  *
    689  * DESCRIPTION: Open an include file and push it on the input file stack.
    690  *
    691  ******************************************************************************/
    692 
    693 FILE *
    694 PrOpenIncludeWithPrefix (
    695     char                    *PrefixDir,
    696     char                    *Filename,
    697     char                    *OpenMode,
    698     char                    **FullPathname)
    699 {
    700     FILE                    *IncludeFile;
    701     char                    *Pathname;
    702 
    703 
    704     /* Build the full pathname to the file */
    705 
    706     Pathname = FlMergePathnames (PrefixDir, Filename);
    707 
    708     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    709         "Include: Opening file - \"%s\"\n",
    710         AslGbl_CurrentLineNumber, Pathname);
    711 
    712     /* Attempt to open the file, push if successful */
    713 
    714     IncludeFile = fopen (Pathname, OpenMode);
    715     if (!IncludeFile)
    716     {
    717         return (NULL);
    718     }
    719 
    720     /* Push the include file on the open input file stack */
    721 
    722     PrPushInputFileStack (IncludeFile, Pathname);
    723     *FullPathname = Pathname;
    724     return (IncludeFile);
    725 }
    726 
    727 
    728 /*******************************************************************************
    729  *
    730  * FUNCTION:    AslPushInputFileStack
    731  *
    732  * PARAMETERS:  InputFile           - Open file pointer
    733  *              Filename            - Name of the file
    734  *
    735  * RETURN:      None
    736  *
    737  * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
    738  *              to this file. Called when an include file is successfully
    739  *              opened.
    740  *
    741  ******************************************************************************/
    742 
    743 void
    744 PrPushInputFileStack (
    745     FILE                    *InputFile,
    746     char                    *Filename)
    747 {
    748     PR_FILE_NODE            *Fnode;
    749 
    750 
    751     AslGbl_HasIncludeFiles = TRUE;
    752 
    753     /* Save the current state in an Fnode */
    754 
    755     Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
    756 
    757     Fnode->File = AslGbl_Files[ASL_FILE_INPUT].Handle;
    758     Fnode->Next = AslGbl_InputFileList;
    759     Fnode->Filename = AslGbl_Files[ASL_FILE_INPUT].Filename;
    760     Fnode->CurrentLineNumber = AslGbl_CurrentLineNumber;
    761 
    762     /* Push it on the stack */
    763 
    764     AslGbl_InputFileList = Fnode;
    765 
    766     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    767         "Push InputFile Stack: handle %p\n\n",
    768         AslGbl_CurrentLineNumber, InputFile);
    769 
    770     /* Reset the global line count and filename */
    771 
    772     AslGbl_Files[ASL_FILE_INPUT].Filename =
    773         UtLocalCacheCalloc (strlen (Filename) + 1);
    774     strcpy (AslGbl_Files[ASL_FILE_INPUT].Filename, Filename);
    775 
    776     AslGbl_Files[ASL_FILE_INPUT].Handle = InputFile;
    777     AslGbl_CurrentLineNumber = 1;
    778 
    779     /* Emit a new #line directive for the include file */
    780 
    781     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
    782 }
    783 
    784 
    785 /*******************************************************************************
    786  *
    787  * FUNCTION:    AslPopInputFileStack
    788  *
    789  * PARAMETERS:  None
    790  *
    791  * RETURN:      0 if a node was popped, -1 otherwise
    792  *
    793  * DESCRIPTION: Pop the top of the input file stack and point the parser to
    794  *              the saved parse buffer contained in the fnode. Also, set the
    795  *              global line counters to the saved values. This function is
    796  *              called when an include file reaches EOF.
    797  *
    798  ******************************************************************************/
    799 
    800 BOOLEAN
    801 PrPopInputFileStack (
    802     void)
    803 {
    804     PR_FILE_NODE            *Fnode;
    805 
    806 
    807     Fnode = AslGbl_InputFileList;
    808     DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
    809         "Pop InputFile Stack, Fnode %p\n\n",
    810         AslGbl_CurrentLineNumber, Fnode);
    811 
    812     if (!Fnode)
    813     {
    814         return (FALSE);
    815     }
    816 
    817     /* Close the current include file */
    818 
    819     fclose (AslGbl_Files[ASL_FILE_INPUT].Handle);
    820 
    821     /* Update the top-of-stack */
    822 
    823     AslGbl_InputFileList = Fnode->Next;
    824 
    825     /* Reset global line counter and filename */
    826 
    827     AslGbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
    828     AslGbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
    829     AslGbl_CurrentLineNumber = Fnode->CurrentLineNumber;
    830 
    831     /* Emit a new #line directive after the include file */
    832 
    833     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
    834         AslGbl_CurrentLineNumber, Fnode->Filename);
    835 
    836     /* All done with this node */
    837 
    838     ACPI_FREE (Fnode);
    839     return (TRUE);
    840 }
    841