-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpp_old.c
executable file
·1506 lines (1370 loc) · 45.4 KB
/
cpp_old.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
#include "stdio.h"
#include <string.h>
#include "cpm.h"
#include <unixio.h>
#include <stdarg.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#if 0
static char sccsid[] = "@(#)cpp.c 1.6 7/1/83";
#endif
/* C command
/* written by John F. Reiser
/* July/August 1978
*/
/* File - CPP.H Created 08.02.2021 Last Modified 24.10.2021 */
#define STATIC static
/* 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
# if gcos
#define ALFSIZ 512 /* alphabet size */
# else
#define ALFSIZ 256 /* alphabet size */
# endif
/* Used macros */
#define isslo (ptrtab==(slotab+COFF))
#define isid(a) ((fastab+COFF)[a]&IB)
#define isspc(a) (ptrtab[a]&SB)
#define isnum(a) ((fastab+COFF)[a]&NB)
#define iscom(a) ((fastab+COFF)[a]&CB)
#define isquo(a) ((fastab+COFF)[a]&QB)
#define iswarn(a) ((fastab+COFF)[a]&WB)
#define eob(a) ((a)>=pend)
#define bob(a) (pbeg>=(a))
#define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
#define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
#define tmac2(c0,c1,cpos)
#define xmac2(c0,c1,cpos,op)
#define BLANK 1
#define IDENT 2
#define NUMBR 3
/* 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
#define DROP 0xFE /* special character not legal ASCII or EBCDIC */
#define WARN DROP
#define SAME 0
#define MAXINC 10 /* nested includes */
#define MAXFRE 14 /* max buffers of macro pushback */
#define MAXFRM 31 /* max number of formals/actuals to a macro */
#define SBSIZE 12000 /* */
#ifndef BUFSIZ
#define BUFSIZ 512
#endif
#define NCPS 31 /* */
#define symsiz 500 /* */
#define NPREDEF 20 /* */
#define SALT '#'
#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
#define BEG 0
#define LF 1
/*
Declarations of variables and arrays
*/
/*char *alloc();*/
char *pbeg,*pbuf,*pend;
char *outp,*inp;
char *newp;
char cinit;
/* some code depends on whether characters are sign or zero extended */
/* #if '\377' < 0 not used here, old cpp doesn't understand */
char macbit[ALFSIZ+11];
char toktyp[ALFSIZ];
#if scw2
char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+NCPS];
#endif
char fastab[ALFSIZ];
char slotab[ALFSIZ];
char *ptrtab;
char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
char sbf[SBSIZE];
char *savch = sbf;
static char warnc = 0xFE /* special character not legal ASCII or EBCDIC */;
int mactop,fretop;
char *instack[MAXFRE],*bufstack[MAXFRE],*endbuf[MAXFRE];
int plvl; /* parenthesis level during scan for macro actuals */
int maclin; /* line number of macro call requiring actuals */
char *macfil; /* file name of macro call requiring actuals */
char *macnam; /* name of macro requiring actuals */
int maclvl; /* # calls since last decrease in nesting level */
char *macforw; /* pointer which must be exceeded to decrease nesting level */
int macdam; /* offset to macforw due to buffer shifting */
int inctop[MAXINC];
char *fnames[MAXINC];
char *dirnams[MAXINC]; /* actual directory of #include files */
FILE *fins[MAXINC];
int lineno[MAXINC];
char *dirs[10]; /* -I and <> directories */
/*char *strdex(), *copy(), *subst(), *trmdir();
struct symtab *stsym();*/
FILE * fin = stdin; /* Code modified by Hi-Tech */
FILE *fout = stdout; /* */
int nd = 1;
int pflag; /* don't put out lines "# 12 foo.c" */
/*int inasm = FALSE; / TRUE if we are in #asm stuff (Don't expand macros) */
int passcom; /* don't delete comments */
int indef; /* in define w_9cb2 */
int rflag; /* allow macro recursion */
int ifno;
char *prespc[NPREDEF];
char **predef = prespc;
char *punspc[NPREDEF];
char **prund = punspc;
int exfail;
struct symtab {
char *name;
char *value;
}
*lastsym/*, *lookup(), *slookup()*/;
struct symtab stab[symsiz];
struct symtab *defloc; /* w_9cb0 */
struct symtab *udfloc;
struct symtab *incloc;
struct symtab *ifloc;
struct symtab *elsloc;
struct symtab *eifloc;
struct symtab *ifdloc;
struct symtab *ifnloc;
/*struct symtab *ysysloc;*/
struct symtab *varloc; /* unused w_9cb8 */
struct symtab *lneloc;
struct symtab *ulnloc;
struct symtab *uflloc; /* w_9cb6 */
struct symtab *uccloc; /* Code added by Hi-Tech w_9ca0 */
struct symtab *asmloc;
struct symtab *easmloc; /* w_9cb4 */
int trulvl;
int flslvl;
/*********************************************************************
Prototype functions are located in sequence of being in original
binary image of CPP.COM
ok++ - Full binary compatibility with code in original file;
ok+ - Code generated during compilation differs slightly,
but is logically correct;
ok - C source code was compiled successfully, but not verified.
*********************************************************************/
void sayline(void); /* sub_013dh */
void dump(void); /* sub_016bh */
char * refill(char *); /* sub_01bch */
char * cotoken(char *); /* sub_0465h */
char * skipbl(char *); /* sub_0a47h */
char * unfill(char *); /* sub_0a7bh */
char * doincl(char *); /* sub_0c59h */
int equfrm(char *, char *, char *);/* sub_0f30h */
int equdef(char *, char *); /* sub_0f7bh */
char * dodef(char *); /* sub_10b2h */
void control(char *); /* sub_16ach */
struct symtab * stsym(char *); /* sub_19cch */
struct symtab * ppsym(char *); /* sub_1a9bh */
void pperror(char *fmt, ...); /* sub_1ad2h */
void yyerror(char *fmt, ...); /* sub_1b49h */
void ppwarn(char *fmt, ...); /* sub_1b67h */
struct symtab * lookup(char *, int); /* sub_1b97h */
struct symtab * slookup(char *, char *, int); /* sub_1d22h */
char * subst(char *, struct symtab *);/* sub_1df5h */
char * trmdir(char *); /* sub_21efh */
char * copy(char *); /* sub_2242h */
char * strdex(char *, int); /* sub_2272h */
int yywrap(void); /* sub_22a2h */
int main(int argc, char **argv); /* sub_22a6h */
int yylex(void); /* sub_29f6h */
int yyparse(); /* sub-2c76h */
int tobinary(char *, int); /* sub_2892h */
/*char ** _getargs(char *, char *); / sub_32bfh /
FILE * fopen(char *, char *); / sub_3f61h /
int fread(char *, int, unsigned, FILE *); / sub-43a4h */
#ifndef CPM
FILE * open(char *, char *);
#endif
#ifdef CPM
extern char ** _getargs();
extern int _argc_;
#endif
/*********************************************************************
* sayline sub_013dh OK++ Used in:
*********************************************************************/
void sayline(void) {
if(pflag == 0)
fprintf(fout, "# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
}
/* data structure guide
/*
/* most of the scanning takes place in the buffer:
/*
/* (low address) (high address)
/* pbeg pbuf pend
/* | <-- BUFSIZ chars --> | <-- BUFSIZ 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.
*/
/*********************************************************************
* dump sub_016bh OK++ Used in:
*
* Write part of buffer which lies between outp and inp.
* This should be a direct call to 'write', but the system slows
* to a crawl if it has to do an unaligned copy. thus we buffer.
* This silly loop is 15% of the total time, thus even the 'putc'
* macro is too slow.
*********************************************************************/
void dump(void) {
FILE * f;
register char * p1;
if((p1 = outp) == inp || flslvl != 0) return;
f = fout;
while(p1 < inp) putc(*p1++, f);
outp = p1;
}
/*********************************************************************
* refill sub_01bch OK++ Used in:
*
* Dump buffer. Save chars from inp to p. Read into buffer at pbuf,
* contiguous with p. Update pointers, return new p.
*********************************************************************/
char * refill(register char *p) {
char * np;
char * op;
int ninbuf;
dump();
np = pbuf - (p - inp);
op = inp;
if(bob(np+1)) {
pperror("token too long");
np = pbeg;
p = inp + BUFSIZ;
}
macdam += np - inp; outp = inp = np;
while(op < p) *np++ = *op++;
p = np;
for(;;) {
if(inctop[ifno] < mactop) {
/* retrieve hunk of pushed-back macro text */
op = instack[--mactop];
np = pbuf;
do { while((*np++ = *op++) != '\0'); }
while(endbuf[mactop] > op); 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(((int)(ninbuf = fread(pbuf, 1, BUFSIZ, fin))) > (int)0) {
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\tcall", macnam);
lineno[ifno] = tlin;
fnames[ifno] = tfil;
np = p;
*np++ = '\n'; /* shut off unterminated quoted string */
while(--n>=0) *np++ = ')'; /* supply missing parens */
pend = np;
*np='\0';
if(plvl < 0) plvl = 0;
return(p);
}
inp = p; dump();
exit(exfail);
}
fclose(fin);
fin = fins[--ifno];
dirs[0] = dirnams[ifno];
sayline();
}
}
}
/*********************************************************************
* cotoken sub_0465h OK++ Used in:
*********************************************************************/
char * cotoken(register char * p) {
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: /* l0490h; */
{
if(eob(--p)) { p = refill(p); goto again; } /* m0: */
else ++p; /* ignore null byte */
}
break;
/*case 1: case 2: case 3: case 4: case 5:
case 6: case 7: case 8: case 9: case 11:
case 12: case 13: case 14: case 15: case 16:
case 17: case 18: case 19: case 20: case 21:
case 22: case 23: case 24: case 25: case 26:
case 27: case 28: case 29: case 30: case 31:
case 32: case 35: case 36: case 37: case 40:
case 41: case 42: case 43: case 44: case 45:
case 46: case 58: case 59: case 63: case 64:
case 91: case 93: case 94: case 96: case 123: / goto m7; l04ddh; /
break;*/
case '|':
case '&': /* l04eah; */
for (;;) { /* sloscan only */
if (*p++ == *inp) break; /* m8: */
if (eob(--p)) p = refill(p);
else break;
} /* goto m8; */
break;
case '=':
case '!':
for (;;) { /* sloscan only */
if (*p++ == '=') break; /* m9: */
if (eob(--p)) p = refill(p);
else break;
} /* goto m9; */
break;
case '<':
case '>': /* l0536h; */
for (;;) { /* sloscan only */
if (*p++ == '=' || p[-2] == p[-1]) break; /* m10: */
if (eob(--p)) p = refill(p);
else break;
} /* goto m10; */
break;
case '\\': /* l057dh; */
for (;;) {
if (*p++ == '\n') {++lineno[ifno]; break;} /* m111: */
if (eob(--p)) p = refill(p); /* m11: */
else {++p; break;} /* m12: */
}
break;
case '/': /* l075fh; */
for(;;) {
if(*p++ == '*') { /* comment K&R */
if((indef) || (!passcom)) { /* Code changed by Hi-Tech */
inp = p - 2; /* m31: */
while((toktyp+COFF)[*(inp-1)] == 1 && inp != outp) { /* m14: */
inp--;
}
dump(); ++flslvl; /* m15: */
}
for (;;) {
while (!iscom(*p++)); /* m16: */
if(p[-1] == '*') for (;;) {
if(*p++ == '/') goto endcom; /* m17: */
if(eob(--p)) {
if((indef) || (!passcom)) { /* Code changed by Hi-Tech */
inp = p; /* m18: */
p = refill(p); /* m20: */
} else if((p - inp) >= BUFSIZ) { /* 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); /* m19: */
}
} else {
break;
}
} else if(p[-1] == '\n') { /* m21: */
++lineno[ifno];
if((indef) || (!passcom)) /* Code changed by Hi-Tech */
fputc('\n', fout); /* m22: */
} else if(eob(--p)) { /* m23: */
if((indef) || (!passcom)) { /* Code changed by Hi-Tech */
inp = p; /* m24: */
p = refill(p); /* m25: */
} else if((p - inp) >= BUFSIZ) { /* m26: */
/* split long comment */
inp = p;
p = refill(p);
putc('*', fout);
putc('/', fout);
outp = inp = p -= 2;
*p++ = '/';
*p++ = '*';
} else {
p = refill(p); /* m25: */
}
} else {
++p; /* ignore null byte m27: */
}
} /* goto m16; */
endcom:
if((indef) || (!passcom)) { /* Code changed by Hi-Tech */ /* m28: */
outp = inp = p; --flslvl; goto again; /* m29: */
}
break;
}
if(eob(--p)) p = refill(p); /* m30: */
else break;
}
break;
case '"':
case '\'': /* l0782h; */
quoc = p[-1]; /* m311: */
for (;;) {
while (!isquo(*p++)); /* m32: */
if (p[-1] == quoc) break;
/* bare \n terminates quotation */
if (p[-1] == '\n') { --p; break; }
if (p[-1] == '\\') { /* m33: */
for (;;) {
/* escaped \n ignored */
if (*p++ == '\n') {++lineno[ifno]; break;} /* m34: */
if (eob(--p)) p = refill(p); /* m35: */
else {++p; break;}
} /* goto m34; */
} else if (eob(--p)) p = refill(p); /* m37: */
else ++p; /* it was a different quote character m36: */
} /* goto m32; */
break;
case '\n': /* l080ch; */
{
++lineno[ifno]; if (isslo) {state = LF; return (p);}/* m311: */
prevlf:
state = BEG; /* m1: */
for (;;) {
if (*p++ == '#') return (p); /* m2: */
if (eob(inp = --p)) p = refill(p);
else goto again;
}
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': /* l0852h; */
for(;;) {
while(isnum(*p++)); /* m39: */
if(eob(--p)) p = refill(p);
else break;
} /* goto m39; */
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': /* l0880h; */
#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; /* m391: */
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++)); /* m40: */
if (eob(--p)) {refill(p); p = inp + 1; continue;}
goto lokid;
endid: if (eob(--p)) {refill(p); p = inp + 1; continue;}
lokid: slookup(inp, p, 0); if(newp) {p = newp; goto again;}
else break;
nomac: while (isid(*p++)); /* m43: */
if (eob(--p)) {p = refill(p); goto nomac;}
else break;
}
break;
} /* end of switch */
if(isslo) return p; /* goto m3; m7: */
} /* end of infinite loop */
}
/****************************************************************
* skipbl sub-0a47h OK++ Get next non-blank token Used in:
****************************************************************/
char * skipbl(register char * p) {
do { outp = inp = p; p = cotoken(p); }
while ((toktyp+COFF)[*inp] == BLANK);
return (p);
}
/****************************************************************
* unfill sub_0a7bh OK++ Used in:
* take <= BUFSIZ chars from right end of buffer and put them on
* instack. Slide rest of buffer to the right, update pointers,
* return new p.
****************************************************************/
char * unfill(register char * p) {
char * np, * op;
int d;
if (mactop >= MAXFRE) {
pperror("%s: too much pushback", macnam);
p = inp = pend; dump(); /* begin flushing pushback */
while(inctop[ifno] < mactop) {
p = refill(p); p = inp = pend; dump();
}
}
if(0 < fretop) np = bufstack[--fretop];
else {
np = savch; savch += BUFSIZ;
if(savch >= (char *)(sbf + SBSIZE)) { /* sbf + SBSIZE = 0x9605 */
pperror("no space"); exit(exfail);
}
*savch++ = '\0';
}
instack[mactop] = np; op = pend - BUFSIZ;
if(op < p) op = p;
for(;;) { while(*np++ = *op++);
if(eob(op)) break;} /* out with old */
endbuf[mactop++] = np; /* mark end of saved text */
np = pbuf + BUFSIZ;
op = pend - BUFSIZ;
pend = np;
if(op < p) op = p;
while(outp < op) *--np = *--op; /* slide over new */
if(bob(np)) pperror("token too\tlong");
d = np - outp;
outp += d;
inp += d;
macdam += d;
return (d + p);
}
/****************************************************************
* doincl sub-0c59h OK++ Used in:
****************************************************************/
char * doincl(register char * p) {
int filok; /* l1; */
int inctype; /* l2; */
char * cp; /* l3; */
char ** dirp; /* l4; */
char * nfil; /* l5; */
char filname[BUFSIZ];
p = skipbl(p); cp = filname;
if(*inp++ == '<') { /* special <> syntax */
inctype = 1;
/*++flslvl; / prevent macro expansion Code removed by Hi-Tech */
for(;;) {
outp = inp = p; p = cotoken(p);
if(*inp == '\n') { --p; *cp='\0'; break; }
if(*inp == '>') { *cp='\0'; break; }
while(inp < p) { *cp++ = *inp++; }
}
/*--flslvl; / reenable macro expansion Code removed by Hi-Tech */
} else if(inp[-1] == '"') { /* regular "" syntax */
inctype = 0;
while(inp < p) *cp++ = *inp++;
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(sbf+SBSIZE-BUFSIZ/*0x9405*/ < (nfil = savch)) {
pperror("no\tspace");
exit(exfail);
}
filok = 0;
for(dirp = dirs + inctype; *dirp; ++dirp) {
if((filname[0] == '/') || **dirp == '\0')
strcpy(nfil, filname); /* prcpy */
else {
strcpy(nfil, *dirp); /* prcpy */
strcat(nfil, filname); /* prcat */
}
if((fins[ifno+1] = fopen(nfil, "r")) != 0) {
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();
/* save current contents of buffer */
while (!eob(p)) p = unfill(p);
inctop[ifno] = mactop;
}
return p;
}
/****************************************************************
* equfrm sub_0f30h OK++ Used in:
****************************************************************/
int equfrm(register char * a, char * p1, char * p2) {
char c; int flag;
c = *p2;
*p2 = '\0';
flag = strcmp(a, p1);
*p2 = c;
return (flag == SAME);
}
/****************************************************************
* equdef sub_0f7bh OK++ Used in: dodef
* Check whether two defines are equal. alltrim and then strcmp
****************************************************************/
int equdef(register char * p1, char * p2) {
int res;
char * p1end;
char * p2end;
char p1save;
char p2save;
char * q;
/* p1 skip leading whitespace */ /* Bug in original code s/b ' ','\t' */
for (; *p1 && (*p1 == '\t' || *p1 == '\t'); p1++); /* m1 : */
/* scan to the end */
for (p1end = 0, q = p1; *q ; q++) { /* m3: */
if (*q == ' ' || *q == '\t')
continue;
p1end = q;
}
if(p1end != 0) { /* token present */
p1save = *++p1end; /* end of token */
*p1end = '\0'; /* trim right */
}
/* p2 */ /* Bug in original code s/b ' ','\t' */
for (; *p2 && (*p2 == '\t' || *p2 == '\t'); p2++); /* m9 : */
for (p2end = 0, q = p2; *q ; q++) { /* m10: */
if (*q == ' ' || *q == '\t')
continue;
p2end = q;
}
if(p2end != 0) {
p2save = *++p2end;
*p2end = '\0';
}
res = strcmp(p1, p2); /* m14: */
if(p1end != 0) *p1end = p1save; /* revert right trim */
if(p2end != 0) *p2end = p2save; /* m15: */
return res; /* m16: */
}
/****************************************************************
* dodef sub-10b2h OK++ Used in:
****************************************************************/
char * dodef(char * p) {
char * psav; /* l1 */
char * cf; /* l2 */
char ** pf; /* l3 */
char ** qf; /* l4 */
int b; /* l5 */
int c; /* l6 */
int params; /* l7 */
struct symtab * np; /* l8 */
char quoc; /* l9 */
char * oldval; /* l10 */
char * oldsavch; /* l11 */
char * formal[MAXFRM]; /* formal[n] is name of nth formal */
char formtxt[BUFSIZ]; /* space for formal names */
register char * pin;
if(sbf+SBSIZE-BUFSIZ /*0x9405*/< savch) { pperror("too much\tdefining"); return p; }
oldsavch = savch; /* to reclaim space if redefinition */
flslvl++; /* prevent macro expansion during 'define' */
p = skipbl(p); pin = inp;
if((toktyp+COFF)[*pin] != IDENT) {
ppwarn("illegal\tmacro name"); while(*inp != '\n') p = skipbl(p); return(p);
}
np = slookup(pin, p, 1);
if(oldval = np->value) savch = oldsavch; /* was previously defined */
b = 1; cf = pin;
while(cf < p) { /* update macbit */
c = *cf++; xmac1(c,b,|=); b = (b + b) & 0xFF;
}
params = 0; outp = inp = p; p = cotoken(p); pin = inp;
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)[*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;
indef = 1;
for(;;) { /* accumulate definition until linefeed */
outp = inp = p; p = cotoken(p); pin = inp; /* m19: */
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)[*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 */
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';
indef = 0;
if((cf = oldval) != NULL) { /* redefinition */
--cf; /* skip no. of params, which may be zero */
while(*--cf); /* go back to the beginning */
#if CPM
if(0 != equdef(++cf, oldsavch)) { /* redefinition different from old */
#else
if(0 != strcmp(++cf, oldsavch)) {
#endif
--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
/****************************************************************
* control sub-16ach OK++
* Find and handle preprocessor control lines
****************************************************************/
void control(register char * p) {
struct symtab * np;
for(;;) {
fasscan(); /* m1: */
p = cotoken(p);
if(*inp == '\n') ++inp;
dump(); /* m2: */
sloscan();
p = skipbl(p);
*--inp = SALT;
outp = inp;
++flslvl;
np = slookup(inp, p, 0);
--flslvl;
if(defloc == np) { /* "define" */
if(flslvl == 0) {
p = dodef(p); /* m3: */
continue; /* goto m1; */
}
} else if(incloc == np) { /* "include" m4: */
if(flslvl == 0) {
p = doincl(p);
continue; /* goto m1; */
}
} else if(ifnloc == np) { /* "ifndef" m5: */
++flslvl;
p = skipbl(p);
np = slookup(inp, p, 0);
--flslvl;
if(flslvl == 0 && np->value == 0) ++trulvl;
else ++flslvl;
} else if(ifdloc == np) { /* ifdef m8: */
++flslvl;
p = skipbl(p);
np = slookup(inp, p, 0);
--flslvl;
if(flslvl == 0 && np->value != 0) ++trulvl;
else ++flslvl;
} else if(eifloc == np) { /* "endif" m11: */
if(flslvl) { if(--flslvl == 0) sayline(); }
else if(trulvl) --trulvl;
else pperror("If-less endif", 0);