File: dmain.c

package info (click to toggle)
pmccabe 2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 876 kB
  • sloc: ansic: 3,084; cpp: 1,080; sh: 401; makefile: 42
file content (795 lines) | stat: -rw-r--r-- 12,116 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* Copyright (c) 2002 Hewlett-Packard under GPL version 2 or later */
#include <stdio.h>
#include <stdbool.h>
#include "dmain.h"
#include "pmccabe.h"

/* Global */
int ncss_Line;

/* $Id: dmain.c,v 1.16 2001/01/26 23:00:32 bame Exp $ */

static int Lastc = '\n';

short Pipe[PIPESIZE];
short *Piperead = Pipe;
short *Pipewrite = Pipe;
short *Pipeend = Pipe + PIPESIZE;
bool skipping = false;

FILE *Input;
char Inputfile[1030];

bool Cppflag = false;

enum
{
    PREPROCESSOR_POSITIVE_IF,
    PREPROCESSOR_NEGATIVE_IF,
    PREPROCESSOR_NEVER_IF,
    PREPROCESSOR_ELSE,
    PREPROCESSOR_ENDIF,
    PREPROCESSOR_INCLUDE,
    PREPROCESSOR_DEFINE,
    PREPROCESSOR_UNKNOWN
};

void
skip_within_line(char char_to_skip)
{
    int c;

    while ((c = getc(Input)) != EOF && c != '\n' && c == char_to_skip) ;

    if (c == EOF)
    {
	char error_message[100];

	sprintf(error_message,
		"expected '%c' before end of line, but got EOF",
		char_to_skip);
	fileerror(error_message);
    }

    ungetc(c, Input);
}

void
skip_whitespace_within_line()
{
    skip_within_line(' ');
    skip_within_line('\t');
    skip_within_line(' ');
    skip_within_line('\t');
}



const char *
string_for_preprocessor_token(int preprocessor_token)
{
    switch (preprocessor_token)
    {
    case PREPROCESSOR_DEFINE:
	return "#define";
    case PREPROCESSOR_POSITIVE_IF:
	return "#ifdef";
    case PREPROCESSOR_NEGATIVE_IF:
	return "#ifndef";
    case PREPROCESSOR_NEVER_IF:
	return "#if 0";
    case PREPROCESSOR_ELSE:
	return "#else";
    case PREPROCESSOR_ENDIF:
	return "#endif";
    case PREPROCESSOR_INCLUDE:
	return "#include";
    default:
	return "#UNKNOWN";
    }

}

int
get_preprocessor_token()
{
    skip_whitespace_within_line();

    char first_letter = getc(Input);
    char second_letter;

    switch (first_letter)
    {
    case 'd':
	/* #define */
    case 'u':
	/* #undef */
	return PREPROCESSOR_DEFINE;
    case 'e':
	second_letter = getc(Input);
	if (second_letter == 'l')
	{
	    /* #else or #elif */
	    return PREPROCESSOR_ELSE;
	}
	else if (second_letter == 'n')
	{
	    /* #endif */
	    return PREPROCESSOR_ENDIF;
	}

	return PREPROCESSOR_UNKNOWN;

    case 'i':
	second_letter = getc(Input);
	if (second_letter == 'n')
	{
	    /* #include */
	    return PREPROCESSOR_INCLUDE;
	}

	if (second_letter == 'f')
	{
	    char third_letter = getc(Input);

	    switch (third_letter)
	    {
	    case 'n':
		/* #ifndef */
		return PREPROCESSOR_NEGATIVE_IF;
	    case 'd':
		/* #ifdef */
		return PREPROCESSOR_POSITIVE_IF;
	    case ' ':
	    case '\t':
	    case '(':
		skip_whitespace_within_line();
		skip_within_line('(');
		char logical_identifier = getc(Input);

		switch (logical_identifier)
		{
		case '0':
		    /* #if 0 */
		    return PREPROCESSOR_NEVER_IF;
		case '!':
		    /* #if ! */
		    return PREPROCESSOR_NEGATIVE_IF;
		case 'f':
		    if (getc(Input) == 'a' &&
			getc(Input) == 'l' && getc(Input) == 's')
		    {
			/* #if false */
			return PREPROCESSOR_NEVER_IF;
		    }
		    break;
		case 'd':
		    if (getc(Input) == 'e' &&
			getc(Input) == 'f' &&
			getc(Input) == 'i' && getc(Input) == 'n')
		    {
			/* #if defined() */
			return PREPROCESSOR_POSITIVE_IF;
		    }
		    break;

		default:
		    return PREPROCESSOR_POSITIVE_IF;
		}
	    }
	}
    }

    return PREPROCESSOR_UNKNOWN;
}

static void
better_decomment()
{
    enum
    {
	NORMAL, C_COMMENT, PREPROCESSOR_DIRECTIVE, CC_COMMENT, STRINGLITERAL,
	CHARLITERAL
    } state = NORMAL;

    register int c, c1;

    /* FIXME: decomment() gets called multiple times if a file is longer than the PIPESIZE macro */
    /*                      in that case, this variable being reset mid-file may cause issues. */
    /*                      Making the variable static (alone) isn't a solution, in missing #endif cases */

    int endifs_required = 0;

    Piperead = Pipewrite = Pipe;

    do
    {
	if ((c = getc(Input)) == EOF)
	{
	    if (skipping || endifs_required > 0)
	    {
		fileerror("expected #endif before EOF");
	    }

	    PUTCHAR_REGARDLESS_OF_SKIPPING(EOF);
	    break;
	}

	switch (state)
	{
	case NORMAL:
	    switch (c)
	    {
	    case '#':
		if (!ISSPACE(Lastc))
		{
		    PUTCHAR(c);
		    continue;
		}

		state = PREPROCESSOR_DIRECTIVE;
		if (Cppflag)
		{
		    PUTS("cpp");
		}

		int preprocessor_token = get_preprocessor_token();

		switch (preprocessor_token)
		{
		case PREPROCESSOR_ELSE:
		    /* FIXME: to keep things simple (by avoiding recursive descent),
		     *       the code below means incorrect behavior with:
		     * #if 0
		     * #else
		     * #endif
		     */
		    if (!skipping)
		    {
			endifs_required++;
		    }
		    skipping = true;
		    break;
		case PREPROCESSOR_NEVER_IF:
		    skipping = true;
		    endifs_required++;

		    break;
		case PREPROCESSOR_NEGATIVE_IF:
		case PREPROCESSOR_POSITIVE_IF:
		    if (skipping)
		    {
			endifs_required++;
		    }

		    break;
		case PREPROCESSOR_ENDIF:

		    if (skipping)
		    {
			endifs_required--;
		    }
		}

		if (skipping && endifs_required == 0)
		{
		    skipping = false;
		}

		if (endifs_required < 0)
		{
		    fileerror("expected fewer #endif statements");
		}

		break;
	    case '\'':
		state = CHARLITERAL;
		if (!skipping)
		    PUTS("CHARLITERAL");
		break;
	    case '"':
		state = STRINGLITERAL;
		if (!skipping)
		    PUTS("STRINGLITERAL");
		break;
	    case '/':
		c1 = getc(Input);
		switch (c1)
		{
		case '/':
		    state = CC_COMMENT;
		    break;
		case '*':
		    state = C_COMMENT;
		    break;
		case EOF:
		    /* ending star-slash comment right before EOF is okay */
		    break;
		default:
		    PUTCHAR(c);
		    ungetc(c1, Input);
		    break;
		}
		break;
	    default:
		PUTCHAR(c);
		break;
	    }
	    break;

	case C_COMMENT:	/* K&R C comment */
	    if (c == '\n')
	    {
		PUTCHAR(c);
	    }
	    else if (c == '*')
	    {
		c1 = getc(Input);
		if (c1 == '/')
		{
		    state = NORMAL;
		}
		else
		{
		    ungetc(c1, Input);
		}
	    }
	    break;

	case PREPROCESSOR_DIRECTIVE:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);

		c1 = getc(Input);
		if (c1 != '\\')
		{
		    ungetc(c1, Input);
		    state = NORMAL;
		    break;
		}
		/* FALL THROUGH */
	    case '\\':
		c1 = getc(Input);
		if (c1 == '\n')
		{
		    PUTCHAR(c1);
		    if (Cppflag)
		    {
			PUTS("cpp");
		    }
		}
		break;
	    }
	    break;

	case CC_COMMENT:	/* C++ comment */
	    if (c == '\n')
	    {
		PUTCHAR(c);
		state = NORMAL;
	    }
	    break;

	case STRINGLITERAL:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);
		break;
	    case '"':
		state = NORMAL;
		break;
		/* preserve embedded nulines */
	    case '\\':
		c1 = getc(Input);
		if (c1 == '\n')
		    PUTCHAR(c1);
		break;
	    }
	    break;

	case CHARLITERAL:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);
		break;
	    case '\'':
		state = NORMAL;
		break;
	    case '\\':
		getc(Input);
		break;
	    }
	    break;
	}
	Lastc = c;
    }
    while (state != NORMAL || Pipewrite - Piperead < PIPESIZE / 2);

    return;
}

static void
rudimentary_decomment()
{
    register int c, c1;

    enum
    { NORMAL,
	C_COMMENT,
	CPP,
	CC_COMMENT,
	STRINGLITERAL,
	CHARLITERAL
    } state = NORMAL;

    Piperead = Pipewrite = Pipe;

    do
    {
	if ((c = getc(Input)) == EOF)
	{
	    PUTCHAR(EOF);
	    break;
	}
	if (c == '\r')
	    continue;

	switch (state)
	{
	case NORMAL:
	    switch (c)
	    {
	    case '#':
		if (Lastc == '\n')
		{
		    state = CPP;
		    if (Cppflag)
			PUTS("cpp");
		}
		else
		{
		    PUTCHAR(c);
		}
		break;
	    case '\'':
		state = CHARLITERAL;
		PUTS("CHARLITERAL");
		break;
	    case '"':
		state = STRINGLITERAL;
		PUTS("STRINGLITERAL");
		break;
	    case '/':
		c1 = getc(Input);
		switch (c1)
		{
		case '/':
		    state = CC_COMMENT;
		    break;
		case '*':
		    state = C_COMMENT;
		    break;
		case EOF:
		    break;
		default:
		    PUTCHAR(c);
		    ungetc(c1, Input);
		    break;
		}
		break;
	    default:
		PUTCHAR(c);
		break;
	    }
	    break;

	case C_COMMENT:	/* K&R C comment */
	    if (c == '\n')
	    {
		PUTCHAR(c);
	    }
	    else if (c == '*')
	    {
		c1 = getc(Input);
		if (c1 == '/')
		{
		    state = NORMAL;
		}
		else
		{
		    ungetc(c1, Input);
		}
	    }
	    break;

	case CPP:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);
		state = NORMAL;
		break;
	    case '\\':
		c1 = getc(Input);
		if (c1 == '\n')
		{
		    PUTCHAR(c1);
		    if (Cppflag)
			PUTS("cpp");
		}
		break;
	    }
	    break;

	case CC_COMMENT:	/* C++ comment */
	    if (c == '\n')
	    {
		PUTCHAR(c);
		state = NORMAL;
	    }
	    break;

	case STRINGLITERAL:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);
		break;
	    case '"':
		state = NORMAL;
		break;
		/* preserve embedded nulines */
	    case '\\':
		c1 = getc(Input);
		if (c1 == '\n')
		    PUTCHAR(c1);
		break;
	    }
	    break;

	case CHARLITERAL:
	    switch (c)
	    {
	    case '\n':
		PUTCHAR(c);
		break;
	    case '\'':
		state = NORMAL;
		break;
	    case '\\':
		getc(Input);
		break;
	    }
	    break;
	}
	Lastc = c;
    }
    while (state != NORMAL || Pipewrite - Piperead < PIPESIZE / 2);
}

void
decomment()
{
    extern bool BetterDecomment;

    if (BetterDecomment)
	better_decomment();
    else
	rudimentary_decomment();
}

void
decomment_file(FILE * f)
{
    int c;
    extern bool Cppflag;

    Input = f;

    Cppflag = true;

    while ((c = Getchar()) != EOF)
    {
	putc(c, stdout);
    }
}

int
decomment_files(int argc, char *argv[])
{
    int result = 0;

    if (argc == 1)
    {
	decomment_file(stdin);
    }
    else
    {
	while (argc > 1)
	{
	    FILE *f;

	    if ((f = fopen(argv[1], "r")) == NULL)
	    {
		result = 2;
		perror(argv[1]);
	    }
	    else
	    {
		decomment_file(f);
		fclose(f);
	    }
	    SHIFT(1);
	}
    }

    return result;
}

void
ncss_Ungetc(int c)
{
    if (c == T_NCNULINE)
    {
	ncss_Line--;
    }
    Ungetc(c);
}

void
ncss_Ungets(char *s)
{
    Ungets(s);
}

int
ncss_Getchar()
{
    int c;
    static int blankline = 1;

    if ((c = Getchar()) != EOF)
    {
	if (blankline)
	{
	    if (!ISSPACE(c))
	    {
		blankline = 0;
	    }
	}
	else
	{
	    if (c == '\n')
	    {
		c = T_NCNULINE;
		blankline = 1;
	    }
	}
    }
    else
    {
	blankline = 1;
    }

    if (c == T_NCNULINE)
	ncss_Line++;

    return c;
}

void
ncss(int *linesp, int *nclinesp)
{
    int c;
    int lines = 0;
    int nclines = 0;
    int boline = 1;

    while ((c = Getchar()) != EOF)
    {
	if (c == '\n')
	{
	    lines++;
	    boline = 1;
	}
	else if (boline && !ISSPACE(c))
	{
	    boline = 0;
	    nclines++;
	}
    }

    *linesp = lines;
    *nclinesp = nclines;
}

void
ncss_file(char *fname, FILE * f, int *linesp, int *nclinesp)
{
    extern bool Cppflag;
    int lines, nclines;

    Input = f;

    Cppflag = true;
    ncss(&lines, &nclines);
    if (!Totalsonly)
    {
	if (lines != 0)
	{
	    int pct_csl =
		(int) (0.4999999 + 100.0 * (lines - nclines) / lines);

	    printf("%6d%4d%6d%4d%7d  %-s\n",
		   lines - nclines,
		   pct_csl, nclines, 100 - pct_csl, lines, fname);
	}
	else
	{
	    printf("%6d n/a%6d n/a%7d  %-s\n",
		   lines - nclines, nclines, lines, fname);
	}
    }

    *linesp += lines;
    *nclinesp += nclines;
}

int
ncss_files(int argc, char *argv[])
{
    int result = 0;
    int lines = 0, nclines = 0;
    int nfiles = argc - 1;

    if (Verbose)
	puts("   CSL PCT  NCSL PCT  TOTAL  FILENAME");

    if (argc == 1)
    {
	ncss_file("stdin", stdin, &lines, &nclines);
    }
    else
    {
	while (argc > 1)
	{
	    FILE *f;

	    if ((f = fopen(argv[1], "r")) == NULL)
	    {
		result = 2;
		perror(argv[1]);
	    }
	    else
	    {
		ncss_file(argv[1], f, &lines, &nclines);
		fclose(f);
	    }
	    SHIFT(1);
	}
    }

    if (Totals)
    {
	if (lines != 0)
	{
	    int pct_csl =
		(int) (0.4999999 + 100.0 * (lines - nclines) / lines);

	    printf("%6d%4d%6d%4d%7d  (total files: %d)\n",
		   lines - nclines,
		   pct_csl, nclines, 100 - pct_csl, lines, nfiles);
	}
	else
	{
	    printf("%6d n/a%6d n/a%7d  (total files: %d)\n",
		   lines - nclines, nclines, lines, nfiles);
	}
    }

    return result;
}