xref: /freebsd-src/sys/contrib/dev/acpica/compiler/prutils.c (revision 804fe2660352e090f4481f2c1d646b508859e79a)
14c52cad2SJung-uk Kim /******************************************************************************
24c52cad2SJung-uk Kim  *
34c52cad2SJung-uk Kim  * Module Name: prutils - Preprocessor utilities
44c52cad2SJung-uk Kim  *
54c52cad2SJung-uk Kim  *****************************************************************************/
64c52cad2SJung-uk Kim 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
11*804fe266SJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
124c52cad2SJung-uk Kim  * All rights reserved.
134c52cad2SJung-uk Kim  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-uk Kim  *
1194c52cad2SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
1204c52cad2SJung-uk Kim  * modification, are permitted provided that the following conditions
1214c52cad2SJung-uk Kim  * are met:
1224c52cad2SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
1234c52cad2SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
1244c52cad2SJung-uk Kim  *    without modification.
1254c52cad2SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1264c52cad2SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
1274c52cad2SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
1284c52cad2SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
1294c52cad2SJung-uk Kim  *    binary redistribution.
1304c52cad2SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
1314c52cad2SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
1324c52cad2SJung-uk Kim  *    from this software without specific prior written permission.
1334c52cad2SJung-uk Kim  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1474c52cad2SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
1484c52cad2SJung-uk Kim  * Software Foundation.
1494c52cad2SJung-uk Kim  *
1500d84335fSJung-uk Kim  *****************************************************************************/
1514c52cad2SJung-uk Kim 
1524c52cad2SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
1534c52cad2SJung-uk Kim 
1544c52cad2SJung-uk Kim #define _COMPONENT          ASL_PREPROCESSOR
1554c52cad2SJung-uk Kim         ACPI_MODULE_NAME    ("prutils")
1564c52cad2SJung-uk Kim 
1574c52cad2SJung-uk Kim 
1584c52cad2SJung-uk Kim /******************************************************************************
1594c52cad2SJung-uk Kim  *
1604c52cad2SJung-uk Kim  * FUNCTION:    PrGetNextToken
1614c52cad2SJung-uk Kim  *
1624c52cad2SJung-uk Kim  * PARAMETERS:  Buffer              - Current line buffer
1634c52cad2SJung-uk Kim  *              MatchString         - String with valid token delimiters
1644c52cad2SJung-uk Kim  *              Next                - Set to next possible token in buffer
1654c52cad2SJung-uk Kim  *
1664c52cad2SJung-uk Kim  * RETURN:      Next token (null-terminated). Modifies the input line.
1674c52cad2SJung-uk Kim  *              Remainder of line is stored in *Next.
1684c52cad2SJung-uk Kim  *
1694c52cad2SJung-uk Kim  * DESCRIPTION: Local implementation of strtok() with local storage for the
1704c52cad2SJung-uk Kim  *              next pointer. Not only thread-safe, but allows multiple
1714c52cad2SJung-uk Kim  *              parsing of substrings such as expressions.
1724c52cad2SJung-uk Kim  *
1734c52cad2SJung-uk Kim  *****************************************************************************/
1744c52cad2SJung-uk Kim 
1754c52cad2SJung-uk Kim char *
1764c52cad2SJung-uk Kim PrGetNextToken (
1774c52cad2SJung-uk Kim     char                    *Buffer,
1784c52cad2SJung-uk Kim     char                    *MatchString,
1794c52cad2SJung-uk Kim     char                    **Next)
1804c52cad2SJung-uk Kim {
1814c52cad2SJung-uk Kim     char                    *TokenStart;
1824c52cad2SJung-uk Kim 
1834c52cad2SJung-uk Kim 
1844c52cad2SJung-uk Kim     if (!Buffer)
1854c52cad2SJung-uk Kim     {
1864c52cad2SJung-uk Kim         /* Use Next if it is valid */
1874c52cad2SJung-uk Kim 
1884c52cad2SJung-uk Kim         Buffer = *Next;
1894c52cad2SJung-uk Kim         if (!(*Next))
1904c52cad2SJung-uk Kim         {
1914c52cad2SJung-uk Kim             return (NULL);
1924c52cad2SJung-uk Kim         }
1934c52cad2SJung-uk Kim     }
1944c52cad2SJung-uk Kim 
1954c52cad2SJung-uk Kim     /* Skip any leading delimiters */
1964c52cad2SJung-uk Kim 
1974c52cad2SJung-uk Kim     while (*Buffer)
1984c52cad2SJung-uk Kim     {
1994c52cad2SJung-uk Kim         if (strchr (MatchString, *Buffer))
2004c52cad2SJung-uk Kim         {
2014c52cad2SJung-uk Kim             Buffer++;
2024c52cad2SJung-uk Kim         }
2034c52cad2SJung-uk Kim         else
2044c52cad2SJung-uk Kim         {
2054c52cad2SJung-uk Kim             break;
2064c52cad2SJung-uk Kim         }
2074c52cad2SJung-uk Kim     }
2084c52cad2SJung-uk Kim 
2094c52cad2SJung-uk Kim     /* Anything left on the line? */
2104c52cad2SJung-uk Kim 
2114c52cad2SJung-uk Kim     if (!(*Buffer))
2124c52cad2SJung-uk Kim     {
2134c52cad2SJung-uk Kim         *Next = NULL;
2144c52cad2SJung-uk Kim         return (NULL);
2154c52cad2SJung-uk Kim     }
2164c52cad2SJung-uk Kim 
2174c52cad2SJung-uk Kim     TokenStart = Buffer;
2184c52cad2SJung-uk Kim 
2194c52cad2SJung-uk Kim     /* Find the end of this token */
2204c52cad2SJung-uk Kim 
2214c52cad2SJung-uk Kim     while (*Buffer)
2224c52cad2SJung-uk Kim     {
2234c52cad2SJung-uk Kim         if (strchr (MatchString, *Buffer))
2244c52cad2SJung-uk Kim         {
2254c52cad2SJung-uk Kim             *Buffer = 0;
2264c52cad2SJung-uk Kim             *Next = Buffer+1;
2274c52cad2SJung-uk Kim             if (!**Next)
2284c52cad2SJung-uk Kim             {
2294c52cad2SJung-uk Kim                 *Next = NULL;
2304c52cad2SJung-uk Kim             }
231f8146b88SJung-uk Kim 
2324c52cad2SJung-uk Kim             return (TokenStart);
2334c52cad2SJung-uk Kim         }
234f8146b88SJung-uk Kim 
2354c52cad2SJung-uk Kim         Buffer++;
2364c52cad2SJung-uk Kim     }
2374c52cad2SJung-uk Kim 
2384c52cad2SJung-uk Kim     *Next = NULL;
2394c52cad2SJung-uk Kim     return (TokenStart);
2404c52cad2SJung-uk Kim }
2414c52cad2SJung-uk Kim 
2424c52cad2SJung-uk Kim 
2434c52cad2SJung-uk Kim /*******************************************************************************
2444c52cad2SJung-uk Kim  *
2454c52cad2SJung-uk Kim  * FUNCTION:    PrError
2464c52cad2SJung-uk Kim  *
2474c52cad2SJung-uk Kim  * PARAMETERS:  Level               - Seriousness (Warning/error, etc.)
2484c52cad2SJung-uk Kim  *              MessageId           - Index into global message buffer
2494c52cad2SJung-uk Kim  *              Column              - Column in current line
2504c52cad2SJung-uk Kim  *
2514c52cad2SJung-uk Kim  * RETURN:      None
2524c52cad2SJung-uk Kim  *
2534c52cad2SJung-uk Kim  * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
2544c52cad2SJung-uk Kim  *
2554c52cad2SJung-uk Kim  ******************************************************************************/
2564c52cad2SJung-uk Kim 
2574c52cad2SJung-uk Kim void
2584c52cad2SJung-uk Kim PrError (
2594c52cad2SJung-uk Kim     UINT8                   Level,
260313a0c13SJung-uk Kim     UINT16                  MessageId,
2614c52cad2SJung-uk Kim     UINT32                  Column)
2624c52cad2SJung-uk Kim {
2634c52cad2SJung-uk Kim #if 0
2646f1f1a63SJung-uk Kim     AcpiOsPrintf ("%s (%u) : %s", AslGbl_Files[ASL_FILE_INPUT].Filename,
2656f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, AslGbl_CurrentLineBuffer);
2664c52cad2SJung-uk Kim #endif
2674c52cad2SJung-uk Kim 
2684c52cad2SJung-uk Kim 
2694c52cad2SJung-uk Kim     if (Column > 120)
2704c52cad2SJung-uk Kim     {
2714c52cad2SJung-uk Kim         Column = 0;
2724c52cad2SJung-uk Kim     }
2734c52cad2SJung-uk Kim 
2744c52cad2SJung-uk Kim     /* TBD: Need Logical line number? */
2754c52cad2SJung-uk Kim 
2764c52cad2SJung-uk Kim     AslCommonError2 (Level, MessageId,
2776f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, Column,
2786f1f1a63SJung-uk Kim         AslGbl_CurrentLineBuffer,
2796f1f1a63SJung-uk Kim         AslGbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
2804c52cad2SJung-uk Kim 
2816f1f1a63SJung-uk Kim     AslGbl_PreprocessorError = TRUE;
2824c52cad2SJung-uk Kim }
2834c52cad2SJung-uk Kim 
2844c52cad2SJung-uk Kim 
2854c52cad2SJung-uk Kim /*******************************************************************************
2864c52cad2SJung-uk Kim  *
287722b1667SJung-uk Kim  * FUNCTION:    PrReplaceResizeSubstring
288722b1667SJung-uk Kim  *
289722b1667SJung-uk Kim  * PARAMETERS:  Args                - Struct containing name, offset & usecount
290722b1667SJung-uk Kim  *              Diff1               - Difference in lengths when new < old
291722b1667SJung-uk Kim  *              Diff2               - Difference in lengths when new > old
292722b1667SJung-uk Kim *               i                   - The curr. no. of iteration of replacement
293722b1667SJung-uk Kim  *              Token               - Substring that replaces Args->Name
294722b1667SJung-uk Kim  *
295722b1667SJung-uk Kim  * RETURN:      None
296722b1667SJung-uk Kim  *
297722b1667SJung-uk Kim  * DESCRIPTION: Advanced substring replacement in a string using resized buffer.
298722b1667SJung-uk Kim  *
299722b1667SJung-uk Kim  ******************************************************************************/
300722b1667SJung-uk Kim 
301722b1667SJung-uk Kim void
302722b1667SJung-uk Kim PrReplaceResizeSubstring(
303722b1667SJung-uk Kim     PR_MACRO_ARG            *Args,
304722b1667SJung-uk Kim     UINT32                  Diff1,
305722b1667SJung-uk Kim     UINT32                  Diff2,
306722b1667SJung-uk Kim     UINT32                  i,
307722b1667SJung-uk Kim     char                    *Token)
308722b1667SJung-uk Kim {
309722b1667SJung-uk Kim     UINT32                  b, PrevOffset;
310722b1667SJung-uk Kim     char                    *temp;
311722b1667SJung-uk Kim     char                    macro_sep[64];
312722b1667SJung-uk Kim 
313722b1667SJung-uk Kim 
314722b1667SJung-uk Kim     AslGbl_MacroTokenReplaceBuffer = (char *) realloc (AslGbl_MacroTokenReplaceBuffer,
315722b1667SJung-uk Kim         (2 * (strlen (AslGbl_MacroTokenBuffer))));
316722b1667SJung-uk Kim 
317722b1667SJung-uk Kim     strcpy (macro_sep, "~,() {}!*/%+-<>=&^|\"\t\n");
318722b1667SJung-uk Kim 
319722b1667SJung-uk Kim     /*
320722b1667SJung-uk Kim      * When the replacement argument (during invocation) length
321722b1667SJung-uk Kim      * < replaced parameter (in the macro function definition
322722b1667SJung-uk Kim      * and its expansion) length
323722b1667SJung-uk Kim      */
324722b1667SJung-uk Kim     if (Diff1 != 0)
325722b1667SJung-uk Kim     {
326722b1667SJung-uk Kim         /*
327722b1667SJung-uk Kim          * We save the offset value to reset it after replacing each
328722b1667SJung-uk Kim          * instance of each arg and setting the offset value to
329722b1667SJung-uk Kim          * the start of the arg to be replaced since it changes
330722b1667SJung-uk Kim          * with each iteration when arg length != token length
331722b1667SJung-uk Kim          */
332722b1667SJung-uk Kim         PrevOffset = Args->Offset[i];
333722b1667SJung-uk Kim         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
334*804fe266SJung-uk Kim         if (temp == NULL)
335*804fe266SJung-uk Kim         {
336*804fe266SJung-uk Kim             return;
337*804fe266SJung-uk Kim         }
338722b1667SJung-uk Kim 
339722b1667SJung-uk Kim ResetHere1:
340722b1667SJung-uk Kim         temp = strstr (temp, Args->Name);
341*804fe266SJung-uk Kim         if (temp == NULL)
342*804fe266SJung-uk Kim         {
343*804fe266SJung-uk Kim             return;
344*804fe266SJung-uk Kim         }
345722b1667SJung-uk Kim         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
346722b1667SJung-uk Kim             strlen (temp);
347722b1667SJung-uk Kim         if (Args->Offset[i] == 0)
348722b1667SJung-uk Kim         {
349722b1667SJung-uk Kim             goto JumpHere1;
350722b1667SJung-uk Kim         }
351722b1667SJung-uk Kim         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
352722b1667SJung-uk Kim             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
353722b1667SJung-uk Kim         {
354722b1667SJung-uk Kim             Args->Offset[i] += 0;
355722b1667SJung-uk Kim         }
356722b1667SJung-uk Kim         else
357722b1667SJung-uk Kim         {
358722b1667SJung-uk Kim             temp += strlen (Args->Name);
359722b1667SJung-uk Kim             goto ResetHere1;
360722b1667SJung-uk Kim         }
361722b1667SJung-uk Kim 
362722b1667SJung-uk Kim         /*
363722b1667SJung-uk Kim          * For now, we simply set the extra char positions (generated
364722b1667SJung-uk Kim          * due to longer name replaced by shorter name) to whitespace
365722b1667SJung-uk Kim          * chars so it will be ignored during compilation
366722b1667SJung-uk Kim          */
367722b1667SJung-uk Kim JumpHere1:
368722b1667SJung-uk Kim         b = strlen (Token) + Args->Offset[i];
369722b1667SJung-uk Kim         memset (&AslGbl_MacroTokenBuffer[b], ' ', Diff1);
370722b1667SJung-uk Kim 
371722b1667SJung-uk Kim # if 0
372722b1667SJung-uk Kim 
373722b1667SJung-uk Kim     /* Work in progress as of 03/08/2023 - experimental 'if' block
374722b1667SJung-uk Kim      * to test code for removing extra whitespaces from the macro
375722b1667SJung-uk Kim      * replacement when replacement arg < replaced param
376722b1667SJung-uk Kim      */
377722b1667SJung-uk Kim         char Buff[8192];
378722b1667SJung-uk Kim         /* char* Replace; */
379722b1667SJung-uk Kim         /* Replace = Buff; */
380722b1667SJung-uk Kim 
381722b1667SJung-uk Kim         for (j = 0; j < strlen (AslGbl_MacroTokenBuffer); j++)
382722b1667SJung-uk Kim         {
383722b1667SJung-uk Kim             Buff[j] = AslGbl_MacroTokenBuffer[j];
384722b1667SJung-uk Kim         }
385722b1667SJung-uk Kim         Buff[strlen (AslGbl_MacroTokenBuffer)] = '\0';
386*804fe266SJung-uk Kim         /* fprintf(stderr, "Buff: %s\n", Buff); */
387722b1667SJung-uk Kim 
388722b1667SJung-uk Kim         UINT32 len = strlen (Buff);
389722b1667SJung-uk Kim 
390722b1667SJung-uk Kim         for (j = 0; j < len; j++)
391722b1667SJung-uk Kim         {
392722b1667SJung-uk Kim             if (Buff[0] == ' ')
393722b1667SJung-uk Kim             {
394722b1667SJung-uk Kim                 for (j = 0; j < (len - 1); j++)
395722b1667SJung-uk Kim                 {
396722b1667SJung-uk Kim                     Buff[j] = Buff[j + 1];
397722b1667SJung-uk Kim                 }
398722b1667SJung-uk Kim                 Buff[j] = '\0';
399722b1667SJung-uk Kim                 len--;
400722b1667SJung-uk Kim                 j = -1;
401722b1667SJung-uk Kim                 continue;
402722b1667SJung-uk Kim             }
403722b1667SJung-uk Kim 
404722b1667SJung-uk Kim             if (Buff[j] == ' ' && Buff[j + 1] == ' ')
405722b1667SJung-uk Kim             {
406722b1667SJung-uk Kim                 for (k = 0; k < (len - 1); k++)
407722b1667SJung-uk Kim                 {
408722b1667SJung-uk Kim                     Buff[j] = Buff[j + 1];
409722b1667SJung-uk Kim                 }
410722b1667SJung-uk Kim                 Buff[j] = '\0';
411722b1667SJung-uk Kim                 len--;
412722b1667SJung-uk Kim                 j--;
413722b1667SJung-uk Kim             }
414722b1667SJung-uk Kim         }
415*804fe266SJung-uk Kim         /* fprintf(stderr, "Buff: %s\n", Buff); */
416722b1667SJung-uk Kim 
417722b1667SJung-uk Kim         for (k = 0; k < strlen (Buff); k++)
418722b1667SJung-uk Kim         {
419722b1667SJung-uk Kim             AslGbl_MacroTokenBuffer[k] = Buff[k];
420722b1667SJung-uk Kim         }
421722b1667SJung-uk Kim #endif
422722b1667SJung-uk Kim 
423722b1667SJung-uk Kim         PrReplaceData (
424722b1667SJung-uk Kim             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
425722b1667SJung-uk Kim             strlen (Token), Token, strlen (Token));
426722b1667SJung-uk Kim 
427722b1667SJung-uk Kim         temp = NULL;
428722b1667SJung-uk Kim         Args->Offset[i] = PrevOffset;
429722b1667SJung-uk Kim     }
430722b1667SJung-uk Kim 
431722b1667SJung-uk Kim     /*
432722b1667SJung-uk Kim      * When the replacement argument (during invocation) length
433722b1667SJung-uk Kim      * > replaced parameter (in the macro function definition
434722b1667SJung-uk Kim      * and its expansion) length
435722b1667SJung-uk Kim      */
436722b1667SJung-uk Kim     else if (Diff2 != 0)
437722b1667SJung-uk Kim     {
438722b1667SJung-uk Kim         /* Doing the same thing with offset value as for prev case */
439722b1667SJung-uk Kim 
440722b1667SJung-uk Kim         PrevOffset = Args->Offset[i];
441722b1667SJung-uk Kim         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
442*804fe266SJung-uk Kim         if (temp == NULL)
443*804fe266SJung-uk Kim         {
444*804fe266SJung-uk Kim             return;
445*804fe266SJung-uk Kim         }
446722b1667SJung-uk Kim 
447722b1667SJung-uk Kim ResetHere2:
448722b1667SJung-uk Kim         temp = strstr (temp, Args->Name);
449*804fe266SJung-uk Kim         if (temp == NULL)
450*804fe266SJung-uk Kim         {
451*804fe266SJung-uk Kim             return;
452*804fe266SJung-uk Kim         }
453722b1667SJung-uk Kim         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
454722b1667SJung-uk Kim             strlen (temp);
455722b1667SJung-uk Kim         if (Args->Offset[i] == 0)
456722b1667SJung-uk Kim         {
457722b1667SJung-uk Kim             goto JumpHere2;
458722b1667SJung-uk Kim         }
459722b1667SJung-uk Kim         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
460722b1667SJung-uk Kim             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
461722b1667SJung-uk Kim         {
462722b1667SJung-uk Kim             Args->Offset[i] += 0;
463722b1667SJung-uk Kim         }
464722b1667SJung-uk Kim         else
465722b1667SJung-uk Kim         {
466722b1667SJung-uk Kim             temp+= strlen (Args->Name);
467722b1667SJung-uk Kim             goto ResetHere2;
468722b1667SJung-uk Kim         }
469722b1667SJung-uk Kim 
470722b1667SJung-uk Kim         /*
471722b1667SJung-uk Kim          * We will need to allocate some extra space in our buffer to
472722b1667SJung-uk Kim          * accommodate the increase in the replacement string length
473722b1667SJung-uk Kim          * over the shorter outgoing arg string and do the replacement
474722b1667SJung-uk Kim          * at the correct offset value which is resetted every iteration
475722b1667SJung-uk Kim          */
476722b1667SJung-uk Kim JumpHere2:
477722b1667SJung-uk Kim         strncpy (AslGbl_MacroTokenReplaceBuffer, AslGbl_MacroTokenBuffer, Args->Offset[i]);
478722b1667SJung-uk Kim         strcat (AslGbl_MacroTokenReplaceBuffer, Token);
479722b1667SJung-uk Kim         strcat (AslGbl_MacroTokenReplaceBuffer, (AslGbl_MacroTokenBuffer +
480722b1667SJung-uk Kim             (Args->Offset[i] + strlen (Args->Name))));
481722b1667SJung-uk Kim 
482722b1667SJung-uk Kim         strcpy (AslGbl_MacroTokenBuffer, AslGbl_MacroTokenReplaceBuffer);
483722b1667SJung-uk Kim 
484722b1667SJung-uk Kim         temp = NULL;
485722b1667SJung-uk Kim         Args->Offset[i] = PrevOffset;
486722b1667SJung-uk Kim     }
487722b1667SJung-uk Kim 
488722b1667SJung-uk Kim     /*
489722b1667SJung-uk Kim      * When the replacement argument (during invocation) length =
490722b1667SJung-uk Kim      * replaced parameter (in the macro function definition and
491722b1667SJung-uk Kim      * its expansion) length
492722b1667SJung-uk Kim      */
493722b1667SJung-uk Kim     else
494722b1667SJung-uk Kim     {
495722b1667SJung-uk Kim 
496722b1667SJung-uk Kim         /*
497722b1667SJung-uk Kim          * We still need to reset the offset for each iteration even when
498722b1667SJung-uk Kim          * arg and param lengths are same since any macro func invocation
499722b1667SJung-uk Kim          * could use various cases for each separate arg-param pair
500722b1667SJung-uk Kim          */
501722b1667SJung-uk Kim         PrevOffset = Args->Offset[i];
502722b1667SJung-uk Kim         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
503*804fe266SJung-uk Kim         if (temp == NULL)
504*804fe266SJung-uk Kim         {
505*804fe266SJung-uk Kim             return;
506*804fe266SJung-uk Kim         }
507722b1667SJung-uk Kim 
508722b1667SJung-uk Kim ResetHere3:
509722b1667SJung-uk Kim         temp = strstr (temp, Args->Name);
510*804fe266SJung-uk Kim         if (temp == NULL)
511*804fe266SJung-uk Kim         {
512*804fe266SJung-uk Kim             return;
513*804fe266SJung-uk Kim         }
514722b1667SJung-uk Kim         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
515722b1667SJung-uk Kim             strlen (temp);
516722b1667SJung-uk Kim         if (Args->Offset[i] == 0)
517722b1667SJung-uk Kim         {
518722b1667SJung-uk Kim             goto JumpHere3;
519722b1667SJung-uk Kim         }
520722b1667SJung-uk Kim         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
521722b1667SJung-uk Kim             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
522722b1667SJung-uk Kim         {
523722b1667SJung-uk Kim             Args->Offset[i] += 0;
524722b1667SJung-uk Kim         }
525722b1667SJung-uk Kim         else
526722b1667SJung-uk Kim         {
527722b1667SJung-uk Kim             temp += strlen (Args->Name);
528722b1667SJung-uk Kim             goto ResetHere3;
529722b1667SJung-uk Kim         }
530722b1667SJung-uk Kim 
531722b1667SJung-uk Kim JumpHere3:
532722b1667SJung-uk Kim         PrReplaceData (
533722b1667SJung-uk Kim             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
534722b1667SJung-uk Kim             strlen (Args->Name), Token, strlen (Token));
535722b1667SJung-uk Kim         temp = NULL;
536722b1667SJung-uk Kim         Args->Offset[i] = PrevOffset;
537722b1667SJung-uk Kim     }
538722b1667SJung-uk Kim }
539722b1667SJung-uk Kim 
540722b1667SJung-uk Kim 
541722b1667SJung-uk Kim /*******************************************************************************
542722b1667SJung-uk Kim  *
5434c52cad2SJung-uk Kim  * FUNCTION:    PrReplaceData
5444c52cad2SJung-uk Kim  *
5454c52cad2SJung-uk Kim  * PARAMETERS:  Buffer              - Original(target) buffer pointer
5464c52cad2SJung-uk Kim  *              LengthToRemove      - Length to be removed from target buffer
5474c52cad2SJung-uk Kim  *              BufferToAdd         - Data to be inserted into target buffer
5484c52cad2SJung-uk Kim  *              LengthToAdd         - Length of BufferToAdd
5494c52cad2SJung-uk Kim  *
550722b1667SJung-uk Kim  * RETURN:      Pointer to where the buffer is replaced with data
5514c52cad2SJung-uk Kim  *
5524c52cad2SJung-uk Kim  * DESCRIPTION: Generic buffer data replacement.
5534c52cad2SJung-uk Kim  *
5544c52cad2SJung-uk Kim  ******************************************************************************/
5554c52cad2SJung-uk Kim 
556722b1667SJung-uk Kim char *
5574c52cad2SJung-uk Kim PrReplaceData (
5584c52cad2SJung-uk Kim     char                    *Buffer,
5594c52cad2SJung-uk Kim     UINT32                  LengthToRemove,
5604c52cad2SJung-uk Kim     char                    *BufferToAdd,
5614c52cad2SJung-uk Kim     UINT32                  LengthToAdd)
5624c52cad2SJung-uk Kim {
5634c52cad2SJung-uk Kim     UINT32                  BufferLength;
5644c52cad2SJung-uk Kim 
5654c52cad2SJung-uk Kim 
5664c52cad2SJung-uk Kim     /* Buffer is a string, so the length must include the terminating zero */
5674c52cad2SJung-uk Kim 
5684c52cad2SJung-uk Kim     BufferLength = strlen (Buffer) + 1;
5694c52cad2SJung-uk Kim 
5704c52cad2SJung-uk Kim     if (LengthToRemove != LengthToAdd)
5714c52cad2SJung-uk Kim     {
5724c52cad2SJung-uk Kim         /*
5734c52cad2SJung-uk Kim          * Move some of the existing data
5744c52cad2SJung-uk Kim          * 1) If adding more bytes than removing, make room for the new data
5754c52cad2SJung-uk Kim          * 2) if removing more bytes than adding, delete the extra space
5764c52cad2SJung-uk Kim          */
5774c52cad2SJung-uk Kim         if (LengthToRemove > 0)
5784c52cad2SJung-uk Kim         {
5794c52cad2SJung-uk Kim             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
5804c52cad2SJung-uk Kim                 (BufferLength - LengthToRemove));
5814c52cad2SJung-uk Kim         }
5824c52cad2SJung-uk Kim     }
5834c52cad2SJung-uk Kim 
584722b1667SJung-uk Kim 
5854c52cad2SJung-uk Kim     /* Now we can move in the new data */
5864c52cad2SJung-uk Kim 
5874c52cad2SJung-uk Kim     if (LengthToAdd > 0)
5884c52cad2SJung-uk Kim     {
5894c52cad2SJung-uk Kim         memmove (Buffer, BufferToAdd, LengthToAdd);
5904c52cad2SJung-uk Kim     }
591722b1667SJung-uk Kim     return (Buffer + LengthToAdd);
5924c52cad2SJung-uk Kim }
5934c52cad2SJung-uk Kim 
5944c52cad2SJung-uk Kim 
5954c52cad2SJung-uk Kim /*******************************************************************************
5964c52cad2SJung-uk Kim  *
5974c52cad2SJung-uk Kim  * FUNCTION:    PrOpenIncludeFile
5984c52cad2SJung-uk Kim  *
5994c52cad2SJung-uk Kim  * PARAMETERS:  Filename            - Filename or pathname for include file
6004c52cad2SJung-uk Kim  *
6014c52cad2SJung-uk Kim  * RETURN:      None.
6024c52cad2SJung-uk Kim  *
6034c52cad2SJung-uk Kim  * DESCRIPTION: Open an include file and push it on the input file stack.
6044c52cad2SJung-uk Kim  *
6054c52cad2SJung-uk Kim  ******************************************************************************/
6064c52cad2SJung-uk Kim 
607a371a5fdSJung-uk Kim FILE *
6084c52cad2SJung-uk Kim PrOpenIncludeFile (
609a371a5fdSJung-uk Kim     char                    *Filename,
610a371a5fdSJung-uk Kim     char                    *OpenMode,
611a371a5fdSJung-uk Kim     char                    **FullPathname)
6124c52cad2SJung-uk Kim {
6134c52cad2SJung-uk Kim     FILE                    *IncludeFile;
6144c52cad2SJung-uk Kim     ASL_INCLUDE_DIR         *NextDir;
6154c52cad2SJung-uk Kim 
6164c52cad2SJung-uk Kim 
617a7a3b383SJung-uk Kim     /* Start the actual include file on the next line */
618a7a3b383SJung-uk Kim 
6196f1f1a63SJung-uk Kim     AslGbl_CurrentLineOffset++;
6204c52cad2SJung-uk Kim 
6214c52cad2SJung-uk Kim     /* Attempt to open the include file */
6224c52cad2SJung-uk Kim     /* If the file specifies an absolute path, just open it */
6234c52cad2SJung-uk Kim 
6244c52cad2SJung-uk Kim     if ((Filename[0] == '/')  ||
6254c52cad2SJung-uk Kim         (Filename[0] == '\\') ||
6264c52cad2SJung-uk Kim         (Filename[1] == ':'))
6274c52cad2SJung-uk Kim     {
628a371a5fdSJung-uk Kim         IncludeFile = PrOpenIncludeWithPrefix (
629a371a5fdSJung-uk Kim             "", Filename, OpenMode, FullPathname);
6304c52cad2SJung-uk Kim         if (!IncludeFile)
6314c52cad2SJung-uk Kim         {
6324c52cad2SJung-uk Kim             goto ErrorExit;
6334c52cad2SJung-uk Kim         }
634a371a5fdSJung-uk Kim         return (IncludeFile);
6354c52cad2SJung-uk Kim     }
6364c52cad2SJung-uk Kim 
6374c52cad2SJung-uk Kim     /*
6384c52cad2SJung-uk Kim      * The include filename is not an absolute path.
6394c52cad2SJung-uk Kim      *
6404c52cad2SJung-uk Kim      * First, search for the file within the "local" directory -- meaning
6414c52cad2SJung-uk Kim      * the same directory that contains the source file.
6424c52cad2SJung-uk Kim      *
6434c52cad2SJung-uk Kim      * Construct the file pathname from the global directory name.
6444c52cad2SJung-uk Kim      */
645a371a5fdSJung-uk Kim     IncludeFile = PrOpenIncludeWithPrefix (
6466f1f1a63SJung-uk Kim         AslGbl_DirectoryPath, Filename, OpenMode, FullPathname);
6474c52cad2SJung-uk Kim     if (IncludeFile)
6484c52cad2SJung-uk Kim     {
649a371a5fdSJung-uk Kim         return (IncludeFile);
6504c52cad2SJung-uk Kim     }
6514c52cad2SJung-uk Kim 
6524c52cad2SJung-uk Kim     /*
6534c52cad2SJung-uk Kim      * Second, search for the file within the (possibly multiple)
6544c52cad2SJung-uk Kim      * directories specified by the -I option on the command line.
6554c52cad2SJung-uk Kim      */
6566f1f1a63SJung-uk Kim     NextDir = AslGbl_IncludeDirList;
6574c52cad2SJung-uk Kim     while (NextDir)
6584c52cad2SJung-uk Kim     {
659a371a5fdSJung-uk Kim         IncludeFile = PrOpenIncludeWithPrefix (
660a371a5fdSJung-uk Kim             NextDir->Dir, Filename, OpenMode, FullPathname);
6614c52cad2SJung-uk Kim         if (IncludeFile)
6624c52cad2SJung-uk Kim         {
663a371a5fdSJung-uk Kim             return (IncludeFile);
6644c52cad2SJung-uk Kim         }
6654c52cad2SJung-uk Kim 
6664c52cad2SJung-uk Kim         NextDir = NextDir->Next;
6674c52cad2SJung-uk Kim     }
6684c52cad2SJung-uk Kim 
6694c52cad2SJung-uk Kim     /* We could not open the include file after trying very hard */
6704c52cad2SJung-uk Kim 
6714c52cad2SJung-uk Kim ErrorExit:
6726f1f1a63SJung-uk Kim     sprintf (AslGbl_MainTokenBuffer, "%s, %s", Filename, strerror (errno));
6734c52cad2SJung-uk Kim     PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
674a371a5fdSJung-uk Kim     return (NULL);
6754c52cad2SJung-uk Kim }
6764c52cad2SJung-uk Kim 
6774c52cad2SJung-uk Kim 
6784c52cad2SJung-uk Kim /*******************************************************************************
6794c52cad2SJung-uk Kim  *
6804c52cad2SJung-uk Kim  * FUNCTION:    FlOpenIncludeWithPrefix
6814c52cad2SJung-uk Kim  *
6824c52cad2SJung-uk Kim  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
6834c52cad2SJung-uk Kim  *                                length string.
6844c52cad2SJung-uk Kim  *              Filename        - The include filename from the source ASL.
6854c52cad2SJung-uk Kim  *
6864c52cad2SJung-uk Kim  * RETURN:      Valid file descriptor if successful. Null otherwise.
6874c52cad2SJung-uk Kim  *
6884c52cad2SJung-uk Kim  * DESCRIPTION: Open an include file and push it on the input file stack.
6894c52cad2SJung-uk Kim  *
6904c52cad2SJung-uk Kim  ******************************************************************************/
6914c52cad2SJung-uk Kim 
6924c52cad2SJung-uk Kim FILE *
6934c52cad2SJung-uk Kim PrOpenIncludeWithPrefix (
6944c52cad2SJung-uk Kim     char                    *PrefixDir,
695a371a5fdSJung-uk Kim     char                    *Filename,
696a371a5fdSJung-uk Kim     char                    *OpenMode,
697a371a5fdSJung-uk Kim     char                    **FullPathname)
6984c52cad2SJung-uk Kim {
6994c52cad2SJung-uk Kim     FILE                    *IncludeFile;
7004c52cad2SJung-uk Kim     char                    *Pathname;
7014c52cad2SJung-uk Kim 
7024c52cad2SJung-uk Kim 
7034c52cad2SJung-uk Kim     /* Build the full pathname to the file */
7044c52cad2SJung-uk Kim 
705a7a3b383SJung-uk Kim     Pathname = FlMergePathnames (PrefixDir, Filename);
7064c52cad2SJung-uk Kim 
707a7a3b383SJung-uk Kim     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
708a7a3b383SJung-uk Kim         "Include: Opening file - \"%s\"\n",
7096f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, Pathname);
7104c52cad2SJung-uk Kim 
7114c52cad2SJung-uk Kim     /* Attempt to open the file, push if successful */
7124c52cad2SJung-uk Kim 
713a371a5fdSJung-uk Kim     IncludeFile = fopen (Pathname, OpenMode);
714ed17e06eSJung-uk Kim     if (!IncludeFile)
7154c52cad2SJung-uk Kim     {
716ed17e06eSJung-uk Kim         return (NULL);
717ed17e06eSJung-uk Kim     }
718ed17e06eSJung-uk Kim 
7194c52cad2SJung-uk Kim     /* Push the include file on the open input file stack */
7204c52cad2SJung-uk Kim 
7214c52cad2SJung-uk Kim     PrPushInputFileStack (IncludeFile, Pathname);
722a371a5fdSJung-uk Kim     *FullPathname = Pathname;
7234c52cad2SJung-uk Kim     return (IncludeFile);
7244c52cad2SJung-uk Kim }
7254c52cad2SJung-uk Kim 
7264c52cad2SJung-uk Kim 
7274c52cad2SJung-uk Kim /*******************************************************************************
7284c52cad2SJung-uk Kim  *
7294c52cad2SJung-uk Kim  * FUNCTION:    AslPushInputFileStack
7304c52cad2SJung-uk Kim  *
7314c52cad2SJung-uk Kim  * PARAMETERS:  InputFile           - Open file pointer
7324c52cad2SJung-uk Kim  *              Filename            - Name of the file
7334c52cad2SJung-uk Kim  *
7344c52cad2SJung-uk Kim  * RETURN:      None
7354c52cad2SJung-uk Kim  *
7364c52cad2SJung-uk Kim  * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
7374c52cad2SJung-uk Kim  *              to this file. Called when an include file is successfully
7384c52cad2SJung-uk Kim  *              opened.
7394c52cad2SJung-uk Kim  *
7404c52cad2SJung-uk Kim  ******************************************************************************/
7414c52cad2SJung-uk Kim 
7424c52cad2SJung-uk Kim void
7434c52cad2SJung-uk Kim PrPushInputFileStack (
7444c52cad2SJung-uk Kim     FILE                    *InputFile,
7454c52cad2SJung-uk Kim     char                    *Filename)
7464c52cad2SJung-uk Kim {
7474c52cad2SJung-uk Kim     PR_FILE_NODE            *Fnode;
7484c52cad2SJung-uk Kim 
7494c52cad2SJung-uk Kim 
7506f1f1a63SJung-uk Kim     AslGbl_HasIncludeFiles = TRUE;
7515ef50723SJung-uk Kim 
7524c52cad2SJung-uk Kim     /* Save the current state in an Fnode */
7534c52cad2SJung-uk Kim 
7544c52cad2SJung-uk Kim     Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
7554c52cad2SJung-uk Kim 
7566f1f1a63SJung-uk Kim     Fnode->File = AslGbl_Files[ASL_FILE_INPUT].Handle;
7576f1f1a63SJung-uk Kim     Fnode->Next = AslGbl_InputFileList;
7586f1f1a63SJung-uk Kim     Fnode->Filename = AslGbl_Files[ASL_FILE_INPUT].Filename;
7596f1f1a63SJung-uk Kim     Fnode->CurrentLineNumber = AslGbl_CurrentLineNumber;
7604c52cad2SJung-uk Kim 
7614c52cad2SJung-uk Kim     /* Push it on the stack */
7624c52cad2SJung-uk Kim 
7636f1f1a63SJung-uk Kim     AslGbl_InputFileList = Fnode;
7644c52cad2SJung-uk Kim 
7654c52cad2SJung-uk Kim     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
766eef1b955SJung-uk Kim         "Push InputFile Stack: handle %p\n\n",
7676f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, InputFile);
7684c52cad2SJung-uk Kim 
7694c52cad2SJung-uk Kim     /* Reset the global line count and filename */
7704c52cad2SJung-uk Kim 
7716f1f1a63SJung-uk Kim     AslGbl_Files[ASL_FILE_INPUT].Filename =
77267d9aa44SJung-uk Kim         UtLocalCacheCalloc (strlen (Filename) + 1);
7736f1f1a63SJung-uk Kim     strcpy (AslGbl_Files[ASL_FILE_INPUT].Filename, Filename);
774313a0c13SJung-uk Kim 
7756f1f1a63SJung-uk Kim     AslGbl_Files[ASL_FILE_INPUT].Handle = InputFile;
7766f1f1a63SJung-uk Kim     AslGbl_CurrentLineNumber = 1;
777eef1b955SJung-uk Kim 
778eef1b955SJung-uk Kim     /* Emit a new #line directive for the include file */
779eef1b955SJung-uk Kim 
780313a0c13SJung-uk Kim     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
7814c52cad2SJung-uk Kim }
7824c52cad2SJung-uk Kim 
7834c52cad2SJung-uk Kim 
7844c52cad2SJung-uk Kim /*******************************************************************************
7854c52cad2SJung-uk Kim  *
7864c52cad2SJung-uk Kim  * FUNCTION:    AslPopInputFileStack
7874c52cad2SJung-uk Kim  *
7884c52cad2SJung-uk Kim  * PARAMETERS:  None
7894c52cad2SJung-uk Kim  *
7904c52cad2SJung-uk Kim  * RETURN:      0 if a node was popped, -1 otherwise
7914c52cad2SJung-uk Kim  *
7924c52cad2SJung-uk Kim  * DESCRIPTION: Pop the top of the input file stack and point the parser to
7934c52cad2SJung-uk Kim  *              the saved parse buffer contained in the fnode. Also, set the
7944c52cad2SJung-uk Kim  *              global line counters to the saved values. This function is
7954c52cad2SJung-uk Kim  *              called when an include file reaches EOF.
7964c52cad2SJung-uk Kim  *
7974c52cad2SJung-uk Kim  ******************************************************************************/
7984c52cad2SJung-uk Kim 
7994c52cad2SJung-uk Kim BOOLEAN
8004c52cad2SJung-uk Kim PrPopInputFileStack (
8014c52cad2SJung-uk Kim     void)
8024c52cad2SJung-uk Kim {
8034c52cad2SJung-uk Kim     PR_FILE_NODE            *Fnode;
8044c52cad2SJung-uk Kim 
8054c52cad2SJung-uk Kim 
8066f1f1a63SJung-uk Kim     Fnode = AslGbl_InputFileList;
8074c52cad2SJung-uk Kim     DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
8084c52cad2SJung-uk Kim         "Pop InputFile Stack, Fnode %p\n\n",
8096f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, Fnode);
8104c52cad2SJung-uk Kim 
8114c52cad2SJung-uk Kim     if (!Fnode)
8124c52cad2SJung-uk Kim     {
8134c52cad2SJung-uk Kim         return (FALSE);
8144c52cad2SJung-uk Kim     }
8154c52cad2SJung-uk Kim 
8164c52cad2SJung-uk Kim     /* Close the current include file */
8174c52cad2SJung-uk Kim 
8186f1f1a63SJung-uk Kim     fclose (AslGbl_Files[ASL_FILE_INPUT].Handle);
8194c52cad2SJung-uk Kim 
8204c52cad2SJung-uk Kim     /* Update the top-of-stack */
8214c52cad2SJung-uk Kim 
8226f1f1a63SJung-uk Kim     AslGbl_InputFileList = Fnode->Next;
8234c52cad2SJung-uk Kim 
8244c52cad2SJung-uk Kim     /* Reset global line counter and filename */
8254c52cad2SJung-uk Kim 
8266f1f1a63SJung-uk Kim     AslGbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
8276f1f1a63SJung-uk Kim     AslGbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
8286f1f1a63SJung-uk Kim     AslGbl_CurrentLineNumber = Fnode->CurrentLineNumber;
829eef1b955SJung-uk Kim 
830eef1b955SJung-uk Kim     /* Emit a new #line directive after the include file */
831eef1b955SJung-uk Kim 
832eef1b955SJung-uk Kim     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
8336f1f1a63SJung-uk Kim         AslGbl_CurrentLineNumber, Fnode->Filename);
8344c52cad2SJung-uk Kim 
8354c52cad2SJung-uk Kim     /* All done with this node */
8364c52cad2SJung-uk Kim 
8374c52cad2SJung-uk Kim     ACPI_FREE (Fnode);
8384c52cad2SJung-uk Kim     return (TRUE);
8394c52cad2SJung-uk Kim }
840