xref: /freebsd/sys/contrib/dev/acpica/compiler/prutils.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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 - 2023, 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 <contrib/dev/acpica/compiler/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 
335 ResetHere1:
336         temp = strstr (temp, Args->Name);
337         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
338             strlen (temp);
339         if (Args->Offset[i] == 0)
340         {
341             goto JumpHere1;
342         }
343         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
344             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
345         {
346             Args->Offset[i] += 0;
347         }
348         else
349         {
350             temp += strlen (Args->Name);
351             goto ResetHere1;
352         }
353 
354         /*
355          * For now, we simply set the extra char positions (generated
356          * due to longer name replaced by shorter name) to whitespace
357          * chars so it will be ignored during compilation
358          */
359 JumpHere1:
360         b = strlen (Token) + Args->Offset[i];
361         memset (&AslGbl_MacroTokenBuffer[b], ' ', Diff1);
362 
363 # if 0
364 
365     /* Work in progress as of 03/08/2023 - experimental 'if' block
366      * to test code for removing extra whitespaces from the macro
367      * replacement when replacement arg < replaced param
368      */
369         char Buff[8192];
370         /* char* Replace; */
371         /* Replace = Buff; */
372 
373         for (j = 0; j < strlen (AslGbl_MacroTokenBuffer); j++)
374         {
375             Buff[j] = AslGbl_MacroTokenBuffer[j];
376         }
377         Buff[strlen (AslGbl_MacroTokenBuffer)] = '\0';
378         //fprintf (stderr, "Buff: %s\n", Buff);
379 
380         UINT32 len = strlen (Buff);
381 
382         for (j = 0; j < len; j++)
383         {
384             if (Buff[0] == ' ')
385             {
386                 for (j = 0; j < (len - 1); j++)
387                 {
388                     Buff[j] = Buff[j + 1];
389                 }
390                 Buff[j] = '\0';
391                 len--;
392                 j = -1;
393                 continue;
394             }
395 
396             if (Buff[j] == ' ' && Buff[j + 1] == ' ')
397             {
398                 for (k = 0; k < (len - 1); k++)
399                 {
400                     Buff[j] = Buff[j + 1];
401                 }
402                 Buff[j] = '\0';
403                 len--;
404                 j--;
405             }
406         }
407         //fprintf(stderr, "Buff: %s\n", Buff);
408 
409         for (k = 0; k < strlen (Buff); k++)
410         {
411             AslGbl_MacroTokenBuffer[k] = Buff[k];
412         }
413 #endif
414 
415         PrReplaceData (
416             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
417             strlen (Token), Token, strlen (Token));
418 
419         temp = NULL;
420         Args->Offset[i] = PrevOffset;
421     }
422 
423     /*
424      * When the replacement argument (during invocation) length
425      * > replaced parameter (in the macro function definition
426      * and its expansion) length
427      */
428     else if (Diff2 != 0)
429     {
430         /* Doing the same thing with offset value as for prev case */
431 
432         PrevOffset = Args->Offset[i];
433         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
434 
435 ResetHere2:
436         temp = strstr (temp, Args->Name);
437         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
438             strlen (temp);
439         if (Args->Offset[i] == 0)
440         {
441             goto JumpHere2;
442         }
443         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
444             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
445         {
446             Args->Offset[i] += 0;
447         }
448         else
449         {
450             temp+= strlen (Args->Name);
451             goto ResetHere2;
452         }
453 
454         /*
455          * We will need to allocate some extra space in our buffer to
456          * accommodate the increase in the replacement string length
457          * over the shorter outgoing arg string and do the replacement
458          * at the correct offset value which is resetted every iteration
459          */
460 JumpHere2:
461         strncpy (AslGbl_MacroTokenReplaceBuffer, AslGbl_MacroTokenBuffer, Args->Offset[i]);
462         strcat (AslGbl_MacroTokenReplaceBuffer, Token);
463         strcat (AslGbl_MacroTokenReplaceBuffer, (AslGbl_MacroTokenBuffer +
464             (Args->Offset[i] + strlen (Args->Name))));
465 
466         strcpy (AslGbl_MacroTokenBuffer, AslGbl_MacroTokenReplaceBuffer);
467 
468         temp = NULL;
469         Args->Offset[i] = PrevOffset;
470     }
471 
472     /*
473      * When the replacement argument (during invocation) length =
474      * replaced parameter (in the macro function definition and
475      * its expansion) length
476      */
477     else
478     {
479 
480         /*
481          * We still need to reset the offset for each iteration even when
482          * arg and param lengths are same since any macro func invocation
483          * could use various cases for each separate arg-param pair
484          */
485         PrevOffset = Args->Offset[i];
486         temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
487 
488 ResetHere3:
489         temp = strstr (temp, Args->Name);
490         Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
491             strlen (temp);
492         if (Args->Offset[i] == 0)
493         {
494             goto JumpHere3;
495         }
496         if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
497             (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
498         {
499             Args->Offset[i] += 0;
500         }
501         else
502         {
503             temp += strlen (Args->Name);
504             goto ResetHere3;
505         }
506 
507 JumpHere3:
508         PrReplaceData (
509             &AslGbl_MacroTokenBuffer[Args->Offset[i]],
510             strlen (Args->Name), Token, strlen (Token));
511         temp = NULL;
512         Args->Offset[i] = PrevOffset;
513     }
514 }
515 
516 
517 /*******************************************************************************
518  *
519  * FUNCTION:    PrReplaceData
520  *
521  * PARAMETERS:  Buffer              - Original(target) buffer pointer
522  *              LengthToRemove      - Length to be removed from target buffer
523  *              BufferToAdd         - Data to be inserted into target buffer
524  *              LengthToAdd         - Length of BufferToAdd
525  *
526  * RETURN:      Pointer to where the buffer is replaced with data
527  *
528  * DESCRIPTION: Generic buffer data replacement.
529  *
530  ******************************************************************************/
531 
532 char *
533 PrReplaceData (
534     char                    *Buffer,
535     UINT32                  LengthToRemove,
536     char                    *BufferToAdd,
537     UINT32                  LengthToAdd)
538 {
539     UINT32                  BufferLength;
540 
541 
542     /* Buffer is a string, so the length must include the terminating zero */
543 
544     BufferLength = strlen (Buffer) + 1;
545 
546     if (LengthToRemove != LengthToAdd)
547     {
548         /*
549          * Move some of the existing data
550          * 1) If adding more bytes than removing, make room for the new data
551          * 2) if removing more bytes than adding, delete the extra space
552          */
553         if (LengthToRemove > 0)
554         {
555             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
556                 (BufferLength - LengthToRemove));
557         }
558     }
559 
560 
561     /* Now we can move in the new data */
562 
563     if (LengthToAdd > 0)
564     {
565         memmove (Buffer, BufferToAdd, LengthToAdd);
566     }
567     return (Buffer + LengthToAdd);
568 }
569 
570 
571 /*******************************************************************************
572  *
573  * FUNCTION:    PrOpenIncludeFile
574  *
575  * PARAMETERS:  Filename            - Filename or pathname for include file
576  *
577  * RETURN:      None.
578  *
579  * DESCRIPTION: Open an include file and push it on the input file stack.
580  *
581  ******************************************************************************/
582 
583 FILE *
584 PrOpenIncludeFile (
585     char                    *Filename,
586     char                    *OpenMode,
587     char                    **FullPathname)
588 {
589     FILE                    *IncludeFile;
590     ASL_INCLUDE_DIR         *NextDir;
591 
592 
593     /* Start the actual include file on the next line */
594 
595     AslGbl_CurrentLineOffset++;
596 
597     /* Attempt to open the include file */
598     /* If the file specifies an absolute path, just open it */
599 
600     if ((Filename[0] == '/')  ||
601         (Filename[0] == '\\') ||
602         (Filename[1] == ':'))
603     {
604         IncludeFile = PrOpenIncludeWithPrefix (
605             "", Filename, OpenMode, FullPathname);
606         if (!IncludeFile)
607         {
608             goto ErrorExit;
609         }
610         return (IncludeFile);
611     }
612 
613     /*
614      * The include filename is not an absolute path.
615      *
616      * First, search for the file within the "local" directory -- meaning
617      * the same directory that contains the source file.
618      *
619      * Construct the file pathname from the global directory name.
620      */
621     IncludeFile = PrOpenIncludeWithPrefix (
622         AslGbl_DirectoryPath, Filename, OpenMode, FullPathname);
623     if (IncludeFile)
624     {
625         return (IncludeFile);
626     }
627 
628     /*
629      * Second, search for the file within the (possibly multiple)
630      * directories specified by the -I option on the command line.
631      */
632     NextDir = AslGbl_IncludeDirList;
633     while (NextDir)
634     {
635         IncludeFile = PrOpenIncludeWithPrefix (
636             NextDir->Dir, Filename, OpenMode, FullPathname);
637         if (IncludeFile)
638         {
639             return (IncludeFile);
640         }
641 
642         NextDir = NextDir->Next;
643     }
644 
645     /* We could not open the include file after trying very hard */
646 
647 ErrorExit:
648     sprintf (AslGbl_MainTokenBuffer, "%s, %s", Filename, strerror (errno));
649     PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
650     return (NULL);
651 }
652 
653 
654 /*******************************************************************************
655  *
656  * FUNCTION:    FlOpenIncludeWithPrefix
657  *
658  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
659  *                                length string.
660  *              Filename        - The include filename from the source ASL.
661  *
662  * RETURN:      Valid file descriptor if successful. Null otherwise.
663  *
664  * DESCRIPTION: Open an include file and push it on the input file stack.
665  *
666  ******************************************************************************/
667 
668 FILE *
669 PrOpenIncludeWithPrefix (
670     char                    *PrefixDir,
671     char                    *Filename,
672     char                    *OpenMode,
673     char                    **FullPathname)
674 {
675     FILE                    *IncludeFile;
676     char                    *Pathname;
677 
678 
679     /* Build the full pathname to the file */
680 
681     Pathname = FlMergePathnames (PrefixDir, Filename);
682 
683     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
684         "Include: Opening file - \"%s\"\n",
685         AslGbl_CurrentLineNumber, Pathname);
686 
687     /* Attempt to open the file, push if successful */
688 
689     IncludeFile = fopen (Pathname, OpenMode);
690     if (!IncludeFile)
691     {
692         return (NULL);
693     }
694 
695     /* Push the include file on the open input file stack */
696 
697     PrPushInputFileStack (IncludeFile, Pathname);
698     *FullPathname = Pathname;
699     return (IncludeFile);
700 }
701 
702 
703 /*******************************************************************************
704  *
705  * FUNCTION:    AslPushInputFileStack
706  *
707  * PARAMETERS:  InputFile           - Open file pointer
708  *              Filename            - Name of the file
709  *
710  * RETURN:      None
711  *
712  * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
713  *              to this file. Called when an include file is successfully
714  *              opened.
715  *
716  ******************************************************************************/
717 
718 void
719 PrPushInputFileStack (
720     FILE                    *InputFile,
721     char                    *Filename)
722 {
723     PR_FILE_NODE            *Fnode;
724 
725 
726     AslGbl_HasIncludeFiles = TRUE;
727 
728     /* Save the current state in an Fnode */
729 
730     Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
731 
732     Fnode->File = AslGbl_Files[ASL_FILE_INPUT].Handle;
733     Fnode->Next = AslGbl_InputFileList;
734     Fnode->Filename = AslGbl_Files[ASL_FILE_INPUT].Filename;
735     Fnode->CurrentLineNumber = AslGbl_CurrentLineNumber;
736 
737     /* Push it on the stack */
738 
739     AslGbl_InputFileList = Fnode;
740 
741     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
742         "Push InputFile Stack: handle %p\n\n",
743         AslGbl_CurrentLineNumber, InputFile);
744 
745     /* Reset the global line count and filename */
746 
747     AslGbl_Files[ASL_FILE_INPUT].Filename =
748         UtLocalCacheCalloc (strlen (Filename) + 1);
749     strcpy (AslGbl_Files[ASL_FILE_INPUT].Filename, Filename);
750 
751     AslGbl_Files[ASL_FILE_INPUT].Handle = InputFile;
752     AslGbl_CurrentLineNumber = 1;
753 
754     /* Emit a new #line directive for the include file */
755 
756     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
757 }
758 
759 
760 /*******************************************************************************
761  *
762  * FUNCTION:    AslPopInputFileStack
763  *
764  * PARAMETERS:  None
765  *
766  * RETURN:      0 if a node was popped, -1 otherwise
767  *
768  * DESCRIPTION: Pop the top of the input file stack and point the parser to
769  *              the saved parse buffer contained in the fnode. Also, set the
770  *              global line counters to the saved values. This function is
771  *              called when an include file reaches EOF.
772  *
773  ******************************************************************************/
774 
775 BOOLEAN
776 PrPopInputFileStack (
777     void)
778 {
779     PR_FILE_NODE            *Fnode;
780 
781 
782     Fnode = AslGbl_InputFileList;
783     DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
784         "Pop InputFile Stack, Fnode %p\n\n",
785         AslGbl_CurrentLineNumber, Fnode);
786 
787     if (!Fnode)
788     {
789         return (FALSE);
790     }
791 
792     /* Close the current include file */
793 
794     fclose (AslGbl_Files[ASL_FILE_INPUT].Handle);
795 
796     /* Update the top-of-stack */
797 
798     AslGbl_InputFileList = Fnode->Next;
799 
800     /* Reset global line counter and filename */
801 
802     AslGbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
803     AslGbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
804     AslGbl_CurrentLineNumber = Fnode->CurrentLineNumber;
805 
806     /* Emit a new #line directive after the include file */
807 
808     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
809         AslGbl_CurrentLineNumber, Fnode->Filename);
810 
811     /* All done with this node */
812 
813     ACPI_FREE (Fnode);
814     return (TRUE);
815 }
816