Sync ACPICA with Intel's version 20180629.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpisrc / asconvrt.c
blob4bf0d0a6b52455ed92311a7c4395259f4a9a977a
1 /******************************************************************************
3 * Module Name: asconvrt - Source conversion code
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpisrc.h"
154 AS_BRACE_INFO Gbl_BraceInfo[] =
156 {" if", 3},
157 {" else if", 8},
158 {" else while", 11},
159 {" else", 5},
160 {" do ", 4},
161 {NULL, 0}
165 /* Local prototypes */
167 static char *
168 AsMatchValidToken (
169 char *Buffer,
170 char *Filename,
171 char TargetChar,
172 AS_SCAN_CALLBACK Callback);
174 static char *
175 AsCheckBracesCallback (
176 char *Buffer,
177 char *Filename,
178 UINT32 LineNumber);
180 static UINT32
181 AsCountLines (
182 char *Buffer,
183 char *Filename);
186 #define MODULE_HEADER_BEGIN "/******************************************************************************\n *\n * Module Name:";
187 #define MODULE_HEADER_END " *****************************************************************************/\n\n"
188 #define INTEL_COPYRIGHT " * Copyright (C) 2000 - 2018, Intel Corp.\n"
190 /* Opening signature of the Intel legal header */
192 char *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
194 UINT32 NonAnsiCommentCount;
196 char CopyRightHeaderEnd[] = INTEL_COPYRIGHT " *\n" MODULE_HEADER_END;
198 /******************************************************************************
200 * FUNCTION: AsCountNonAnsiComments
202 * DESCRIPTION: Count the number of "//" comments. This type of comment is
203 * non-ANSI C.
205 * NOTE: July 2014: Allows // within quoted strings and within normal
206 * comments. Eliminates extraneous warnings from this utility.
208 ******************************************************************************/
210 void
211 AsCountNonAnsiComments (
212 char *Buffer,
213 char *Filename)
216 AsMatchValidToken (Buffer, Filename, 0, NULL);
218 /* Error if any slash-slash comments found */
220 if (NonAnsiCommentCount)
222 AsPrint ("Non-ANSI // Comments Found", NonAnsiCommentCount, Filename);
223 Gbl_NonAnsiComments += NonAnsiCommentCount;
228 /******************************************************************************
230 * FUNCTION: AsCheckForBraces
232 * DESCRIPTION: Check for an open brace after each if/else/do (etc.)
233 * statement
235 ******************************************************************************/
237 void
238 AsCheckForBraces (
239 char *Buffer,
240 char *Filename)
243 AsMatchValidToken (Buffer, Filename, 0, AsCheckBracesCallback);
247 /******************************************************************************
249 * FUNCTION: AsCheckBracesCallback
251 * DESCRIPTION: Check if/else/do statements. Ensure that braces
252 * are always used.
254 * TBD: Currently, don't check while() statements. The problem is that there
255 * are two forms: do {} while (); and while () {}.
257 ******************************************************************************/
259 static char *
260 AsCheckBracesCallback (
261 char *Buffer,
262 char *Filename,
263 UINT32 LineNumber)
265 char *SubBuffer = Buffer;
266 char *NextBrace;
267 char *NextSemicolon;
268 AS_BRACE_INFO *BraceInfo;
271 for (BraceInfo = Gbl_BraceInfo; BraceInfo->Operator; BraceInfo++)
273 if (!(strncmp (BraceInfo->Operator, SubBuffer, BraceInfo->Length)))
275 SubBuffer += (BraceInfo->Length - 1);
277 /* Find next brace and the next semicolon */
279 NextBrace = AsMatchValidToken (SubBuffer, Filename, '{', NULL);
280 NextSemicolon = AsMatchValidToken (SubBuffer, Filename, ';', NULL);
282 /* Next brace should appear before next semicolon */
284 if ((!NextBrace) ||
285 (NextSemicolon && (NextBrace > NextSemicolon)))
287 Gbl_MissingBraces++;
289 if (!Gbl_QuietMode)
291 printf ("Missing braces for <%s>, line %u: %s\n",
292 BraceInfo->Operator + 1, LineNumber, Filename);
296 return (SubBuffer);
300 /* No match, just return original buffer */
302 return (Buffer);
306 /******************************************************************************
308 * FUNCTION: AsMatchValidToken
310 * DESCRIPTION: Find the next matching token in the input buffer.
312 ******************************************************************************/
314 static char *
315 AsMatchValidToken (
316 char *Buffer,
317 char *Filename,
318 char TargetChar,
319 AS_SCAN_CALLBACK Callback)
321 char *SubBuffer = Buffer;
322 char *StringStart;
323 UINT32 TotalLines;
326 TotalLines = 1;
327 NonAnsiCommentCount = 0;
329 /* Scan from current position up to the end if necessary */
331 while (*SubBuffer)
333 /* Skip normal comments */
335 if ((*SubBuffer == '/') &&
336 (*(SubBuffer + 1) == '*'))
338 /* Must maintain line count */
340 SubBuffer += 2;
341 while (strncmp ("*/", SubBuffer, 2))
343 if (*SubBuffer == '\n')
345 TotalLines++;
347 SubBuffer++;
350 SubBuffer += 2;
351 continue;
354 /* Skip single quoted chars */
356 if (*SubBuffer == '\'')
358 SubBuffer++;
359 if (!(*SubBuffer))
361 break;
364 if (*SubBuffer == '\\')
366 SubBuffer++;
369 SubBuffer++;
370 continue;
373 /* Skip quoted strings */
375 if (*SubBuffer == '"')
377 StringStart = SubBuffer;
378 SubBuffer++;
379 if (!(*SubBuffer))
381 break;
384 while (*SubBuffer != '"')
386 if ((*SubBuffer == '\n') ||
387 (!(*SubBuffer)))
389 AsPrint ("Unbalanced quoted string",1, Filename);
390 printf (" %.32s (line %u)\n", StringStart, TotalLines);
391 break;
394 /* Handle escapes within the string */
396 if (*SubBuffer == '\\')
398 SubBuffer++;
401 SubBuffer++;
404 SubBuffer++;
405 continue;
408 /* Now we can check for a slash-slash comment */
410 if ((*SubBuffer == '/') &&
411 (*(SubBuffer + 1) == '/'))
413 NonAnsiCommentCount++;
415 /* Skip to end-of-line */
417 while ((*SubBuffer != '\n') &&
418 (*SubBuffer))
420 SubBuffer++;
423 if (!(*SubBuffer))
425 break;
428 if (*SubBuffer == '\n')
430 TotalLines++;
433 SubBuffer++;
434 continue;
437 /* Finally, check for a newline */
439 if (*SubBuffer == '\n')
441 TotalLines++;
442 SubBuffer++;
443 continue;
446 /* Normal character, do the user actions */
448 if (Callback)
450 SubBuffer = Callback (SubBuffer, Filename, TotalLines);
453 if (TargetChar && (*SubBuffer == TargetChar))
455 return (SubBuffer);
458 SubBuffer++;
461 return (NULL);
465 /******************************************************************************
467 * FUNCTION: AsRemoveExtraLines
469 * DESCRIPTION: Remove all extra lines at the start and end of the file.
471 ******************************************************************************/
473 void
474 AsRemoveExtraLines (
475 char *FileBuffer,
476 char *Filename)
478 char *FileEnd;
479 int Length;
482 /* Remove any extra lines at the start of the file */
484 while (*FileBuffer == '\n')
486 printf ("Removing extra line at start of file: %s\n", Filename);
487 AsRemoveData (FileBuffer, FileBuffer + 1);
490 /* Remove any extra lines at the end of the file */
492 Length = strlen (FileBuffer);
493 FileEnd = FileBuffer + (Length - 2);
495 while (*FileEnd == '\n')
497 printf ("Removing extra line at end of file: %s\n", Filename);
498 AsRemoveData (FileEnd, FileEnd + 1);
499 FileEnd--;
504 /******************************************************************************
506 * FUNCTION: AsRemoveSpacesAfterPeriod
508 * DESCRIPTION: Remove an extra space after a period.
510 ******************************************************************************/
512 void
513 AsRemoveSpacesAfterPeriod (
514 char *FileBuffer,
515 char *Filename)
517 int ReplaceCount = 0;
518 char *Possible;
521 Possible = FileBuffer;
522 while (Possible)
524 Possible = strstr (Possible, ". ");
525 if (Possible)
527 if ((*(Possible -1) == '.') ||
528 (*(Possible -1) == '\"') ||
529 (*(Possible -1) == '\n'))
531 Possible += 3;
532 continue;
535 Possible = AsReplaceData (Possible, 3, ". ", 2);
536 ReplaceCount++;
540 if (ReplaceCount)
542 printf ("Removed %d extra blanks after a period: %s\n",
543 ReplaceCount, Filename);
548 /******************************************************************************
550 * FUNCTION: AsMatchExactWord
552 * DESCRIPTION: Check previous and next characters for whitespace
554 ******************************************************************************/
556 BOOLEAN
557 AsMatchExactWord (
558 char *Word,
559 UINT32 WordLength)
561 char NextChar;
562 char PrevChar;
565 NextChar = Word[WordLength];
566 PrevChar = * (Word -1);
568 if (isalnum ((int) NextChar) ||
569 (NextChar == '_') ||
570 isalnum ((int) PrevChar) ||
571 (PrevChar == '_'))
573 return (FALSE);
576 return (TRUE);
580 /******************************************************************************
582 * FUNCTION: AsPrint
584 * DESCRIPTION: Common formatted print
586 ******************************************************************************/
588 void
589 AsPrint (
590 char *Message,
591 UINT32 Count,
592 char *Filename)
595 if (Gbl_QuietMode)
597 return;
600 printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
604 /******************************************************************************
606 * FUNCTION: AsTrimLines
608 * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
609 * check for tabs.
611 ******************************************************************************/
613 void
614 AsTrimLines (
615 char *Buffer,
616 char *Filename)
618 char *SubBuffer = Buffer;
619 char *StartWhiteSpace = NULL;
620 UINT32 SpaceCount = 0;
623 while (*SubBuffer)
625 while (*SubBuffer != '\n')
627 if (!*SubBuffer)
629 goto Exit;
632 if (*SubBuffer == ' ')
634 if (!StartWhiteSpace)
636 StartWhiteSpace = SubBuffer;
639 else
641 StartWhiteSpace = NULL;
644 SubBuffer++;
647 if (StartWhiteSpace)
649 SpaceCount += (SubBuffer - StartWhiteSpace);
651 /* Remove the spaces */
653 SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
654 StartWhiteSpace = NULL;
657 SubBuffer++;
661 Exit:
662 if (SpaceCount)
664 Gbl_MadeChanges = TRUE;
665 AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
670 /******************************************************************************
672 * FUNCTION: AsTrimWhitespace
674 * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
675 * this can happen during the translation when lines are removed.
677 ******************************************************************************/
679 void
680 AsTrimWhitespace (
681 char *Buffer)
683 char *SubBuffer;
684 int ReplaceCount = 1;
687 while (ReplaceCount)
689 ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n",
690 REPLACE_SUBSTRINGS, Buffer);
694 * Check for exactly one blank line after the copyright header
697 /* Find the header */
699 SubBuffer = strstr (Buffer, HeaderBegin);
700 if (!SubBuffer)
702 return;
705 /* Find the end of the header */
707 SubBuffer = strstr (SubBuffer, "*/");
708 SubBuffer = AsSkipPastChar (SubBuffer, '\n');
710 /* Replace a double blank line with a single */
712 if (!strncmp (SubBuffer, "\n\n", 2))
714 AsReplaceData (SubBuffer, 2, "\n", 1);
715 AcpiOsPrintf ("Found multiple blank lines after copyright\n");
718 /* If no blank line after header, insert one */
720 else if (*SubBuffer != '\n')
722 AsInsertData (SubBuffer, "\n", 1);
723 AcpiOsPrintf ("Inserted blank line after copyright\n");
728 /******************************************************************************
730 * FUNCTION: AsReplaceHeader
732 * DESCRIPTION: Replace the default Intel legal header with a new header
734 ******************************************************************************/
736 void
737 AsReplaceHeader (
738 char *Buffer,
739 char *NewHeader)
741 char *SubBuffer;
742 char *TokenEnd;
745 /* Find the original header */
747 SubBuffer = strstr (Buffer, HeaderBegin);
748 if (!SubBuffer)
750 return;
753 /* Find the end of the original header */
755 TokenEnd = strstr (SubBuffer, "*/");
756 TokenEnd = AsSkipPastChar (TokenEnd, '\n');
758 /* Delete old header, insert new one */
760 AsReplaceData (SubBuffer, TokenEnd - SubBuffer,
761 NewHeader, strlen (NewHeader));
765 /******************************************************************************
767 * FUNCTION: AsDoSpdxHeader
769 * DESCRIPTION: Replace the default Intel legal header with a new header
771 ******************************************************************************/
773 void
774 AsDoSpdxHeader (
775 char *Buffer,
776 char *SpdxHeader)
778 char *SubBuffer;
781 /* Place an SPDX header at the very top */
783 AsReplaceData (Buffer, 0,
784 SpdxHeader, strlen (SpdxHeader));
786 /* Place an Intel copyright notice in the module header */
788 SubBuffer = strstr (Buffer, MODULE_HEADER_END);
789 if (!SubBuffer)
791 return;
794 AsReplaceData (SubBuffer, strlen (MODULE_HEADER_END),
795 CopyRightHeaderEnd, strlen (CopyRightHeaderEnd));
798 /******************************************************************************
800 * FUNCTION: AsReplaceString
802 * DESCRIPTION: Replace all instances of a target string with a replacement
803 * string. Returns count of the strings replaced.
805 ******************************************************************************/
808 AsReplaceString (
809 char *Target,
810 char *Replacement,
811 UINT8 Type,
812 char *Buffer)
814 char *SubString1;
815 char *SubString2;
816 char *SubBuffer;
817 int TargetLength;
818 int ReplacementLength;
819 int ReplaceCount = 0;
822 TargetLength = strlen (Target);
823 ReplacementLength = strlen (Replacement);
825 SubBuffer = Buffer;
826 SubString1 = Buffer;
828 while (SubString1)
830 /* Find the target string */
832 SubString1 = strstr (SubBuffer, Target);
833 if (!SubString1)
835 return (ReplaceCount);
839 * Check for translation escape string -- means to ignore
840 * blocks of code while replacing
842 if (Gbl_IgnoreTranslationEscapes)
844 SubString2 = NULL;
846 else
848 SubString2 = strstr (SubBuffer, AS_START_IGNORE);
851 if ((SubString2) &&
852 (SubString2 < SubString1))
854 /* Find end of the escape block starting at "Substring2" */
856 SubString2 = strstr (SubString2, AS_STOP_IGNORE);
857 if (!SubString2)
859 /* Didn't find terminator */
861 return (ReplaceCount);
864 /* Move buffer to end of escape block and continue */
866 SubBuffer = SubString2;
869 /* Do the actual replace if the target was found */
871 else
873 if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
875 if (!AsMatchExactWord (SubString1, TargetLength))
877 SubBuffer = SubString1 + 1;
878 continue;
882 SubBuffer = AsReplaceData (SubString1, TargetLength,
883 Replacement, ReplacementLength);
885 if ((Type & EXTRA_INDENT_C) &&
886 (!Gbl_StructDefs))
888 SubBuffer = AsInsertData (SubBuffer, " ", 8);
891 ReplaceCount++;
895 return (ReplaceCount);
899 /******************************************************************************
901 * FUNCTION: AsConvertToLineFeeds
903 * DESCRIPTION: Convert all CR/LF pairs to LF only.
905 ******************************************************************************/
907 void
908 AsConvertToLineFeeds (
909 char *Buffer)
911 char *SubString;
912 char *SubBuffer;
915 SubBuffer = Buffer;
916 SubString = Buffer;
918 while (SubString)
920 /* Find the target string */
922 SubString = strstr (SubBuffer, "\r\n");
923 if (!SubString)
925 return;
928 SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
933 /******************************************************************************
935 * FUNCTION: AsInsertCarriageReturns
937 * DESCRIPTION: Convert lone LFs to CR/LF pairs.
939 ******************************************************************************/
941 void
942 AsInsertCarriageReturns (
943 char *Buffer)
945 char *SubString;
946 char *SubBuffer;
949 SubBuffer = Buffer;
950 SubString = Buffer;
952 while (SubString)
954 /* Find the target string */
956 SubString = strstr (SubBuffer, "\n");
957 if (!SubString)
959 return;
962 SubBuffer = AsInsertData (SubString, "\r", 1);
963 SubBuffer += 1;
968 /******************************************************************************
970 * FUNCTION: AsBracesOnSameLine
972 * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
973 * or while statement (leave function opening brace on separate
974 * line).
976 ******************************************************************************/
978 void
979 AsBracesOnSameLine (
980 char *Buffer)
982 char *SubBuffer = Buffer;
983 char *Beginning;
984 char *StartOfThisLine;
985 char *Next;
986 BOOLEAN BlockBegin = TRUE;
989 while (*SubBuffer)
991 /* Ignore comments */
993 if ((SubBuffer[0] == '/') &&
994 (SubBuffer[1] == '*'))
996 SubBuffer = strstr (SubBuffer, "*/");
997 if (!SubBuffer)
999 return;
1002 SubBuffer += 2;
1003 continue;
1006 /* Ignore quoted strings */
1008 if (*SubBuffer == '\"')
1010 SubBuffer++;
1011 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1012 if (!SubBuffer)
1014 return;
1018 if (!strncmp ("\n}", SubBuffer, 2))
1021 * A newline followed by a closing brace closes a function
1022 * or struct or initializer block
1024 BlockBegin = TRUE;
1028 * Move every standalone brace up to the previous line
1029 * Check for digit will ignore initializer lists surrounded by braces.
1030 * This will work until we we need more complex detection.
1032 if ((*SubBuffer == '{') && !isdigit ((int) SubBuffer[1]))
1034 if (BlockBegin)
1036 BlockBegin = FALSE;
1038 else
1041 * Backup to previous non-whitespace
1043 Beginning = SubBuffer - 1;
1044 while ((*Beginning == ' ') ||
1045 (*Beginning == '\n'))
1047 Beginning--;
1050 StartOfThisLine = Beginning;
1051 while (*StartOfThisLine != '\n')
1053 StartOfThisLine--;
1057 * Move the brace up to the previous line, UNLESS:
1059 * 1) There is a conditional compile on the line (starts with '#')
1060 * 2) Previous line ends with an '=' (Start of initializer block)
1061 * 3) Previous line ends with a comma (part of an init list)
1062 * 4) Previous line ends with a backslash (part of a macro)
1064 if ((StartOfThisLine[1] != '#') &&
1065 (*Beginning != '\\') &&
1066 (*Beginning != '/') &&
1067 (*Beginning != '{') &&
1068 (*Beginning != '=') &&
1069 (*Beginning != ','))
1071 Beginning++;
1072 SubBuffer++;
1074 Gbl_MadeChanges = TRUE;
1076 #ifdef ADD_EXTRA_WHITESPACE
1077 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
1078 #else
1079 /* Find non-whitespace start of next line */
1081 Next = SubBuffer + 1;
1082 while ((*Next == ' ') ||
1083 (*Next == '\t'))
1085 Next++;
1088 /* Find non-whitespace start of this line */
1090 StartOfThisLine++;
1091 while ((*StartOfThisLine == ' ') ||
1092 (*StartOfThisLine == '\t'))
1094 StartOfThisLine++;
1098 * Must be a single-line comment to need more whitespace
1099 * Even then, we don't need more if the previous statement
1100 * is an "else".
1102 if ((Next[0] == '/') &&
1103 (Next[1] == '*') &&
1104 (Next[2] != '\n') &&
1106 (!strncmp (StartOfThisLine, "else if", 7) ||
1107 !strncmp (StartOfThisLine, "else while", 10) ||
1108 strncmp (StartOfThisLine, "else", 4)))
1110 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
1112 else
1114 AsReplaceData (Beginning, SubBuffer - Beginning, " {", 2);
1116 #endif
1121 SubBuffer++;
1126 /******************************************************************************
1128 * FUNCTION: AsTabify4
1130 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1131 * preserved.
1133 ******************************************************************************/
1135 void
1136 AsTabify4 (
1137 char *Buffer)
1139 char *SubBuffer = Buffer;
1140 char *NewSubBuffer;
1141 UINT32 SpaceCount = 0;
1142 UINT32 Column = 0;
1145 while (*SubBuffer)
1147 if (*SubBuffer == '\n')
1149 Column = 0;
1151 else
1153 Column++;
1156 /* Ignore comments */
1158 if ((SubBuffer[0] == '/') &&
1159 (SubBuffer[1] == '*'))
1161 SubBuffer = strstr (SubBuffer, "*/");
1162 if (!SubBuffer)
1164 return;
1167 SubBuffer += 2;
1168 continue;
1171 /* Ignore quoted strings */
1173 if (*SubBuffer == '\"')
1175 SubBuffer++;
1176 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1177 if (!SubBuffer)
1179 return;
1181 SpaceCount = 0;
1184 if (*SubBuffer == ' ')
1186 SpaceCount++;
1188 if (SpaceCount >= 4)
1190 SpaceCount = 0;
1192 NewSubBuffer = (SubBuffer + 1) - 4;
1193 *NewSubBuffer = '\t';
1194 NewSubBuffer++;
1196 /* Remove the spaces */
1198 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
1201 if ((Column % 4) == 0)
1203 SpaceCount = 0;
1206 else
1208 SpaceCount = 0;
1211 SubBuffer++;
1216 /******************************************************************************
1218 * FUNCTION: AsTabify8
1220 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1221 * preserved.
1223 ******************************************************************************/
1225 void
1226 AsTabify8 (
1227 char *Buffer)
1229 char *SubBuffer = Buffer;
1230 char *NewSubBuffer;
1231 char *CommentEnd = NULL;
1232 UINT32 SpaceCount = 0;
1233 UINT32 Column = 0;
1234 UINT32 TabCount = 0;
1235 UINT32 LastLineTabCount = 0;
1236 UINT32 LastLineColumnStart = 0;
1237 UINT32 ThisColumnStart = 0;
1238 UINT32 ThisTabCount = 0;
1239 char *FirstNonBlank = NULL;
1242 while (*SubBuffer)
1244 if (*SubBuffer == '\n')
1246 /* This is a standalone blank line */
1248 FirstNonBlank = NULL;
1249 Column = 0;
1250 SpaceCount = 0;
1251 TabCount = 0;
1252 SubBuffer++;
1253 continue;
1256 if (!FirstNonBlank)
1258 /* Find the first non-blank character on this line */
1260 FirstNonBlank = SubBuffer;
1261 while (*FirstNonBlank == ' ')
1263 FirstNonBlank++;
1267 * This mechanism limits the difference in tab counts from
1268 * line to line. It helps avoid the situation where a second
1269 * continuation line (which was indented correctly for tabs=4) would
1270 * get indented off the screen if we just blindly converted to tabs.
1272 ThisColumnStart = FirstNonBlank - SubBuffer;
1274 if (LastLineTabCount == 0)
1276 ThisTabCount = 0;
1278 else if (ThisColumnStart == LastLineColumnStart)
1280 ThisTabCount = LastLineTabCount -1;
1282 else
1284 ThisTabCount = LastLineTabCount + 1;
1288 Column++;
1290 /* Check if we are in a comment */
1292 if ((SubBuffer[0] == '*') &&
1293 (SubBuffer[1] == '/'))
1295 SpaceCount = 0;
1296 SubBuffer += 2;
1298 if (*SubBuffer == '\n')
1300 if (TabCount > 0)
1302 LastLineTabCount = TabCount;
1303 TabCount = 0;
1306 FirstNonBlank = NULL;
1307 LastLineColumnStart = ThisColumnStart;
1308 SubBuffer++;
1311 continue;
1314 /* Check for comment open */
1316 if ((SubBuffer[0] == '/') &&
1317 (SubBuffer[1] == '*'))
1319 /* Find the end of the comment, it must exist */
1321 CommentEnd = strstr (SubBuffer, "*/");
1322 if (!CommentEnd)
1324 return;
1327 /* Toss the rest of this line or single-line comment */
1329 while ((SubBuffer < CommentEnd) &&
1330 (*SubBuffer != '\n'))
1332 SubBuffer++;
1335 if (*SubBuffer == '\n')
1337 if (TabCount > 0)
1339 LastLineTabCount = TabCount;
1340 TabCount = 0;
1343 FirstNonBlank = NULL;
1344 LastLineColumnStart = ThisColumnStart;
1347 SpaceCount = 0;
1348 continue;
1351 /* Ignore quoted strings */
1353 if ((!CommentEnd) && (*SubBuffer == '\"'))
1355 SubBuffer++;
1356 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1357 if (!SubBuffer)
1359 return;
1362 SpaceCount = 0;
1365 if (*SubBuffer != ' ')
1367 /* Not a space, skip to end of line */
1369 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
1370 if (!SubBuffer)
1372 return;
1374 if (TabCount > 0)
1376 LastLineTabCount = TabCount;
1377 TabCount = 0;
1380 FirstNonBlank = NULL;
1381 LastLineColumnStart = ThisColumnStart;
1382 Column = 0;
1383 SpaceCount = 0;
1385 else
1387 /* Another space */
1389 SpaceCount++;
1391 if (SpaceCount >= 4)
1393 /* Replace this group of spaces with a tab character */
1395 SpaceCount = 0;
1397 NewSubBuffer = SubBuffer - 3;
1399 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
1401 *NewSubBuffer = '\t';
1402 NewSubBuffer++;
1403 SubBuffer++;
1404 TabCount++;
1407 /* Remove the spaces */
1409 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
1410 continue;
1414 SubBuffer++;
1419 /******************************************************************************
1421 * FUNCTION: AsCountLines
1423 * DESCRIPTION: Count the number of lines in the input buffer. Also count
1424 * the number of long lines (lines longer than 80 chars).
1426 ******************************************************************************/
1428 static UINT32
1429 AsCountLines (
1430 char *Buffer,
1431 char *Filename)
1433 char *SubBuffer = Buffer;
1434 char *EndOfLine;
1435 UINT32 LineCount = 0;
1436 UINT32 LongLineCount = 0;
1439 while (*SubBuffer)
1441 EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
1442 if (!EndOfLine)
1444 Gbl_TotalLines += LineCount;
1445 return (LineCount);
1448 if ((EndOfLine - SubBuffer) > 80)
1450 LongLineCount++;
1451 VERBOSE_PRINT (("long: %.80s\n", SubBuffer));
1454 LineCount++;
1455 SubBuffer = EndOfLine + 1;
1458 if (LongLineCount)
1460 VERBOSE_PRINT (("%u Lines longer than 80 found in %s\n",
1461 LongLineCount, Filename));
1463 Gbl_LongLines += LongLineCount;
1466 Gbl_TotalLines += LineCount;
1467 return (LineCount);
1471 /******************************************************************************
1473 * FUNCTION: AsCountTabs
1475 * DESCRIPTION: Simply count the number of tabs in the input file buffer
1477 ******************************************************************************/
1479 void
1480 AsCountTabs (
1481 char *Buffer,
1482 char *Filename)
1484 UINT32 i;
1485 UINT32 TabCount = 0;
1488 for (i = 0; Buffer[i]; i++)
1490 if (Buffer[i] == '\t')
1492 TabCount++;
1496 if (TabCount)
1498 AsPrint ("Tabs found", TabCount, Filename);
1499 Gbl_Tabs += TabCount;
1502 AsCountLines (Buffer, Filename);
1506 /******************************************************************************
1508 * FUNCTION: AsCountSourceLines
1510 * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1511 * comment, and 2) not a blank line.
1513 ******************************************************************************/
1515 void
1516 AsCountSourceLines (
1517 char *Buffer,
1518 char *Filename)
1520 char *SubBuffer = Buffer;
1521 UINT32 LineCount = 0;
1522 UINT32 WhiteCount = 0;
1523 UINT32 CommentCount = 0;
1526 while (*SubBuffer)
1528 /* Detect comments (// comments are not used, non-ansii) */
1530 if ((SubBuffer[0] == '/') &&
1531 (SubBuffer[1] == '*'))
1533 SubBuffer += 2;
1535 /* First line of multi-line comment is often just whitespace */
1537 if (SubBuffer[0] == '\n')
1539 WhiteCount++;
1540 SubBuffer++;
1542 else
1544 CommentCount++;
1547 /* Find end of comment */
1549 while (SubBuffer[0] && SubBuffer[1] &&
1550 !(((SubBuffer[0] == '*') &&
1551 (SubBuffer[1] == '/'))))
1553 if (SubBuffer[0] == '\n')
1555 CommentCount++;
1558 SubBuffer++;
1562 /* A linefeed followed by a non-linefeed is a valid source line */
1564 else if ((SubBuffer[0] == '\n') &&
1565 (SubBuffer[1] != '\n'))
1567 LineCount++;
1570 /* Two back-to-back linefeeds indicate a whitespace line */
1572 else if ((SubBuffer[0] == '\n') &&
1573 (SubBuffer[1] == '\n'))
1575 WhiteCount++;
1578 SubBuffer++;
1581 /* Adjust comment count for legal header */
1583 if (Gbl_HeaderSize < CommentCount)
1585 CommentCount -= Gbl_HeaderSize;
1586 Gbl_HeaderLines += Gbl_HeaderSize;
1589 Gbl_SourceLines += LineCount;
1590 Gbl_WhiteLines += WhiteCount;
1591 Gbl_CommentLines += CommentCount;
1593 VERBOSE_PRINT (("%u Comment %u White %u Code %u Lines in %s\n",
1594 CommentCount, WhiteCount, LineCount,
1595 LineCount + WhiteCount + CommentCount, Filename));
1599 /******************************************************************************
1601 * FUNCTION: AsInsertPrefix
1603 * DESCRIPTION: Insert struct or union prefixes
1605 ******************************************************************************/
1607 void
1608 AsInsertPrefix (
1609 char *Buffer,
1610 char *Keyword,
1611 UINT8 Type)
1613 char *SubString;
1614 char *SubBuffer;
1615 char *EndKeyword;
1616 int InsertLength;
1617 char *InsertString;
1618 int TrailingSpaces;
1619 char LowerKeyword[128];
1620 int KeywordLength;
1623 switch (Type)
1625 case SRC_TYPE_STRUCT:
1627 InsertString = "struct ";
1628 break;
1630 case SRC_TYPE_UNION:
1632 InsertString = "union ";
1633 break;
1635 default:
1637 return;
1640 strcpy (LowerKeyword, Keyword);
1641 AcpiUtStrlwr (LowerKeyword);
1643 SubBuffer = Buffer;
1644 SubString = Buffer;
1645 InsertLength = strlen (InsertString);
1646 KeywordLength = strlen (Keyword);
1649 while (SubString)
1651 /* Find an instance of the keyword */
1653 SubString = strstr (SubBuffer, LowerKeyword);
1654 if (!SubString)
1656 return;
1659 SubBuffer = SubString;
1661 /* Must be standalone word, not a substring */
1663 if (AsMatchExactWord (SubString, KeywordLength))
1665 /* Make sure the keyword isn't already prefixed with the insert */
1667 if (!strncmp (SubString - InsertLength, InsertString, InsertLength))
1669 /* Add spaces if not already at the end-of-line */
1671 if (*(SubBuffer + KeywordLength) != '\n')
1673 /* Already present, add spaces after to align structure members */
1675 #if 0
1676 /* ONLY FOR C FILES */
1677 AsInsertData (SubBuffer + KeywordLength, " ", 8);
1678 #endif
1680 goto Next;
1683 /* Make sure the keyword isn't at the end of a struct/union */
1684 /* Note: This code depends on a single space after the brace */
1686 if (*(SubString - 2) == '}')
1688 goto Next;
1691 /* Prefix the keyword with the insert string */
1693 Gbl_MadeChanges = TRUE;
1695 /* Is there room for insertion */
1697 EndKeyword = SubString + strlen (LowerKeyword);
1699 TrailingSpaces = 0;
1700 while (EndKeyword[TrailingSpaces] == ' ')
1702 TrailingSpaces++;
1706 * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1708 SubBuffer = SubString + InsertLength;
1710 if (TrailingSpaces > InsertLength)
1712 /* Insert the keyword */
1714 memmove (SubBuffer, SubString, KeywordLength);
1716 /* Insert the keyword */
1718 memmove (SubString, InsertString, InsertLength);
1720 else
1722 AsInsertData (SubString, InsertString, InsertLength);
1726 Next:
1727 SubBuffer += KeywordLength;
1731 #ifdef ACPI_FUTURE_IMPLEMENTATION
1732 /******************************************************************************
1734 * FUNCTION: AsTrimComments
1736 * DESCRIPTION: Finds 3-line comments with only a single line of text
1738 ******************************************************************************/
1740 void
1741 AsTrimComments (
1742 char *Buffer,
1743 char *Filename)
1745 char *SubBuffer = Buffer;
1746 char *Ptr1;
1747 char *Ptr2;
1748 UINT32 LineCount;
1749 UINT32 ShortCommentCount = 0;
1752 while (1)
1754 /* Find comment open, within procedure level */
1756 SubBuffer = strstr (SubBuffer, " /*");
1757 if (!SubBuffer)
1759 goto Exit;
1762 /* Find comment terminator */
1764 Ptr1 = strstr (SubBuffer, "*/");
1765 if (!Ptr1)
1767 goto Exit;
1770 /* Find next EOL (from original buffer) */
1772 Ptr2 = strstr (SubBuffer, "\n");
1773 if (!Ptr2)
1775 goto Exit;
1778 /* Ignore one-line comments */
1780 if (Ptr1 < Ptr2)
1782 /* Normal comment, ignore and continue; */
1784 SubBuffer = Ptr2;
1785 continue;
1788 /* Examine multi-line comment */
1790 LineCount = 1;
1791 while (Ptr1 > Ptr2)
1793 /* Find next EOL */
1795 Ptr2++;
1796 Ptr2 = strstr (Ptr2, "\n");
1797 if (!Ptr2)
1799 goto Exit;
1802 LineCount++;
1805 SubBuffer = Ptr1;
1807 if (LineCount <= 3)
1809 ShortCommentCount++;
1814 Exit:
1816 if (ShortCommentCount)
1818 AsPrint ("Short Comments found", ShortCommentCount, Filename);
1821 #endif
1823 #ifdef ACPI_UNUSED_FUNCTIONS
1824 /******************************************************************************
1826 * FUNCTION: AsCheckAndSkipLiterals
1828 * DESCRIPTION: Generic routine to skip comments and quoted string literals.
1829 * Keeps a line count.
1831 ******************************************************************************/
1833 static char *
1834 AsCheckAndSkipLiterals (
1835 char *Buffer,
1836 UINT32 *TotalLines);
1839 static char *
1840 AsCheckAndSkipLiterals (
1841 char *Buffer,
1842 UINT32 *TotalLines)
1844 UINT32 NewLines = 0;
1845 char *SubBuffer = Buffer;
1846 char *LiteralEnd;
1849 /* Ignore comments */
1851 if ((SubBuffer[0] == '/') &&
1852 (SubBuffer[1] == '*'))
1854 LiteralEnd = strstr (SubBuffer, "*/");
1855 SubBuffer += 2; /* Get past comment opening */
1857 if (!LiteralEnd)
1859 return (SubBuffer);
1862 while (SubBuffer < LiteralEnd)
1864 if (*SubBuffer == '\n')
1866 NewLines++;
1869 SubBuffer++;
1872 SubBuffer += 2; /* Get past comment close */
1875 /* Ignore quoted strings */
1877 else if (*SubBuffer == '\"')
1879 SubBuffer++;
1880 LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
1881 if (!LiteralEnd)
1883 return (SubBuffer);
1887 if (TotalLines)
1889 (*TotalLines) += NewLines;
1891 return (SubBuffer);
1893 #endif