1 /******************************************************************************
2  *
3  * Module Name: asconvrt - Source conversion code
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
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.
20  *
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
27  *
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;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
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.
53  *
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.
65  *
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.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
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.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
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.
89  *
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.
98  *
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.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
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.
133  *
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.
145  *
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.
149  *
150  *****************************************************************************/
151 
152 #include "acpisrc.h"
153 
154 AS_BRACE_INFO               Gbl_BraceInfo[] =
155 {
156     {" if",         3},
157     {" else if",    8},
158     {" else while", 11},
159     {" else",       5},
160     {" do ",        4},
161     {NULL,          0}
162 };
163 
164 
165 /* Local prototypes */
166 
167 static char *
168 AsMatchValidToken (
169     char                    *Buffer,
170     char                    *Filename,
171     char                    TargetChar,
172     AS_SCAN_CALLBACK        Callback);
173 
174 static char *
175 AsCheckBracesCallback (
176     char                    *Buffer,
177     char                    *Filename,
178     UINT32                  LineNumber);
179 
180 static UINT32
181 AsCountLines (
182     char                    *Buffer,
183     char                    *Filename);
184 
185 
186 
187 #define MODULE_HEADER_BEGIN "/******************************************************************************\n *\n * Module Name:";
188 #define MODULE_HEADER_END   " *****************************************************************************/\n\n"
189 #define INTEL_COPYRIGHT     " * Copyright (C) 2000 - 2018, Intel Corp.\n"
190 
191 /* Opening signature of the Intel legal header */
192 
193 char        *HeaderBegin = "/******************************************************************************\n *\n * 1. Copyright Notice";
194 
195 UINT32      NonAnsiCommentCount;
196 
197 char        CopyRightHeaderEnd[] = INTEL_COPYRIGHT " *\n" MODULE_HEADER_END;
198 
199 /******************************************************************************
200  *
201  * FUNCTION:    AsCountNonAnsiComments
202  *
203  * DESCRIPTION: Count the number of "//" comments. This type of comment is
204  *              non-ANSI C.
205  *
206  * NOTE: July 2014: Allows // within quoted strings and within normal
207  *       comments. Eliminates extraneous warnings from this utility.
208  *
209  ******************************************************************************/
210 
211 void
212 AsCountNonAnsiComments (
213     char                    *Buffer,
214     char                    *Filename)
215 {
216 
217     AsMatchValidToken (Buffer, Filename, 0, NULL);
218 
219     /* Error if any slash-slash comments found */
220 
221     if (NonAnsiCommentCount)
222     {
223         AsPrint ("Non-ANSI // Comments Found", NonAnsiCommentCount, Filename);
224         Gbl_NonAnsiComments += NonAnsiCommentCount;
225     }
226 }
227 
228 
229 /******************************************************************************
230  *
231  * FUNCTION:    AsCheckForBraces
232  *
233  * DESCRIPTION: Check for an open brace after each if/else/do (etc.)
234  *              statement
235  *
236  ******************************************************************************/
237 
238 void
239 AsCheckForBraces (
240     char                    *Buffer,
241     char                    *Filename)
242 {
243 
244     AsMatchValidToken (Buffer, Filename, 0, AsCheckBracesCallback);
245 }
246 
247 
248 /******************************************************************************
249  *
250  * FUNCTION:    AsCheckBracesCallback
251  *
252  * DESCRIPTION: Check if/else/do statements. Ensure that braces
253  *              are always used.
254  *
255  * TBD: Currently, don't check while() statements. The problem is that there
256  * are two forms: do {} while (); and while () {}.
257  *
258  ******************************************************************************/
259 
260 static char *
261 AsCheckBracesCallback (
262     char                    *Buffer,
263     char                    *Filename,
264     UINT32                  LineNumber)
265 {
266     char                    *SubBuffer = Buffer;
267     char                    *NextBrace;
268     char                    *NextSemicolon;
269     AS_BRACE_INFO           *BraceInfo;
270 
271 
272     for (BraceInfo = Gbl_BraceInfo; BraceInfo->Operator; BraceInfo++)
273     {
274         if (!(strncmp (BraceInfo->Operator, SubBuffer, BraceInfo->Length)))
275         {
276             SubBuffer += (BraceInfo->Length - 1);
277 
278             /* Find next brace and the next semicolon */
279 
280             NextBrace = AsMatchValidToken (SubBuffer, Filename, '{', NULL);
281             NextSemicolon = AsMatchValidToken (SubBuffer, Filename, ';', NULL);
282 
283             /* Next brace should appear before next semicolon */
284 
285             if ((!NextBrace) ||
286                (NextSemicolon && (NextBrace > NextSemicolon)))
287             {
288                 Gbl_MissingBraces++;
289 
290                 if (!Gbl_QuietMode)
291                 {
292                     printf ("Missing braces for <%s>, line %u: %s\n",
293                         BraceInfo->Operator + 1, LineNumber, Filename);
294                 }
295             }
296 
297             return (SubBuffer);
298         }
299     }
300 
301     /* No match, just return original buffer */
302 
303     return (Buffer);
304 }
305 
306 
307 /******************************************************************************
308  *
309  * FUNCTION:    AsMatchValidToken
310  *
311  * DESCRIPTION: Find the next matching token in the input buffer.
312  *
313  ******************************************************************************/
314 
315 static char *
316 AsMatchValidToken (
317     char                    *Buffer,
318     char                    *Filename,
319     char                    TargetChar,
320     AS_SCAN_CALLBACK        Callback)
321 {
322     char                    *SubBuffer = Buffer;
323     char                    *StringStart;
324     UINT32                  TotalLines;
325 
326 
327     TotalLines = 1;
328     NonAnsiCommentCount = 0;
329 
330     /* Scan from current position up to the end if necessary */
331 
332     while (*SubBuffer)
333     {
334         /* Skip normal comments */
335 
336         if ((*SubBuffer == '/') &&
337             (*(SubBuffer + 1) == '*'))
338         {
339             /* Must maintain line count */
340 
341             SubBuffer += 2;
342             while (strncmp ("*/", SubBuffer, 2))
343             {
344                 if (*SubBuffer == '\n')
345                 {
346                     TotalLines++;
347                 }
348                 SubBuffer++;
349             }
350 
351             SubBuffer += 2;
352             continue;
353         }
354 
355         /* Skip single quoted chars */
356 
357         if (*SubBuffer == '\'')
358         {
359             SubBuffer++;
360             if (!(*SubBuffer))
361             {
362                 break;
363             }
364 
365             if (*SubBuffer == '\\')
366             {
367                 SubBuffer++;
368             }
369 
370             SubBuffer++;
371             continue;
372         }
373 
374         /* Skip quoted strings */
375 
376         if (*SubBuffer == '"')
377         {
378             StringStart = SubBuffer;
379             SubBuffer++;
380             if (!(*SubBuffer))
381             {
382                 break;
383             }
384 
385             while (*SubBuffer != '"')
386             {
387                 if ((*SubBuffer == '\n') ||
388                     (!(*SubBuffer)))
389                 {
390                     AsPrint ("Unbalanced quoted string",1, Filename);
391                     printf ("    %.32s (line %u)\n", StringStart, TotalLines);
392                     break;
393                 }
394 
395                 /* Handle escapes within the string */
396 
397                 if (*SubBuffer == '\\')
398                 {
399                     SubBuffer++;
400                 }
401 
402                 SubBuffer++;
403             }
404 
405             SubBuffer++;
406             continue;
407         }
408 
409         /* Now we can check for a slash-slash comment */
410 
411         if ((*SubBuffer == '/') &&
412             (*(SubBuffer + 1) == '/'))
413         {
414             NonAnsiCommentCount++;
415 
416             /* Skip to end-of-line */
417 
418             while ((*SubBuffer != '\n') &&
419                 (*SubBuffer))
420             {
421                 SubBuffer++;
422             }
423 
424             if (!(*SubBuffer))
425             {
426                 break;
427             }
428 
429             if (*SubBuffer == '\n')
430             {
431                 TotalLines++;
432             }
433 
434             SubBuffer++;
435             continue;
436         }
437 
438         /* Finally, check for a newline */
439 
440         if (*SubBuffer == '\n')
441         {
442             TotalLines++;
443             SubBuffer++;
444             continue;
445         }
446 
447         /* Normal character, do the user actions */
448 
449         if (Callback)
450         {
451             SubBuffer = Callback (SubBuffer, Filename, TotalLines);
452         }
453 
454         if (TargetChar && (*SubBuffer == TargetChar))
455         {
456             return (SubBuffer);
457         }
458 
459         SubBuffer++;
460     }
461 
462     return (NULL);
463 }
464 
465 
466 /******************************************************************************
467  *
468  * FUNCTION:    AsRemoveExtraLines
469  *
470  * DESCRIPTION: Remove all extra lines at the start and end of the file.
471  *
472  ******************************************************************************/
473 
474 void
475 AsRemoveExtraLines (
476     char                    *FileBuffer,
477     char                    *Filename)
478 {
479     char                    *FileEnd;
480     int                     Length;
481 
482 
483     /* Remove any extra lines at the start of the file */
484 
485     while (*FileBuffer == '\n')
486     {
487         printf ("Removing extra line at start of file: %s\n", Filename);
488         AsRemoveData (FileBuffer, FileBuffer + 1);
489     }
490 
491     /* Remove any extra lines at the end of the file */
492 
493     Length = strlen (FileBuffer);
494     FileEnd = FileBuffer + (Length - 2);
495 
496     while (*FileEnd == '\n')
497     {
498         printf ("Removing extra line at end of file: %s\n", Filename);
499         AsRemoveData (FileEnd, FileEnd + 1);
500         FileEnd--;
501     }
502 }
503 
504 
505 /******************************************************************************
506  *
507  * FUNCTION:    AsRemoveSpacesAfterPeriod
508  *
509  * DESCRIPTION: Remove an extra space after a period.
510  *
511  ******************************************************************************/
512 
513 void
514 AsRemoveSpacesAfterPeriod (
515     char                    *FileBuffer,
516     char                    *Filename)
517 {
518     int                     ReplaceCount = 0;
519     char                    *Possible;
520 
521 
522     Possible = FileBuffer;
523     while (Possible)
524     {
525         Possible = strstr (Possible, ".  ");
526         if (Possible)
527         {
528             if ((*(Possible -1) == '.')  ||
529                 (*(Possible -1) == '\"') ||
530                 (*(Possible -1) == '\n'))
531             {
532                 Possible += 3;
533                 continue;
534             }
535 
536             Possible = AsReplaceData (Possible, 3, ". ", 2);
537             ReplaceCount++;
538         }
539     }
540 
541     if (ReplaceCount)
542     {
543         printf ("Removed %d extra blanks after a period: %s\n",
544             ReplaceCount, Filename);
545     }
546 }
547 
548 
549 /******************************************************************************
550  *
551  * FUNCTION:    AsMatchExactWord
552  *
553  * DESCRIPTION: Check previous and next characters for whitespace
554  *
555  ******************************************************************************/
556 
557 BOOLEAN
558 AsMatchExactWord (
559     char                    *Word,
560     UINT32                  WordLength)
561 {
562     char                    NextChar;
563     char                    PrevChar;
564 
565 
566     NextChar = Word[WordLength];
567     PrevChar = * (Word -1);
568 
569     if (isalnum ((int) NextChar) ||
570         (NextChar == '_')  ||
571         isalnum ((int) PrevChar) ||
572         (PrevChar == '_'))
573     {
574         return (FALSE);
575     }
576 
577     return (TRUE);
578 }
579 
580 
581 /******************************************************************************
582  *
583  * FUNCTION:    AsPrint
584  *
585  * DESCRIPTION: Common formatted print
586  *
587  ******************************************************************************/
588 
589 void
590 AsPrint (
591     char                    *Message,
592     UINT32                  Count,
593     char                    *Filename)
594 {
595 
596     if (Gbl_QuietMode)
597     {
598         return;
599     }
600 
601     printf ("-- %4u %28.28s : %s\n", Count, Message, Filename);
602 }
603 
604 
605 /******************************************************************************
606  *
607  * FUNCTION:    AsTrimLines
608  *
609  * DESCRIPTION: Remove extra blanks from the end of source lines. Does not
610  *              check for tabs.
611  *
612  ******************************************************************************/
613 
614 void
615 AsTrimLines (
616     char                    *Buffer,
617     char                    *Filename)
618 {
619     char                    *SubBuffer = Buffer;
620     char                    *StartWhiteSpace = NULL;
621     UINT32                  SpaceCount = 0;
622 
623 
624     while (*SubBuffer)
625     {
626         while (*SubBuffer != '\n')
627         {
628             if (!*SubBuffer)
629             {
630                 goto Exit;
631             }
632 
633             if (*SubBuffer == ' ')
634             {
635                 if (!StartWhiteSpace)
636                 {
637                     StartWhiteSpace = SubBuffer;
638                 }
639             }
640             else
641             {
642                 StartWhiteSpace = NULL;
643             }
644 
645             SubBuffer++;
646         }
647 
648         if (StartWhiteSpace)
649         {
650             SpaceCount += (SubBuffer - StartWhiteSpace);
651 
652             /* Remove the spaces */
653 
654             SubBuffer = AsRemoveData (StartWhiteSpace, SubBuffer);
655             StartWhiteSpace = NULL;
656         }
657 
658         SubBuffer++;
659     }
660 
661 
662 Exit:
663     if (SpaceCount)
664     {
665         Gbl_MadeChanges = TRUE;
666         AsPrint ("Extraneous spaces removed", SpaceCount, Filename);
667     }
668 }
669 
670 
671 /******************************************************************************
672  *
673  * FUNCTION:    AsTrimWhitespace
674  *
675  * DESCRIPTION: Remove "excess" blank lines - any more than 2 blank lines.
676  *              this can happen during the translation when lines are removed.
677  *
678  ******************************************************************************/
679 
680 void
681 AsTrimWhitespace (
682     char                    *Buffer)
683 {
684     char                    *SubBuffer;
685     int                     ReplaceCount = 1;
686 
687 
688     while (ReplaceCount)
689     {
690         ReplaceCount = AsReplaceString ("\n\n\n\n", "\n\n\n",
691             REPLACE_SUBSTRINGS, Buffer);
692     }
693 
694     /*
695      * Check for exactly one blank line after the copyright header
696      */
697 
698     /* Find the header */
699 
700     SubBuffer = strstr (Buffer, HeaderBegin);
701     if (!SubBuffer)
702     {
703         return;
704     }
705 
706     /* Find the end of the header */
707 
708     SubBuffer = strstr (SubBuffer, "*/");
709     SubBuffer = AsSkipPastChar (SubBuffer, '\n');
710 
711     /* Replace a double blank line with a single */
712 
713     if (!strncmp (SubBuffer, "\n\n", 2))
714     {
715         AsReplaceData (SubBuffer, 2, "\n", 1);
716         AcpiOsPrintf ("Found multiple blank lines after copyright\n");
717     }
718 
719     /* If no blank line after header, insert one */
720 
721     else if (*SubBuffer != '\n')
722     {
723         AsInsertData (SubBuffer, "\n", 1);
724         AcpiOsPrintf ("Inserted blank line after copyright\n");
725     }
726 }
727 
728 
729 /******************************************************************************
730  *
731  * FUNCTION:    AsReplaceHeader
732  *
733  * DESCRIPTION: Replace the default Intel legal header with a new header
734  *
735  ******************************************************************************/
736 
737 void
738 AsReplaceHeader (
739     char                    *Buffer,
740     char                    *NewHeader)
741 {
742     char                    *SubBuffer;
743     char                    *TokenEnd;
744 
745 
746     /* Find the original header */
747 
748     SubBuffer = strstr (Buffer, HeaderBegin);
749     if (!SubBuffer)
750     {
751         return;
752     }
753 
754     /* Find the end of the original header */
755 
756     TokenEnd = strstr (SubBuffer, "*/");
757     TokenEnd = AsSkipPastChar (TokenEnd, '\n');
758 
759     /* Delete old header, insert new one */
760 
761     AsReplaceData (SubBuffer, TokenEnd - SubBuffer,
762         NewHeader, strlen (NewHeader));
763 }
764 
765 
766 /******************************************************************************
767  *
768  * FUNCTION:    AsDoSpdxHeader
769  *
770  * DESCRIPTION: Replace the default Intel legal header with a new header
771  *
772  ******************************************************************************/
773 
774 void
775 AsDoSpdxHeader (
776     char                    *Buffer,
777     char                    *SpdxHeader)
778 {
779     char                    *SubBuffer;
780 
781 
782     /* Place an SPDX header at the very top */
783 
784     AsReplaceData (Buffer, 0,
785         SpdxHeader, strlen (SpdxHeader));
786 
787     /* Place an Intel copyright notice in the module header */
788 
789     SubBuffer = strstr (Buffer, MODULE_HEADER_END);
790     if (!SubBuffer)
791     {
792         return;
793     }
794 
795     AsReplaceData (SubBuffer, strlen (MODULE_HEADER_END),
796         CopyRightHeaderEnd, strlen (CopyRightHeaderEnd));
797 }
798 
799 /******************************************************************************
800  *
801  * FUNCTION:    AsReplaceString
802  *
803  * DESCRIPTION: Replace all instances of a target string with a replacement
804  *              string. Returns count of the strings replaced.
805  *
806  ******************************************************************************/
807 
808 int
809 AsReplaceString (
810     char                    *Target,
811     char                    *Replacement,
812     UINT8                   Type,
813     char                    *Buffer)
814 {
815     char                    *SubString1;
816     char                    *SubString2;
817     char                    *SubBuffer;
818     int                     TargetLength;
819     int                     ReplacementLength;
820     int                     ReplaceCount = 0;
821 
822 
823     TargetLength = strlen (Target);
824     ReplacementLength = strlen (Replacement);
825 
826     SubBuffer = Buffer;
827     SubString1 = Buffer;
828 
829     while (SubString1)
830     {
831         /* Find the target string */
832 
833         SubString1 = strstr (SubBuffer, Target);
834         if (!SubString1)
835         {
836             return (ReplaceCount);
837         }
838 
839         /*
840          * Check for translation escape string -- means to ignore
841          * blocks of code while replacing
842          */
843         if (Gbl_IgnoreTranslationEscapes)
844         {
845             SubString2 = NULL;
846         }
847         else
848         {
849             SubString2 = strstr (SubBuffer, AS_START_IGNORE);
850         }
851 
852         if ((SubString2) &&
853             (SubString2 < SubString1))
854         {
855             /* Find end of the escape block starting at "Substring2" */
856 
857             SubString2 = strstr (SubString2, AS_STOP_IGNORE);
858             if (!SubString2)
859             {
860                 /* Didn't find terminator */
861 
862                 return (ReplaceCount);
863             }
864 
865             /* Move buffer to end of escape block and continue */
866 
867             SubBuffer = SubString2;
868         }
869 
870         /* Do the actual replace if the target was found */
871 
872         else
873         {
874             if ((Type & REPLACE_MASK) == REPLACE_WHOLE_WORD)
875             {
876                 if (!AsMatchExactWord (SubString1, TargetLength))
877                 {
878                     SubBuffer = SubString1 + 1;
879                     continue;
880                 }
881             }
882 
883             SubBuffer = AsReplaceData (SubString1, TargetLength,
884                 Replacement, ReplacementLength);
885 
886             if ((Type & EXTRA_INDENT_C) &&
887                 (!Gbl_StructDefs))
888             {
889                 SubBuffer = AsInsertData (SubBuffer, "        ", 8);
890             }
891 
892             ReplaceCount++;
893         }
894     }
895 
896     return (ReplaceCount);
897 }
898 
899 
900 /******************************************************************************
901  *
902  * FUNCTION:    AsConvertToLineFeeds
903  *
904  * DESCRIPTION: Convert all CR/LF pairs to LF only.
905  *
906  ******************************************************************************/
907 
908 void
909 AsConvertToLineFeeds (
910     char                    *Buffer)
911 {
912     char                    *SubString;
913     char                    *SubBuffer;
914 
915 
916     SubBuffer = Buffer;
917     SubString = Buffer;
918 
919     while (SubString)
920     {
921         /* Find the target string */
922 
923         SubString = strstr (SubBuffer, "\r\n");
924         if (!SubString)
925         {
926             return;
927         }
928 
929         SubBuffer = AsReplaceData (SubString, 1, NULL, 0);
930     }
931 }
932 
933 
934 /******************************************************************************
935  *
936  * FUNCTION:    AsInsertCarriageReturns
937  *
938  * DESCRIPTION: Convert lone LFs to CR/LF pairs.
939  *
940  ******************************************************************************/
941 
942 void
943 AsInsertCarriageReturns (
944     char                    *Buffer)
945 {
946     char                    *SubString;
947     char                    *SubBuffer;
948 
949 
950     SubBuffer = Buffer;
951     SubString = Buffer;
952 
953     while (SubString)
954     {
955         /* Find the target string */
956 
957         SubString = strstr (SubBuffer, "\n");
958         if (!SubString)
959         {
960             return;
961         }
962 
963         SubBuffer = AsInsertData (SubString, "\r", 1);
964         SubBuffer += 1;
965     }
966 }
967 
968 
969 /******************************************************************************
970  *
971  * FUNCTION:    AsBracesOnSameLine
972  *
973  * DESCRIPTION: Move opening braces up to the same line as an if, for, else,
974  *              or while statement (leave function opening brace on separate
975  *              line).
976  *
977  ******************************************************************************/
978 
979 void
980 AsBracesOnSameLine (
981     char                    *Buffer)
982 {
983     char                    *SubBuffer = Buffer;
984     char                    *Beginning;
985     char                    *StartOfThisLine;
986     char                    *Next;
987     BOOLEAN                 BlockBegin = TRUE;
988 
989 
990     while (*SubBuffer)
991     {
992         /* Ignore comments */
993 
994         if ((SubBuffer[0] == '/') &&
995             (SubBuffer[1] == '*'))
996         {
997             SubBuffer = strstr (SubBuffer, "*/");
998             if (!SubBuffer)
999             {
1000                 return;
1001             }
1002 
1003             SubBuffer += 2;
1004             continue;
1005         }
1006 
1007         /* Ignore quoted strings */
1008 
1009         if (*SubBuffer == '\"')
1010         {
1011             SubBuffer++;
1012             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1013             if (!SubBuffer)
1014             {
1015                 return;
1016             }
1017         }
1018 
1019         if (!strncmp ("\n}", SubBuffer, 2))
1020         {
1021             /*
1022              * A newline followed by a closing brace closes a function
1023              * or struct or initializer block
1024              */
1025             BlockBegin = TRUE;
1026         }
1027 
1028         /*
1029          * Move every standalone brace up to the previous line
1030          * Check for digit will ignore initializer lists surrounded by braces.
1031          * This will work until we we need more complex detection.
1032          */
1033         if ((*SubBuffer == '{') && !isdigit ((int) SubBuffer[1]))
1034         {
1035             if (BlockBegin)
1036             {
1037                 BlockBegin = FALSE;
1038             }
1039             else
1040             {
1041                 /*
1042                  * Backup to previous non-whitespace
1043                  */
1044                 Beginning = SubBuffer - 1;
1045                 while ((*Beginning == ' ')   ||
1046                        (*Beginning == '\n'))
1047                 {
1048                     Beginning--;
1049                 }
1050 
1051                 StartOfThisLine = Beginning;
1052                 while (*StartOfThisLine != '\n')
1053                 {
1054                     StartOfThisLine--;
1055                 }
1056 
1057                 /*
1058                  * Move the brace up to the previous line, UNLESS:
1059                  *
1060                  * 1) There is a conditional compile on the line (starts with '#')
1061                  * 2) Previous line ends with an '=' (Start of initializer block)
1062                  * 3) Previous line ends with a comma (part of an init list)
1063                  * 4) Previous line ends with a backslash (part of a macro)
1064                  */
1065                 if ((StartOfThisLine[1] != '#') &&
1066                     (*Beginning != '\\') &&
1067                     (*Beginning != '/') &&
1068                     (*Beginning != '{') &&
1069                     (*Beginning != '=') &&
1070                     (*Beginning != ','))
1071                 {
1072                     Beginning++;
1073                     SubBuffer++;
1074 
1075                     Gbl_MadeChanges = TRUE;
1076 
1077 #ifdef ADD_EXTRA_WHITESPACE
1078                     AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
1079 #else
1080                     /* Find non-whitespace start of next line */
1081 
1082                     Next = SubBuffer + 1;
1083                     while ((*Next == ' ')   ||
1084                            (*Next == '\t'))
1085                     {
1086                         Next++;
1087                     }
1088 
1089                     /* Find non-whitespace start of this line */
1090 
1091                     StartOfThisLine++;
1092                     while ((*StartOfThisLine == ' ')   ||
1093                            (*StartOfThisLine == '\t'))
1094                     {
1095                         StartOfThisLine++;
1096                     }
1097 
1098                     /*
1099                      * Must be a single-line comment to need more whitespace
1100                      * Even then, we don't need more if the previous statement
1101                      * is an "else".
1102                      */
1103                     if ((Next[0] == '/')  &&
1104                         (Next[1] == '*')  &&
1105                         (Next[2] != '\n') &&
1106 
1107                         (!strncmp (StartOfThisLine, "else if", 7)     ||
1108                          !strncmp (StartOfThisLine, "else while", 10) ||
1109                           strncmp (StartOfThisLine, "else", 4)))
1110                     {
1111                         AsReplaceData (Beginning, SubBuffer - Beginning, " {\n", 3);
1112                     }
1113                     else
1114                     {
1115                         AsReplaceData (Beginning, SubBuffer - Beginning, " {", 2);
1116                     }
1117 #endif
1118                 }
1119             }
1120         }
1121 
1122         SubBuffer++;
1123     }
1124 }
1125 
1126 
1127 /******************************************************************************
1128  *
1129  * FUNCTION:    AsTabify4
1130  *
1131  * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1132  *              preserved.
1133  *
1134  ******************************************************************************/
1135 
1136 void
1137 AsTabify4 (
1138     char                    *Buffer)
1139 {
1140     char                    *SubBuffer = Buffer;
1141     char                    *NewSubBuffer;
1142     UINT32                  SpaceCount = 0;
1143     UINT32                  Column = 0;
1144 
1145 
1146     while (*SubBuffer)
1147     {
1148         if (*SubBuffer == '\n')
1149         {
1150             Column = 0;
1151         }
1152         else
1153         {
1154             Column++;
1155         }
1156 
1157         /* Ignore comments */
1158 
1159         if ((SubBuffer[0] == '/') &&
1160             (SubBuffer[1] == '*'))
1161         {
1162             SubBuffer = strstr (SubBuffer, "*/");
1163             if (!SubBuffer)
1164             {
1165                 return;
1166             }
1167 
1168             SubBuffer += 2;
1169             continue;
1170         }
1171 
1172         /* Ignore quoted strings */
1173 
1174         if (*SubBuffer == '\"')
1175         {
1176             SubBuffer++;
1177             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1178             if (!SubBuffer)
1179             {
1180                 return;
1181             }
1182             SpaceCount = 0;
1183         }
1184 
1185         if (*SubBuffer == ' ')
1186         {
1187             SpaceCount++;
1188 
1189             if (SpaceCount >= 4)
1190             {
1191                 SpaceCount = 0;
1192 
1193                 NewSubBuffer = (SubBuffer + 1) - 4;
1194                 *NewSubBuffer = '\t';
1195                 NewSubBuffer++;
1196 
1197                 /* Remove the spaces */
1198 
1199                 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer + 1);
1200             }
1201 
1202             if ((Column % 4) == 0)
1203             {
1204                 SpaceCount = 0;
1205             }
1206         }
1207         else
1208         {
1209             SpaceCount = 0;
1210         }
1211 
1212         SubBuffer++;
1213     }
1214 }
1215 
1216 
1217 /******************************************************************************
1218  *
1219  * FUNCTION:    AsTabify8
1220  *
1221  * DESCRIPTION: Convert the text to tabbed text. Alignment of text is
1222  *              preserved.
1223  *
1224  ******************************************************************************/
1225 
1226 void
1227 AsTabify8 (
1228     char                    *Buffer)
1229 {
1230     char                    *SubBuffer = Buffer;
1231     char                    *NewSubBuffer;
1232     char                    *CommentEnd = NULL;
1233     UINT32                  SpaceCount = 0;
1234     UINT32                  Column = 0;
1235     UINT32                  TabCount = 0;
1236     UINT32                  LastLineTabCount = 0;
1237     UINT32                  LastLineColumnStart = 0;
1238     UINT32                  ThisColumnStart = 0;
1239     UINT32                  ThisTabCount =  0;
1240     char                    *FirstNonBlank = NULL;
1241 
1242 
1243     while (*SubBuffer)
1244     {
1245         if (*SubBuffer == '\n')
1246         {
1247             /* This is a standalone blank line */
1248 
1249             FirstNonBlank = NULL;
1250             Column = 0;
1251             SpaceCount = 0;
1252             TabCount = 0;
1253             SubBuffer++;
1254             continue;
1255         }
1256 
1257         if (!FirstNonBlank)
1258         {
1259             /* Find the first non-blank character on this line */
1260 
1261             FirstNonBlank = SubBuffer;
1262             while (*FirstNonBlank == ' ')
1263             {
1264                 FirstNonBlank++;
1265             }
1266 
1267             /*
1268              * This mechanism limits the difference in tab counts from
1269              * line to line. It helps avoid the situation where a second
1270              * continuation line (which was indented correctly for tabs=4) would
1271              * get indented off the screen if we just blindly converted to tabs.
1272              */
1273             ThisColumnStart = FirstNonBlank - SubBuffer;
1274 
1275             if (LastLineTabCount == 0)
1276             {
1277                 ThisTabCount = 0;
1278             }
1279             else if (ThisColumnStart == LastLineColumnStart)
1280             {
1281                 ThisTabCount = LastLineTabCount -1;
1282             }
1283             else
1284             {
1285                 ThisTabCount = LastLineTabCount + 1;
1286             }
1287         }
1288 
1289         Column++;
1290 
1291         /* Check if we are in a comment */
1292 
1293         if ((SubBuffer[0] == '*') &&
1294             (SubBuffer[1] == '/'))
1295         {
1296             SpaceCount = 0;
1297             SubBuffer += 2;
1298 
1299             if (*SubBuffer == '\n')
1300             {
1301                 if (TabCount > 0)
1302                 {
1303                     LastLineTabCount = TabCount;
1304                     TabCount = 0;
1305                 }
1306 
1307                 FirstNonBlank = NULL;
1308                 LastLineColumnStart = ThisColumnStart;
1309                 SubBuffer++;
1310             }
1311 
1312             continue;
1313         }
1314 
1315         /* Check for comment open */
1316 
1317         if ((SubBuffer[0] == '/') &&
1318             (SubBuffer[1] == '*'))
1319         {
1320             /* Find the end of the comment, it must exist */
1321 
1322             CommentEnd = strstr (SubBuffer, "*/");
1323             if (!CommentEnd)
1324             {
1325                 return;
1326             }
1327 
1328             /* Toss the rest of this line or single-line comment */
1329 
1330             while ((SubBuffer < CommentEnd) &&
1331                    (*SubBuffer != '\n'))
1332             {
1333                 SubBuffer++;
1334             }
1335 
1336             if (*SubBuffer == '\n')
1337             {
1338                 if (TabCount > 0)
1339                 {
1340                     LastLineTabCount = TabCount;
1341                     TabCount = 0;
1342                 }
1343 
1344                 FirstNonBlank = NULL;
1345                 LastLineColumnStart = ThisColumnStart;
1346             }
1347 
1348             SpaceCount = 0;
1349             continue;
1350         }
1351 
1352         /* Ignore quoted strings */
1353 
1354         if ((!CommentEnd) && (*SubBuffer == '\"'))
1355         {
1356             SubBuffer++;
1357             SubBuffer = AsSkipPastChar (SubBuffer, '\"');
1358             if (!SubBuffer)
1359             {
1360                 return;
1361             }
1362 
1363             SpaceCount = 0;
1364         }
1365 
1366         if (*SubBuffer != ' ')
1367         {
1368             /* Not a space, skip to end of line */
1369 
1370             SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
1371             if (!SubBuffer)
1372             {
1373                 return;
1374             }
1375             if (TabCount > 0)
1376             {
1377                 LastLineTabCount = TabCount;
1378                 TabCount = 0;
1379             }
1380 
1381             FirstNonBlank = NULL;
1382             LastLineColumnStart = ThisColumnStart;
1383             Column = 0;
1384             SpaceCount = 0;
1385         }
1386         else
1387         {
1388             /* Another space */
1389 
1390             SpaceCount++;
1391 
1392             if (SpaceCount >= 4)
1393             {
1394                 /* Replace this group of spaces with a tab character */
1395 
1396                 SpaceCount = 0;
1397 
1398                 NewSubBuffer = SubBuffer - 3;
1399 
1400                 if (TabCount <= ThisTabCount ? (ThisTabCount +1) : 0)
1401                 {
1402                     *NewSubBuffer = '\t';
1403                     NewSubBuffer++;
1404                     SubBuffer++;
1405                     TabCount++;
1406                 }
1407 
1408                 /* Remove the spaces */
1409 
1410                 SubBuffer = AsRemoveData (NewSubBuffer, SubBuffer);
1411                 continue;
1412             }
1413         }
1414 
1415         SubBuffer++;
1416     }
1417 }
1418 
1419 
1420 /******************************************************************************
1421  *
1422  * FUNCTION:    AsCountLines
1423  *
1424  * DESCRIPTION: Count the number of lines in the input buffer. Also count
1425  *              the number of long lines (lines longer than 80 chars).
1426  *
1427  ******************************************************************************/
1428 
1429 static UINT32
1430 AsCountLines (
1431     char                    *Buffer,
1432     char                    *Filename)
1433 {
1434     char                    *SubBuffer = Buffer;
1435     char                    *EndOfLine;
1436     UINT32                  LineCount = 0;
1437     UINT32                  LongLineCount = 0;
1438 
1439 
1440     while (*SubBuffer)
1441     {
1442         EndOfLine = AsSkipUntilChar (SubBuffer, '\n');
1443         if (!EndOfLine)
1444         {
1445             Gbl_TotalLines += LineCount;
1446             return (LineCount);
1447         }
1448 
1449         if ((EndOfLine - SubBuffer) > 80)
1450         {
1451             LongLineCount++;
1452             VERBOSE_PRINT (("long: %.80s\n", SubBuffer));
1453         }
1454 
1455         LineCount++;
1456         SubBuffer = EndOfLine + 1;
1457     }
1458 
1459     if (LongLineCount)
1460     {
1461         VERBOSE_PRINT (("%u Lines longer than 80 found in %s\n",
1462             LongLineCount, Filename));
1463 
1464         Gbl_LongLines += LongLineCount;
1465     }
1466 
1467     Gbl_TotalLines += LineCount;
1468     return (LineCount);
1469 }
1470 
1471 
1472 /******************************************************************************
1473  *
1474  * FUNCTION:    AsCountTabs
1475  *
1476  * DESCRIPTION: Simply count the number of tabs in the input file buffer
1477  *
1478  ******************************************************************************/
1479 
1480 void
1481 AsCountTabs (
1482     char                    *Buffer,
1483     char                    *Filename)
1484 {
1485     UINT32                  i;
1486     UINT32                  TabCount = 0;
1487 
1488 
1489     for (i = 0; Buffer[i]; i++)
1490     {
1491         if (Buffer[i] == '\t')
1492         {
1493             TabCount++;
1494         }
1495     }
1496 
1497     if (TabCount)
1498     {
1499         AsPrint ("Tabs found", TabCount, Filename);
1500         Gbl_Tabs += TabCount;
1501     }
1502 
1503     AsCountLines (Buffer, Filename);
1504 }
1505 
1506 
1507 /******************************************************************************
1508  *
1509  * FUNCTION:    AsCountSourceLines
1510  *
1511  * DESCRIPTION: Count the number of C source lines. Defined by 1) not a
1512  *              comment, and 2) not a blank line.
1513  *
1514  ******************************************************************************/
1515 
1516 void
1517 AsCountSourceLines (
1518     char                    *Buffer,
1519     char                    *Filename)
1520 {
1521     char                    *SubBuffer = Buffer;
1522     UINT32                  LineCount = 0;
1523     UINT32                  WhiteCount = 0;
1524     UINT32                  CommentCount = 0;
1525 
1526 
1527     while (*SubBuffer)
1528     {
1529         /* Detect comments (// comments are not used, non-ansii) */
1530 
1531         if ((SubBuffer[0] == '/') &&
1532             (SubBuffer[1] == '*'))
1533         {
1534             SubBuffer += 2;
1535 
1536             /* First line of multi-line comment is often just whitespace */
1537 
1538             if (SubBuffer[0] == '\n')
1539             {
1540                 WhiteCount++;
1541                 SubBuffer++;
1542             }
1543             else
1544             {
1545                 CommentCount++;
1546             }
1547 
1548             /* Find end of comment */
1549 
1550             while (SubBuffer[0] && SubBuffer[1] &&
1551                 !(((SubBuffer[0] == '*') &&
1552                     (SubBuffer[1] == '/'))))
1553             {
1554                 if (SubBuffer[0] == '\n')
1555                 {
1556                     CommentCount++;
1557                 }
1558 
1559                 SubBuffer++;
1560             }
1561         }
1562 
1563         /* A linefeed followed by a non-linefeed is a valid source line */
1564 
1565         else if ((SubBuffer[0] == '\n') &&
1566                  (SubBuffer[1] != '\n'))
1567         {
1568             LineCount++;
1569         }
1570 
1571         /* Two back-to-back linefeeds indicate a whitespace line */
1572 
1573         else if ((SubBuffer[0] == '\n') &&
1574                  (SubBuffer[1] == '\n'))
1575         {
1576             WhiteCount++;
1577         }
1578 
1579         SubBuffer++;
1580     }
1581 
1582     /* Adjust comment count for legal header */
1583 
1584     if (Gbl_HeaderSize < CommentCount)
1585     {
1586         CommentCount -= Gbl_HeaderSize;
1587         Gbl_HeaderLines += Gbl_HeaderSize;
1588     }
1589 
1590     Gbl_SourceLines += LineCount;
1591     Gbl_WhiteLines += WhiteCount;
1592     Gbl_CommentLines += CommentCount;
1593 
1594     VERBOSE_PRINT (("%u Comment %u White %u Code %u Lines in %s\n",
1595         CommentCount, WhiteCount, LineCount,
1596         LineCount + WhiteCount + CommentCount, Filename));
1597 }
1598 
1599 
1600 /******************************************************************************
1601  *
1602  * FUNCTION:    AsInsertPrefix
1603  *
1604  * DESCRIPTION: Insert struct or union prefixes
1605  *
1606  ******************************************************************************/
1607 
1608 void
1609 AsInsertPrefix (
1610     char                    *Buffer,
1611     char                    *Keyword,
1612     UINT8                   Type)
1613 {
1614     char                    *SubString;
1615     char                    *SubBuffer;
1616     char                    *EndKeyword;
1617     int                     InsertLength;
1618     char                    *InsertString;
1619     int                     TrailingSpaces;
1620     char                    LowerKeyword[128];
1621     int                     KeywordLength;
1622 
1623 
1624     switch (Type)
1625     {
1626     case SRC_TYPE_STRUCT:
1627 
1628         InsertString = "struct ";
1629         break;
1630 
1631     case SRC_TYPE_UNION:
1632 
1633         InsertString = "union ";
1634         break;
1635 
1636     default:
1637 
1638         return;
1639     }
1640 
1641     strcpy (LowerKeyword, Keyword);
1642     AcpiUtStrlwr (LowerKeyword);
1643 
1644     SubBuffer = Buffer;
1645     SubString = Buffer;
1646     InsertLength = strlen (InsertString);
1647     KeywordLength = strlen (Keyword);
1648 
1649 
1650     while (SubString)
1651     {
1652         /* Find an instance of the keyword */
1653 
1654         SubString = strstr (SubBuffer, LowerKeyword);
1655         if (!SubString)
1656         {
1657             return;
1658         }
1659 
1660         SubBuffer = SubString;
1661 
1662         /* Must be standalone word, not a substring */
1663 
1664         if (AsMatchExactWord (SubString, KeywordLength))
1665         {
1666             /* Make sure the keyword isn't already prefixed with the insert */
1667 
1668             if (!strncmp (SubString - InsertLength, InsertString, InsertLength))
1669             {
1670                 /* Add spaces if not already at the end-of-line */
1671 
1672                 if (*(SubBuffer + KeywordLength) != '\n')
1673                 {
1674                     /* Already present, add spaces after to align structure members */
1675 
1676 #if 0
1677 /* ONLY FOR C FILES */
1678                     AsInsertData (SubBuffer + KeywordLength, "        ", 8);
1679 #endif
1680                 }
1681                 goto Next;
1682             }
1683 
1684             /* Make sure the keyword isn't at the end of a struct/union */
1685             /* Note: This code depends on a single space after the brace */
1686 
1687             if (*(SubString - 2) == '}')
1688             {
1689                 goto Next;
1690             }
1691 
1692             /* Prefix the keyword with the insert string */
1693 
1694             Gbl_MadeChanges = TRUE;
1695 
1696             /* Is there room for insertion */
1697 
1698             EndKeyword = SubString + strlen (LowerKeyword);
1699 
1700             TrailingSpaces = 0;
1701             while (EndKeyword[TrailingSpaces] == ' ')
1702             {
1703                 TrailingSpaces++;
1704             }
1705 
1706             /*
1707              * Use "if (TrailingSpaces > 1)" if we want to ignore casts
1708              */
1709             SubBuffer = SubString + InsertLength;
1710 
1711             if (TrailingSpaces > InsertLength)
1712             {
1713                 /* Insert the keyword */
1714 
1715                 memmove (SubBuffer, SubString, KeywordLength);
1716 
1717                 /* Insert the keyword */
1718 
1719                 memmove (SubString, InsertString, InsertLength);
1720             }
1721             else
1722             {
1723                 AsInsertData (SubString, InsertString, InsertLength);
1724             }
1725         }
1726 
1727 Next:
1728         SubBuffer += KeywordLength;
1729     }
1730 }
1731 
1732 #ifdef ACPI_FUTURE_IMPLEMENTATION
1733 /******************************************************************************
1734  *
1735  * FUNCTION:    AsTrimComments
1736  *
1737  * DESCRIPTION: Finds 3-line comments with only a single line of text
1738  *
1739  ******************************************************************************/
1740 
1741 void
1742 AsTrimComments (
1743     char                    *Buffer,
1744     char                    *Filename)
1745 {
1746     char                    *SubBuffer = Buffer;
1747     char                    *Ptr1;
1748     char                    *Ptr2;
1749     UINT32                  LineCount;
1750     UINT32                  ShortCommentCount = 0;
1751 
1752 
1753     while (1)
1754     {
1755         /* Find comment open, within procedure level */
1756 
1757         SubBuffer = strstr (SubBuffer, "    /*");
1758         if (!SubBuffer)
1759         {
1760             goto Exit;
1761         }
1762 
1763         /* Find comment terminator */
1764 
1765         Ptr1 = strstr (SubBuffer, "*/");
1766         if (!Ptr1)
1767         {
1768             goto Exit;
1769         }
1770 
1771         /* Find next EOL (from original buffer) */
1772 
1773         Ptr2 = strstr (SubBuffer, "\n");
1774         if (!Ptr2)
1775         {
1776             goto Exit;
1777         }
1778 
1779         /* Ignore one-line comments */
1780 
1781         if (Ptr1 < Ptr2)
1782         {
1783             /* Normal comment, ignore and continue; */
1784 
1785             SubBuffer = Ptr2;
1786             continue;
1787         }
1788 
1789         /* Examine multi-line comment */
1790 
1791         LineCount = 1;
1792         while (Ptr1 > Ptr2)
1793         {
1794             /* Find next EOL */
1795 
1796             Ptr2++;
1797             Ptr2 = strstr (Ptr2, "\n");
1798             if (!Ptr2)
1799             {
1800                 goto Exit;
1801             }
1802 
1803             LineCount++;
1804         }
1805 
1806         SubBuffer = Ptr1;
1807 
1808         if (LineCount <= 3)
1809         {
1810             ShortCommentCount++;
1811         }
1812     }
1813 
1814 
1815 Exit:
1816 
1817     if (ShortCommentCount)
1818     {
1819         AsPrint ("Short Comments found", ShortCommentCount, Filename);
1820     }
1821 }
1822 #endif
1823 
1824 #ifdef ACPI_UNUSED_FUNCTIONS
1825 /******************************************************************************
1826  *
1827  * FUNCTION:    AsCheckAndSkipLiterals
1828  *
1829  * DESCRIPTION: Generic routine to skip comments and quoted string literals.
1830  *              Keeps a line count.
1831  *
1832  ******************************************************************************/
1833 
1834 static char *
1835 AsCheckAndSkipLiterals (
1836     char                    *Buffer,
1837     UINT32                  *TotalLines);
1838 
1839 
1840 static char *
1841 AsCheckAndSkipLiterals (
1842     char                    *Buffer,
1843     UINT32                  *TotalLines)
1844 {
1845     UINT32                  NewLines = 0;
1846     char                    *SubBuffer = Buffer;
1847     char                    *LiteralEnd;
1848 
1849 
1850     /* Ignore comments */
1851 
1852     if ((SubBuffer[0] == '/') &&
1853         (SubBuffer[1] == '*'))
1854     {
1855         LiteralEnd = strstr (SubBuffer, "*/");
1856         SubBuffer += 2;     /* Get past comment opening */
1857 
1858         if (!LiteralEnd)
1859         {
1860             return (SubBuffer);
1861         }
1862 
1863         while (SubBuffer < LiteralEnd)
1864         {
1865             if (*SubBuffer == '\n')
1866             {
1867                 NewLines++;
1868             }
1869 
1870             SubBuffer++;
1871         }
1872 
1873         SubBuffer += 2;     /* Get past comment close */
1874     }
1875 
1876     /* Ignore quoted strings */
1877 
1878     else if (*SubBuffer == '\"')
1879     {
1880         SubBuffer++;
1881         LiteralEnd = AsSkipPastChar (SubBuffer, '\"');
1882         if (!LiteralEnd)
1883         {
1884             return (SubBuffer);
1885         }
1886     }
1887 
1888     if (TotalLines)
1889     {
1890         (*TotalLines) += NewLines;
1891     }
1892     return (SubBuffer);
1893 }
1894 #endif
1895