1 /******************************************************************************
3 * Module Name: prutils - Preprocessor utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "aslcompiler.h"
45 #include "dtcompiler.h"
48 #define _COMPONENT ASL_PREPROCESSOR
49 ACPI_MODULE_NAME ("prutils")
52 /******************************************************************************
54 * FUNCTION: PrGetNextToken
56 * PARAMETERS: Buffer - Current line buffer
57 * MatchString - String with valid token delimiters
58 * Next - Set to next possible token in buffer
60 * RETURN: Next token (null-terminated). Modifies the input line.
61 * Remainder of line is stored in *Next.
63 * DESCRIPTION: Local implementation of strtok() with local storage for the
64 * next pointer. Not only thread-safe, but allows multiple
65 * parsing of substrings such as expressions.
67 *****************************************************************************/
80 /* Use Next if it is valid */
89 /* Skip any leading delimiters */
93 if (strchr (MatchString, *Buffer))
103 /* Anything left on the line? */
113 /* Find the end of this token */
117 if (strchr (MatchString, *Buffer))
137 /*******************************************************************************
141 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
142 * MessageId - Index into global message buffer
143 * Column - Column in current line
147 * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
149 ******************************************************************************/
158 AcpiOsPrintf ("%s (%u) : %s", Gbl_Files[ASL_FILE_INPUT].Filename,
159 Gbl_CurrentLineNumber, Gbl_CurrentLineBuffer);
168 /* TBD: Need Logical line number? */
170 AslCommonError2 (Level, MessageId,
171 Gbl_CurrentLineNumber, Column,
172 Gbl_CurrentLineBuffer,
173 Gbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
175 Gbl_PreprocessorError = TRUE;
179 /*******************************************************************************
181 * FUNCTION: PrReplaceData
183 * PARAMETERS: Buffer - Original(target) buffer pointer
184 * LengthToRemove - Length to be removed from target buffer
185 * BufferToAdd - Data to be inserted into target buffer
186 * LengthToAdd - Length of BufferToAdd
190 * DESCRIPTION: Generic buffer data replacement.
192 ******************************************************************************/
197 UINT32 LengthToRemove,
204 /* Buffer is a string, so the length must include the terminating zero */
206 BufferLength = strlen (Buffer) + 1;
208 if (LengthToRemove != LengthToAdd)
211 * Move some of the existing data
212 * 1) If adding more bytes than removing, make room for the new data
213 * 2) if removing more bytes than adding, delete the extra space
215 if (LengthToRemove > 0)
217 memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
218 (BufferLength - LengthToRemove));
222 /* Now we can move in the new data */
226 memmove (Buffer, BufferToAdd, LengthToAdd);
231 /*******************************************************************************
233 * FUNCTION: PrOpenIncludeFile
235 * PARAMETERS: Filename - Filename or pathname for include file
239 * DESCRIPTION: Open an include file and push it on the input file stack.
241 ******************************************************************************/
250 ASL_INCLUDE_DIR *NextDir;
253 /* Start the actual include file on the next line */
255 Gbl_CurrentLineOffset++;
257 /* Attempt to open the include file */
258 /* If the file specifies an absolute path, just open it */
260 if ((Filename[0] == '/') ||
261 (Filename[0] == '\\') ||
262 (Filename[1] == ':'))
264 IncludeFile = PrOpenIncludeWithPrefix (
265 "", Filename, OpenMode, FullPathname);
270 return (IncludeFile);
274 * The include filename is not an absolute path.
276 * First, search for the file within the "local" directory -- meaning
277 * the same directory that contains the source file.
279 * Construct the file pathname from the global directory name.
281 IncludeFile = PrOpenIncludeWithPrefix (
282 Gbl_DirectoryPath, Filename, OpenMode, FullPathname);
285 return (IncludeFile);
289 * Second, search for the file within the (possibly multiple)
290 * directories specified by the -I option on the command line.
292 NextDir = Gbl_IncludeDirList;
295 IncludeFile = PrOpenIncludeWithPrefix (
296 NextDir->Dir, Filename, OpenMode, FullPathname);
299 return (IncludeFile);
302 NextDir = NextDir->Next;
305 /* We could not open the include file after trying very hard */
308 sprintf (Gbl_MainTokenBuffer, "%s, %s", Filename, strerror (errno));
309 PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
314 /*******************************************************************************
316 * FUNCTION: FlOpenIncludeWithPrefix
318 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
320 * Filename - The include filename from the source ASL.
322 * RETURN: Valid file descriptor if successful. Null otherwise.
324 * DESCRIPTION: Open an include file and push it on the input file stack.
326 ******************************************************************************/
329 PrOpenIncludeWithPrefix (
339 /* Build the full pathname to the file */
341 Pathname = FlMergePathnames (PrefixDir, Filename);
343 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
344 "Include: Opening file - \"%s\"\n",
345 Gbl_CurrentLineNumber, Pathname);
347 /* Attempt to open the file, push if successful */
349 IncludeFile = fopen (Pathname, OpenMode);
352 fprintf (stderr, "Could not open include file %s\n", Pathname);
356 /* Push the include file on the open input file stack */
358 PrPushInputFileStack (IncludeFile, Pathname);
359 *FullPathname = Pathname;
360 return (IncludeFile);
364 /*******************************************************************************
366 * FUNCTION: AslPushInputFileStack
368 * PARAMETERS: InputFile - Open file pointer
369 * Filename - Name of the file
373 * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
374 * to this file. Called when an include file is successfully
377 ******************************************************************************/
380 PrPushInputFileStack (
387 Gbl_HasIncludeFiles = TRUE;
389 /* Save the current state in an Fnode */
391 Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
393 Fnode->File = Gbl_Files[ASL_FILE_INPUT].Handle;
394 Fnode->Next = Gbl_InputFileList;
395 Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
396 Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
398 /* Push it on the stack */
400 Gbl_InputFileList = Fnode;
402 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
403 "Push InputFile Stack: handle %p\n\n",
404 Gbl_CurrentLineNumber, InputFile);
406 /* Reset the global line count and filename */
408 Gbl_Files[ASL_FILE_INPUT].Filename =
409 UtStringCacheCalloc (strlen (Filename) + 1);
410 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
412 Gbl_Files[ASL_FILE_INPUT].Handle = InputFile;
413 Gbl_CurrentLineNumber = 1;
415 /* Emit a new #line directive for the include file */
417 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
421 /*******************************************************************************
423 * FUNCTION: AslPopInputFileStack
427 * RETURN: 0 if a node was popped, -1 otherwise
429 * DESCRIPTION: Pop the top of the input file stack and point the parser to
430 * the saved parse buffer contained in the fnode. Also, set the
431 * global line counters to the saved values. This function is
432 * called when an include file reaches EOF.
434 ******************************************************************************/
437 PrPopInputFileStack (
443 Fnode = Gbl_InputFileList;
444 DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
445 "Pop InputFile Stack, Fnode %p\n\n",
446 Gbl_CurrentLineNumber, Fnode);
453 /* Close the current include file */
455 fclose (Gbl_Files[ASL_FILE_INPUT].Handle);
457 /* Update the top-of-stack */
459 Gbl_InputFileList = Fnode->Next;
461 /* Reset global line counter and filename */
463 Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
464 Gbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
465 Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
467 /* Emit a new #line directive after the include file */
469 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
470 Gbl_CurrentLineNumber, Fnode->Filename);
472 /* All done with this node */