Debian Codegraph @

cc/cpp/token.c

raw symbols SCIP
1 /* $Id: token.c,v 1.214 2021/09/12 12:06:30 gmcgarry Exp $ */
2
3 /*
4 * Copyright (c) 2004,2009 Anders Magnusson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * Tokenizer for the C preprocessor.
29 * There are three main routines:
30 * - fastscan() loops over the input stream searching for magic
31 * characters that may require actions.
32 * - yylex() returns something from the input stream that
33 * is suitable for yacc.
34 *
35 * Other functions of common use:
36 * - inpch() returns a raw character from the current input stream.
37 * - inch() is like inpch but \\n and trigraphs are expanded.
38 * - unch() pushes back a character to the input stream.
39 *
40 * Input data can be read from either stdio or a buffer.
41 * If a buffer is read, it will return EOF when ended and then jump back
42 * to the previous buffer.
43 * - setibuf(usch *ptr). Buffer to read from, until NULL, return EOF.
44 * When EOF returned, pop buffer.
45 * - setobuf(usch *ptr). Buffer to write to
46 *
47 * There are three places data is read:
48 * - fastscan() which has a small loop that will scan over input data.
49 * - flscan() where everything is skipped except directives (flslvl)
50 * - inch() that everything else uses.
51 *
52 * 5.1.1.2 Translation phases:
53 * 1) Convert UCN to UTF-8 which is what pcc uses internally (chkucn).
54 * Remove \r (unwanted)
55 * Convert trigraphs (chktg)
56 * 2) Remove \\\n. Need extra care for identifiers and #line.
57 * 3) Tokenize.
58 * Remove comments (fastcmnt)
59 */
60 /* (low address) (high address)
61 * pbeg pend
62 * | |
63 * _______________________________________________________________________
64 * |_______________________________________________________________________|
65 * | | |
66 * |<-- waiting -->| |<-- waiting -->
67 * | to be |<-- current -->| to be
68 * | written | token | scanned
69 * | | |
70 * outp inp p
71 *
72 * *outp first char not yet written to output file
73 * *inp first char of current token
74 * *p first char not yet scanned
75 */
76
77 #ifndef pdp11
78 #include "config.h"
79 #endif
80
81 #include <stdlib.h>
82 #include <string.h>
83 #if defined(HAVE_UNISTD_H) || defined(pdp11)
84 #include <unistd.h>
85 #endif
86 #include <fcntl.h>
87
88 #ifndef pdp11
89 #include "compat.h"
90 #endif
91 #include "cpp.h"
92
93 static void cvtdig(int);
94 static int dig2num(int);
95 static int charcon(void);
96 static void elsestmt(void);
97 static void ifdefstmt(void);
98 static void ifndefstmt(void);
99 static void endifstmt(void);
100 static void ifstmt(void);
101 static void cpperror(void);
102 static void cppwarning(void);
103 static void undefstmt(void);
104 static void pragmastmt(void);
105 static void elifstmt(void);
106
107 #define unch(x) *--inp = x
108
109 /* protection against recursion in #include */
110 #define MAX_INCLEVEL 100
111 int inclevel;
112 int incmnt, instr;
113 extern int skpows;
114 int escln;
115
116 struct includ *ifiles;
117 usch *pbeg, *outp, *inp, *pend;
118
119 /* used by yylex() buffer expansion */
120 static struct iobuf *lb;
121 static usch *lpbeg, *lpend, *linp;
122 static int lif;
123
124 static usch *ucn(usch *p, usch *q);
125 static void fastcmnt2(int);
126 static int chktg2(int ch);
127
128 /* some common special combos for initialization */
129 #define C_NL (C_SPEC|C_WSNL|C_PACK|C_ESTR)
130 #define C_DX (C_SPEC|C_ID0|C_DIGIT)
131 #define C_I (C_SPEC|C_ID0)
132 #define C_IX (C_SPEC|C_ID0)
133 #define C_NBS (C_SPEC|C_Q|C_PACK|C_ESTR)
134
135 #define FIRST_128 \
136 C_NBS, 0, 0, 0, C_SPEC, C_SPEC, 0, 0, \
137 0, C_WSNL, C_NL, 0, 0, C_PACK, 0, 0, \
138 0, 0, 0, 0, 0, 0, 0, 0, \
139 0, 0, 0, 0, 0, 0, 0, 0, \
140 \
141 C_WSNL, C_2, C_SPEC|C_ESTR, 0, 0, 0, C_2, C_SPEC|C_ESTR, \
142 0, 0, 0, C_2, 0, C_2, 0, C_SPEC|C_Q, \
143 C_DX, C_DX, C_DX, C_DX, C_DX, C_DX, C_DX, C_DX, \
144 C_DX, C_DX, 0, 0, C_2, C_2, C_2, C_PACK, \
145 \
146 0, C_IX, C_IX, C_IX, C_IX, C_IX, C_IX, C_I, \
147 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
148 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
149 C_I, C_I, C_I, 0, C_PACK|C_ESTR, 0, 0, C_I, \
150 \
151 0, C_IX, C_IX, C_IX, C_IX, C_IX, C_IX, C_I, \
152 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
153 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
154 C_I, C_I, C_I, 0, C_2, 0, 0, 0,
155
156 /* utf-8 */
157 #define LAST_128 \
158 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
159 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
160 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
161 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
162 \
163 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
164 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
165 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
166 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
167 \
168 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
169 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
170 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
171 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
172 \
173 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
174 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
175 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I, \
176 C_I, C_I, C_I, C_I, C_I, C_I, C_I, C_I,
177
178 short spechr[256] = {
179 #ifdef CHAR_UNSIGNED
180 FIRST_128 LAST_128
181 #else
182 LAST_128 FIRST_128
183 #endif
184 };
185
186 #define ENDFREE 4 /* space left at end of buffer */
187
188 #if LIBVMF
189 #define INFLIRD (BYTESPERSEG-PBMAX-ENDFREE)
190 #else
191 #define INFLIRD (CPPBUF-PBMAX-ENDFREE)
192 #endif
193
194 static int numnl;
195
196 /*
197 * Convert trigraphs and remove \\n from input stream.
198 */
199 static void
200 packbuf(void)
201 {
202 static usch pbb[10];
203 register usch *p, *q;
204 register int l;
205 usch *rq;
206
207 #ifdef PCC_DEBUG
208 if (*inp == 0 && pend > inp)
209 error("*inp == 0");
210 #endif
211
212 q = pbeg + PBMAX;
213 /* if we found potential trigraph */
214 if (pbb[9]) {
215 p = pbb+10;
216 while ((*--inp = *--p))
217 q--;
218 inp++;
219 pbb[9] = 0;
220 }
221
222 p = inp;
223 rq = q;
224 if (numnl == 0) {
225 for (;;) {
226 while (ISPACK(*p++) == 0)
227 ;
228 if (--p >= pend)
229 return;
230
231 switch (*p) {
232 case '?':
233 if (p[1] == '?'&& chktg2(p[2]))
234 goto slow;
235 psave: if (pend-p < 3) {
236 /* Save for future use */
237 q = pbb+10;
238 while (pend > p)
239 *--q = *--pend;
240 *--q = 0;
241 *pend = 0;
242 return;
243 }
244 break;
245
246 case '\r':
247 goto slow;
248
249 case '\\':
250 if (p[1] == 0)
251 goto psave;
252 if (p[1] == '\n' || (p[1] | 040) == 'u')
253 goto slow;
254 /* avoid fake escape next loop */
255 if (p[1] == '\\')
256 p++;
257 break;
258
259 default:
260 break;
261 }
262 p++;
263 }
264 slow: q = p;
265 }
266
267 /* need to pack, so we must write as well */
268
269 for (;;) {
270 while (ISPACK(*q++ = *p++) == 0)
271 ;
272 if (--p >= pend) {
273 *--q = 0;
274 pend = q;
275 inp = rq;
276 return;
277 }
278 q--;
279
280 switch (*p) {
281 case '\\':
282 if ((l = p[1]) == 0)
283 goto psave2;
284 if (l == '\n') {
285 p += 2;
286 numnl++;
287 } else if (l == 'u') {
288 if (pend-p < 6)
289 goto psave2;
290 q = ucn(p, q);
291 p += 6;
292 } else if (l == 'U') {
293 if (pend-p < 10)
294 goto psave2;
295 q = ucn(p, q);
296 p += 10;
297 } else
298 p++, q++;
299 break;
300
301 case '\r':
302 p++;
303 break;
304
305 case '\n':
306 p++, q++;
307 while (numnl > 0)
308 *q++ = '\n', numnl--;
309 break;
310
311 case '?':
312 if (pend-p < 3)
313 goto psave2;
314 if (p[1] == '?' && (l = chktg2(p[2]))) {
315 /* found trigraph */
316 p += 2;
317 *p = l;
318 } else
319 p++, q++;
320 break;
321
322 case 0:
323 error("stray 0");
324 default:
325 p++, q++;
326 break;
327 }
328
329 }
330
331 psave2:
332 /* Save for future use */
333 #ifdef PCC_DEBUG
334 if (pend-p > 9)
335 error("pend-p > 9");
336 #endif
337
338 inp = rq;
339 rq = pend;
340 pend = q;
341 *q = 0;
342 q = pbb+10;
343 while (rq > p)
344 *--q = *--rq;
345 *--q = 0;
346 return;
347 }
348
349 /*
350 * fill up the input buffer
351 * n tells how nany chars at least. 0 == standard.
352 * 0 if EOF, != 0 if something could fill up buf.
353 */
354 int
355 inpbuf(void)
356 {
357 register usch *ninp, *oinp;
358 register int len;
359
360 if (ifiles->infil == -1)
361 return 0;
362
363 if (inp < pend)
364 error("inp < pend");
365
366 ninp = pbeg + PBMAX + numnl;
367 oinp = inp;
368 while (oinp < pend)
369 *ninp++ = *oinp++;
370 pend = pbeg + INFLIRD + PBMAX;
371 inp = pbeg+PBMAX+numnl;
372
373 if ((len = (int)read(ifiles->infil, ninp, pend - ninp)) < 0)
374 error("read error on file %s", ifiles->orgfn);
375
376 ninp += len;
377 pend = ninp;
378 *pend = 0;
379
380 #if 0
381 { usch *w = inp; while (w < pend) { if (*w == 0) error("*w == 0"); w++; } }
382 #endif
383 packbuf();
384 #if 0
385 { usch *w = inp; while (w < pend) { if (*w == 0) error("*w == 0-2"); w++; } }
386 #endif
387 return pend-inp;
388 }
389
390 #ifdef notyet
391 /*
392 * moves data from inp to p to beginning of buffer.
393 * fill up the input buffer from p to INFLIRD-numnl
394 * update pointers (pend, inp, outp, p)
395 */
396 static usch *
397 refill(usch *p)
398 {
399 register usch *ninp, *oinp;
400 register int len;
401
402 /* dump(); */
403 ninp = pbeg+PBMAX;
404 oinp = inp;
405 while (oinp < pend)
406 *ninp++ = *oinp++;
407 p -= (oinp - ninp);
408 pend -= (oinp - ninp);
409 outp = inp = pbeg+PBMAX;
410
411 if (ifiles->infil == -1)
412 return 0;
413
414 ninp = pbeg + PBMAX + INFLIRD - numnl;
415 do {
416 if ((len = (int)read(ifiles->infil, pend, ninp-pend)) < 0)
417 error("read error on file %s", ifiles->orgfn);
418 if (len == 0)
419 break;
420 pend += len;
421 } while (pend < ninp);
422
423 *pend = 0;
424 packbuf();
425 return p;
426 }
427 #endif
428
429 /*
430 * Return a quick-cooked character.
431 * If buffer empty; return 0.
432 */
433 static int
434 qcchar(void)
435 {
436 register int ch;
437
438 newone: if (ISCQ(ch = *inp++) == 0)
439 return ch;
440
441 switch (ch) {
442 case 0:
443 inp--;
444 if (lb) {
445 pend = lpend, pbeg = lpbeg, inp = linp;
446 ifiles->infil = lif;
447 bufree(lb);
448 lb = 0;
449 goto newone;
450 }
451 if (inpbuf())
452 goto newone;
453 return 0; /* end of file */
454
455 case '/':
456 if (Cflag || incmnt || instr)
457 return '/';
458 incmnt++;
459 ch = qcchar();
460 incmnt--;
461 if (ch == '/' || ch == '*') {
462 int n = ifiles->lineno;
463 fastcmnt2(ch);
464 if (n == ifiles->lineno)
465 return ' ';
466 } else {
467 *--inp = ch;
468 return '/';
469 }
470 goto newone;
471 }
472 error("ch error");
473 return 0; /* XXX */
474 }
475
476 /*
477 * Return trigraph mapping char or 0.
478 */
479 static int
480 chktg2(register int ch)
481 {
482 switch (ch) {
483 case '=': return '#';
484 case '(': return '[';
485 case ')': return ']';
486 case '<': return '{';
487 case '>': return '}';
488 case '/': return '\\';
489 case '\'': return '^';
490 case '!': return '|';
491 case '-': return '~';
492 }
493 return 0;
494 }
495
496 /*
497 * deal with comments in the fast scanner.
498 */
499 static void
500 fastcmnt2(register int ch)
501 {
502 register int lastline = ifiles->lineno;
503
504 incmnt = 1;
505 if (ch == '/') { /* C++ comment */
506 while ((ch = qcchar()) != '\n')
507 ;
508 unch(ch);
509 } else if (ch == '*') {
510 for (;;) {
511 ch = *inp++;
512 if (ISCQ(ch)) {
513 --inp;
514 if ((ch = qcchar()) == 0)
515 break;
516 }
517 if (ch == '*') {
518 if ((ch = qcchar()) == '/') {
519 break;
520 } else
521 unch(ch);
522 } else if (ch == '\n') {
523 putch('\n');
524 ifiles->lineno++;
525 }
526 }
527 } else
528 error("fastcmnt2");
529 if (ch == 0)
530 error("comment at line %d never ends", lastline);
531 incmnt = 0;
532 }
533
534 /*
535 * check for universal-character-name on input, and
536 * unput to the pushback buffer encoded as UTF-8.
537 */
538 static usch *
539 ucn(register usch *p, register usch *q)
540 {
541 unsigned long cp, m;
542 register int ch;
543 usch bs[6];
544 int n;
545
546 p++;
547 n = *p++ == 'u' ? 4 : 8;
548 cp = 0;
549 while (n-- > 0) {
550 if ((ch = (unsigned char)*p++) == 0 ||
551 (ISDIGIT(ch) || ((ch|040) >= 'a' && (ch|040) <= 'f')) == 0) {
552 #if 0 /* leave untouched */
553 warning("invalid universal character name");
554 #endif
555 return q;
556 }
557 cp = cp * 16 + dig2num(ch);
558 }
559
560 #if 0
561 if ((cp < 0xa0 && cp != 0x24 && cp != 0x40 && cp != 0x60)
562 || (cp >= 0xd800 && cp <= 0xdfff)) /* 6.4.3.2 */
563 error("universal character name cannot be used");
564
565 if (cp > 0x7fffffff)
566 error("universal character name out of range");
567 #endif
568
569 n = 0;
570 m = 0x7f;
571 p = bs;
572 while (cp > m) {
573 *p++ = (0x80 | (cp & 0x3f));
574 cp >>= 6;
575 m >>= (n++ ? 1 : 2);
576 }
577 *p++ = (((m << 1) ^ 0xfe) | cp);
578 while (p > bs)
579 *q++ = *--p;
580 return q;
581 }
582
583 /*
584 * deal with comments when -C is active.
585 * Save comments in expanded macros???
586 */
587 void
588 Ccmnt2(register struct iobuf *ob, register int ch)
589 {
590
591 if (skpows)
592 cntline();
593
594 if (ch == '/') { /* C++ comment */
595 putob(ob, ch);
596 do {
597 putob(ob, ch);
598 } while ((ch = qcchar()) && ch != '\n');
599 unch(ch);
600 } else if (ch == '*') {
601 strtobuf((usch *)"/*", ob);
602 for (;;) {
603 ch = qcchar();
604 putob(ob, ch);
605 if (ch == '*') {
606 if ((ch = qcchar()) == '/') {
607 putob(ob, ch);
608 break;
609 } else
610 unch(ch);
611 } else if (ch == '\n') {
612 ifiles->lineno++;
613 }
614 }
615 }
616 }
617
618 /*
619 * Traverse over spaces and comments from the input stream,
620 * Returns first non-space character.
621 */
622 static int
623 fastspc(void)
624 {
625 register int ch;
626
627 while ((ch = qcchar()), ISWS(ch))
628 ;
629 return ch;
630 }
631
632 /*
633 * readin chars and store in buf. Warn about too long names.
634 */
635 usch *
636 bufid(int ch, register struct iobuf *ob)
637 {
638 register int n = ob->cptr;
639
640 do {
641 if (ob->cptr - n == MAXIDSZ)
642 warning("identifier exceeds C99 5.2.4.1");
643 if (ob->cptr < ob->bsz)
644 ob->buf[ob->cptr++] = ch;
645 else
646 putob(ob, ch);
647 } while (ISID(ch = qcchar()));
648 ob->buf[ob->cptr] = 0; /* legal */
649 unch(ch);
650 return ob->buf+n;
651 }
652
653 usch idbuf[MAXIDSZ+1];
654 /*
655 * readin chars and store in buf. Warn about too long names.
656 */
657 usch *
658 readid(int ch)
659 {
660 register int p = 0;
661
662 do {
663 if (p == MAXIDSZ)
664 warning("identifier exceeds C99 5.2.4.1, truncating");
665 if (p < MAXIDSZ)
666 idbuf[p++] = ch;
667 } while (ISID(ch = qcchar()));
668 idbuf[p] = 0;
669 unch(ch);
670 return idbuf;
671 }
672
673 /*
674 * Scan quickly the input file searching for:
675 * - '#' directives
676 * - keywords (if not flslvl)
677 * - comments
678 *
679 * Handle strings, numbers and trigraphs with care.
680 * Only data from pp files are scanned here, never any rescans.
681 * This loop is always at trulvl.
682 */
683 void
684 fastscan(void)
685 {
686 struct iobuf *ob;
687 struct symtab *nl;
688 register int ch, c2;
689 register usch *p;
690
691 goto run;
692
693 for (;;) {
694 /* tight loop to find special chars */
695 /* should use getchar/putchar here */
696 for (;;) {
697 if (inp < pend)
698 ch = *inp++;
699 else
700 ch = qcchar();
701
702 if ((ISSPEC(ch)) != 0)
703 break;
704 putch(ch);
705 }
706
707 switch (ch) {
708 case 0:
709 return;
710
711 case WARN:
712 case CONC:
713 error("bad char passed");
714 break;
715
716 case '/': /* Comments */
717 incmnt++;
718 ch = qcchar();
719 incmnt--;
720 if (ch == '/' || ch == '*') {
721 if (Cflag == 0) {
722 int n = ifiles->lineno;
723 fastcmnt2(ch);
724 if (n == ifiles->lineno)
725 putch(' '); /* 5.1.1.2 p3 */
726 } else {
727 ob = getobuf(BNORMAL);
728 Ccmnt2(ob, ch);
729 ob->buf[ob->cptr] = 0;
730 putstr(ob->buf);
731 bufree(ob);
732 }
733 } else {
734 putch('/');
735 unch(ch);
736 }
737 break;
738
739 case '\n': /* newlines, for pp directives */
740 /* take care of leftover \n */
741 while (escln > 0) {
742 putch('\n');
743 escln--;
744 ifiles->lineno++;
745 }
746 putch('\n');
747 ifiles->lineno++;
748
749 /* search for a # */
750 run: while ((ch = qcchar()) == '\t' || ch == ' ')
751 putch(ch);
752 if (ch == '%') {
753 if ((c2 = qcchar()) != ':')
754 unch(c2);
755 else
756 ch = '#';
757 }
758 if (ch == '#')
759 ppdir();
760 else
761 unch(ch);
762 break;
763
764 case '\'': /* character constant */
765 if (tflag) {
766 putch(ch);
767 break; /* character constants ignored */
768 }
769 /* FALLTHROUGH */
770 case '\"': /* strings */
771 if (skpows)
772 cntline();
773 p = inp;
774 *--inp = ch;
775 for (;;) {
776 while (ISESTR(*p++) == 0)
777 ;
778 if ((c2 = *--p) == 0) {
779 putstr(inp);
780 inp = p;
781 inpbuf();
782 p = inp-1;
783 } else if (c2 == '\\') {
784 p++;
785 } else if (c2 == '\n') {
786 warning("unterminated literal");
787 p--;
788 break;
789 } else if (c2 == ch)
790 break;
791 p++;
792 }
793 while (inp <= p)
794 putch(*inp++);
795 break;
796
797 case '0': case '1': case '2': case '3': case '4':
798 case '5': case '6': case '7': case '8': case '9':
799 if (skpows)
800 cntline();
801 for (;;) {
802 putch(ch);
803 if (*inp == 0 || *inp == '\\')
804 ch = qcchar();
805 else
806 ch = *inp++;
807 if (ch == 0)
808 break;
809 if ((ISID(ch)) == 0 && ch != '.')
810 break;
811 if ((ch|040) == 'e' || (ch|040) == 'p') {
812 if ((c2 = qcchar()) == '-' || c2 == '+') {
813 putch(ch);
814 ch = c2;
815 } else
816 unch(c2);
817 }
818 }
819 *--inp = ch;
820 break;
821
822 case 'L':
823 case 'U':
824 case 'u':
825 if (*inp == 0)
826 inpbuf();
827 if ((c2 = *inp) == '\"' || c2 == '\'') {
828 putch(ch);
829 break;
830 }
831 if (c2 == '8' && ch == 'u') {
832 if (inp[1] == 0)
833 inpbuf();
834 if (inp[1] == '\"') {
835 inp++;
836 putstr((usch *)"u8");
837 break;
838 }
839 }
840 /* FALLTHROUGH */
841 default:
842 #ifdef PCC_DEBUG
843 if ((ISID(ch)) == 0)
844 error("fastscan");
845 #endif
846 if (flslvl)
847 error("fastscan flslvl");
848
849 p = readid(ch);
850 if ((nl = lookup(p, FIND)) != NULL) {
851 if ((ob = kfind(nl)) != NULL) {
852 if (*ob->buf == '-' || *ob->buf == '+')
853 putch(' ');
854 if (skpows)
855 cntline();
856 ob->buf[ob->cptr] = 0;
857 putstr(ob->buf);
858 if (ob->cptr > 0 &&
859 (ob->buf[ob->cptr-1] == '-' ||
860 ob->buf[ob->cptr-1] == '+'))
861 putch(' ');
862 bufree(ob);
863 }
864 } else {
865 putstr(p);
866 }
867 break;
868 }
869 }
870 }
871
872 /*
873 */
874 int
875 yylex(void)
876 {
877 extern int readinc;
878 register int ch, c2, t;
879 struct iobuf *ob;
880 struct symtab *nl;
881
882 igen: while ((ch = qcchar()) == ' ' || ch == '\t')
883 ;
884 t = ISDIGIT(ch) ? NUMBER : ch;
885 if (ch < 128 && (ISC2(ch)))
886 c2 = qcchar();
887 else
888 c2 = 0;
889
890 switch (t) {
891 case '=':
892 if (c2 == '=') ch = EQ;
893 else goto pb;
894 break;
895 case '!':
896 if (c2 == '=') ch = NE;
897 else goto pb;
898 break;
899 case '|':
900 if (c2 == '|') ch = OROR;
901 else goto pb;
902 break;
903 case '&':
904 if (c2 == '&') ch = ANDAND;
905 else goto pb;
906 break;
907 case '<':
908 if (readinc) {
909 unch(c2);
910 t = '>';
911 goto str;
912 }
913 if (c2 == '<') ch = LS; else
914 if (c2 == '=') ch = LE;
915 else goto pb;
916 break;
917 case '>':
918 if (c2 == '>') ch = RS; else
919 if (c2 == '=') ch = GE;
920 else goto pb;
921 break;
922 case '+':
923 case '-':
924 if (ch == c2)
925 error("invalid preprocessor operator %c%c", ch, c2);
926 goto pb;
927
928 case 'L':
929 case 'u':
930 case 'U':
931 if (*inp != '\'')
932 goto ident;
933 inp++;
934 /* FALLTHROUGH */
935 case '\'':
936 yynode.op = NUMBER;
937 yynode.nd_val = charcon();
938 ch = NUMBER;
939 break;
940
941 case NUMBER:
942 cvtdig(ch);
943 ch = NUMBER;
944 break;
945
946 case '\"':
947 str: ob = getobuf(BNORMAL);
948 do {
949 putob(ob, ch);
950 if ((ch = qcchar()) == '\\') {
951 putob(ob, ch);
952 ch = qcchar();
953 } else if (ch == '\n')
954 error("bad lex string");
955 } while (ch != t);
956 putob(ob, ch);
957 putob(ob, 0);
958 ob->cptr--;
959 yynode.nd_ob = ob;
960 ch = STRING;
961 break;
962
963 case '\n':
964 *--inp = t;
965 ch = WARN;
966 break;
967
968 default:
969 ident: if (ISID0(t) == 0)
970 break;
971
972 yynode.op = NUMBER;
973 yynode.nd_val = 0;
974 ch = NUMBER;
975 if ((nl = lookup(readid(t), FIND))) {
976 if (nl->type == DEFLOC) {
977 c2 = 0;
978 while ((t = qcchar()), ISWS(t))
979 ;
980 if (t == '(')
981 c2++, t = qcchar();
982 yynode.nd_val = lookup(readid(t), FIND) != NULL;
983 while ((t = qcchar()), ISWS(t))
984 ;
985 if (c2) {
986 if (t != ')')
987 error("bad defined");
988 } else
989 *--inp = t;
990 } else /* if (nl) */ {
991 if (nl->type == FUNLIKE) {
992 while ((t = qcchar()), ISWS(t))
993 ;
994 *--inp = t;
995 if (t != '(')
996 break;
997 }
998 if ((ob = kfind(nl))) {
999 ob->buf[ob->cptr] = 0;
1000 lpbeg = pbeg, lpend = pend, linp = inp;
1001 lif = ifiles->infil, ifiles->infil = -1;
1002 lb = ob;
1003 inp = pbeg = ob->buf,
1004 pend = pbeg + ob->cptr;
1005 goto igen;
1006 }
1007 }
1008 }
1009 break;
1010 }
1011 //fprintf(stderr, "uulex1: ch '%c' %d val=%lld '%s'\n", ch, ch, yynode.nd_val, inp);
1012 return ch;
1013
1014 pb: *--inp = c2;
1015 //fprintf(stderr, "uulex2: ch '%c' %d val=%lld '%s'\n", ch, ch, yynode.nd_val, inp);
1016 return ch;
1017 }
1018
1019 /*
1020 * A new file included.
1021 * If ifiles == NULL, this is the first file and already opened (stdin).
1022 */
1023 void
1024 pushfile(const usch *file, const usch *fn, int idx, void *incs)
1025 {
1026 struct includ ibuf;
1027 register struct includ *ic;
1028 register int otrulvl;
1029
1030 ic = &ibuf;
1031 ic->next = ifiles;
1032
1033 if (file != NULL) {
1034 if ((ic->infil = open((const char *)file, O_RDONLY)) < 0)
1035 error("pushfile: error open %s", file);
1036 ic->orgfn = ic->fname = file;
1037 if (++inclevel > MAX_INCLEVEL)
1038 error("limit for nested includes exceeded");
1039 } else {
1040 ic->infil = 0;
1041 ic->orgfn = ic->fname = (const usch *)"<stdin>";
1042 }
1043 #if LIBVMF
1044 if (ifiles) {
1045 vmmodify(ifiles->vseg);
1046 vmunlock(ifiles->vseg);
1047 }
1048 ic->vseg = vmmapseg(&ibspc, inclevel);
1049 vmlock(ic->vseg);
1050 #endif
1051 ifiles = ic;
1052
1053 ic->opend = pend - pbeg;
1054 ic->oinp = inp - pbeg;
1055 ic->opbeg = pbeg;
1056 /* dump(); */
1057 #if LIBVMF
1058 pend = inp = pbeg = (usch *)ifiles->vseg->s_cinfo;
1059 #else
1060 pend = inp = pbeg = xmalloc(CPPBUF);
1061 *inp = 0;
1062 #endif
1063 ic->lineno = 1;
1064 escln = 0;
1065 ic->idx = idx;
1066 ic->incs = incs;
1067 ic->fn = fn;
1068 prtline(1);
1069 otrulvl = trulvl;
1070
1071 fastscan();
1072
1073 if (otrulvl != trulvl || flslvl)
1074 error("unterminated conditional");
1075
1076 ifiles = ic->next;
1077 inclevel--;
1078 #if LIBVMF
1079 vmmodify(ic->vseg);
1080 vmunlock(ic->vseg);
1081 if (ifiles) {
1082 ifiles->vseg = vmmapseg(&ibspc, inclevel);
1083 vmlock(ifiles->vseg);
1084
1085 pbeg = (usch *)ifiles->vseg->s_cinfo;
1086 pend = pbeg + ic->opend;
1087 inp = pbeg + ic->oinp;
1088 /* XXX adjust offsets */
1089 }
1090 #else /* LIBVMF */
1091 free(pbeg);
1092 pbeg = ic->opbeg;
1093 pend = pbeg + ic->opend;
1094 inp = pbeg + ic->oinp;
1095 #endif /* LIBVMF */
1096 close(ic->infil);
1097 }
1098
1099 /*
1100 * Print current position to output file.
1101 */
1102 void
1103 prtline(int nl)
1104 {
1105 register struct iobuf *ob;
1106
1107 if (Mflag) {
1108 if (dMflag)
1109 return; /* no output */
1110 if (ifiles->lineno == 1 &&
1111 (MMDflag == 0 || ifiles->idx != SYSINC)) {
1112 ob = bsheap(0, "%s: %s\n", Mfile, ifiles->fname);
1113 if (MPflag &&
1114 strcmp((const char *)ifiles->fname, (char *)MPfile))
1115 bsheap(ob, "%s:\n", ifiles->fname);
1116 write(1, ob->buf, ob->cptr);
1117 bufree(ob);
1118 }
1119 } else if (!Pflag) {
1120 skpows = 0;
1121 ob = getobuf(BNORMAL);
1122 bsheap(ob, "\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
1123 if (ifiles->idx == SYSINC)
1124 strtobuf((usch *)" 3", ob);
1125 if (nl) putob(ob, '\n');
1126 ob->buf[ob->cptr] = 0;
1127 putstr(ob->buf);
1128 bufree(ob);
1129 } else
1130 putch('\n');
1131 }
1132
1133 static int
1134 dig2num(register int c)
1135 {
1136 if (c >= 'a')
1137 c = c - 'a' + 10;
1138 else if (c >= 'A')
1139 c = c - 'A' + 10;
1140 else
1141 c = c - '0';
1142 return c;
1143 }
1144
1145 /*
1146 * Convert string numbers to unsigned long long and check overflow.
1147 */
1148 static void
1149 cvtdig(register int c)
1150 {
1151 unsigned long long rv = 0;
1152 unsigned long long rv2 = 0;
1153 register int rad;
1154
1155 if (c == '0') {
1156 rad = 8;
1157 if (((c = qcchar()) | 0x20) == 'x') {
1158 rad <<= 1;
1159 c = qcchar();
1160 } else
1161 *--inp = c, c = '0';
1162 } else
1163 rad = 10;
1164
1165 while (ISDIGIT(c) || ((c|040) >= 'a' && (c|040) <= 'f')) {
1166 rv = rv * rad + dig2num(c);
1167 /* check overflow */
1168 if (rv / rad < rv2)
1169 error("constant is out of range");
1170 rv2 = rv;
1171 c = qcchar();
1172 }
1173
1174 yynode.op = NUMBER;
1175 while ((c | 0x20) == 'l' || (c | 0x20) == 'u') {
1176 if ((c | 0x20) == 'u')
1177 yynode.op = UNUMBER;
1178 c = qcchar();
1179 }
1180 *--inp = c;
1181 yynode.nd_uval = rv;
1182 if ((rad == 8 || rad == 16) && yynode.nd_val < 0)
1183 yynode.op = UNUMBER;
1184 if (yynode.op == NUMBER && yynode.nd_val < 0)
1185 /* too large for signed, see 6.4.4.1 */
1186 error("constant is out of range");
1187 }
1188
1189 static int
1190 charcon(void)
1191 {
1192 register int val, c;
1193
1194 val = 0;
1195 if ((c = qcchar()) == '\\') {
1196 switch (c = qcchar()) {
1197 case 'a': val = '\a'; break;
1198 case 'b': val = '\b'; break;
1199 case 'f': val = '\f'; break;
1200 case 'n': val = '\n'; break;
1201 case 'r': val = '\r'; break;
1202 case 't': val = '\t'; break;
1203 case 'v': val = '\v'; break;
1204 case '\"': val = '\"'; break;
1205 case '\'': val = '\''; break;
1206 case '\\': val = '\\'; break;
1207 case 'x':
1208 while (ISDIGIT(c = qcchar()) ||
1209 ((c|040) >= 'a' && (c|040) <= 'f'))
1210 val = val * 16 + dig2num(c);
1211 *--inp = c;
1212 break;
1213 case '0': case '1': case '2': case '3': case '4':
1214 case '5': case '6': case '7':
1215 do
1216 val = val * 8 + (c - '0');
1217 while ((ISDIGIT(c = qcchar())));
1218 *--inp = c;
1219 break;
1220 default: val = c;
1221 }
1222
1223 } else
1224 val = c;
1225 if (qcchar() != '\'')
1226 error("bad charcon");
1227 return val;
1228 }
1229
1230 static void
1231 chknl(int ignore)
1232 {
1233 register void (*f)(const char *, ...);
1234 register int t;
1235
1236 f = ignore ? warning : error;
1237 if ((t = fastspc()) != '\n') {
1238 if (t) {
1239 #ifndef pdp11
1240 f("newline expected");
1241 #endif
1242 /* ignore rest of line */
1243 while ((t = qcchar()) > 0 && t != '\n')
1244 ;
1245 } else
1246 f("no newline at end of file");
1247 }
1248 unch(t);
1249 }
1250
1251 static void
1252 elsestmt(void)
1253 {
1254 if (flslvl) {
1255 if (elflvl > trulvl)
1256 ;
1257 else if (--flslvl!=0)
1258 flslvl++;
1259 else
1260 trulvl++;
1261 } else if (trulvl) {
1262 flslvl++;
1263 trulvl--;
1264 } else
1265 error("#else in non-conditional section");
1266 if (elslvl==trulvl+flslvl)
1267 error("too many #else");
1268 elslvl=trulvl+flslvl;
1269 chknl(1);
1270 }
1271
1272 static void
1273 ifdefstmt(void)
1274 {
1275 register usch *bp;
1276 register int ch;
1277
1278 if (!ISID0(ch = fastspc()))
1279 error("bad #ifdef");
1280 bp = readid(ch);
1281
1282 if (lookup(bp, FIND) == NULL)
1283 flslvl++;
1284 else
1285 trulvl++;
1286 chknl(0);
1287 }
1288
1289 static void
1290 ifndefstmt(void)
1291 {
1292 register usch *bp;
1293 register int ch;
1294
1295 if (!ISID0(ch = fastspc()))
1296 error("bad #ifndef");
1297 bp = readid(ch);
1298 if (lookup(bp, FIND) != NULL)
1299 flslvl++;
1300 else
1301 trulvl++;
1302 chknl(0);
1303 }
1304
1305 static void
1306 endifstmt(void)
1307 {
1308 if (flslvl)
1309 flslvl--;
1310 else if (trulvl)
1311 trulvl--;
1312 else
1313 error("#endif in non-conditional section");
1314 if (flslvl == 0)
1315 elflvl = 0;
1316 elslvl = 0;
1317 chknl(1);
1318 }
1319
1320 static void
1321 ifstmt(void)
1322 {
1323 register int oCflag = Cflag;
1324
1325 Cflag = 0;
1326 yyparse() ? trulvl++ : flslvl++;
1327 Cflag = oCflag;
1328 }
1329
1330 static void
1331 elifstmt(void)
1332 {
1333 register int oCflag = Cflag;
1334
1335 Cflag = 0;
1336 if (flslvl == 0)
1337 elflvl = trulvl;
1338 if (flslvl) {
1339 if (elflvl > trulvl)
1340 ;
1341 else if (--flslvl!=0)
1342 flslvl++;
1343 else if (yyparse())
1344 trulvl++;
1345 else
1346 flslvl++;
1347 } else if (trulvl) {
1348 flslvl++;
1349 trulvl--;
1350 } else
1351 error("#elif in non-conditional section");
1352 Cflag = oCflag;
1353 }
1354
1355 /* save line into iobuf */
1356 static void
1357 prwoe(void)
1358 {
1359 register usch *p;
1360
1361 p = inp;
1362 for (;;) {
1363 while (ISESTR(*p++) == 0)
1364 ;
1365 if (*--p == 0) {
1366 fprintf(stderr, "%s", inp);
1367 inp = p;
1368 inpbuf();
1369 p = inp-1;
1370 } else if (*p == '\n')
1371 break;
1372 p++;
1373 }
1374 *p = 0;
1375 fprintf(stderr, "%s\n", inp);
1376 *p = '\n';
1377 inp = p;
1378 }
1379
1380 static void
1381 cpperror(void)
1382 {
1383 write(1, pbbeg, pbinp-pbbeg);
1384
1385 fprintf(stderr, "#error");
1386 prwoe();
1387 exit(1);
1388 }
1389
1390 static void
1391 cppwarning(void)
1392 {
1393 extern int warnings;
1394
1395 fprintf(stderr, "#warning");
1396 prwoe();
1397 warnings++;
1398 }
1399
1400 static void
1401 undefstmt(void)
1402 {
1403 register struct symtab *np;
1404 register usch *bp;
1405 register int ch;
1406
1407 if (!ISID0(ch = fastspc()))
1408 error("bad #undef");
1409 bp = readid(ch);
1410 if ((np = lookup(bp, FIND)) != NULL)
1411 np->valoff = 0;
1412 chknl(0);
1413 }
1414
1415 static void
1416 identstmt(void)
1417 {
1418 int x = 0;
1419
1420 if (yylex() == STRING) {
1421 bufree(yynode.nd_ob);
1422 x = yylex();
1423 }
1424 if (x != WARN)
1425 error("bad #ident directive");
1426 }
1427
1428 static void
1429 pragmastmt(void)
1430 {
1431 register int ch;
1432
1433 putstr((const usch *)"\n#pragma");
1434 while ((ch = qcchar()) != '\n' && ch > 0)
1435 putch(ch);
1436 unch(ch);
1437 prtline(1);
1438 }
1439
1440 int
1441 cinput(void)
1442 {
1443
1444 return qcchar();
1445 }
1446
1447 #define DIR_FLSLVL 001
1448 #define DIR_FLSINC 002
1449 static struct {
1450 const char *name;
1451 void (*fun)(void);
1452 int flags;
1453 } ppd[] = {
1454 { "ifndef", ifndefstmt, DIR_FLSINC },
1455 { "ifdef", ifdefstmt, DIR_FLSINC },
1456 { "if", ifstmt, DIR_FLSINC },
1457 { "include", include, 0 },
1458 { "else", elsestmt, DIR_FLSLVL },
1459 { "endif", endifstmt, DIR_FLSLVL },
1460 { "error", cpperror, 0 },
1461 { "warning", cppwarning, 0 },
1462 { "define", define, 0 },
1463 { "undef", undefstmt, 0 },
1464 { "line", line, 0 },
1465 { "pragma", pragmastmt, 0 },
1466 { "elif", elifstmt, DIR_FLSLVL },
1467 { "ident", identstmt, 0 },
1468 #ifdef GCC_COMPAT
1469 { "include_next", include_next, 0 },
1470 #endif
1471 };
1472 #define NPPD (int)(sizeof(ppd) / sizeof(ppd[0]))
1473
1474 static void
1475 skpln(void)
1476 {
1477 register int ch;
1478
1479 /* just ignore the rest of the line */
1480 while ((ch = qcchar()) != 0) {
1481 if (ch == '\n') {
1482 unch('\n');
1483 break;
1484 }
1485 }
1486 }
1487
1488 /*
1489 * do an even faster scan than fastscan while at flslvl.
1490 * just search for a new directive.
1491 */
1492 static void
1493 flscan(void)
1494 {
1495 register int ch;
1496
1497 for (;;) {
1498 ch = qcchar();
1499 again: switch (ch) {
1500 case 0:
1501 return;
1502 case '\n':
1503 putch('\n');
1504 ifiles->lineno++;
1505 while ((ch = qcchar()) == ' ' || ch == '\t')
1506 ;
1507 if (ch == '#')
1508 return;
1509 if (ch == '%' && (ch = qcchar()) == ':')
1510 return;
1511 goto again;
1512 case '\'':
1513 while ((ch = qcchar()) != '\'') {
1514 if (ch == '\\')
1515 qcchar();
1516 if (ch == '\n')
1517 goto again;
1518 }
1519 break;
1520 case '\"':
1521 instr = 1;
1522 while ((ch = qcchar()) != '\"') {
1523 switch (ch) {
1524 case '\\':
1525 incmnt = 1;
1526 qcchar();
1527 incmnt = 0;
1528 break;
1529 case '\n':
1530 goto again;
1531 case 0:
1532 instr = 0;
1533 return;
1534 }
1535 }
1536 instr = 0;
1537 break;
1538 case '/':
1539 ch = qcchar();
1540 if (ch == '/' || ch == '*')
1541 fastcmnt2(ch);
1542 goto again;
1543 }
1544 }
1545 }
1546
1547
1548 /*
1549 * Handle a preprocessor directive.
1550 * # is already found.
1551 */
1552 void
1553 ppdir(void)
1554 {
1555 register int ch, i, oldC;
1556 usch *bp;
1557
1558 oldC = Cflag;
1559 redo: Cflag = 0;
1560 if ((ch = fastspc()) == '\n') { /* empty directive */
1561 unch(ch);
1562 Cflag = oldC;
1563 return;
1564 }
1565 Cflag = oldC;
1566 if ((ISID0(ch)) == 0)
1567 goto out;
1568 bp = readid(ch);
1569
1570 /* got some keyword */
1571 for (i = 0; i < NPPD; i++) {
1572 if (bp[0] == ppd[i].name[0] &&
1573 strcmp((char *)bp, ppd[i].name) == 0) {
1574 if (flslvl == 0) {
1575 (*ppd[i].fun)();
1576 if (flslvl == 0)
1577 return;
1578 } else {
1579 if (ppd[i].flags & DIR_FLSLVL) {
1580 (*ppd[i].fun)();
1581 if (flslvl == 0)
1582 return;
1583 } else if (ppd[i].flags & DIR_FLSINC)
1584 flslvl++;
1585 }
1586 flscan();
1587 goto redo;
1588 }
1589 }
1590 if (flslvl == 0) {
1591 if (Aflag)
1592 skpln();
1593 return;
1594 }
1595 flscan();
1596 goto redo;
1597
1598 out:
1599 if (flslvl == 0 && Aflag == 0)
1600 error("invalid preprocessor directive");
1601
1602 unch(ch);
1603 skpln();
1604 }