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