prutils.c revision 1.1.1.15 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: prutils - Preprocessor utilities
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1.1.14 christos * Copyright (C) 2000 - 2023, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1.1.12 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "aslcompiler.h"
45 1.1 christos
46 1.1 christos #define _COMPONENT ASL_PREPROCESSOR
47 1.1 christos ACPI_MODULE_NAME ("prutils")
48 1.1 christos
49 1.1 christos
50 1.1 christos /******************************************************************************
51 1.1 christos *
52 1.1 christos * FUNCTION: PrGetNextToken
53 1.1 christos *
54 1.1 christos * PARAMETERS: Buffer - Current line buffer
55 1.1 christos * MatchString - String with valid token delimiters
56 1.1 christos * Next - Set to next possible token in buffer
57 1.1 christos *
58 1.1 christos * RETURN: Next token (null-terminated). Modifies the input line.
59 1.1 christos * Remainder of line is stored in *Next.
60 1.1 christos *
61 1.1 christos * DESCRIPTION: Local implementation of strtok() with local storage for the
62 1.1 christos * next pointer. Not only thread-safe, but allows multiple
63 1.1 christos * parsing of substrings such as expressions.
64 1.1 christos *
65 1.1 christos *****************************************************************************/
66 1.1 christos
67 1.1 christos char *
68 1.1 christos PrGetNextToken (
69 1.1 christos char *Buffer,
70 1.1 christos char *MatchString,
71 1.1 christos char **Next)
72 1.1 christos {
73 1.1 christos char *TokenStart;
74 1.1 christos
75 1.1 christos
76 1.1 christos if (!Buffer)
77 1.1 christos {
78 1.1 christos /* Use Next if it is valid */
79 1.1 christos
80 1.1 christos Buffer = *Next;
81 1.1 christos if (!(*Next))
82 1.1 christos {
83 1.1 christos return (NULL);
84 1.1 christos }
85 1.1 christos }
86 1.1 christos
87 1.1 christos /* Skip any leading delimiters */
88 1.1 christos
89 1.1 christos while (*Buffer)
90 1.1 christos {
91 1.1 christos if (strchr (MatchString, *Buffer))
92 1.1 christos {
93 1.1 christos Buffer++;
94 1.1 christos }
95 1.1 christos else
96 1.1 christos {
97 1.1 christos break;
98 1.1 christos }
99 1.1 christos }
100 1.1 christos
101 1.1 christos /* Anything left on the line? */
102 1.1 christos
103 1.1 christos if (!(*Buffer))
104 1.1 christos {
105 1.1 christos *Next = NULL;
106 1.1 christos return (NULL);
107 1.1 christos }
108 1.1 christos
109 1.1 christos TokenStart = Buffer;
110 1.1 christos
111 1.1 christos /* Find the end of this token */
112 1.1 christos
113 1.1 christos while (*Buffer)
114 1.1 christos {
115 1.1 christos if (strchr (MatchString, *Buffer))
116 1.1 christos {
117 1.1 christos *Buffer = 0;
118 1.1 christos *Next = Buffer+1;
119 1.1 christos if (!**Next)
120 1.1 christos {
121 1.1 christos *Next = NULL;
122 1.1 christos }
123 1.1.1.5 christos
124 1.1 christos return (TokenStart);
125 1.1 christos }
126 1.1.1.5 christos
127 1.1 christos Buffer++;
128 1.1 christos }
129 1.1 christos
130 1.1 christos *Next = NULL;
131 1.1 christos return (TokenStart);
132 1.1 christos }
133 1.1 christos
134 1.1 christos
135 1.1 christos /*******************************************************************************
136 1.1 christos *
137 1.1 christos * FUNCTION: PrError
138 1.1 christos *
139 1.1 christos * PARAMETERS: Level - Seriousness (Warning/error, etc.)
140 1.1 christos * MessageId - Index into global message buffer
141 1.1 christos * Column - Column in current line
142 1.1 christos *
143 1.1 christos * RETURN: None
144 1.1 christos *
145 1.1 christos * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
146 1.1 christos *
147 1.1 christos ******************************************************************************/
148 1.1 christos
149 1.1 christos void
150 1.1 christos PrError (
151 1.1 christos UINT8 Level,
152 1.1.1.2 christos UINT16 MessageId,
153 1.1 christos UINT32 Column)
154 1.1 christos {
155 1.1 christos #if 0
156 1.1.1.9 christos AcpiOsPrintf ("%s (%u) : %s", AslGbl_Files[ASL_FILE_INPUT].Filename,
157 1.1.1.9 christos AslGbl_CurrentLineNumber, AslGbl_CurrentLineBuffer);
158 1.1 christos #endif
159 1.1 christos
160 1.1 christos
161 1.1 christos if (Column > 120)
162 1.1 christos {
163 1.1 christos Column = 0;
164 1.1 christos }
165 1.1 christos
166 1.1 christos /* TBD: Need Logical line number? */
167 1.1 christos
168 1.1 christos AslCommonError2 (Level, MessageId,
169 1.1.1.9 christos AslGbl_CurrentLineNumber, Column,
170 1.1.1.9 christos AslGbl_CurrentLineBuffer,
171 1.1.1.9 christos AslGbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
172 1.1 christos
173 1.1.1.9 christos AslGbl_PreprocessorError = TRUE;
174 1.1 christos }
175 1.1 christos
176 1.1 christos
177 1.1 christos /*******************************************************************************
178 1.1 christos *
179 1.1.1.14 christos * FUNCTION: PrReplaceResizeSubstring
180 1.1.1.14 christos *
181 1.1.1.14 christos * PARAMETERS: Args - Struct containing name, offset & usecount
182 1.1.1.14 christos * Diff1 - Difference in lengths when new < old
183 1.1.1.14 christos * Diff2 - Difference in lengths when new > old
184 1.1.1.14 christos * i - The curr. no. of iteration of replacement
185 1.1.1.14 christos * Token - Substring that replaces Args->Name
186 1.1.1.14 christos *
187 1.1.1.14 christos * RETURN: None
188 1.1.1.14 christos *
189 1.1.1.14 christos * DESCRIPTION: Advanced substring replacement in a string using resized buffer.
190 1.1.1.14 christos *
191 1.1.1.14 christos ******************************************************************************/
192 1.1.1.14 christos
193 1.1.1.14 christos void
194 1.1.1.14 christos PrReplaceResizeSubstring(
195 1.1.1.14 christos PR_MACRO_ARG *Args,
196 1.1.1.14 christos UINT32 Diff1,
197 1.1.1.14 christos UINT32 Diff2,
198 1.1.1.14 christos UINT32 i,
199 1.1.1.14 christos char *Token)
200 1.1.1.14 christos {
201 1.1.1.14 christos UINT32 b, PrevOffset;
202 1.1.1.14 christos char *temp;
203 1.1.1.14 christos char macro_sep[64];
204 1.1.1.14 christos
205 1.1.1.14 christos
206 1.1.1.14 christos AslGbl_MacroTokenReplaceBuffer = (char *) realloc (AslGbl_MacroTokenReplaceBuffer,
207 1.1.1.14 christos (2 * (strlen (AslGbl_MacroTokenBuffer))));
208 1.1.1.14 christos
209 1.1.1.14 christos strcpy (macro_sep, "~,() {}!*/%+-<>=&^|\"\t\n");
210 1.1.1.14 christos
211 1.1.1.14 christos /*
212 1.1.1.14 christos * When the replacement argument (during invocation) length
213 1.1.1.14 christos * < replaced parameter (in the macro function definition
214 1.1.1.14 christos * and its expansion) length
215 1.1.1.14 christos */
216 1.1.1.14 christos if (Diff1 != 0)
217 1.1.1.14 christos {
218 1.1.1.14 christos /*
219 1.1.1.14 christos * We save the offset value to reset it after replacing each
220 1.1.1.14 christos * instance of each arg and setting the offset value to
221 1.1.1.14 christos * the start of the arg to be replaced since it changes
222 1.1.1.14 christos * with each iteration when arg length != token length
223 1.1.1.14 christos */
224 1.1.1.14 christos PrevOffset = Args->Offset[i];
225 1.1.1.14 christos temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
226 1.1.1.15 christos if (temp == NULL)
227 1.1.1.15 christos {
228 1.1.1.15 christos return;
229 1.1.1.15 christos }
230 1.1.1.14 christos
231 1.1.1.14 christos ResetHere1:
232 1.1.1.14 christos temp = strstr (temp, Args->Name);
233 1.1.1.15 christos if (temp == NULL)
234 1.1.1.15 christos {
235 1.1.1.15 christos return;
236 1.1.1.15 christos }
237 1.1.1.14 christos Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
238 1.1.1.14 christos strlen (temp);
239 1.1.1.14 christos if (Args->Offset[i] == 0)
240 1.1.1.14 christos {
241 1.1.1.14 christos goto JumpHere1;
242 1.1.1.14 christos }
243 1.1.1.14 christos if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
244 1.1.1.14 christos (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
245 1.1.1.14 christos {
246 1.1.1.14 christos Args->Offset[i] += 0;
247 1.1.1.14 christos }
248 1.1.1.14 christos else
249 1.1.1.14 christos {
250 1.1.1.14 christos temp += strlen (Args->Name);
251 1.1.1.14 christos goto ResetHere1;
252 1.1.1.14 christos }
253 1.1.1.14 christos
254 1.1.1.14 christos /*
255 1.1.1.14 christos * For now, we simply set the extra char positions (generated
256 1.1.1.14 christos * due to longer name replaced by shorter name) to whitespace
257 1.1.1.14 christos * chars so it will be ignored during compilation
258 1.1.1.14 christos */
259 1.1.1.14 christos JumpHere1:
260 1.1.1.14 christos b = strlen (Token) + Args->Offset[i];
261 1.1.1.14 christos memset (&AslGbl_MacroTokenBuffer[b], ' ', Diff1);
262 1.1.1.14 christos
263 1.1.1.14 christos # if 0
264 1.1.1.14 christos
265 1.1.1.14 christos /* Work in progress as of 03/08/2023 - experimental 'if' block
266 1.1.1.14 christos * to test code for removing extra whitespaces from the macro
267 1.1.1.14 christos * replacement when replacement arg < replaced param
268 1.1.1.14 christos */
269 1.1.1.14 christos char Buff[8192];
270 1.1.1.14 christos /* char* Replace; */
271 1.1.1.14 christos /* Replace = Buff; */
272 1.1.1.14 christos
273 1.1.1.14 christos for (j = 0; j < strlen (AslGbl_MacroTokenBuffer); j++)
274 1.1.1.14 christos {
275 1.1.1.14 christos Buff[j] = AslGbl_MacroTokenBuffer[j];
276 1.1.1.14 christos }
277 1.1.1.14 christos Buff[strlen (AslGbl_MacroTokenBuffer)] = '\0';
278 1.1.1.15 christos /* fprintf(stderr, "Buff: %s\n", Buff); */
279 1.1.1.14 christos
280 1.1.1.14 christos UINT32 len = strlen (Buff);
281 1.1.1.14 christos
282 1.1.1.14 christos for (j = 0; j < len; j++)
283 1.1.1.14 christos {
284 1.1.1.14 christos if (Buff[0] == ' ')
285 1.1.1.14 christos {
286 1.1.1.14 christos for (j = 0; j < (len - 1); j++)
287 1.1.1.14 christos {
288 1.1.1.14 christos Buff[j] = Buff[j + 1];
289 1.1.1.14 christos }
290 1.1.1.14 christos Buff[j] = '\0';
291 1.1.1.14 christos len--;
292 1.1.1.14 christos j = -1;
293 1.1.1.14 christos continue;
294 1.1.1.14 christos }
295 1.1.1.14 christos
296 1.1.1.14 christos if (Buff[j] == ' ' && Buff[j + 1] == ' ')
297 1.1.1.14 christos {
298 1.1.1.14 christos for (k = 0; k < (len - 1); k++)
299 1.1.1.14 christos {
300 1.1.1.14 christos Buff[j] = Buff[j + 1];
301 1.1.1.14 christos }
302 1.1.1.14 christos Buff[j] = '\0';
303 1.1.1.14 christos len--;
304 1.1.1.14 christos j--;
305 1.1.1.14 christos }
306 1.1.1.14 christos }
307 1.1.1.15 christos /* fprintf(stderr, "Buff: %s\n", Buff); */
308 1.1.1.14 christos
309 1.1.1.14 christos for (k = 0; k < strlen (Buff); k++)
310 1.1.1.14 christos {
311 1.1.1.14 christos AslGbl_MacroTokenBuffer[k] = Buff[k];
312 1.1.1.14 christos }
313 1.1.1.14 christos #endif
314 1.1.1.14 christos
315 1.1.1.14 christos PrReplaceData (
316 1.1.1.14 christos &AslGbl_MacroTokenBuffer[Args->Offset[i]],
317 1.1.1.14 christos strlen (Token), Token, strlen (Token));
318 1.1.1.14 christos
319 1.1.1.14 christos temp = NULL;
320 1.1.1.14 christos Args->Offset[i] = PrevOffset;
321 1.1.1.14 christos }
322 1.1.1.14 christos
323 1.1.1.14 christos /*
324 1.1.1.14 christos * When the replacement argument (during invocation) length
325 1.1.1.14 christos * > replaced parameter (in the macro function definition
326 1.1.1.14 christos * and its expansion) length
327 1.1.1.14 christos */
328 1.1.1.14 christos else if (Diff2 != 0)
329 1.1.1.14 christos {
330 1.1.1.14 christos /* Doing the same thing with offset value as for prev case */
331 1.1.1.14 christos
332 1.1.1.14 christos PrevOffset = Args->Offset[i];
333 1.1.1.14 christos temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
334 1.1.1.15 christos if (temp == NULL)
335 1.1.1.15 christos {
336 1.1.1.15 christos return;
337 1.1.1.15 christos }
338 1.1.1.14 christos
339 1.1.1.14 christos ResetHere2:
340 1.1.1.14 christos temp = strstr (temp, Args->Name);
341 1.1.1.15 christos if (temp == NULL)
342 1.1.1.15 christos {
343 1.1.1.15 christos return;
344 1.1.1.15 christos }
345 1.1.1.14 christos Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
346 1.1.1.14 christos strlen (temp);
347 1.1.1.14 christos if (Args->Offset[i] == 0)
348 1.1.1.14 christos {
349 1.1.1.14 christos goto JumpHere2;
350 1.1.1.14 christos }
351 1.1.1.14 christos if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
352 1.1.1.14 christos (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
353 1.1.1.14 christos {
354 1.1.1.14 christos Args->Offset[i] += 0;
355 1.1.1.14 christos }
356 1.1.1.14 christos else
357 1.1.1.14 christos {
358 1.1.1.14 christos temp+= strlen (Args->Name);
359 1.1.1.14 christos goto ResetHere2;
360 1.1.1.14 christos }
361 1.1.1.14 christos
362 1.1.1.14 christos /*
363 1.1.1.14 christos * We will need to allocate some extra space in our buffer to
364 1.1.1.14 christos * accommodate the increase in the replacement string length
365 1.1.1.14 christos * over the shorter outgoing arg string and do the replacement
366 1.1.1.14 christos * at the correct offset value which is resetted every iteration
367 1.1.1.14 christos */
368 1.1.1.14 christos JumpHere2:
369 1.1.1.14 christos strncpy (AslGbl_MacroTokenReplaceBuffer, AslGbl_MacroTokenBuffer, Args->Offset[i]);
370 1.1.1.14 christos strcat (AslGbl_MacroTokenReplaceBuffer, Token);
371 1.1.1.14 christos strcat (AslGbl_MacroTokenReplaceBuffer, (AslGbl_MacroTokenBuffer +
372 1.1.1.14 christos (Args->Offset[i] + strlen (Args->Name))));
373 1.1.1.14 christos
374 1.1.1.14 christos strcpy (AslGbl_MacroTokenBuffer, AslGbl_MacroTokenReplaceBuffer);
375 1.1.1.14 christos
376 1.1.1.14 christos temp = NULL;
377 1.1.1.14 christos Args->Offset[i] = PrevOffset;
378 1.1.1.14 christos }
379 1.1.1.14 christos
380 1.1.1.14 christos /*
381 1.1.1.14 christos * When the replacement argument (during invocation) length =
382 1.1.1.14 christos * replaced parameter (in the macro function definition and
383 1.1.1.14 christos * its expansion) length
384 1.1.1.14 christos */
385 1.1.1.14 christos else
386 1.1.1.14 christos {
387 1.1.1.14 christos
388 1.1.1.14 christos /*
389 1.1.1.14 christos * We still need to reset the offset for each iteration even when
390 1.1.1.14 christos * arg and param lengths are same since any macro func invocation
391 1.1.1.14 christos * could use various cases for each separate arg-param pair
392 1.1.1.14 christos */
393 1.1.1.14 christos PrevOffset = Args->Offset[i];
394 1.1.1.14 christos temp = strstr (AslGbl_MacroTokenBuffer, Args->Name);
395 1.1.1.15 christos if (temp == NULL)
396 1.1.1.15 christos {
397 1.1.1.15 christos return;
398 1.1.1.15 christos }
399 1.1.1.14 christos
400 1.1.1.14 christos ResetHere3:
401 1.1.1.14 christos temp = strstr (temp, Args->Name);
402 1.1.1.15 christos if (temp == NULL)
403 1.1.1.15 christos {
404 1.1.1.15 christos return;
405 1.1.1.15 christos }
406 1.1.1.14 christos Args->Offset[i] = strlen (AslGbl_MacroTokenBuffer) -
407 1.1.1.14 christos strlen (temp);
408 1.1.1.14 christos if (Args->Offset[i] == 0)
409 1.1.1.14 christos {
410 1.1.1.14 christos goto JumpHere3;
411 1.1.1.14 christos }
412 1.1.1.14 christos if ((strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] - 1)])) &&
413 1.1.1.14 christos (strchr (macro_sep, AslGbl_MacroTokenBuffer[(Args->Offset[i] + strlen (Args->Name))])))
414 1.1.1.14 christos {
415 1.1.1.14 christos Args->Offset[i] += 0;
416 1.1.1.14 christos }
417 1.1.1.14 christos else
418 1.1.1.14 christos {
419 1.1.1.14 christos temp += strlen (Args->Name);
420 1.1.1.14 christos goto ResetHere3;
421 1.1.1.14 christos }
422 1.1.1.14 christos
423 1.1.1.14 christos JumpHere3:
424 1.1.1.14 christos PrReplaceData (
425 1.1.1.14 christos &AslGbl_MacroTokenBuffer[Args->Offset[i]],
426 1.1.1.14 christos strlen (Args->Name), Token, strlen (Token));
427 1.1.1.14 christos temp = NULL;
428 1.1.1.14 christos Args->Offset[i] = PrevOffset;
429 1.1.1.14 christos }
430 1.1.1.14 christos }
431 1.1.1.14 christos
432 1.1.1.14 christos
433 1.1.1.14 christos /*******************************************************************************
434 1.1.1.14 christos *
435 1.1 christos * FUNCTION: PrReplaceData
436 1.1 christos *
437 1.1 christos * PARAMETERS: Buffer - Original(target) buffer pointer
438 1.1 christos * LengthToRemove - Length to be removed from target buffer
439 1.1 christos * BufferToAdd - Data to be inserted into target buffer
440 1.1 christos * LengthToAdd - Length of BufferToAdd
441 1.1 christos *
442 1.1.1.14 christos * RETURN: Pointer to where the buffer is replaced with data
443 1.1 christos *
444 1.1 christos * DESCRIPTION: Generic buffer data replacement.
445 1.1 christos *
446 1.1 christos ******************************************************************************/
447 1.1 christos
448 1.1.1.14 christos char *
449 1.1 christos PrReplaceData (
450 1.1 christos char *Buffer,
451 1.1 christos UINT32 LengthToRemove,
452 1.1 christos char *BufferToAdd,
453 1.1 christos UINT32 LengthToAdd)
454 1.1 christos {
455 1.1 christos UINT32 BufferLength;
456 1.1 christos
457 1.1 christos
458 1.1 christos /* Buffer is a string, so the length must include the terminating zero */
459 1.1 christos
460 1.1 christos BufferLength = strlen (Buffer) + 1;
461 1.1 christos
462 1.1 christos if (LengthToRemove != LengthToAdd)
463 1.1 christos {
464 1.1 christos /*
465 1.1 christos * Move some of the existing data
466 1.1 christos * 1) If adding more bytes than removing, make room for the new data
467 1.1 christos * 2) if removing more bytes than adding, delete the extra space
468 1.1 christos */
469 1.1 christos if (LengthToRemove > 0)
470 1.1 christos {
471 1.1 christos memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
472 1.1 christos (BufferLength - LengthToRemove));
473 1.1 christos }
474 1.1 christos }
475 1.1 christos
476 1.1.1.14 christos
477 1.1 christos /* Now we can move in the new data */
478 1.1 christos
479 1.1 christos if (LengthToAdd > 0)
480 1.1 christos {
481 1.1 christos memmove (Buffer, BufferToAdd, LengthToAdd);
482 1.1 christos }
483 1.1.1.14 christos return (Buffer + LengthToAdd);
484 1.1 christos }
485 1.1 christos
486 1.1 christos
487 1.1 christos /*******************************************************************************
488 1.1 christos *
489 1.1 christos * FUNCTION: PrOpenIncludeFile
490 1.1 christos *
491 1.1 christos * PARAMETERS: Filename - Filename or pathname for include file
492 1.1 christos *
493 1.1 christos * RETURN: None.
494 1.1 christos *
495 1.1 christos * DESCRIPTION: Open an include file and push it on the input file stack.
496 1.1 christos *
497 1.1 christos ******************************************************************************/
498 1.1 christos
499 1.1.1.4 christos FILE *
500 1.1 christos PrOpenIncludeFile (
501 1.1.1.4 christos char *Filename,
502 1.1.1.4 christos char *OpenMode,
503 1.1.1.4 christos char **FullPathname)
504 1.1 christos {
505 1.1 christos FILE *IncludeFile;
506 1.1 christos ASL_INCLUDE_DIR *NextDir;
507 1.1 christos
508 1.1 christos
509 1.1 christos /* Start the actual include file on the next line */
510 1.1 christos
511 1.1.1.9 christos AslGbl_CurrentLineOffset++;
512 1.1 christos
513 1.1 christos /* Attempt to open the include file */
514 1.1 christos /* If the file specifies an absolute path, just open it */
515 1.1 christos
516 1.1 christos if ((Filename[0] == '/') ||
517 1.1 christos (Filename[0] == '\\') ||
518 1.1 christos (Filename[1] == ':'))
519 1.1 christos {
520 1.1.1.4 christos IncludeFile = PrOpenIncludeWithPrefix (
521 1.1.1.4 christos "", Filename, OpenMode, FullPathname);
522 1.1 christos if (!IncludeFile)
523 1.1 christos {
524 1.1 christos goto ErrorExit;
525 1.1 christos }
526 1.1.1.4 christos return (IncludeFile);
527 1.1 christos }
528 1.1 christos
529 1.1 christos /*
530 1.1 christos * The include filename is not an absolute path.
531 1.1 christos *
532 1.1 christos * First, search for the file within the "local" directory -- meaning
533 1.1 christos * the same directory that contains the source file.
534 1.1 christos *
535 1.1 christos * Construct the file pathname from the global directory name.
536 1.1 christos */
537 1.1.1.4 christos IncludeFile = PrOpenIncludeWithPrefix (
538 1.1.1.9 christos AslGbl_DirectoryPath, Filename, OpenMode, FullPathname);
539 1.1 christos if (IncludeFile)
540 1.1 christos {
541 1.1.1.4 christos return (IncludeFile);
542 1.1 christos }
543 1.1 christos
544 1.1 christos /*
545 1.1 christos * Second, search for the file within the (possibly multiple)
546 1.1 christos * directories specified by the -I option on the command line.
547 1.1 christos */
548 1.1.1.9 christos NextDir = AslGbl_IncludeDirList;
549 1.1 christos while (NextDir)
550 1.1 christos {
551 1.1.1.4 christos IncludeFile = PrOpenIncludeWithPrefix (
552 1.1.1.4 christos NextDir->Dir, Filename, OpenMode, FullPathname);
553 1.1 christos if (IncludeFile)
554 1.1 christos {
555 1.1.1.4 christos return (IncludeFile);
556 1.1 christos }
557 1.1 christos
558 1.1 christos NextDir = NextDir->Next;
559 1.1 christos }
560 1.1 christos
561 1.1 christos /* We could not open the include file after trying very hard */
562 1.1 christos
563 1.1 christos ErrorExit:
564 1.1.1.9 christos sprintf (AslGbl_MainTokenBuffer, "%s, %s", Filename, strerror (errno));
565 1.1 christos PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
566 1.1.1.4 christos return (NULL);
567 1.1 christos }
568 1.1 christos
569 1.1 christos
570 1.1 christos /*******************************************************************************
571 1.1 christos *
572 1.1 christos * FUNCTION: FlOpenIncludeWithPrefix
573 1.1 christos *
574 1.1 christos * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
575 1.1 christos * length string.
576 1.1 christos * Filename - The include filename from the source ASL.
577 1.1 christos *
578 1.1 christos * RETURN: Valid file descriptor if successful. Null otherwise.
579 1.1 christos *
580 1.1 christos * DESCRIPTION: Open an include file and push it on the input file stack.
581 1.1 christos *
582 1.1 christos ******************************************************************************/
583 1.1 christos
584 1.1 christos FILE *
585 1.1 christos PrOpenIncludeWithPrefix (
586 1.1 christos char *PrefixDir,
587 1.1.1.4 christos char *Filename,
588 1.1.1.4 christos char *OpenMode,
589 1.1.1.4 christos char **FullPathname)
590 1.1 christos {
591 1.1 christos FILE *IncludeFile;
592 1.1 christos char *Pathname;
593 1.1 christos
594 1.1 christos
595 1.1 christos /* Build the full pathname to the file */
596 1.1 christos
597 1.1 christos Pathname = FlMergePathnames (PrefixDir, Filename);
598 1.1 christos
599 1.1 christos DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
600 1.1 christos "Include: Opening file - \"%s\"\n",
601 1.1.1.9 christos AslGbl_CurrentLineNumber, Pathname);
602 1.1 christos
603 1.1 christos /* Attempt to open the file, push if successful */
604 1.1 christos
605 1.1.1.4 christos IncludeFile = fopen (Pathname, OpenMode);
606 1.1 christos if (!IncludeFile)
607 1.1 christos {
608 1.1 christos return (NULL);
609 1.1 christos }
610 1.1 christos
611 1.1 christos /* Push the include file on the open input file stack */
612 1.1 christos
613 1.1 christos PrPushInputFileStack (IncludeFile, Pathname);
614 1.1.1.4 christos *FullPathname = Pathname;
615 1.1 christos return (IncludeFile);
616 1.1 christos }
617 1.1 christos
618 1.1 christos
619 1.1 christos /*******************************************************************************
620 1.1 christos *
621 1.1 christos * FUNCTION: AslPushInputFileStack
622 1.1 christos *
623 1.1 christos * PARAMETERS: InputFile - Open file pointer
624 1.1 christos * Filename - Name of the file
625 1.1 christos *
626 1.1 christos * RETURN: None
627 1.1 christos *
628 1.1 christos * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
629 1.1 christos * to this file. Called when an include file is successfully
630 1.1 christos * opened.
631 1.1 christos *
632 1.1 christos ******************************************************************************/
633 1.1 christos
634 1.1 christos void
635 1.1 christos PrPushInputFileStack (
636 1.1 christos FILE *InputFile,
637 1.1 christos char *Filename)
638 1.1 christos {
639 1.1 christos PR_FILE_NODE *Fnode;
640 1.1 christos
641 1.1 christos
642 1.1.1.9 christos AslGbl_HasIncludeFiles = TRUE;
643 1.1.1.4 christos
644 1.1 christos /* Save the current state in an Fnode */
645 1.1 christos
646 1.1 christos Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
647 1.1 christos
648 1.1.1.9 christos Fnode->File = AslGbl_Files[ASL_FILE_INPUT].Handle;
649 1.1.1.9 christos Fnode->Next = AslGbl_InputFileList;
650 1.1.1.9 christos Fnode->Filename = AslGbl_Files[ASL_FILE_INPUT].Filename;
651 1.1.1.9 christos Fnode->CurrentLineNumber = AslGbl_CurrentLineNumber;
652 1.1 christos
653 1.1 christos /* Push it on the stack */
654 1.1 christos
655 1.1.1.9 christos AslGbl_InputFileList = Fnode;
656 1.1 christos
657 1.1 christos DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
658 1.1 christos "Push InputFile Stack: handle %p\n\n",
659 1.1.1.9 christos AslGbl_CurrentLineNumber, InputFile);
660 1.1 christos
661 1.1 christos /* Reset the global line count and filename */
662 1.1 christos
663 1.1.1.9 christos AslGbl_Files[ASL_FILE_INPUT].Filename =
664 1.1.1.7 christos UtLocalCacheCalloc (strlen (Filename) + 1);
665 1.1.1.9 christos strcpy (AslGbl_Files[ASL_FILE_INPUT].Filename, Filename);
666 1.1.1.2 christos
667 1.1.1.9 christos AslGbl_Files[ASL_FILE_INPUT].Handle = InputFile;
668 1.1.1.9 christos AslGbl_CurrentLineNumber = 1;
669 1.1 christos
670 1.1 christos /* Emit a new #line directive for the include file */
671 1.1 christos
672 1.1.1.2 christos FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
673 1.1 christos }
674 1.1 christos
675 1.1 christos
676 1.1 christos /*******************************************************************************
677 1.1 christos *
678 1.1 christos * FUNCTION: AslPopInputFileStack
679 1.1 christos *
680 1.1 christos * PARAMETERS: None
681 1.1 christos *
682 1.1 christos * RETURN: 0 if a node was popped, -1 otherwise
683 1.1 christos *
684 1.1 christos * DESCRIPTION: Pop the top of the input file stack and point the parser to
685 1.1 christos * the saved parse buffer contained in the fnode. Also, set the
686 1.1 christos * global line counters to the saved values. This function is
687 1.1 christos * called when an include file reaches EOF.
688 1.1 christos *
689 1.1 christos ******************************************************************************/
690 1.1 christos
691 1.1 christos BOOLEAN
692 1.1 christos PrPopInputFileStack (
693 1.1 christos void)
694 1.1 christos {
695 1.1 christos PR_FILE_NODE *Fnode;
696 1.1 christos
697 1.1 christos
698 1.1.1.9 christos Fnode = AslGbl_InputFileList;
699 1.1 christos DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
700 1.1 christos "Pop InputFile Stack, Fnode %p\n\n",
701 1.1.1.9 christos AslGbl_CurrentLineNumber, Fnode);
702 1.1 christos
703 1.1 christos if (!Fnode)
704 1.1 christos {
705 1.1 christos return (FALSE);
706 1.1 christos }
707 1.1 christos
708 1.1 christos /* Close the current include file */
709 1.1 christos
710 1.1.1.9 christos fclose (AslGbl_Files[ASL_FILE_INPUT].Handle);
711 1.1 christos
712 1.1 christos /* Update the top-of-stack */
713 1.1 christos
714 1.1.1.9 christos AslGbl_InputFileList = Fnode->Next;
715 1.1 christos
716 1.1 christos /* Reset global line counter and filename */
717 1.1 christos
718 1.1.1.9 christos AslGbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
719 1.1.1.9 christos AslGbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
720 1.1.1.9 christos AslGbl_CurrentLineNumber = Fnode->CurrentLineNumber;
721 1.1 christos
722 1.1 christos /* Emit a new #line directive after the include file */
723 1.1 christos
724 1.1 christos FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
725 1.1.1.9 christos AslGbl_CurrentLineNumber, Fnode->Filename);
726 1.1 christos
727 1.1 christos /* All done with this node */
728 1.1 christos
729 1.1 christos ACPI_FREE (Fnode);
730 1.1 christos return (TRUE);
731 1.1 christos }
732