Subversion Repositories Kolibri OS

Rev

Rev 1498 | Details | Compare with Previous | Last modification | View Log | RSS feed

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