forked from illumos/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp.c
1604 lines (1517 loc) · 41.1 KB
/
cpp.c
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* C command
* written by John F. Reiser
* July/August 1978
*/
/* Copyright (c) 2012 Joyent, Inc. All rights reserved. */
/*
* This implementation is based on the UNIX 32V release from 1978
* with permission from Caldera Inc.
*
* Copyright (c) 2010 J. Schilling
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code and documentation must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement: This product includes
* software developed or owned by Caldera International, Inc.
*
* 4. Neither the name of Caldera International, Inc. nor the names of other
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR
* ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "cpp.h"
#define SYMLEN 128
static int symlen = SYMLEN;
#define SALT '#'
#ifndef BUFSIZ
#define BUFSIZ 512
#endif
static char *pbeg;
static char *pbuf;
static char *pend;
char *outp,*inp;
char *newp;
static char cinit;
/* some code depends on whether characters are sign or zero extended */
/* #if '\377' < 0 not used here, old cpp doesn't understand */
#if pdp11 | vax | '\377' < 0
#define COFF 128
#else
#define COFF 0
#endif
#define ALFSIZ 256 /* alphabet size */
static char macbit[ALFSIZ+11];
static char toktyp[ALFSIZ];
#define BLANK 1 /* white space (" \t\v\f\r") */
#define IDENT 2 /* valid char for identifier names */
#define NUMBR 3 /* chars is of "0123456789." */
/*
* a superimposed code is used to reduce the number of calls to the
* symbol table lookup routine. (if the kth character of an identifier
* is 'a' and there are no macro names whose kth character is 'a'
* then the identifier cannot be a macro name, hence there is no need
* to look in the symbol table.) 'scw1' enables the test based on
* single characters and their position in the identifier. 'scw2'
* enables the test based on adjacent pairs of characters and their
* position in the identifier. scw1 typically costs 1 indexed fetch,
* an AND, and a jump per character of identifier, until the identifier
* is known as a non-macro name or until the end of the identifier.
* scw1 is inexpensive. scw2 typically costs 4 indexed fetches,
* an add, an AND, and a jump per character of identifier, but it is also
* slightly more effective at reducing symbol table searches.
* scw2 usually costs too much because the symbol table search is
* usually short; but if symbol table search should become expensive,
* the code is here.
* using both scw1 and scw2 is of dubious value.
*/
#define scw1 1
#define scw2 0
#if scw2
char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+SYMLEN];
#endif
#if scw1
#define b0 1
#define b1 2
#define b2 4
#define b3 8
#define b4 16
#define b5 32
#define b6 64
#define b7 128
#endif
#define IB 1
#define SB 2
#define NB 4
#define CB 8
#define QB 16
#define WB 32
char fastab[ALFSIZ];
static char slotab[ALFSIZ];
static char *ptrtab;
/*
* Cast the array index to int in order to avoid GCCs warnings:
* warning: subscript has type `char'
*/
#define isslo (ptrtab==(slotab+COFF))
#define isid(a) ((fastab+COFF)[(int)a]&IB)
#define isspc(a) (ptrtab[(int)a]&SB)
#define isnum(a) ((fastab+COFF)[(int)a]&NB)
#define iscom(a) ((fastab+COFF)[(int)a]&CB)
#define isquo(a) ((fastab+COFF)[(int)a]&QB)
#define iswarn(a) ((fastab+COFF)[(int)a]&WB)
#define eob(a) ((a)>=pend)
#define bob(a) (pbeg>=(a))
#define BUFFERSIZ 8192
static char buffer[SYMLEN+BUFFERSIZ+BUFFERSIZ+SYMLEN];
/*
* SBSIZE was 12000 in 1978, we need to have a way to
* malloc more space.
*/
#define SBSIZE 512000
static char sbf[SBSIZE];
static char *savch = sbf;
# define DROP 0xFE /* special character not legal ASCII or EBCDIC */
# define WARN DROP
# define SAME 0
# define MAXINC 16 /* max include nesting depth */
# define MAXIDIRS 20 /* max # of -I directories */
# define MAXFRE 14 /* max buffers of macro pushback */
# define MAXFRM 31 /* max number of formals/actuals to a macro */
static char warnc = (char)WARN;
static int mactop;
static int fretop;
static char *instack[MAXFRE];
static char *bufstack[MAXFRE];
static char *endbuf[MAXFRE];
static int plvl; /* parenthesis level during scan for macro actuals */
static int maclin; /* line number of macro call requiring actuals */
static char *macfil; /* file name of macro call requiring actuals */
static char *macnam; /* name of macro requiring actuals */
static int maclvl; /* # calls since last decrease in nesting level */
static char *macforw; /* ptr which must be exceeded to decrease nesting lvl */
static int macdam; /* offset to macforw due to buffer shifting */
static int inctop[MAXINC];
static char *fnames[MAXINC];
static char *dirnams[MAXINC]; /* actual directory of #include files */
static int fins[MAXINC];
static int lineno[MAXINC];
/*
* We need:
* "" include dir as dirs[0] +
* MAXIDIRS +
* system default include dir +
* a NULL pointer at the end
*/
static char *dirs[MAXIDIRS+3]; /* -I and <> directories */
static int fin = STDIN_FILENO;
static FILE *fout; /* Init in main(), Mac OS is nonPOSIX */
static int nd = 1;
static int pflag; /* don't put out lines "# 12 foo.c" */
static int passcom; /* don't delete comments */
static int rflag; /* allow macro recursion */
static int hflag; /* Print included filenames */
static int nopredef; /* -undef all */
static int ifno;
# define NPREDEF 64
static char *prespc[NPREDEF];
static char **predef = prespc;
static char *punspc[NPREDEF];
static char **prund = punspc;
static int exfail;
static struct symtab *lastsym;
static void sayline(char *);
static void dump(void);
static char *refill(char *);
static char *cotoken(char *);
char *skipbl(char *);
static char *unfill(char *);
static char *doincl(char *);
static int equfrm(char *, char *, char *);
static char *dodef(char *);
void control(char *);
static struct symtab *stsym(char *);
static struct symtab *ppsym(char *);
void pperror(char *fmt, ...);
void yyerror(char *fmt, ...);
static void ppwarn(char *fmt, ...);
struct symtab *lookup(char *, int);
static struct symtab *slookup(char *, char *, int);
static char *subst(char *, struct symtab *);
static char *trmdir(char *);
static char *copy(char *);
static char *strdex(char *, int);
int yywrap(void);
int main(int argc, char **argav);
#define symsiz 16000
static struct symtab stab[symsiz];
static struct symtab *defloc;
static struct symtab *udfloc;
static struct symtab *incloc;
static struct symtab *ifloc;
static struct symtab *elsloc;
static struct symtab *eifloc;
static struct symtab *elifloc;
static struct symtab *ifdloc;
static struct symtab *ifnloc;
static struct symtab *ysysloc;
static struct symtab *varloc;
static struct symtab *lneloc;
static struct symtab *ulnloc;
static struct symtab *uflloc;
static struct symtab *idtloc;
static struct symtab *pragmaloc;
static struct symtab *errorloc;
static int trulvl;
int flslvl;
static int elflvl;
static int elslvl;
/*
* The sun cpp prints a classification token past the
* "# linenumber filename" lines:
*/
#define NOINCLUDE "" /* Not related to enter/leave incl. file */
#define ENTERINCLUDE "1" /* We are just entering an include file */
#define LEAVEINCLUDE "2" /* We are just leaving an include file */
/* ARGSUSED */
static void
sayline(what)
char *what;
{
if (pflag==0)
fprintf(fout,"# %d \"%s\" %s\n", lineno[ifno], fnames[ifno], what);
}
/*
* data structure guide
*
* most of the scanning takes place in the buffer:
*
* (low address) (high address)
* pbeg pbuf pend
* | <-- BUFFERSIZ chars --> | <-- BUFFERSIZ chars --> |
* _______________________________________________________________________
* |_______________________________________________________________________|
* | | |
* |<-- waiting -->| |<-- waiting -->
* | to be |<-- current -->| to be
* | written | token | scanned
* | | |
* outp inp p
*
* *outp first char not yet written to output file
* *inp first char of current token
* *p first char not yet scanned
*
* macro expansion: write from *outp to *inp (chars waiting to be written),
* ignore from *inp to *p (chars of the macro call), place generated
* characters in front of *p (in reverse order), update pointers,
* resume scanning.
*
* symbol table pointers point to just beyond the end of macro definitions;
* the first preceding character is the number of formal parameters.
* the appearance of a formal in the body of a definition is marked by
* 2 chars: the char WARN, and a char containing the parameter number.
* the first char of a definition is preceded by a zero character.
*
* when macro expansion attempts to back up over the beginning of the
* buffer, some characters preceding *pend are saved in a side buffer,
* the address of the side buffer is put on 'instack', and the rest
* of the main buffer is moved to the right. the end of the saved buffer
* is kept in 'endbuf' since there may be nulls in the saved buffer.
*
* similar action is taken when an 'include' statement is processed,
* except that the main buffer must be completely emptied. the array
* element 'inctop[ifno]' records the last side buffer saved when
* file 'ifno' was included. these buffers remain dormant while
* the file is being read, and are reactivated at end-of-file.
*
* instack[0 : mactop] holds the addresses of all pending side buffers.
* instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
* are dormant, waiting for end-of-file on the current file.
*
* space for side buffers is obtained from 'savch' and is never returned.
* bufstack[0:fretop-1] holds addresses of side buffers which
* are available for use.
*/
static void
dump() {
register char *p1;
if ((p1=outp)==inp || flslvl!=0) return;
fwrite(p1, inp - p1, 1, fout);
outp=p1;
}
static char *
refill(p) register char *p; {
/*
* dump buffer. save chars from inp to p. read into buffer at pbuf,
* contiguous with p. update pointers, return new p.
*/
register char *np,*op; register int ninbuf;
dump(); np=pbuf-(p-inp); op=inp;
if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFFERSIZ;}
macdam += np-inp; outp=inp=np;
while (op<p) *np++= *op++;
p=np;
for (;;) {
if (mactop>inctop[ifno]) {
/* retrieve hunk of pushed-back macro text */
op=instack[--mactop]; np=pbuf;
do {
while ((*np++= *op++) != '\0');
} while (op<endbuf[mactop]); pend=np-1;
/* make buffer space avail for 'include' processing */
if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
return(p);
} else {/* get more text from file(s) */
maclvl=0;
if (0<(ninbuf=read(fin,pbuf,BUFFERSIZ))) {
pend=pbuf+ninbuf; *pend='\0';
return(p);
}
/* end of #include file */
if (ifno==0) {/* end of input */
if (plvl > 0) {
int n=plvl,tlin=lineno[ifno];
char *tfil=fnames[ifno];
lineno[ifno]=maclin;
fnames[ifno]=macfil;
pperror("%s: unterminated macro call",
macnam);
lineno[ifno]=tlin; fnames[ifno]=tfil;
np=p;
/*
* shut off unterminated quoted string
*/
*np++='\n';
/* supply missing parens */
while (--n>=0) *np++=')';
pend=np; *np='\0';
if (plvl<0) plvl=0;
return(p);
}
inp=p; dump(); exit(exfail);
}
close(fin);
fin=fins[--ifno];
dirs[0]=dirnams[ifno];
sayline(LEAVEINCLUDE);
}
}
}
#define BEG 0
#define LF 1
static char *
cotoken(p) register char *p; {
register int c,i; char quoc;
static int state = BEG;
if (state!=BEG) goto prevlf;
for (;;) {
again:
while (!isspc(*p++));
switch (*(inp=p-1)) {
case 0: {
if (eob(--p)) {p=refill(p); goto again;}
else ++p; /* ignore null byte */
} break;
case '|': case '&': for (;;) {/* sloscan only */
if (*p++== *inp) break;
if (eob(--p)) p=refill(p);
else break;
} break;
case '=': case '!': for (;;) {/* sloscan only */
if (*p++=='=') break;
if (eob(--p)) p=refill(p);
else break;
} break;
case '<': case '>': for (;;) {/* sloscan only */
if (*p++=='=' || p[-2]==p[-1]) break;
if (eob(--p)) p=refill(p);
else break;
} break;
case '\\': for (;;) {
if (*p++=='\n') {++lineno[ifno]; break;}
if (eob(--p)) p=refill(p);
else {++p; break;}
} break;
case '/': for (;;) {
if (*p++=='*') {/* comment */
if (!passcom) {inp=p-2; dump(); ++flslvl;}
for (;;) {
while (!iscom(*p++));
if (p[-1]=='*') for (;;) {
if (*p++=='/') goto endcom;
if (eob(--p)) {
if (!passcom) {
inp=p;
p=refill(p);
} else if ((p-inp)>=BUFFERSIZ) {
/* split long comment */
inp=p;
/*
* last char written
* is '*'
*/
p=refill(p);
/*
* terminate first part
*/
putc('/',fout);
/*
* and fake start of 2nd
*/
outp=inp=p-=3;
*p++='/';
*p++='*';
*p++='*';
} else {
p=refill(p);
}
} else {
break;
}
} else if (p[-1]=='\n') {
++lineno[ifno];
if (!passcom)
putc('\n',fout);
} else if (eob(--p)) {
if (!passcom) {
inp=p; p=refill(p);
} else if ((p-inp)>=BUFFERSIZ) {
/* split long comment */
inp=p; p=refill(p);
putc('*',fout); putc('/',fout);
outp=inp=p-=2;
*p++='/';
*p++='*';
} else {
p=refill(p);
}
} else {
++p; /* ignore null byte */
}
}
endcom:
if (!passcom) {outp=inp=p; --flslvl; goto again;}
break;
}
if (eob(--p)) p=refill(p);
else break;
} break;
case '"': case '\'': {
quoc=p[-1];
for (;;) {
while (!isquo(*p++));
if (p[-1]==quoc)
break;
if (p[-1]=='\n') {
--p;
break;
} /* bare \n terminates quotation */
if (p[-1]=='\\') {
for (;;) {
if (*p++=='\n') {
++lineno[ifno];
break;
} /* escaped \n ignored */
if (eob(--p)) {
p=refill(p);
} else {
++p;
break;
}
}
} else if (eob(--p)) {
p=refill(p);
} else {
++p; /* it was a different quote character */
}
}
} break;
case '\n': {
++lineno[ifno]; if (isslo) {state=LF; return(p);}
prevlf:
state=BEG;
for (;;) {
if (*p++=='#') return(p);
if (eob(inp= --p)) p=refill(p);
else goto again;
}
}
/* NOTREACHED */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
for (;;) {
while (isnum(*p++));
if (eob(--p)) p=refill(p);
else break;
} break;
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z': case '_':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
#if scw1
#define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
#define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
#else
#define tmac1(c,bit)
#define xmac1(c,bit,op)
#endif
#if scw2
#define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
#define xmac2(c0,c1,cpos,op)\
((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
#else
#define tmac2(c0,c1,cpos)
#define xmac2(c0,c1,cpos,op)
#endif
if (flslvl) goto nomac;
for (;;) {
c= p[-1]; tmac1(c,b0);
i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
tmac2(i,0,7);
while (isid(*p++));
if (eob(--p)) {refill(p); p=inp+1; continue;}
goto lokid;
endid:
if (eob(--p)) {refill(p); p=inp+1; continue;}
tmac2(p[-1],0,-1+(p-inp));
lokid:
slookup(inp,p,0); if (newp) {p=newp; goto again;}
else break;
nomac:
while (isid(*p++));
if (eob(--p)) {p=refill(p); goto nomac;}
else break;
} break;
} /* end of switch */
if (isslo) return(p);
} /* end of infinite loop */
}
/*
* XXX: This unconditionally consumes one token (presuming it's blank? that we
* already consumed it?). That's pretty terrible, but it's also very fragile,
* and I don't want to change it.
*/
char *
skipbl(p) register char *p; {/* get next non-blank token */
do {
outp=inp=p;
p=cotoken(p);
} while ((toktyp+COFF)[(int)*inp]==BLANK);
return(p);
}
static char *
unfill(p) register char *p; {
/*
* take <= BUFFERSIZ chars from right end of buffer and put them on instack .
* slide rest of buffer to the right, update pointers, return new p.
*/
register char *np,*op; register int d;
if (mactop>=MAXFRE) {
pperror("%s: too much pushback",macnam);
p=inp=pend; dump(); /* begin flushing pushback */
while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
}
if (fretop>0)
np=bufstack[--fretop];
else {
np=savch; savch+=BUFFERSIZ;
if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
*savch++='\0';
}
instack[mactop]=np; op=pend-BUFFERSIZ; if (op<p) op=p;
for (;;) {
while ((*np++= *op++) != '\0');
if (eob(op))
break;
} /* out with old */
endbuf[mactop++]=np; /* mark end of saved text */
np=pbuf+BUFFERSIZ;
op=pend-BUFFERSIZ;
pend=np;
if (op<p)
op=p;
while (outp<op) *--np= *--op; /* slide over new */
if (bob(np))
pperror("token too long");
d=np-outp; outp+=d; inp+=d; macdam+=d;
return(p+d);
}
static char *
doincl(p) register char *p; {
int filok,inctype;
register char *cp; char **dirp,*nfil; char filname[BUFFERSIZ];
filname[0] = '\0'; /* Make lint quiet */
p=skipbl(p); cp=filname;
if (*inp++=='<') {/* special <> syntax */
inctype=1;
for (;;) {
outp=inp=p; p=cotoken(p);
if (*inp=='\n') {--p; *cp='\0'; break;}
if (*inp=='>') { *cp='\0'; break;}
# ifdef gimpel
if (*inp=='.' && !intss()) *inp='#';
# endif
while (inp<p) *cp++= *inp++;
}
} else if (inp[-1]=='"') {/* regular "" syntax */
inctype=0;
# ifdef gimpel
while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
# else
while (inp<p) *cp++= *inp++;
# endif
if (*--cp=='"') *cp='\0';
} else {pperror("bad include syntax",0); inctype=2;}
/* flush current file to \n , then write \n */
++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
inp=p; dump(); if (inctype==2) return(p);
/* look for included file */
if (ifno+1 >=MAXINC) {
pperror("Unreasonable include nesting",0); return(p);
}
if ((nfil=savch)>sbf+SBSIZE-BUFFERSIZ) {
pperror("no space");
exit(exfail);
}
filok=0;
for (dirp=dirs+inctype; *dirp; ++dirp) {
if (filname[0]=='/' || **dirp=='\0') {
strcpy(nfil,filname);
} else {
strcpy(nfil,*dirp);
# if unix
strcat(nfil,"/");
# endif
strcat(nfil,filname);
}
if (0<(fins[ifno+1]=open(nfil, O_RDONLY))) {
filok=1; fin=fins[++ifno]; break;
}
}
if (filok==0) {
pperror("Can't find include file %s",filname);
} else {
lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
sayline(ENTERINCLUDE);
if (hflag)
fprintf(stderr, "%s\n", nfil);
/* save current contents of buffer */
while (!eob(p)) p=unfill(p);
inctop[ifno]=mactop;
}
return(p);
}
static int
equfrm(a,p1,p2) register char *a,*p1,*p2; {
register char c; int flag;
c= *p2; *p2='\0';
flag=strcmp(a,p1); *p2=c; return(flag==SAME);
}
static char *
dodef(p) char *p; {/* process '#define' */
register char *pin,*psav,*cf;
char **pf,**qf; int b,c,params; struct symtab *np;
char *oldval,*oldsavch;
char *formal[MAXFRM]; /* formal[n] is name of nth formal */
char formtxt[BUFFERSIZ]; /* space for formal names */
formtxt[0] = '\0'; /* Make lint quiet */
if (savch>sbf+SBSIZE-BUFFERSIZ) {
pperror("too much defining");
return(p);
}
oldsavch=savch; /* to reclaim space if redefinition */
++flslvl; /* prevent macro expansion during 'define' */
p=skipbl(p); pin=inp;
if ((toktyp+COFF)[(int)*pin]!=IDENT) {
ppwarn("illegal macro name");
while (*inp!='\n')
p=skipbl(p);
return(p);
}
np=slookup(pin,p,1);
if (getenv("CPP_DEBUG_DEFINITIONS") != NULL)
fprintf(stderr, "*** defining %s at %s:%d\n",
np->name, fnames[ifno], lineno[ifno]);
if ((oldval=np->value) != NULL)
savch=oldsavch; /* was previously defined */
b=1; cf=pin;
while (cf<p) {/* update macbit */
c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
if (cf!=p) {
xmac2(c,*cf,-1+(cf-pin),|=);
} else {
xmac2(c,0,-1+(cf-pin),|=);
}
}
params=0; outp=inp=p; p=cotoken(p); pin=inp;
formal[0] = ""; /* Prepare for hack at next line... */
pf = formal; /* Make gcc/lint quiet, pf only used with params!=0 */
if (*pin=='(') {/* with parameters; identify the formals */
cf=formtxt; pf=formal;
for (;;) {
p=skipbl(p); pin=inp;
if (*pin=='\n') {
--lineno[ifno];
--p;
pperror("%s: missing )",np->name);
break;
}
if (*pin==')') break;
if (*pin==',') continue;
if ((toktyp+COFF)[(int)*pin]!=IDENT) {
c= *p;
*p='\0';
pperror("bad formal: %s",pin);
*p=c;
} else if (pf>= &formal[MAXFRM]) {
c= *p;
*p='\0';
pperror("too many formals: %s",pin);
*p=c;
} else {
*pf++=cf;
while (pin<p)
*cf++= *pin++;
*cf++='\0';
++params;
}
}
if (params==0)
--params; /* #define foo() ... */
} else if (*pin=='\n') {
--lineno[ifno];
--p;
}
/*
* remember beginning of macro body, so that we can
* warn if a redefinition is different from old value.
*/
oldsavch=psav=savch;
for (;;) {/* accumulate definition until linefeed */
outp=inp=p; p=cotoken(p); pin=inp;
if (*pin=='\\' && pin[1]=='\n')
continue; /* ignore escaped lf */
if (*pin=='\n') break;
if (params) {
/* mark the appearance of formals in the definiton */
if ((toktyp+COFF)[(int)*pin]==IDENT) {
for (qf=pf; --qf>=formal; ) {
if (equfrm(*qf,pin,p)) {
*psav++=qf-formal+1;
*psav++=WARN;
pin=p;
break;
}
}
} else if (*pin=='"' || *pin=='\'') {
/* inside quotation marks, too */
char quoc= *pin;
for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
while (pin<p && !isid(*pin))
*psav++= *pin++;
cf=pin;
while (cf<p && isid(*cf))
++cf;
for (qf=pf; --qf>=formal; ) {
if (equfrm(*qf,pin,cf)) {
*psav++=qf-formal+1;
*psav++=WARN;
pin=cf;
break;
}
}
while (pin<cf)
*psav++= *pin++;
}
}
}
while (pin<p) *psav++= *pin++;
}
*psav++=params; *psav++='\0';
if ((cf=oldval)!=NULL) {/* redefinition */
--cf; /* skip no. of params, which may be zero */
while (*--cf); /* go back to the beginning */
if (0!=strcmp(++cf,oldsavch)) {
/* redefinition different from old */
--lineno[ifno];
ppwarn("%s redefined",np->name);
++lineno[ifno];
np->value=psav-1;
} else {
psav=oldsavch; /* identical redef.; reclaim space */
}
} else {
np->value=psav-1;
}
--flslvl; inp=pin; savch=psav; return(p);
}
#define fasscan() ptrtab=fastab+COFF
#define sloscan() ptrtab=slotab+COFF
void
control(p) register char *p; {/* find and handle preprocessor control lines */
register struct symtab *np;
for (;;) {
fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
sloscan(); p=skipbl(p);
*--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
if (np==defloc) {/* define */
if (flslvl==0) {p=dodef(p); continue;}
} else if (np==incloc) {/* include */
if (flslvl==0) {p=doincl(p); continue;}
} else if (np==ifnloc) {/* ifndef */
++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
if (flslvl==0 && np->value==0) ++trulvl;
else ++flslvl;
} else if (np==ifdloc) {/* ifdef */
++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
if (flslvl==0 && np->value!=0) ++trulvl;
else ++flslvl;
} else if (np==eifloc) {/* endif */
if (flslvl) {if (--flslvl==0) sayline(NOINCLUDE);}
else if (trulvl) --trulvl;
else pperror("If-less endif",0);
if (flslvl == 0)
elflvl = 0;
elslvl = 0;
} else if (np==elifloc) {/* elif */
if (flslvl == 0)
elflvl = trulvl;
if (flslvl) {
if (elflvl > trulvl) {
;
} else if (--flslvl != 0) {
++flslvl;
} else {
newp = p;
if (yyparse()) {
++trulvl;
sayline(NOINCLUDE);
} else {
++flslvl;
}
p = newp;
}
} else if (trulvl) {
++flslvl;
--trulvl;
} else
pperror("If-less elif");
} else if (np==elsloc) {/* else */
if (flslvl) {
if (elflvl > trulvl)
;
else if (--flslvl!=0) ++flslvl;
else {++trulvl; sayline(NOINCLUDE);}
}
else if (trulvl) {++flslvl; --trulvl;}
else pperror("If-less else",0);
if (elslvl==trulvl+flslvl)
pperror("Too many #else's");
elslvl=trulvl+flslvl;
} else if (np==udfloc) {/* undefine */
if (flslvl==0) {
++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
}
} else if (np==ifloc) {/* if */
newp=p;
if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
p=newp;
} else if (np == idtloc) { /* ident */
if (pflag == 0)
while (*inp != '\n') /* pass text */
p = cotoken(p);
} else if (np == pragmaloc) { /* pragma */
while (*inp != '\n') /* pass text */
p = cotoken(p);
} else if (np == errorloc) { /* error */
#ifdef EXIT_ON_ERROR
if (trulvl > 0) {
char ebuf[BUFFERSIZ];
p = ebuf;
while (*inp != '\n') {
if (*inp == '\0')
if (eob(--inp)) {
inp = refill(inp);