Sync usage with man page.
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / tools / acpisrc / asconvrt.c
blobe17e51f1bb494c0c7ba153ffcba36afbcac17263
2 /******************************************************************************
4 * Module Name: asconvrt - Source conversion code
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 char *
122 AsCheckAndSkipLiterals (
123 char *Buffer,
124 UINT32 *TotalLines);
126 UINT32
127 AsCountLines (
128 char *Buffer,
129 char *Filename);
131 /* Opening signature of the Intel legal header */
133 char *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
136 /******************************************************************************
138 * FUNCTION: AsMatchExactWord
140 * DESCRIPTION: Check previous and next characters for whitespace
142 ******************************************************************************/
144 BOOLEAN
145 AsMatchExactWord (
146 char *Word,
147 UINT32 WordLength)
149 char NextChar;
150 char PrevChar;
153 NextChar = Word[WordLength];
154 PrevChar = * (Word -1);
156 if (isalnum (NextChar) ||
157 (NextChar == '_') ||
158 isalnum (PrevChar) ||
159 (PrevChar == '_'))
161 return (FALSE);
164 return (TRUE);
168 /******************************************************************************
170 * FUNCTION: AsPrint
172 * DESCRIPTION: Common formatted print
174 ******************************************************************************/
176 void
177 AsPrint (
178 char *Message,
179 UINT32 Count,
180 char *Filename)
183 if (Gbl_QuietMode)
185 return;
188 printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
192 /******************************************************************************
194 * FUNCTION: AsCheckAndSkipLiterals
196 * DESCRIPTION: Generic routine to skip comments and quoted string literals.
197 * Keeps a line count.
199 ******************************************************************************/
201 char *
202 AsCheckAndSkipLiterals (
203 char *Buffer,
204 UINT32 *TotalLines)
206 UINT32 NewLines = 0;
207 char *SubBuffer = Buffer;
208 char *LiteralEnd;
211 /* Ignore comments */
213 if ((SubBuffer[0] == '/') &&
214 (SubBuffer[1] == '*'))
216 LiteralEnd = strstr (SubBuffer, "*/");
217 SubBuffer += 2; /* Get past comment opening */
219 if (!LiteralEnd)
221 return SubBuffer;
224 while (SubBuffer < LiteralEnd)
226 if (*SubBuffer == '\n')
228 NewLines++;
231 SubBuffer++;
234 SubBuffer += 2; /* Get past comment close */
237 /* Ignore quoted strings */
239 else if (*SubBuffer == '\"')
241 SubBuffer++;
242 LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
243 if (!LiteralEnd)
245 return SubBuffer;
249 if (TotalLines)
251 (*TotalLines) += NewLines;
253 return SubBuffer;
257 /******************************************************************************
259 * FUNCTION: AsAsCheckForBraces
261 * DESCRIPTION: Check for an open brace after each if statement
263 ******************************************************************************/
265 void
266 AsCheckForBraces (
267 char *Buffer,
268 char *Filename)
270 char *SubBuffer = Buffer;
271 char *NextBrace;
272 char *NextSemicolon;
273 char *NextIf;
274 UINT32 TotalLines = 1;
277 while (*SubBuffer)
280 SubBuffer = AsCheckAndSkipLiterals (SubBuffer, &TotalLines);
282 if (*SubBuffer == '\n')
284 TotalLines++;
286 else if (!(strncmp (" if", SubBuffer, 3)))
288 SubBuffer += 2;
289 NextBrace = strstr (SubBuffer, "{");
290 NextSemicolon = strstr (SubBuffer, ";");
291 NextIf = strstr (SubBuffer, " if");
293 if ((!NextBrace) ||
294 (NextSemicolon && (NextBrace > NextSemicolon)) ||
295 (NextIf && (NextBrace > NextIf)))
297 Gbl_MissingBraces++;
299 if (!Gbl_QuietMode)
301 printf ("Missing braces for <if>, line %d: %s\n", TotalLines, Filename);
305 else if (!(strncmp (" else if", SubBuffer, 8)))
307 SubBuffer += 7;
308 NextBrace = strstr (SubBuffer, "{");
309 NextSemicolon = strstr (SubBuffer, ";");
310 NextIf = strstr (SubBuffer, " if");
312 if ((!NextBrace) ||
313 (NextSemicolon && (NextBrace > NextSemicolon)) ||
314 (NextIf && (NextBrace > NextIf)))
316 Gbl_MissingBraces++;
318 if (!Gbl_QuietMode)
320 printf ("Missing braces for <if>, line %d: %s\n", TotalLines, Filename);
324 else if (!(strncmp (" else", SubBuffer, 5)))
326 SubBuffer += 4;
327 NextBrace = strstr (SubBuffer, "{");
328 NextSemicolon = strstr (SubBuffer, ";");
329 NextIf = strstr (SubBuffer, " if");
331 if ((!NextBrace) ||
332 (NextSemicolon && (NextBrace > NextSemicolon)) ||
333 (NextIf && (NextBrace > NextIf)))
335 Gbl_MissingBraces++;
337 if (!Gbl_QuietMode)
339 printf ("Missing braces for <else>, line %d: %s\n", TotalLines, Filename);
344 SubBuffer++;
349 /******************************************************************************
351 * FUNCTION: AsTrimLines
353 * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
354 * check for tabs.
356 ******************************************************************************/
358 void
359 AsTrimLines (
360 char *Buffer,
361 char *Filename)
363 char *SubBuffer = Buffer;
364 char *StartWhiteSpace = NULL;
365 UINT32 SpaceCount = 0;
368 while (*SubBuffer)
370 while (*SubBuffer != '\n')
372 if (!*SubBuffer)
374 goto Exit;
377 if (*SubBuffer == ' ')
379 if (!StartWhiteSpace)
381 StartWhiteSpace = SubBuffer;
384 else
386 StartWhiteSpace = NULL;
389 SubBuffer++;
392 if (StartWhiteSpace)
394 SpaceCount += (SubBuffer - StartWhiteSpace);
396 /* Remove the spaces */
398 SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
399 StartWhiteSpace = NULL;
402 SubBuffer++;
406 Exit:
407 if (SpaceCount)
409 Gbl_MadeChanges = TRUE;
410 AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
415 /******************************************************************************
417 * FUNCTION: AsTrimWhitespace
419 * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
420 * this can happen during the translation when lines are removed.
422 ******************************************************************************/
424 void
425 AsTrimWhitespace (
426 char *Buffer)
428 int ReplaceCount = 1;
431 while (ReplaceCount)
433 ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n", REPLACE_SUBSTRINGS, Buffer);
438 /******************************************************************************
440 * FUNCTION: AsReplaceHeader
442 * DESCRIPTION: Replace the default Intel legal header with a new header
444 ******************************************************************************/
446 void
447 AsReplaceHeader (
448 char *Buffer,
449 char *NewHeader)
451 char *SubBuffer;
452 char *TokenEnd;
455 /* Find the original header */
457 SubBuffer = strstr (Buffer, HeaderBegin);
458 if (!SubBuffer)
460 return;
463 /* Find the end of the original header */
465 TokenEnd = strstr (SubBuffer, "*/");
466 TokenEnd = AsSkipPastChar (TokenEnd, '\n');
468 /* Delete old header, insert new one */
470 AsReplaceData (SubBuffer, TokenEnd - SubBuffer, NewHeader, strlen (NewHeader));
474 /******************************************************************************
476 * FUNCTION: AsReplaceString
478 * DESCRIPTION: Replace all instances of a target string with a replacement
479 * string. Returns count of the strings replaced.
481 ******************************************************************************/
484 AsReplaceString (
485 char *Target,
486 char *Replacement,
487 UINT8 Type,
488 char *Buffer)
490 char *SubString1;
491 char *SubString2;
492 char *SubBuffer;
493 int TargetLength;
494 int ReplacementLength;
495 int ReplaceCount = 0;
498 TargetLength = strlen (Target);
499 ReplacementLength = strlen (Replacement);
501 SubBuffer = Buffer;
502 SubString1 = Buffer;
504 while (SubString1)
506 /* Find the target string */
508 SubString1 = strstr (SubBuffer, Target);
509 if (!SubString1)
511 return ReplaceCount;
515 * Check for translation escape string -- means to ignore
516 * blocks of code while replacing
518 SubString2 = strstr (SubBuffer, AS_START_IGNORE);
520 if ((SubString2) &&
521 (SubString2 < SubString1))
523 /* Find end of the escape block starting at "Substring2" */
525 SubString2 = strstr (SubString2, AS_STOP_IGNORE);
526 if (!SubString2)
528 /* Didn't find terminator */
530 return ReplaceCount;
533 /* Move buffer to end of escape block and continue */
535 SubBuffer = SubString2;
538 /* Do the actual replace if the target was found */
540 else
542 if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
544 if (!AsMatchExactWord (SubString1, TargetLength))
546 SubBuffer = SubString1 + 1;
547 continue;
551 SubBuffer = AsReplaceData (SubString1, TargetLength, Replacement, ReplacementLength);
553 if ((Type & EXTRA_INDENT_C) &&
554 (!Gbl_StructDefs))
556 SubBuffer = AsInsertData (SubBuffer, " ", 8);
559 ReplaceCount++;
563 return ReplaceCount;
567 /******************************************************************************
569 * FUNCTION: AsConvertToLineFeeds
571 * DESCRIPTION:
573 ******************************************************************************/
575 void
576 AsConvertToLineFeeds (
577 char *Buffer)
579 char *SubString;
580 char *SubBuffer;
583 SubBuffer = Buffer;
584 SubString = Buffer;
586 while (SubString)
588 /* Find the target string */
590 SubString = strstr (SubBuffer, "\r\n");
591 if (!SubString)
593 return;
596 SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
598 return;
602 /******************************************************************************
604 * FUNCTION: AsInsertCarriageReturns
606 * DESCRIPTION:
608 ******************************************************************************/
610 void
611 AsInsertCarriageReturns (
612 char *Buffer)
614 char *SubString;
615 char *SubBuffer;
618 SubBuffer = Buffer;
619 SubString = Buffer;
621 while (SubString)
623 /* Find the target string */
625 SubString = strstr (SubBuffer, "\n");
626 if (!SubString)
628 return;
631 SubBuffer = AsInsertData (SubString, "\r", 1);
632 SubBuffer += 1;
634 return;
638 /******************************************************************************
640 * FUNCTION: AsBracesOnSameLine
642 * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
643 * or while statement (leave function opening brace on separate
644 * line).
646 ******************************************************************************/
648 void
649 AsBracesOnSameLine (
650 char *Buffer)
652 UINT32 Length;
653 char *SubBuffer = Buffer;
654 char *Beginning;
655 char *StartOfThisLine;
656 char *Next;
657 BOOLEAN BlockBegin = TRUE;
660 while (*SubBuffer)
662 /* Ignore comments */
664 if ((SubBuffer[0] == '/') &&
665 (SubBuffer[1] == '*'))
667 SubBuffer = strstr (SubBuffer, "*/");
668 if (!SubBuffer)
670 return;
673 SubBuffer += 2;
674 continue;
677 /* Ignore quoted strings */
679 if (*SubBuffer == '\"')
681 SubBuffer++;
682 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
683 if (!SubBuffer)
685 return;
689 if (!strncmp ("\n}", SubBuffer, 2))
692 * A newline followed by a closing brace closes a function
693 * or struct or initializer block
695 BlockBegin = TRUE;
699 * Move every standalone brace up to the previous line
700 * Check for digit will ignore initializer lists surrounded by braces.
701 * This will work until we we need more complex detection.
703 if ((*SubBuffer == '{') && !isdigit (SubBuffer[1]))
705 if (BlockBegin)
707 BlockBegin = FALSE;
709 else
712 * Backup to previous non-whitespace
714 Beginning = SubBuffer - 1;
715 while ((*Beginning == ' ') ||
716 (*Beginning == '\n'))
718 Beginning--;
721 StartOfThisLine = Beginning;
722 while (*StartOfThisLine != '\n')
724 StartOfThisLine--;
728 * Move the brace up to the previous line, UNLESS:
730 * 1) There is a conditional compile on the line (starts with '#')
731 * 2) Previous line ends with an '=' (Start of initializer block)
732 * 3) Previous line ends with a comma (part of an init list)
733 * 4) Previous line ends with a backslash (part of a macro)
735 if ((StartOfThisLine[1] != '#') &&
736 (*Beginning != '\\') &&
737 (*Beginning != '/') &&
738 (*Beginning != '{') &&
739 (*Beginning != '=') &&
740 (*Beginning != ','))
742 Beginning++;
743 SubBuffer++;
744 Length = strlen (SubBuffer);
746 Gbl_MadeChanges = TRUE;
748 #ifdef ADD_EXTRA_WHITESPACE
749 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
750 #else
751 /* Find non-whitespace start of next line */
753 Next = SubBuffer + 1;
754 while ((*Next == ' ') ||
755 (*Next == '\t'))
757 Next++;
760 /* Find non-whitespace start of this line */
762 StartOfThisLine++;
763 while ((*StartOfThisLine == ' ') ||
764 (*StartOfThisLine == '\t'))
766 StartOfThisLine++;
770 * Must be a single-line comment to need more whitespace
771 * Even then, we don't need more if the previous statement
772 * is an "else".
774 if ((Next[0] == '/') &&
775 (Next[1] == '*') &&
776 (Next[2] != '\n') &&
778 (!strncmp (StartOfThisLine, "else if", 7) ||
779 !strncmp (StartOfThisLine, "else while", 10) ||
780 strncmp (StartOfThisLine, "else", 4)))
782 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
784 else
786 AsReplaceData (Beginning, SubBuffer - Beginning, " {", 2);
788 #endif
793 SubBuffer++;
798 /******************************************************************************
800 * FUNCTION: AsTabify4
802 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
803 * preserved.
805 ******************************************************************************/
807 void
808 AsTabify4 (
809 char *Buffer)
811 char *SubBuffer = Buffer;
812 char *NewSubBuffer;
813 UINT32 SpaceCount = 0;
814 UINT32 Column = 0;
817 while (*SubBuffer)
819 if (*SubBuffer == '\n')
821 Column = 0;
823 else
825 Column++;
828 /* Ignore comments */
830 if ((SubBuffer[0] == '/') &&
831 (SubBuffer[1] == '*'))
833 SubBuffer = strstr (SubBuffer, "*/");
834 if (!SubBuffer)
836 return;
839 SubBuffer += 2;
840 continue;
843 /* Ignore quoted strings */
845 if (*SubBuffer == '\"')
847 SubBuffer++;
848 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
849 if (!SubBuffer)
851 return;
853 SpaceCount = 0;
856 if (*SubBuffer == ' ')
858 SpaceCount++;
860 if (SpaceCount >= 4)
862 SpaceCount = 0;
864 NewSubBuffer = (SubBuffer + 1) - 4;
865 *NewSubBuffer = '\t';
866 NewSubBuffer++;
868 /* Remove the spaces */
870 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
873 if ((Column % 4) == 0)
875 SpaceCount = 0;
878 else
880 SpaceCount = 0;
883 SubBuffer++;
888 /******************************************************************************
890 * FUNCTION: AsTabify8
892 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
893 * preserved.
895 ******************************************************************************/
897 void
898 AsTabify8 (
899 char *Buffer)
901 char *SubBuffer = Buffer;
902 char *NewSubBuffer;
903 char *CommentEnd = NULL;
904 UINT32 SpaceCount = 0;
905 UINT32 Column = 0;
906 UINT32 TabCount = 0;
907 UINT32 LastLineTabCount = 0;
908 UINT32 LastLineColumnStart = 0;
909 UINT32 ThisColumnStart = 0;
910 UINT32 ThisTabCount = 0;
911 char *FirstNonBlank = NULL;
914 while (*SubBuffer)
916 if (*SubBuffer == '\n')
918 /* This is a standalone blank line */
920 FirstNonBlank = NULL;
921 Column = 0;
922 SpaceCount = 0;
923 TabCount = 0;
924 SubBuffer++;
925 continue;
928 if (!FirstNonBlank)
930 /* Find the first non-blank character on this line */
932 FirstNonBlank = SubBuffer;
933 while (*FirstNonBlank == ' ')
935 FirstNonBlank++;
939 * This mechanism limits the difference in tab counts from
940 * line to line. It helps avoid the situation where a second
941 * continuation line (which was indented correctly for tabs=4) would
942 * get indented off the screen if we just blindly converted to tabs.
944 ThisColumnStart = FirstNonBlank - SubBuffer;
946 if (LastLineTabCount == 0)
948 ThisTabCount = 0;
950 else if (ThisColumnStart == LastLineColumnStart)
952 ThisTabCount = LastLineTabCount -1;
954 else
956 ThisTabCount = LastLineTabCount + 1;
960 Column++;
962 /* Check if we are in a comment */
964 if ((SubBuffer[0] == '*') &&
965 (SubBuffer[1] == '/'))
967 SpaceCount = 0;
968 SubBuffer += 2;
970 if (*SubBuffer == '\n')
972 if (TabCount > 0)
974 LastLineTabCount = TabCount;
975 TabCount = 0;
977 FirstNonBlank = NULL;
978 LastLineColumnStart = ThisColumnStart;
979 SubBuffer++;
982 continue;
985 /* Check for comment open */
987 if ((SubBuffer[0] == '/') &&
988 (SubBuffer[1] == '*'))
990 /* Find the end of the comment, it must exist */
992 CommentEnd = strstr (SubBuffer, "*/");
993 if (!CommentEnd)
995 return;
998 /* Toss the rest of this line or single-line comment */
1000 while ((SubBuffer < CommentEnd) &&
1001 (*SubBuffer != '\n'))
1003 SubBuffer++;
1006 if (*SubBuffer == '\n')
1008 if (TabCount > 0)
1010 LastLineTabCount = TabCount;
1011 TabCount = 0;
1013 FirstNonBlank = NULL;
1014 LastLineColumnStart = ThisColumnStart;
1017 SpaceCount = 0;
1018 continue;
1021 /* Ignore quoted strings */
1023 if ((!CommentEnd) && (*SubBuffer == '\"'))
1025 SubBuffer++;
1026 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1027 if (!SubBuffer)
1029 return;
1031 SpaceCount = 0;
1034 if (*SubBuffer != ' ')
1036 /* Not a space, skip to end of line */
1038 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
1039 if (!SubBuffer)
1041 return;
1043 if (TabCount > 0)
1045 LastLineTabCount = TabCount;
1046 TabCount = 0;
1049 FirstNonBlank = NULL;
1050 LastLineColumnStart = ThisColumnStart;
1051 Column = 0;
1052 SpaceCount = 0;
1054 else
1056 /* Another space */
1058 SpaceCount++;
1060 if (SpaceCount >= 4)
1062 /* Replace this group of spaces with a tab character */
1064 SpaceCount = 0;
1066 NewSubBuffer = SubBuffer - 3;
1068 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
1070 *NewSubBuffer = '\t';
1071 NewSubBuffer++;
1072 SubBuffer++;
1073 TabCount++;
1076 /* Remove the spaces */
1078 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
1079 continue;
1083 SubBuffer++;
1088 /******************************************************************************
1090 * FUNCTION: AsCountLines
1092 * DESCRIPTION: Count the number of lines in the input buffer. Also count
1093 * the number of long lines (lines longer than 80 chars).
1095 ******************************************************************************/
1097 UINT32
1098 AsCountLines (
1099 char *Buffer,
1100 char *Filename)
1102 char *SubBuffer = Buffer;
1103 char *EndOfLine;
1104 UINT32 LineCount = 0;
1105 UINT32 LongLineCount = 0;
1108 while (*SubBuffer)
1110 EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
1111 if (!EndOfLine)
1113 Gbl_TotalLines += LineCount;
1114 return LineCount;
1117 if ((EndOfLine - SubBuffer) > 80)
1119 LongLineCount++;
1120 VERBOSE_PRINT (("long: %.80s\n", SubBuffer));
1123 LineCount++;
1124 SubBuffer = EndOfLine + 1;
1127 if (LongLineCount)
1129 VERBOSE_PRINT (("%d Lines longer than 80 found in %s\n", LongLineCount, Filename));
1130 Gbl_LongLines += LongLineCount;
1133 Gbl_TotalLines += LineCount;
1134 return LineCount;
1138 /******************************************************************************
1140 * FUNCTION: AsCountTabs
1142 * DESCRIPTION: Simply count the number of tabs in the input file buffer
1144 ******************************************************************************/
1146 void
1147 AsCountTabs (
1148 char *Buffer,
1149 char *Filename)
1151 UINT32 i;
1152 UINT32 TabCount = 0;
1155 for (i = 0; Buffer[i]; i++)
1157 if (Buffer[i] == '\t')
1159 TabCount++;
1163 if (TabCount)
1165 AsPrint ("Tabs found", TabCount, Filename);
1166 Gbl_Tabs += TabCount;
1169 AsCountLines (Buffer, Filename);
1173 /******************************************************************************
1175 * FUNCTION: AsCountNonAnsiComments
1177 * DESCRIPTION: Count the number of "//" comments. This type of comment is
1178 * non-ANSI C.
1180 ******************************************************************************/
1182 void
1183 AsCountNonAnsiComments (
1184 char *Buffer,
1185 char *Filename)
1187 char *SubBuffer = Buffer;
1188 UINT32 CommentCount = 0;
1191 while (SubBuffer)
1193 SubBuffer = strstr (SubBuffer, "//");
1194 if (SubBuffer)
1196 CommentCount++;
1197 SubBuffer += 2;
1201 if (CommentCount)
1203 AsPrint ("Non-ANSI Comments found", CommentCount, Filename);
1204 Gbl_NonAnsiComments += CommentCount;
1209 /******************************************************************************
1211 * FUNCTION: AsCountSourceLines
1213 * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1214 * comment, and 2) not a blank line.
1216 ******************************************************************************/
1218 void
1219 AsCountSourceLines (
1220 char *Buffer,
1221 char *Filename)
1223 char *SubBuffer = Buffer;
1224 UINT32 LineCount = 0;
1225 UINT32 WhiteCount = 0;
1226 UINT32 CommentCount = 0;
1229 while (*SubBuffer)
1231 /* Detect comments (// comments are not used, non-ansii) */
1233 if ((SubBuffer[0] == '/') &&
1234 (SubBuffer[1] == '*'))
1236 SubBuffer += 2;
1238 /* First line of multi-line comment is often just whitespace */
1240 if (SubBuffer[0] == '\n')
1242 WhiteCount++;
1243 SubBuffer++;
1245 else
1247 CommentCount++;
1250 /* Find end of comment */
1252 while (SubBuffer[0] && SubBuffer[1] &&
1253 !(((SubBuffer[0] == '*') &&
1254 (SubBuffer[1] == '/'))))
1256 if (SubBuffer[0] == '\n')
1258 CommentCount++;
1261 SubBuffer++;
1265 /* A linefeed followed by a non-linefeed is a valid source line */
1267 else if ((SubBuffer[0] == '\n') &&
1268 (SubBuffer[1] != '\n'))
1270 LineCount++;
1273 /* Two back-to-back linefeeds indicate a whitespace line */
1275 else if ((SubBuffer[0] == '\n') &&
1276 (SubBuffer[1] == '\n'))
1278 WhiteCount++;
1281 SubBuffer++;
1284 /* Adjust comment count for legal header */
1286 if (Gbl_HeaderSize < CommentCount)
1288 CommentCount -= Gbl_HeaderSize;
1289 Gbl_HeaderLines += Gbl_HeaderSize;
1292 Gbl_SourceLines += LineCount;
1293 Gbl_WhiteLines += WhiteCount;
1294 Gbl_CommentLines += CommentCount;
1296 VERBOSE_PRINT (("%d Comment %d White %d Code %d Lines in %s\n",
1297 CommentCount, WhiteCount, LineCount, LineCount+WhiteCount+CommentCount, Filename));
1301 /******************************************************************************
1303 * FUNCTION: AsInsertPrefix
1305 * DESCRIPTION: Insert struct or union prefixes
1307 ******************************************************************************/
1309 void
1310 AsInsertPrefix (
1311 char *Buffer,
1312 char *Keyword,
1313 UINT8 Type)
1315 char *SubString;
1316 char *SubBuffer;
1317 char *EndKeyword;
1318 int StrLength;
1319 int InsertLength;
1320 char *InsertString;
1321 int TrailingSpaces;
1322 char LowerKeyword[128];
1323 int KeywordLength;
1326 switch (Type)
1328 case SRC_TYPE_STRUCT:
1329 InsertString = "struct ";
1330 break;
1332 case SRC_TYPE_UNION:
1333 InsertString = "union ";
1334 break;
1336 default:
1337 return;
1340 strcpy (LowerKeyword, Keyword);
1341 strlwr (LowerKeyword);
1343 SubBuffer = Buffer;
1344 SubString = Buffer;
1345 InsertLength = strlen (InsertString);
1346 KeywordLength = strlen (Keyword);
1349 while (SubString)
1351 /* Find an instance of the keyword */
1353 SubString = strstr (SubBuffer, LowerKeyword);
1355 if (!SubString)
1357 return;
1360 SubBuffer = SubString;
1362 /* Must be standalone word, not a substring */
1364 if (AsMatchExactWord (SubString, KeywordLength))
1366 /* Make sure the keyword isn't already prefixed with the insert */
1368 if (!strncmp (SubString - InsertLength, InsertString, InsertLength))
1370 /* Add spaces if not already at the end-of-line */
1372 if (*(SubBuffer + KeywordLength) != '\n')
1374 /* Already present, add spaces after to align structure members */
1376 #if 0
1377 /* ONLY FOR C FILES */
1378 AsInsertData (SubBuffer + KeywordLength, " ", 8);
1379 #endif
1381 goto Next;
1384 /* Make sure the keyword isn't at the end of a struct/union */
1385 /* Note: This code depends on a single space after the brace */
1387 if (*(SubString - 2) == '}')
1389 goto Next;
1392 /* Prefix the keyword with the insert string */
1394 Gbl_MadeChanges = TRUE;
1395 StrLength = strlen (SubString);
1397 /* Is there room for insertion */
1399 EndKeyword = SubString + strlen (LowerKeyword);
1401 TrailingSpaces = 0;
1402 while (EndKeyword[TrailingSpaces] == ' ')
1404 TrailingSpaces++;
1408 * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1410 SubBuffer = SubString + InsertLength;
1412 if (TrailingSpaces > InsertLength)
1414 /* Insert the keyword */
1416 memmove (SubBuffer, SubString, KeywordLength);
1418 /* Insert the keyword */
1420 memmove (SubString, InsertString, InsertLength);
1422 else
1424 AsInsertData (SubString, InsertString, InsertLength);
1428 Next:
1429 SubBuffer += KeywordLength;
1433 #ifdef ACPI_FUTURE_IMPLEMENTATION
1434 /******************************************************************************
1436 * FUNCTION: AsTrimComments
1438 * DESCRIPTION: Finds 3-line comments with only a single line of text
1440 ******************************************************************************/
1442 void
1443 AsTrimComments (
1444 char *Buffer,
1445 char *Filename)
1447 char *SubBuffer = Buffer;
1448 char *Ptr1;
1449 char *Ptr2;
1450 UINT32 LineCount;
1451 UINT32 ShortCommentCount = 0;
1454 while (1)
1456 /* Find comment open, within procedure level */
1458 SubBuffer = strstr (SubBuffer, " /*");
1459 if (!SubBuffer)
1461 goto Exit;
1464 /* Find comment terminator */
1466 Ptr1 = strstr (SubBuffer, "*/");
1467 if (!Ptr1)
1469 goto Exit;
1472 /* Find next EOL (from original buffer) */
1474 Ptr2 = strstr (SubBuffer, "\n");
1475 if (!Ptr2)
1477 goto Exit;
1480 /* Ignore one-line comments */
1482 if (Ptr1 < Ptr2)
1484 /* Normal comment, ignore and continue; */
1486 SubBuffer = Ptr2;
1487 continue;
1490 /* Examine multi-line comment */
1492 LineCount = 1;
1493 while (Ptr1 > Ptr2)
1495 /* Find next EOL */
1497 Ptr2++;
1498 Ptr2 = strstr (Ptr2, "\n");
1499 if (!Ptr2)
1501 goto Exit;
1504 LineCount++;
1507 SubBuffer = Ptr1;
1509 if (LineCount <= 3)
1511 ShortCommentCount++;
1516 Exit:
1518 if (ShortCommentCount)
1520 AsPrint ("Short Comments found", ShortCommentCount, Filename);
1523 #endif