Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / tools / acpisrc / asremove.c
blob37b167bdc4947e075e4ae14a009638614bd8fc36
1 /******************************************************************************
3 * Module Name: asremove - Source conversion - removal functions
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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 "acpisrc.h"
46 /* Local prototypes */
48 void
49 AsRemoveStatement (
50 char *Buffer,
51 char *Keyword,
52 UINT32 Type);
55 /******************************************************************************
57 * FUNCTION: AsRemoveStatement
59 * DESCRIPTION: Remove all statements that contain the given keyword.
60 * Limitations: Removes text from the start of the line that
61 * contains the keyword to the next semicolon. Currently
62 * doesn't ignore comments.
64 ******************************************************************************/
66 void
67 AsRemoveStatement (
68 char *Buffer,
69 char *Keyword,
70 UINT32 Type)
72 char *SubString;
73 char *SubBuffer;
74 int KeywordLength;
77 KeywordLength = strlen (Keyword);
78 SubBuffer = Buffer;
79 SubString = Buffer;
82 while (SubString)
84 SubString = strstr (SubBuffer, Keyword);
86 if (SubString)
88 SubBuffer = SubString;
90 if ((Type == REPLACE_WHOLE_WORD) &&
91 (!AsMatchExactWord (SubString, KeywordLength)))
93 SubBuffer++;
94 continue;
97 /* Find start of this line */
99 while (*SubString != '\n')
101 SubString--;
103 SubString++;
105 /* Find end of this statement */
107 SubBuffer = AsSkipPastChar (SubBuffer, ';');
108 if (!SubBuffer)
110 return;
113 /* Find end of this line */
115 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
116 if (!SubBuffer)
118 return;
121 /* If next line is blank, remove it too */
123 if (*SubBuffer == '\n')
125 SubBuffer++;
128 /* Remove the lines */
130 SubBuffer = AsRemoveData (SubString, SubBuffer);
136 /******************************************************************************
138 * FUNCTION: AsRemoveConditionalCompile
140 * DESCRIPTION: Remove a "#ifdef" statement, and all text that it encompasses.
141 * Limitations: cannot handle nested ifdefs.
143 ******************************************************************************/
145 void
146 AsRemoveConditionalCompile (
147 char *Buffer,
148 char *Keyword)
150 char *SubString;
151 char *SubBuffer;
152 char *IfPtr;
153 char *EndifPtr;
154 char *ElsePtr;
155 char *Comment;
156 int KeywordLength;
159 KeywordLength = strlen (Keyword);
160 SubBuffer = Buffer;
161 SubString = Buffer;
164 while (SubString)
166 SubBuffer = strstr (SubString, Keyword);
167 if (!SubBuffer)
169 return;
173 * Check for translation escape string -- means to ignore
174 * blocks of code while replacing
176 if (Gbl_IgnoreTranslationEscapes)
178 Comment = NULL;
180 else
182 Comment = strstr (SubString, AS_START_IGNORE);
185 if ((Comment) &&
186 (Comment < SubBuffer))
188 SubString = strstr (Comment, AS_STOP_IGNORE);
189 if (!SubString)
191 return;
194 SubString += 3;
195 continue;
198 /* Check for ordinary comment */
200 Comment = strstr (SubString, "/*");
202 if ((Comment) &&
203 (Comment < SubBuffer))
205 SubString = strstr (Comment, "*/");
206 if (!SubString)
208 return;
211 SubString += 2;
212 continue;
215 SubString = SubBuffer;
216 if (!AsMatchExactWord (SubString, KeywordLength))
218 SubString++;
219 continue;
222 /* Find start of this line */
224 while (*SubString != '\n' && (SubString > Buffer))
226 SubString--;
228 SubString++;
230 /* Find the "#ifxxxx" */
232 IfPtr = strstr (SubString, "#if");
233 if (!IfPtr)
235 return;
238 if (IfPtr > SubBuffer)
240 /* Not the right #if */
242 SubString = SubBuffer + strlen (Keyword);
243 continue;
246 /* Find closing #endif or #else */
248 EndifPtr = strstr (SubBuffer, "#endif");
249 if (!EndifPtr)
251 /* There has to be an #endif */
253 return;
256 ElsePtr = strstr (SubBuffer, "#else");
257 if ((ElsePtr) &&
258 (EndifPtr > ElsePtr))
260 /* This #ifdef contains an #else clause */
261 /* Find end of this line */
263 SubBuffer = AsSkipPastChar (ElsePtr, '\n');
264 if (!SubBuffer)
266 return;
269 /* Remove the #ifdef .... #else code */
271 AsRemoveData (SubString, SubBuffer);
273 /* Next, we will remove the #endif statement */
275 EndifPtr = strstr (SubString, "#endif");
276 if (!EndifPtr)
278 /* There has to be an #endif */
280 return;
283 SubString = EndifPtr;
286 /* Remove the ... #endif part */
287 /* Find end of this line */
289 SubBuffer = AsSkipPastChar (EndifPtr, '\n');
290 if (!SubBuffer)
292 return;
295 /* Remove the lines */
297 SubBuffer = AsRemoveData (SubString, SubBuffer);
302 /******************************************************************************
304 * FUNCTION: AsRemoveMacro
306 * DESCRIPTION: Remove every line that contains the keyword. Does not
307 * skip comments.
309 ******************************************************************************/
311 void
312 AsRemoveMacro (
313 char *Buffer,
314 char *Keyword)
316 char *SubString;
317 char *SubBuffer;
318 int NestLevel;
321 SubBuffer = Buffer;
322 SubString = Buffer;
325 while (SubString)
327 SubString = strstr (SubBuffer, Keyword);
329 if (SubString)
331 SubBuffer = SubString;
333 /* Find start of the macro parameters */
335 while (*SubString != '(')
337 SubString++;
339 SubString++;
341 /* Remove the macro name and opening paren */
343 SubString = AsRemoveData (SubBuffer, SubString);
345 NestLevel = 1;
346 while (*SubString)
348 if (*SubString == '(')
350 NestLevel++;
352 else if (*SubString == ')')
354 NestLevel--;
357 SubString++;
359 if (NestLevel == 0)
361 break;
365 /* Remove the closing paren */
367 SubBuffer = AsRemoveData (SubString-1, SubString);
373 /******************************************************************************
375 * FUNCTION: AsRemoveLine
377 * DESCRIPTION: Remove every line that contains the keyword. Does not
378 * skip comments.
380 ******************************************************************************/
382 void
383 AsRemoveLine (
384 char *Buffer,
385 char *Keyword)
387 char *SubString;
388 char *SubBuffer;
391 SubBuffer = Buffer;
392 SubString = Buffer;
395 while (SubString)
397 SubString = strstr (SubBuffer, Keyword);
399 if (SubString)
401 SubBuffer = SubString;
403 /* Find start of this line */
405 while (*SubString != '\n')
407 SubString--;
409 SubString++;
411 /* Find end of this line */
413 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
414 if (!SubBuffer)
416 return;
419 /* Remove the line */
421 SubBuffer = AsRemoveData (SubString, SubBuffer);
427 /******************************************************************************
429 * FUNCTION: AsReduceTypedefs
431 * DESCRIPTION: Eliminate certain typedefs
433 ******************************************************************************/
435 void
436 AsReduceTypedefs (
437 char *Buffer,
438 char *Keyword)
440 char *SubString;
441 char *SubBuffer;
442 int NestLevel;
445 SubBuffer = Buffer;
446 SubString = Buffer;
449 while (SubString)
451 SubString = strstr (SubBuffer, Keyword);
453 if (SubString)
455 /* Remove the typedef itself */
457 SubBuffer = SubString + strlen ("typedef") + 1;
458 SubBuffer = AsRemoveData (SubString, SubBuffer);
460 /* Find the opening brace of the struct or union */
462 while (*SubString != '{')
464 SubString++;
466 SubString++;
468 /* Find the closing brace. Handles nested braces */
470 NestLevel = 1;
471 while (*SubString)
473 if (*SubString == '{')
475 NestLevel++;
477 else if (*SubString == '}')
479 NestLevel--;
482 SubString++;
484 if (NestLevel == 0)
486 break;
490 /* Remove an extra line feed if present */
492 if (!strncmp (SubString - 3, "\n\n", 2))
494 *(SubString -2) = '}';
495 SubString--;
498 /* Find the end of the typedef name */
500 SubBuffer = AsSkipUntilChar (SubString, ';');
502 /* And remove the typedef name */
504 SubBuffer = AsRemoveData (SubString, SubBuffer);
510 /******************************************************************************
512 * FUNCTION: AsRemoveEmptyBlocks
514 * DESCRIPTION: Remove any C blocks (e.g., if {}) that contain no code. This
515 * can happen as a result of removing lines such as DEBUG_PRINT.
517 ******************************************************************************/
519 void
520 AsRemoveEmptyBlocks (
521 char *Buffer,
522 char *Filename)
524 char *SubBuffer;
525 char *BlockStart;
526 BOOLEAN EmptyBlock = TRUE;
527 BOOLEAN AnotherPassRequired = TRUE;
528 UINT32 BlockCount = 0;
531 while (AnotherPassRequired)
533 SubBuffer = Buffer;
534 AnotherPassRequired = FALSE;
536 while (*SubBuffer)
538 if (*SubBuffer == '{')
540 BlockStart = SubBuffer;
541 EmptyBlock = TRUE;
543 SubBuffer++;
544 while (*SubBuffer != '}')
546 if ((*SubBuffer != ' ') &&
547 (*SubBuffer != '\n'))
549 EmptyBlock = FALSE;
550 break;
552 SubBuffer++;
555 if (EmptyBlock)
557 /* Find start of the first line of the block */
559 while (*BlockStart != '\n')
561 BlockStart--;
564 /* Find end of the last line of the block */
566 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
567 if (!SubBuffer)
569 break;
572 /* Remove the block */
574 SubBuffer = AsRemoveData (BlockStart, SubBuffer);
575 BlockCount++;
576 AnotherPassRequired = TRUE;
577 continue;
581 SubBuffer++;
585 if (BlockCount)
587 Gbl_MadeChanges = TRUE;
588 AsPrint ("Code blocks deleted", BlockCount, Filename);
593 /******************************************************************************
595 * FUNCTION: AsRemoveDebugMacros
597 * DESCRIPTION: Remove all "Debug" macros -- macros that produce debug output.
599 ******************************************************************************/
601 void
602 AsRemoveDebugMacros (
603 char *Buffer)
605 AsRemoveConditionalCompile (Buffer, "ACPI_DEBUG_OUTPUT");
607 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT", REPLACE_WHOLE_WORD);
608 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT_RAW", REPLACE_WHOLE_WORD);
609 AsRemoveStatement (Buffer, "DEBUG_EXEC", REPLACE_WHOLE_WORD);
610 AsRemoveStatement (Buffer, "FUNCTION_ENTRY", REPLACE_WHOLE_WORD);
611 AsRemoveStatement (Buffer, "PROC_NAME", REPLACE_WHOLE_WORD);
612 AsRemoveStatement (Buffer, "FUNCTION_TRACE", REPLACE_SUBSTRINGS);
613 AsRemoveStatement (Buffer, "DUMP_", REPLACE_SUBSTRINGS);
615 AsReplaceString ("return_VOID", "return", REPLACE_WHOLE_WORD, Buffer);
616 AsReplaceString ("return_PTR", "return", REPLACE_WHOLE_WORD, Buffer);
617 AsReplaceString ("return_ACPI_STATUS", "return", REPLACE_WHOLE_WORD, Buffer);
618 AsReplaceString ("return_acpi_status", "return", REPLACE_WHOLE_WORD, Buffer);
619 AsReplaceString ("return_VALUE", "return", REPLACE_WHOLE_WORD, Buffer);
623 /******************************************************************************
625 * FUNCTION: AsCleanupSpecialMacro
627 * DESCRIPTION: For special macro invocations (invoked without ";" at the end
628 * of the lines), do the following:
629 * 1. Remove spaces appended by indent at the beginning of lines.
630 * 2. Add an empty line between two special macro invocations.
632 ******************************************************************************/
634 void
635 AsCleanupSpecialMacro (
636 char *Buffer,
637 char *Keyword)
639 char *SubString;
640 char *SubBuffer;
641 char *LastNonSpace;
644 SubBuffer = Buffer;
645 SubString = Buffer;
647 while (SubString)
649 SubString = strstr (SubBuffer, Keyword);
651 if (SubString)
653 /* Find start of the line */
655 SubBuffer = SubString;
656 while (*(SubBuffer - 1) == ' ')
658 SubBuffer--;
661 if (*(SubBuffer - 1) == '\n')
663 /* Find last non-space character */
665 LastNonSpace = SubBuffer - 1;
666 while (isspace ((int) *LastNonSpace))
668 LastNonSpace--;
671 if (*LastNonSpace != '\\')
673 /* Remove the extra spaces */
675 SubString = AsRemoveData (SubBuffer, SubString);
677 /* Enforce an empty line between the invocations */
679 if (*(SubBuffer - 2) == ')')
681 AsInsertData (SubBuffer, "\n", 1);
686 SubBuffer = SubString + strlen (Keyword);