asconvrt.c revision 1.1 1
2 /******************************************************************************
3 *
4 * Module Name: asconvrt - Source conversion code
5 *
6 *****************************************************************************/
7
8 /******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
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.
21 *
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
28 *
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;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
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.
54 *
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.
66 *
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.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
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.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
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.
90 *
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.
99 *
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.
114 *
115 *****************************************************************************/
116
117 #include "acpisrc.h"
118
119 /* Local prototypes */
120
121 char *
122 AsCheckAndSkipLiterals (
123 char *Buffer,
124 UINT32 *TotalLines);
125
126 UINT32
127 AsCountLines (
128 char *Buffer,
129 char *Filename);
130
131 /* Opening signature of the Intel legal header */
132
133 char *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
134
135
136 /******************************************************************************
137 *
138 * FUNCTION: AsMatchExactWord
139 *
140 * DESCRIPTION: Check previous and next characters for whitespace
141 *
142 ******************************************************************************/
143
144 BOOLEAN
145 AsMatchExactWord (
146 char *Word,
147 UINT32 WordLength)
148 {
149 char NextChar;
150 char PrevChar;
151
152
153 NextChar = Word[WordLength];
154 PrevChar = * (Word -1);
155
156 if (isalnum ((int) NextChar) ||
157 (NextChar == '_') ||
158 isalnum ((int) PrevChar) ||
159 (PrevChar == '_'))
160 {
161 return (FALSE);
162 }
163
164 return (TRUE);
165 }
166
167
168 /******************************************************************************
169 *
170 * FUNCTION: AsPrint
171 *
172 * DESCRIPTION: Common formatted print
173 *
174 ******************************************************************************/
175
176 void
177 AsPrint (
178 char *Message,
179 UINT32 Count,
180 char *Filename)
181 {
182
183 if (Gbl_QuietMode)
184 {
185 return;
186 }
187
188 printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
189 }
190
191
192 /******************************************************************************
193 *
194 * FUNCTION: AsCheckAndSkipLiterals
195 *
196 * DESCRIPTION: Generic routine to skip comments and quoted string literals.
197 * Keeps a line count.
198 *
199 ******************************************************************************/
200
201 char *
202 AsCheckAndSkipLiterals (
203 char *Buffer,
204 UINT32 *TotalLines)
205 {
206 UINT32 NewLines = 0;
207 char *SubBuffer = Buffer;
208 char *LiteralEnd;
209
210
211 /* Ignore comments */
212
213 if ((SubBuffer[0] == '/') &&
214 (SubBuffer[1] == '*'))
215 {
216 LiteralEnd = strstr (SubBuffer, "*/");
217 SubBuffer += 2; /* Get past comment opening */
218
219 if (!LiteralEnd)
220 {
221 return SubBuffer;
222 }
223
224 while (SubBuffer < LiteralEnd)
225 {
226 if (*SubBuffer == '\n')
227 {
228 NewLines++;
229 }
230
231 SubBuffer++;
232 }
233
234 SubBuffer += 2; /* Get past comment close */
235 }
236
237 /* Ignore quoted strings */
238
239 else if (*SubBuffer == '\"')
240 {
241 SubBuffer++;
242 LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
243 if (!LiteralEnd)
244 {
245 return SubBuffer;
246 }
247 }
248
249 if (TotalLines)
250 {
251 (*TotalLines) += NewLines;
252 }
253 return SubBuffer;
254 }
255
256
257 /******************************************************************************
258 *
259 * FUNCTION: AsAsCheckForBraces
260 *
261 * DESCRIPTION: Check for an open brace after each if statement
262 *
263 ******************************************************************************/
264
265 void
266 AsCheckForBraces (
267 char *Buffer,
268 char *Filename)
269 {
270 char *SubBuffer = Buffer;
271 char *NextBrace;
272 char *NextSemicolon;
273 char *NextIf;
274 UINT32 TotalLines = 1;
275
276
277 while (*SubBuffer)
278 {
279
280 SubBuffer = AsCheckAndSkipLiterals (SubBuffer, &TotalLines);
281
282 if (*SubBuffer == '\n')
283 {
284 TotalLines++;
285 }
286 else if (!(strncmp (" if", SubBuffer, 3)))
287 {
288 SubBuffer += 2;
289 NextBrace = strstr (SubBuffer, "{");
290 NextSemicolon = strstr (SubBuffer, ";");
291 NextIf = strstr (SubBuffer, " if");
292
293 if ((!NextBrace) ||
294 (NextSemicolon && (NextBrace > NextSemicolon)) ||
295 (NextIf && (NextBrace > NextIf)))
296 {
297 Gbl_MissingBraces++;
298
299 if (!Gbl_QuietMode)
300 {
301 printf ("Missing braces for <if>, line %u: %s\n", TotalLines, Filename);
302 }
303 }
304 }
305 else if (!(strncmp (" else if", SubBuffer, 8)))
306 {
307 SubBuffer += 7;
308 NextBrace = strstr (SubBuffer, "{");
309 NextSemicolon = strstr (SubBuffer, ";");
310 NextIf = strstr (SubBuffer, " if");
311
312 if ((!NextBrace) ||
313 (NextSemicolon && (NextBrace > NextSemicolon)) ||
314 (NextIf && (NextBrace > NextIf)))
315 {
316 Gbl_MissingBraces++;
317
318 if (!Gbl_QuietMode)
319 {
320 printf ("Missing braces for <if>, line %u: %s\n", TotalLines, Filename);
321 }
322 }
323 }
324 else if (!(strncmp (" else", SubBuffer, 5)))
325 {
326 SubBuffer += 4;
327 NextBrace = strstr (SubBuffer, "{");
328 NextSemicolon = strstr (SubBuffer, ";");
329 NextIf = strstr (SubBuffer, " if");
330
331 if ((!NextBrace) ||
332 (NextSemicolon && (NextBrace > NextSemicolon)) ||
333 (NextIf && (NextBrace > NextIf)))
334 {
335 Gbl_MissingBraces++;
336
337 if (!Gbl_QuietMode)
338 {
339 printf ("Missing braces for <else>, line %u: %s\n", TotalLines, Filename);
340 }
341 }
342 }
343
344 SubBuffer++;
345 }
346 }
347
348
349 /******************************************************************************
350 *
351 * FUNCTION: AsTrimLines
352 *
353 * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
354 * check for tabs.
355 *
356 ******************************************************************************/
357
358 void
359 AsTrimLines (
360 char *Buffer,
361 char *Filename)
362 {
363 char *SubBuffer = Buffer;
364 char *StartWhiteSpace = NULL;
365 UINT32 SpaceCount = 0;
366
367
368 while (*SubBuffer)
369 {
370 while (*SubBuffer != '\n')
371 {
372 if (!*SubBuffer)
373 {
374 goto Exit;
375 }
376
377 if (*SubBuffer == ' ')
378 {
379 if (!StartWhiteSpace)
380 {
381 StartWhiteSpace = SubBuffer;
382 }
383 }
384 else
385 {
386 StartWhiteSpace = NULL;
387 }
388
389 SubBuffer++;
390 }
391
392 if (StartWhiteSpace)
393 {
394 SpaceCount += (SubBuffer - StartWhiteSpace);
395
396 /* Remove the spaces */
397
398 SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
399 StartWhiteSpace = NULL;
400 }
401
402 SubBuffer++;
403 }
404
405
406 Exit:
407 if (SpaceCount)
408 {
409 Gbl_MadeChanges = TRUE;
410 AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
411 }
412 }
413
414
415 /******************************************************************************
416 *
417 * FUNCTION: AsTrimWhitespace
418 *
419 * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
420 * this can happen during the translation when lines are removed.
421 *
422 ******************************************************************************/
423
424 void
425 AsTrimWhitespace (
426 char *Buffer)
427 {
428 int ReplaceCount = 1;
429
430
431 while (ReplaceCount)
432 {
433 ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n", REPLACE_SUBSTRINGS, Buffer);
434 }
435 }
436
437
438 /******************************************************************************
439 *
440 * FUNCTION: AsReplaceHeader
441 *
442 * DESCRIPTION: Replace the default Intel legal header with a new header
443 *
444 ******************************************************************************/
445
446 void
447 AsReplaceHeader (
448 char *Buffer,
449 char *NewHeader)
450 {
451 char *SubBuffer;
452 char *TokenEnd;
453
454
455 /* Find the original header */
456
457 SubBuffer = strstr (Buffer, HeaderBegin);
458 if (!SubBuffer)
459 {
460 return;
461 }
462
463 /* Find the end of the original header */
464
465 TokenEnd = strstr (SubBuffer, "*/");
466 TokenEnd = AsSkipPastChar (TokenEnd, '\n');
467
468 /* Delete old header, insert new one */
469
470 AsReplaceData (SubBuffer, TokenEnd - SubBuffer, NewHeader, strlen (NewHeader));
471 }
472
473
474 /******************************************************************************
475 *
476 * FUNCTION: AsReplaceString
477 *
478 * DESCRIPTION: Replace all instances of a target string with a replacement
479 * string. Returns count of the strings replaced.
480 *
481 ******************************************************************************/
482
483 int
484 AsReplaceString (
485 char *Target,
486 char *Replacement,
487 UINT8 Type,
488 char *Buffer)
489 {
490 char *SubString1;
491 char *SubString2;
492 char *SubBuffer;
493 int TargetLength;
494 int ReplacementLength;
495 int ReplaceCount = 0;
496
497
498 TargetLength = strlen (Target);
499 ReplacementLength = strlen (Replacement);
500
501 SubBuffer = Buffer;
502 SubString1 = Buffer;
503
504 while (SubString1)
505 {
506 /* Find the target string */
507
508 SubString1 = strstr (SubBuffer, Target);
509 if (!SubString1)
510 {
511 return ReplaceCount;
512 }
513
514 /*
515 * Check for translation escape string -- means to ignore
516 * blocks of code while replacing
517 */
518 SubString2 = strstr (SubBuffer, AS_START_IGNORE);
519
520 if ((SubString2) &&
521 (SubString2 < SubString1))
522 {
523 /* Find end of the escape block starting at "Substring2" */
524
525 SubString2 = strstr (SubString2, AS_STOP_IGNORE);
526 if (!SubString2)
527 {
528 /* Didn't find terminator */
529
530 return ReplaceCount;
531 }
532
533 /* Move buffer to end of escape block and continue */
534
535 SubBuffer = SubString2;
536 }
537
538 /* Do the actual replace if the target was found */
539
540 else
541 {
542 if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
543 {
544 if (!AsMatchExactWord (SubString1, TargetLength))
545 {
546 SubBuffer = SubString1 + 1;
547 continue;
548 }
549 }
550
551 SubBuffer = AsReplaceData (SubString1, TargetLength, Replacement, ReplacementLength);
552
553 if ((Type & EXTRA_INDENT_C) &&
554 (!Gbl_StructDefs))
555 {
556 SubBuffer = AsInsertData (SubBuffer, " ", 8);
557 }
558
559 ReplaceCount++;
560 }
561 }
562
563 return ReplaceCount;
564 }
565
566
567 /******************************************************************************
568 *
569 * FUNCTION: AsConvertToLineFeeds
570 *
571 * DESCRIPTION:
572 *
573 ******************************************************************************/
574
575 void
576 AsConvertToLineFeeds (
577 char *Buffer)
578 {
579 char *SubString;
580 char *SubBuffer;
581
582
583 SubBuffer = Buffer;
584 SubString = Buffer;
585
586 while (SubString)
587 {
588 /* Find the target string */
589
590 SubString = strstr (SubBuffer, "\r\n");
591 if (!SubString)
592 {
593 return;
594 }
595
596 SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
597 }
598 return;
599 }
600
601
602 /******************************************************************************
603 *
604 * FUNCTION: AsInsertCarriageReturns
605 *
606 * DESCRIPTION:
607 *
608 ******************************************************************************/
609
610 void
611 AsInsertCarriageReturns (
612 char *Buffer)
613 {
614 char *SubString;
615 char *SubBuffer;
616
617
618 SubBuffer = Buffer;
619 SubString = Buffer;
620
621 while (SubString)
622 {
623 /* Find the target string */
624
625 SubString = strstr (SubBuffer, "\n");
626 if (!SubString)
627 {
628 return;
629 }
630
631 SubBuffer = AsInsertData (SubString, "\r", 1);
632 SubBuffer += 1;
633 }
634 return;
635 }
636
637
638 /******************************************************************************
639 *
640 * FUNCTION: AsBracesOnSameLine
641 *
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).
645 *
646 ******************************************************************************/
647
648 void
649 AsBracesOnSameLine (
650 char *Buffer)
651 {
652 UINT32 Length;
653 char *SubBuffer = Buffer;
654 char *Beginning;
655 char *StartOfThisLine;
656 char *Next;
657 BOOLEAN BlockBegin = TRUE;
658
659
660 while (*SubBuffer)
661 {
662 /* Ignore comments */
663
664 if ((SubBuffer[0] == '/') &&
665 (SubBuffer[1] == '*'))
666 {
667 SubBuffer = strstr (SubBuffer, "*/");
668 if (!SubBuffer)
669 {
670 return;
671 }
672
673 SubBuffer += 2;
674 continue;
675 }
676
677 /* Ignore quoted strings */
678
679 if (*SubBuffer == '\"')
680 {
681 SubBuffer++;
682 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
683 if (!SubBuffer)
684 {
685 return;
686 }
687 }
688
689 if (!strncmp ("\n}", SubBuffer, 2))
690 {
691 /*
692 * A newline followed by a closing brace closes a function
693 * or struct or initializer block
694 */
695 BlockBegin = TRUE;
696 }
697
698 /*
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.
702 */
703 if ((*SubBuffer == '{') && !isdigit ((int) SubBuffer[1]))
704 {
705 if (BlockBegin)
706 {
707 BlockBegin = FALSE;
708 }
709 else
710 {
711 /*
712 * Backup to previous non-whitespace
713 */
714 Beginning = SubBuffer - 1;
715 while ((*Beginning == ' ') ||
716 (*Beginning == '\n'))
717 {
718 Beginning--;
719 }
720
721 StartOfThisLine = Beginning;
722 while (*StartOfThisLine != '\n')
723 {
724 StartOfThisLine--;
725 }
726
727 /*
728 * Move the brace up to the previous line, UNLESS:
729 *
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)
734 */
735 if ((StartOfThisLine[1] != '#') &&
736 (*Beginning != '\\') &&
737 (*Beginning != '/') &&
738 (*Beginning != '{') &&
739 (*Beginning != '=') &&
740 (*Beginning != ','))
741 {
742 Beginning++;
743 SubBuffer++;
744 Length = strlen (SubBuffer);
745
746 Gbl_MadeChanges = TRUE;
747
748 #ifdef ADD_EXTRA_WHITESPACE
749 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
750 #else
751 /* Find non-whitespace start of next line */
752
753 Next = SubBuffer + 1;
754 while ((*Next == ' ') ||
755 (*Next == '\t'))
756 {
757 Next++;
758 }
759
760 /* Find non-whitespace start of this line */
761
762 StartOfThisLine++;
763 while ((*StartOfThisLine == ' ') ||
764 (*StartOfThisLine == '\t'))
765 {
766 StartOfThisLine++;
767 }
768
769 /*
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".
773 */
774 if ((Next[0] == '/') &&
775 (Next[1] == '*') &&
776 (Next[2] != '\n') &&
777
778 (!strncmp (StartOfThisLine, "else if", 7) ||
779 !strncmp (StartOfThisLine, "else while", 10) ||
780 strncmp (StartOfThisLine, "else", 4)))
781 {
782 AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
783 }
784 else
785 {
786 AsReplaceData (Beginning, SubBuffer - Beginning, " {", 2);
787 }
788 #endif
789 }
790 }
791 }
792
793 SubBuffer++;
794 }
795 }
796
797
798 /******************************************************************************
799 *
800 * FUNCTION: AsTabify4
801 *
802 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
803 * preserved.
804 *
805 ******************************************************************************/
806
807 void
808 AsTabify4 (
809 char *Buffer)
810 {
811 char *SubBuffer = Buffer;
812 char *NewSubBuffer;
813 UINT32 SpaceCount = 0;
814 UINT32 Column = 0;
815
816
817 while (*SubBuffer)
818 {
819 if (*SubBuffer == '\n')
820 {
821 Column = 0;
822 }
823 else
824 {
825 Column++;
826 }
827
828 /* Ignore comments */
829
830 if ((SubBuffer[0] == '/') &&
831 (SubBuffer[1] == '*'))
832 {
833 SubBuffer = strstr (SubBuffer, "*/");
834 if (!SubBuffer)
835 {
836 return;
837 }
838
839 SubBuffer += 2;
840 continue;
841 }
842
843 /* Ignore quoted strings */
844
845 if (*SubBuffer == '\"')
846 {
847 SubBuffer++;
848 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
849 if (!SubBuffer)
850 {
851 return;
852 }
853 SpaceCount = 0;
854 }
855
856 if (*SubBuffer == ' ')
857 {
858 SpaceCount++;
859
860 if (SpaceCount >= 4)
861 {
862 SpaceCount = 0;
863
864 NewSubBuffer = (SubBuffer + 1) - 4;
865 *NewSubBuffer = '\t';
866 NewSubBuffer++;
867
868 /* Remove the spaces */
869
870 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
871 }
872
873 if ((Column % 4) == 0)
874 {
875 SpaceCount = 0;
876 }
877 }
878 else
879 {
880 SpaceCount = 0;
881 }
882
883 SubBuffer++;
884 }
885 }
886
887
888 /******************************************************************************
889 *
890 * FUNCTION: AsTabify8
891 *
892 * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
893 * preserved.
894 *
895 ******************************************************************************/
896
897 void
898 AsTabify8 (
899 char *Buffer)
900 {
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;
912
913
914 while (*SubBuffer)
915 {
916 if (*SubBuffer == '\n')
917 {
918 /* This is a standalone blank line */
919
920 FirstNonBlank = NULL;
921 Column = 0;
922 SpaceCount = 0;
923 TabCount = 0;
924 SubBuffer++;
925 continue;
926 }
927
928 if (!FirstNonBlank)
929 {
930 /* Find the first non-blank character on this line */
931
932 FirstNonBlank = SubBuffer;
933 while (*FirstNonBlank == ' ')
934 {
935 FirstNonBlank++;
936 }
937
938 /*
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.
943 */
944 ThisColumnStart = FirstNonBlank - SubBuffer;
945
946 if (LastLineTabCount == 0)
947 {
948 ThisTabCount = 0;
949 }
950 else if (ThisColumnStart == LastLineColumnStart)
951 {
952 ThisTabCount = LastLineTabCount -1;
953 }
954 else
955 {
956 ThisTabCount = LastLineTabCount + 1;
957 }
958 }
959
960 Column++;
961
962 /* Check if we are in a comment */
963
964 if ((SubBuffer[0] == '*') &&
965 (SubBuffer[1] == '/'))
966 {
967 SpaceCount = 0;
968 SubBuffer += 2;
969
970 if (*SubBuffer == '\n')
971 {
972 if (TabCount > 0)
973 {
974 LastLineTabCount = TabCount;
975 TabCount = 0;
976 }
977 FirstNonBlank = NULL;
978 LastLineColumnStart = ThisColumnStart;
979 SubBuffer++;
980 }
981
982 continue;
983 }
984
985 /* Check for comment open */
986
987 if ((SubBuffer[0] == '/') &&
988 (SubBuffer[1] == '*'))
989 {
990 /* Find the end of the comment, it must exist */
991
992 CommentEnd = strstr (SubBuffer, "*/");
993 if (!CommentEnd)
994 {
995 return;
996 }
997
998 /* Toss the rest of this line or single-line comment */
999
1000 while ((SubBuffer < CommentEnd) &&
1001 (*SubBuffer != '\n'))
1002 {
1003 SubBuffer++;
1004 }
1005
1006 if (*SubBuffer == '\n')
1007 {
1008 if (TabCount > 0)
1009 {
1010 LastLineTabCount = TabCount;
1011 TabCount = 0;
1012 }
1013 FirstNonBlank = NULL;
1014 LastLineColumnStart = ThisColumnStart;
1015 }
1016
1017 SpaceCount = 0;
1018 continue;
1019 }
1020
1021 /* Ignore quoted strings */
1022
1023 if ((!CommentEnd) && (*SubBuffer == '\"'))
1024 {
1025 SubBuffer++;
1026 SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1027 if (!SubBuffer)
1028 {
1029 return;
1030 }
1031 SpaceCount = 0;
1032 }
1033
1034 if (*SubBuffer != ' ')
1035 {
1036 /* Not a space, skip to end of line */
1037
1038 SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
1039 if (!SubBuffer)
1040 {
1041 return;
1042 }
1043 if (TabCount > 0)
1044 {
1045 LastLineTabCount = TabCount;
1046 TabCount = 0;
1047 }
1048
1049 FirstNonBlank = NULL;
1050 LastLineColumnStart = ThisColumnStart;
1051 Column = 0;
1052 SpaceCount = 0;
1053 }
1054 else
1055 {
1056 /* Another space */
1057
1058 SpaceCount++;
1059
1060 if (SpaceCount >= 4)
1061 {
1062 /* Replace this group of spaces with a tab character */
1063
1064 SpaceCount = 0;
1065
1066 NewSubBuffer = SubBuffer - 3;
1067
1068 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
1069 {
1070 *NewSubBuffer = '\t';
1071 NewSubBuffer++;
1072 SubBuffer++;
1073 TabCount++;
1074 }
1075
1076 /* Remove the spaces */
1077
1078 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
1079 continue;
1080 }
1081 }
1082
1083 SubBuffer++;
1084 }
1085 }
1086
1087
1088 /******************************************************************************
1089 *
1090 * FUNCTION: AsCountLines
1091 *
1092 * DESCRIPTION: Count the number of lines in the input buffer. Also count
1093 * the number of long lines (lines longer than 80 chars).
1094 *
1095 ******************************************************************************/
1096
1097 UINT32
1098 AsCountLines (
1099 char *Buffer,
1100 char *Filename)
1101 {
1102 char *SubBuffer = Buffer;
1103 char *EndOfLine;
1104 UINT32 LineCount = 0;
1105 UINT32 LongLineCount = 0;
1106
1107
1108 while (*SubBuffer)
1109 {
1110 EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
1111 if (!EndOfLine)
1112 {
1113 Gbl_TotalLines += LineCount;
1114 return LineCount;
1115 }
1116
1117 if ((EndOfLine - SubBuffer) > 80)
1118 {
1119 LongLineCount++;
1120 VERBOSE_PRINT (("long: %.80s\n", SubBuffer));
1121 }
1122
1123 LineCount++;
1124 SubBuffer = EndOfLine + 1;
1125 }
1126
1127 if (LongLineCount)
1128 {
1129 VERBOSE_PRINT (("%u Lines longer than 80 found in %s\n", LongLineCount, Filename));
1130 Gbl_LongLines += LongLineCount;
1131 }
1132
1133 Gbl_TotalLines += LineCount;
1134 return LineCount;
1135 }
1136
1137
1138 /******************************************************************************
1139 *
1140 * FUNCTION: AsCountTabs
1141 *
1142 * DESCRIPTION: Simply count the number of tabs in the input file buffer
1143 *
1144 ******************************************************************************/
1145
1146 void
1147 AsCountTabs (
1148 char *Buffer,
1149 char *Filename)
1150 {
1151 UINT32 i;
1152 UINT32 TabCount = 0;
1153
1154
1155 for (i = 0; Buffer[i]; i++)
1156 {
1157 if (Buffer[i] == '\t')
1158 {
1159 TabCount++;
1160 }
1161 }
1162
1163 if (TabCount)
1164 {
1165 AsPrint ("Tabs found", TabCount, Filename);
1166 Gbl_Tabs += TabCount;
1167 }
1168
1169 AsCountLines (Buffer, Filename);
1170 }
1171
1172
1173 /******************************************************************************
1174 *
1175 * FUNCTION: AsCountNonAnsiComments
1176 *
1177 * DESCRIPTION: Count the number of "//" comments. This type of comment is
1178 * non-ANSI C.
1179 *
1180 ******************************************************************************/
1181
1182 void
1183 AsCountNonAnsiComments (
1184 char *Buffer,
1185 char *Filename)
1186 {
1187 char *SubBuffer = Buffer;
1188 UINT32 CommentCount = 0;
1189
1190
1191 while (SubBuffer)
1192 {
1193 SubBuffer = strstr (SubBuffer, "//");
1194 if (SubBuffer)
1195 {
1196 CommentCount++;
1197 SubBuffer += 2;
1198 }
1199 }
1200
1201 if (CommentCount)
1202 {
1203 AsPrint ("Non-ANSI Comments found", CommentCount, Filename);
1204 Gbl_NonAnsiComments += CommentCount;
1205 }
1206 }
1207
1208
1209 /******************************************************************************
1210 *
1211 * FUNCTION: AsCountSourceLines
1212 *
1213 * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1214 * comment, and 2) not a blank line.
1215 *
1216 ******************************************************************************/
1217
1218 void
1219 AsCountSourceLines (
1220 char *Buffer,
1221 char *Filename)
1222 {
1223 char *SubBuffer = Buffer;
1224 UINT32 LineCount = 0;
1225 UINT32 WhiteCount = 0;
1226 UINT32 CommentCount = 0;
1227
1228
1229 while (*SubBuffer)
1230 {
1231 /* Detect comments (// comments are not used, non-ansii) */
1232
1233 if ((SubBuffer[0] == '/') &&
1234 (SubBuffer[1] == '*'))
1235 {
1236 SubBuffer += 2;
1237
1238 /* First line of multi-line comment is often just whitespace */
1239
1240 if (SubBuffer[0] == '\n')
1241 {
1242 WhiteCount++;
1243 SubBuffer++;
1244 }
1245 else
1246 {
1247 CommentCount++;
1248 }
1249
1250 /* Find end of comment */
1251
1252 while (SubBuffer[0] && SubBuffer[1] &&
1253 !(((SubBuffer[0] == '*') &&
1254 (SubBuffer[1] == '/'))))
1255 {
1256 if (SubBuffer[0] == '\n')
1257 {
1258 CommentCount++;
1259 }
1260
1261 SubBuffer++;
1262 }
1263 }
1264
1265 /* A linefeed followed by a non-linefeed is a valid source line */
1266
1267 else if ((SubBuffer[0] == '\n') &&
1268 (SubBuffer[1] != '\n'))
1269 {
1270 LineCount++;
1271 }
1272
1273 /* Two back-to-back linefeeds indicate a whitespace line */
1274
1275 else if ((SubBuffer[0] == '\n') &&
1276 (SubBuffer[1] == '\n'))
1277 {
1278 WhiteCount++;
1279 }
1280
1281 SubBuffer++;
1282 }
1283
1284 /* Adjust comment count for legal header */
1285
1286 if (Gbl_HeaderSize < CommentCount)
1287 {
1288 CommentCount -= Gbl_HeaderSize;
1289 Gbl_HeaderLines += Gbl_HeaderSize;
1290 }
1291
1292 Gbl_SourceLines += LineCount;
1293 Gbl_WhiteLines += WhiteCount;
1294 Gbl_CommentLines += CommentCount;
1295
1296 VERBOSE_PRINT (("%u Comment %u White %u Code %u Lines in %s\n",
1297 CommentCount, WhiteCount, LineCount, LineCount+WhiteCount+CommentCount, Filename));
1298 }
1299
1300
1301 /******************************************************************************
1302 *
1303 * FUNCTION: AsInsertPrefix
1304 *
1305 * DESCRIPTION: Insert struct or union prefixes
1306 *
1307 ******************************************************************************/
1308
1309 void
1310 AsInsertPrefix (
1311 char *Buffer,
1312 char *Keyword,
1313 UINT8 Type)
1314 {
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;
1324
1325
1326 switch (Type)
1327 {
1328 case SRC_TYPE_STRUCT:
1329 InsertString = "struct ";
1330 break;
1331
1332 case SRC_TYPE_UNION:
1333 InsertString = "union ";
1334 break;
1335
1336 default:
1337 return;
1338 }
1339
1340 strcpy (LowerKeyword, Keyword);
1341 strlwr (LowerKeyword);
1342
1343 SubBuffer = Buffer;
1344 SubString = Buffer;
1345 InsertLength = strlen (InsertString);
1346 KeywordLength = strlen (Keyword);
1347
1348
1349 while (SubString)
1350 {
1351 /* Find an instance of the keyword */
1352
1353 SubString = strstr (SubBuffer, LowerKeyword);
1354
1355 if (!SubString)
1356 {
1357 return;
1358 }
1359
1360 SubBuffer = SubString;
1361
1362 /* Must be standalone word, not a substring */
1363
1364 if (AsMatchExactWord (SubString, KeywordLength))
1365 {
1366 /* Make sure the keyword isn't already prefixed with the insert */
1367
1368 if (!strncmp (SubString - InsertLength, InsertString, InsertLength))
1369 {
1370 /* Add spaces if not already at the end-of-line */
1371
1372 if (*(SubBuffer + KeywordLength) != '\n')
1373 {
1374 /* Already present, add spaces after to align structure members */
1375
1376 #if 0
1377 /* ONLY FOR C FILES */
1378 AsInsertData (SubBuffer + KeywordLength, " ", 8);
1379 #endif
1380 }
1381 goto Next;
1382 }
1383
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 */
1386
1387 if (*(SubString - 2) == '}')
1388 {
1389 goto Next;
1390 }
1391
1392 /* Prefix the keyword with the insert string */
1393
1394 Gbl_MadeChanges = TRUE;
1395 StrLength = strlen (SubString);
1396
1397 /* Is there room for insertion */
1398
1399 EndKeyword = SubString + strlen (LowerKeyword);
1400
1401 TrailingSpaces = 0;
1402 while (EndKeyword[TrailingSpaces] == ' ')
1403 {
1404 TrailingSpaces++;
1405 }
1406
1407 /*
1408 * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1409 */
1410 SubBuffer = SubString + InsertLength;
1411
1412 if (TrailingSpaces > InsertLength)
1413 {
1414 /* Insert the keyword */
1415
1416 memmove (SubBuffer, SubString, KeywordLength);
1417
1418 /* Insert the keyword */
1419
1420 memmove (SubString, InsertString, InsertLength);
1421 }
1422 else
1423 {
1424 AsInsertData (SubString, InsertString, InsertLength);
1425 }
1426 }
1427
1428 Next:
1429 SubBuffer += KeywordLength;
1430 }
1431 }
1432
1433 #ifdef ACPI_FUTURE_IMPLEMENTATION
1434 /******************************************************************************
1435 *
1436 * FUNCTION: AsTrimComments
1437 *
1438 * DESCRIPTION: Finds 3-line comments with only a single line of text
1439 *
1440 ******************************************************************************/
1441
1442 void
1443 AsTrimComments (
1444 char *Buffer,
1445 char *Filename)
1446 {
1447 char *SubBuffer = Buffer;
1448 char *Ptr1;
1449 char *Ptr2;
1450 UINT32 LineCount;
1451 UINT32 ShortCommentCount = 0;
1452
1453
1454 while (1)
1455 {
1456 /* Find comment open, within procedure level */
1457
1458 SubBuffer = strstr (SubBuffer, " /*");
1459 if (!SubBuffer)
1460 {
1461 goto Exit;
1462 }
1463
1464 /* Find comment terminator */
1465
1466 Ptr1 = strstr (SubBuffer, "*/");
1467 if (!Ptr1)
1468 {
1469 goto Exit;
1470 }
1471
1472 /* Find next EOL (from original buffer) */
1473
1474 Ptr2 = strstr (SubBuffer, "\n");
1475 if (!Ptr2)
1476 {
1477 goto Exit;
1478 }
1479
1480 /* Ignore one-line comments */
1481
1482 if (Ptr1 < Ptr2)
1483 {
1484 /* Normal comment, ignore and continue; */
1485
1486 SubBuffer = Ptr2;
1487 continue;
1488 }
1489
1490 /* Examine multi-line comment */
1491
1492 LineCount = 1;
1493 while (Ptr1 > Ptr2)
1494 {
1495 /* Find next EOL */
1496
1497 Ptr2++;
1498 Ptr2 = strstr (Ptr2, "\n");
1499 if (!Ptr2)
1500 {
1501 goto Exit;
1502 }
1503
1504 LineCount++;
1505 }
1506
1507 SubBuffer = Ptr1;
1508
1509 if (LineCount <= 3)
1510 {
1511 ShortCommentCount++;
1512 }
1513 }
1514
1515
1516 Exit:
1517
1518 if (ShortCommentCount)
1519 {
1520 AsPrint ("Short Comments found", ShortCommentCount, Filename);
1521 }
1522 }
1523 #endif
1524
1525
1526