Sync usage with man page.
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / tools / acpisrc / asremove.c
blob18f72f3c157811b809357652ebc0f756ab75d00d
2 /******************************************************************************
4 * Module Name: asremove - Source conversion - removal functions
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #include "acpisrc.h"
119 /* Local prototypes */
121 void
122 AsRemoveStatement (
123 char *Buffer,
124 char *Keyword,
125 UINT32 Type);
128 /******************************************************************************
130 * FUNCTION: AsRemoveStatement
132 * DESCRIPTION: Remove all statements that contain the given keyword.
133 * Limitations: Removes text from the start of the line that
134 * contains the keyword to the next semicolon. Currently
135 * doesn't ignore comments.
137 ******************************************************************************/
139 void
140 AsRemoveStatement (
141 char *Buffer,
142 char *Keyword,
143 UINT32 Type)
145 char *SubString;
146 char *SubBuffer;
147 int KeywordLength;
150 KeywordLength = strlen (Keyword);
151 SubBuffer = Buffer;
152 SubString = Buffer;
155 while (SubString)
157 SubString = strstr (SubBuffer, Keyword);
159 if (SubString)
161 SubBuffer = SubString;
163 if ((Type == REPLACE_WHOLE_WORD) &&
164 (!AsMatchExactWord (SubString, KeywordLength)))
166 SubBuffer++;
167 continue;
170 /* Find start of this line */
172 while (*SubString != '\n')
174 SubString--;
176 SubString++;
178 /* Find end of this statement */
180 SubBuffer = AsSkipPastChar (SubBuffer, ';');
181 if (!SubBuffer)
183 return;
186 /* Find end of this line */
188 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
189 if (!SubBuffer)
191 return;
194 /* If next line is blank, remove it too */
196 if (*SubBuffer == '\n')
198 SubBuffer++;
201 /* Remove the lines */
203 SubBuffer = AsRemoveData (SubString, SubBuffer);
209 /******************************************************************************
211 * FUNCTION: AsRemoveConditionalCompile
213 * DESCRIPTION: Remove a "#ifdef" statement, and all text that it encompasses.
214 * Limitations: cannot handle nested ifdefs.
216 ******************************************************************************/
218 void
219 AsRemoveConditionalCompile (
220 char *Buffer,
221 char *Keyword)
223 char *SubString;
224 char *SubBuffer;
225 char *IfPtr;
226 char *EndifPtr;
227 char *ElsePtr;
228 char *Comment;
229 int KeywordLength;
232 KeywordLength = strlen (Keyword);
233 SubBuffer = Buffer;
234 SubString = Buffer;
237 while (SubString)
239 SubBuffer = strstr (SubString, Keyword);
240 if (!SubBuffer)
242 return;
246 * Check for translation escape string -- means to ignore
247 * blocks of code while replacing
249 Comment = strstr (SubString, AS_START_IGNORE);
251 if ((Comment) &&
252 (Comment < SubBuffer))
254 SubString = strstr (Comment, AS_STOP_IGNORE);
255 if (!SubString)
257 return;
260 SubString += 3;
261 continue;
264 /* Check for ordinary comment */
266 Comment = strstr (SubString, "/*");
268 if ((Comment) &&
269 (Comment < SubBuffer))
271 SubString = strstr (Comment, "*/");
272 if (!SubString)
274 return;
277 SubString += 2;
278 continue;
281 SubString = SubBuffer;
282 if (!AsMatchExactWord (SubString, KeywordLength))
284 SubString++;
285 continue;
288 /* Find start of this line */
290 while (*SubString != '\n' && (SubString > Buffer))
292 SubString--;
294 SubString++;
296 /* Find the "#ifxxxx" */
298 IfPtr = strstr (SubString, "#if");
299 if (!IfPtr)
301 return;
304 if (IfPtr > SubBuffer)
306 /* Not the right #if */
308 SubString = SubBuffer + strlen (Keyword);
309 continue;
312 /* Find closing #endif or #else */
314 EndifPtr = strstr (SubBuffer, "#endif");
315 if (!EndifPtr)
317 /* There has to be an #endif */
319 return;
322 ElsePtr = strstr (SubBuffer, "#else");
323 if ((ElsePtr) &&
324 (EndifPtr > ElsePtr))
326 /* This #ifdef contains an #else clause */
327 /* Find end of this line */
329 SubBuffer = AsSkipPastChar (ElsePtr, '\n');
330 if (!SubBuffer)
332 return;
335 /* Remove the #ifdef .... #else code */
337 AsRemoveData (SubString, SubBuffer);
339 /* Next, we will remove the #endif statement */
341 EndifPtr = strstr (SubString, "#endif");
342 if (!EndifPtr)
344 /* There has to be an #endif */
346 return;
349 SubString = EndifPtr;
352 /* Remove the ... #endif part */
353 /* Find end of this line */
355 SubBuffer = AsSkipPastChar (EndifPtr, '\n');
356 if (!SubBuffer)
358 return;
361 /* Remove the lines */
363 SubBuffer = AsRemoveData (SubString, SubBuffer);
368 /******************************************************************************
370 * FUNCTION: AsRemoveMacro
372 * DESCRIPTION: Remove every line that contains the keyword. Does not
373 * skip comments.
375 ******************************************************************************/
377 void
378 AsRemoveMacro (
379 char *Buffer,
380 char *Keyword)
382 char *SubString;
383 char *SubBuffer;
384 int NestLevel;
387 SubBuffer = Buffer;
388 SubString = Buffer;
391 while (SubString)
393 SubString = strstr (SubBuffer, Keyword);
395 if (SubString)
397 SubBuffer = SubString;
399 /* Find start of the macro parameters */
401 while (*SubString != '(')
403 SubString++;
405 SubString++;
407 /* Remove the macro name and opening paren */
409 SubString = AsRemoveData (SubBuffer, SubString);
411 NestLevel = 1;
412 while (*SubString)
414 if (*SubString == '(')
416 NestLevel++;
418 else if (*SubString == ')')
420 NestLevel--;
423 SubString++;
425 if (NestLevel == 0)
427 break;
431 /* Remove the closing paren */
433 SubBuffer = AsRemoveData (SubString-1, SubString);
439 /******************************************************************************
441 * FUNCTION: AsRemoveLine
443 * DESCRIPTION: Remove every line that contains the keyword. Does not
444 * skip comments.
446 ******************************************************************************/
448 void
449 AsRemoveLine (
450 char *Buffer,
451 char *Keyword)
453 char *SubString;
454 char *SubBuffer;
457 SubBuffer = Buffer;
458 SubString = Buffer;
461 while (SubString)
463 SubString = strstr (SubBuffer, Keyword);
465 if (SubString)
467 SubBuffer = SubString;
469 /* Find start of this line */
471 while (*SubString != '\n')
473 SubString--;
475 SubString++;
477 /* Find end of this line */
479 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
480 if (!SubBuffer)
482 return;
485 /* Remove the line */
487 SubBuffer = AsRemoveData (SubString, SubBuffer);
493 /******************************************************************************
495 * FUNCTION: AsReduceTypedefs
497 * DESCRIPTION: Eliminate certain typedefs
499 ******************************************************************************/
501 void
502 AsReduceTypedefs (
503 char *Buffer,
504 char *Keyword)
506 char *SubString;
507 char *SubBuffer;
508 int NestLevel;
511 SubBuffer = Buffer;
512 SubString = Buffer;
515 while (SubString)
517 SubString = strstr (SubBuffer, Keyword);
519 if (SubString)
521 /* Remove the typedef itself */
523 SubBuffer = SubString + strlen ("typedef") + 1;
524 SubBuffer = AsRemoveData (SubString, SubBuffer);
526 /* Find the opening brace of the struct or union */
528 while (*SubString != '{')
530 SubString++;
532 SubString++;
534 /* Find the closing brace. Handles nested braces */
536 NestLevel = 1;
537 while (*SubString)
539 if (*SubString == '{')
541 NestLevel++;
543 else if (*SubString == '}')
545 NestLevel--;
548 SubString++;
550 if (NestLevel == 0)
552 break;
556 /* Remove an extra line feed if present */
558 if (!strncmp (SubString - 3, "\n\n", 2))
560 *(SubString -2) = '}';
561 SubString--;
564 /* Find the end of the typedef name */
566 SubBuffer = AsSkipUntilChar (SubString, ';');
568 /* And remove the typedef name */
570 SubBuffer = AsRemoveData (SubString, SubBuffer);
576 /******************************************************************************
578 * FUNCTION: AsRemoveEmptyBlocks
580 * DESCRIPTION: Remove any C blocks (e.g., if {}) that contain no code. This
581 * can happen as a result of removing lines such as DEBUG_PRINT.
583 ******************************************************************************/
585 void
586 AsRemoveEmptyBlocks (
587 char *Buffer,
588 char *Filename)
590 char *SubBuffer;
591 char *BlockStart;
592 BOOLEAN EmptyBlock = TRUE;
593 BOOLEAN AnotherPassRequired = TRUE;
594 UINT32 BlockCount = 0;
597 while (AnotherPassRequired)
599 SubBuffer = Buffer;
600 AnotherPassRequired = FALSE;
602 while (*SubBuffer)
604 if (*SubBuffer == '{')
606 BlockStart = SubBuffer;
607 EmptyBlock = TRUE;
609 SubBuffer++;
610 while (*SubBuffer != '}')
612 if ((*SubBuffer != ' ') &&
613 (*SubBuffer != '\n'))
615 EmptyBlock = FALSE;
616 break;
618 SubBuffer++;
621 if (EmptyBlock)
623 /* Find start of the first line of the block */
625 while (*BlockStart != '\n')
627 BlockStart--;
630 /* Find end of the last line of the block */
632 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
633 if (!SubBuffer)
635 break;
638 /* Remove the block */
640 SubBuffer = AsRemoveData (BlockStart, SubBuffer);
641 BlockCount++;
642 AnotherPassRequired = TRUE;
643 continue;
647 SubBuffer++;
651 if (BlockCount)
653 Gbl_MadeChanges = TRUE;
654 AsPrint ("Code blocks deleted", BlockCount, Filename);
659 /******************************************************************************
661 * FUNCTION: AsRemoveDebugMacros
663 * DESCRIPTION: Remove all "Debug" macros -- macros that produce debug output.
665 ******************************************************************************/
667 void
668 AsRemoveDebugMacros (
669 char *Buffer)
671 AsRemoveConditionalCompile (Buffer, "ACPI_DEBUG_OUTPUT");
673 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT", REPLACE_WHOLE_WORD);
674 AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT_RAW", REPLACE_WHOLE_WORD);
675 AsRemoveStatement (Buffer, "DEBUG_EXEC", REPLACE_WHOLE_WORD);
676 AsRemoveStatement (Buffer, "FUNCTION_ENTRY", REPLACE_WHOLE_WORD);
677 AsRemoveStatement (Buffer, "PROC_NAME", REPLACE_WHOLE_WORD);
678 AsRemoveStatement (Buffer, "FUNCTION_TRACE", REPLACE_SUBSTRINGS);
679 AsRemoveStatement (Buffer, "DUMP_", REPLACE_SUBSTRINGS);
681 AsReplaceString ("return_VOID", "return", REPLACE_WHOLE_WORD, Buffer);
682 AsReplaceString ("return_PTR", "return", REPLACE_WHOLE_WORD, Buffer);
683 AsReplaceString ("return_ACPI_STATUS", "return", REPLACE_WHOLE_WORD, Buffer);
684 AsReplaceString ("return_acpi_status", "return", REPLACE_WHOLE_WORD, Buffer);
685 AsReplaceString ("return_VALUE", "return", REPLACE_WHOLE_WORD, Buffer);