Print this page
cpp: enable as many warnings as practical
Split |
Close |
Expand all |
Collapse all |
--- old/cpp/cpp.c
+++ new/cpp/cpp.c
1 1 /*
2 2 * C command
3 3 * written by John F. Reiser
4 4 * July/August 1978
5 5 */
6 6 /* Copyright (c) 2012 Joyent, Inc. All rights reserved. */
7 7 /*
8 8 * This implementation is based on the UNIX 32V release from 1978
9 9 * with permission from Caldera Inc.
10 10 *
11 11 * Copyright (c) 2010 J. Schilling
12 12 * All rights reserved.
13 13 *
14 14 * Redistribution and use in source and binary forms, with or without
15 15 * modification, are permitted provided that the following conditions
16 16 * are met:
17 17 * 1. Redistributions of source code must retain the above copyright
18 18 * notice, this list of conditions and the following disclaimer.
19 19 * 2. Redistributions in binary form must reproduce the above copyright
20 20 * notice, this list of conditions and the following disclaimer in the
21 21 * documentation and/or other materials provided with the distribution.
22 22 * 3. Neither the name of the copyright holder nor the names of contributors
23 23 * may be used to endorse or promote products derived from this software
24 24 * without specific prior written permission.
25 25 *
26 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
27 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
30 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 36 * SUCH DAMAGE.
37 37 */
38 38 /*
39 39 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
40 40 *
41 41 * Redistribution and use in source and binary forms, with or without
42 42 * modification, are permitted provided that the following conditions are
43 43 * met:
44 44 * 1. Redistributions of source code and documentation must retain the above
45 45 * copyright notice, this list of conditions and the following
46 46 * disclaimer.
47 47 *
48 48 * 2. Redistributions in binary form must reproduce the above copyright
49 49 * notice, this list of conditions and the following disclaimer in the
50 50 * documentation and/or other materials provided with the distribution.
51 51 *
52 52 * 3. All advertising materials mentioning features or use of this software
53 53 * must display the following acknowledgement: This product includes
54 54 * software developed or owned by Caldera International, Inc.
55 55 *
56 56 * 4. Neither the name of Caldera International, Inc. nor the names of other
57 57 * contributors may be used to endorse or promote products derived from
58 58 * this software without specific prior written permission.
59 59 *
60 60 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61 61 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62 62 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63 63 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 64 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR
65 65 * ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
69 69 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
70 70 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 71 * POSSIBILITY OF SUCH DAMAGE.
72 72 */
73 73 #include <stdio.h>
74 74 #include <unistd.h>
75 75 #include <stdlib.h>
76 76 #include <fcntl.h>
77 77 #include <string.h>
78 78 #include <stdarg.h>
79 79 #include <ctype.h>
80 80
81 81 #include "cpp.h"
82 82
83 83 #define SYMLEN 128
84 84 static int symlen = SYMLEN;
85 85
86 86
87 87 #define SALT '#'
88 88 #ifndef BUFSIZ
89 89 #define BUFSIZ 512
90 90 #endif
91 91
92 92 static char *pbeg;
93 93 static char *pbuf;
94 94 static char *pend;
95 95 char *outp,*inp;
96 96 char *newp;
97 97 static char cinit;
98 98
99 99 /* some code depends on whether characters are sign or zero extended */
100 100 /* #if '\377' < 0 not used here, old cpp doesn't understand */
101 101 #if pdp11 | vax | '\377' < 0
102 102 #define COFF 128
103 103 #else
104 104 #define COFF 0
105 105 #endif
106 106
107 107 #define ALFSIZ 256 /* alphabet size */
108 108 static char macbit[ALFSIZ+11];
109 109 static char toktyp[ALFSIZ];
110 110 #define BLANK 1 /* white space (" \t\v\f\r") */
111 111 #define IDENT 2 /* valid char for identifier names */
112 112 #define NUMBR 3 /* chars is of "0123456789." */
113 113
114 114 /*
115 115 * a superimposed code is used to reduce the number of calls to the
116 116 * symbol table lookup routine. (if the kth character of an identifier
117 117 * is 'a' and there are no macro names whose kth character is 'a'
118 118 * then the identifier cannot be a macro name, hence there is no need
119 119 * to look in the symbol table.) 'scw1' enables the test based on
120 120 * single characters and their position in the identifier. 'scw2'
121 121 * enables the test based on adjacent pairs of characters and their
122 122 * position in the identifier. scw1 typically costs 1 indexed fetch,
123 123 * an AND, and a jump per character of identifier, until the identifier
124 124 * is known as a non-macro name or until the end of the identifier.
125 125 * scw1 is inexpensive. scw2 typically costs 4 indexed fetches,
126 126 * an add, an AND, and a jump per character of identifier, but it is also
127 127 * slightly more effective at reducing symbol table searches.
128 128 * scw2 usually costs too much because the symbol table search is
129 129 * usually short; but if symbol table search should become expensive,
130 130 * the code is here.
131 131 * using both scw1 and scw2 is of dubious value.
132 132 */
133 133 #define scw1 1
134 134 #define scw2 0
135 135
136 136 #if scw2
137 137 char t21[ALFSIZ],t22[ALFSIZ],t23[ALFSIZ+SYMLEN];
138 138 #endif
139 139
140 140 #if scw1
141 141 #define b0 1
142 142 #define b1 2
143 143 #define b2 4
144 144 #define b3 8
145 145 #define b4 16
146 146 #define b5 32
147 147 #define b6 64
148 148 #define b7 128
149 149 #endif
150 150
151 151 #define IB 1
152 152 #define SB 2
153 153 #define NB 4
154 154 #define CB 8
155 155 #define QB 16
156 156 #define WB 32
157 157 char fastab[ALFSIZ];
158 158 static char slotab[ALFSIZ];
159 159 static char *ptrtab;
160 160
161 161 /*
162 162 * Cast the array index to int in order to avoid GCCs warnings:
163 163 * warning: subscript has type `char'
164 164 */
165 165 #define isslo (ptrtab==(slotab+COFF))
166 166 #define isid(a) ((fastab+COFF)[(int)a]&IB)
167 167 #define isspc(a) (ptrtab[(int)a]&SB)
168 168 #define isnum(a) ((fastab+COFF)[(int)a]&NB)
169 169 #define iscom(a) ((fastab+COFF)[(int)a]&CB)
170 170 #define isquo(a) ((fastab+COFF)[(int)a]&QB)
171 171 #define iswarn(a) ((fastab+COFF)[(int)a]&WB)
172 172
173 173 #define eob(a) ((a)>=pend)
174 174 #define bob(a) (pbeg>=(a))
175 175
176 176 #define BUFFERSIZ 8192
177 177 static char buffer[SYMLEN+BUFFERSIZ+BUFFERSIZ+SYMLEN];
178 178
179 179 /*
180 180 * SBSIZE was 12000 in 1978, we need to have a way to
181 181 * malloc more space.
182 182 */
183 183 #define SBSIZE 512000
184 184 static char sbf[SBSIZE];
185 185 static char *savch = sbf;
186 186
187 187 # define DROP 0xFE /* special character not legal ASCII or EBCDIC */
188 188 # define WARN DROP
189 189 # define SAME 0
190 190 # define MAXINC 16 /* max include nesting depth */
191 191 # define MAXIDIRS 20 /* max # of -I directories */
192 192 # define MAXFRE 14 /* max buffers of macro pushback */
193 193 # define MAXFRM 31 /* max number of formals/actuals to a macro */
194 194
195 195 static char warnc = (char)WARN;
196 196
197 197 static int mactop;
198 198 static int fretop;
199 199 static char *instack[MAXFRE];
200 200 static char *bufstack[MAXFRE];
201 201 static char *endbuf[MAXFRE];
202 202
203 203 static int plvl; /* parenthesis level during scan for macro actuals */
204 204 static int maclin; /* line number of macro call requiring actuals */
205 205 static char *macfil; /* file name of macro call requiring actuals */
206 206 static char *macnam; /* name of macro requiring actuals */
207 207 static int maclvl; /* # calls since last decrease in nesting level */
208 208 static char *macforw; /* ptr which must be exceeded to decrease nesting lvl */
209 209 static int macdam; /* offset to macforw due to buffer shifting */
210 210
211 211 static int inctop[MAXINC];
212 212 static char *fnames[MAXINC];
213 213 static char *dirnams[MAXINC]; /* actual directory of #include files */
214 214 static int fins[MAXINC];
215 215 static int lineno[MAXINC];
216 216
217 217 /*
218 218 * We need:
219 219 * "" include dir as dirs[0] +
220 220 * MAXIDIRS +
221 221 * system default include dir +
222 222 * a NULL pointer at the end
223 223 */
224 224 static char *dirs[MAXIDIRS+3]; /* -I and <> directories */
225 225 static int fin = STDIN_FILENO;
226 226 static FILE *fout; /* Init in main(), Mac OS is nonPOSIX */
227 227 static int nd = 1;
228 228 static int pflag; /* don't put out lines "# 12 foo.c" */
229 229 static int passcom; /* don't delete comments */
230 230 static int rflag; /* allow macro recursion */
231 231 static int hflag; /* Print included filenames */
232 232 static int nopredef; /* -undef all */
233 233 static int ifno;
234 234 # define NPREDEF 64
235 235 static char *prespc[NPREDEF];
236 236 static char **predef = prespc;
237 237 static char *punspc[NPREDEF];
238 238 static char **prund = punspc;
239 239 static int exfail;
240 240 static struct symtab *lastsym;
241 241
↓ open down ↓ |
241 lines elided |
↑ open up ↑ |
242 242
243 243 static void sayline(char *);
244 244 static void dump(void);
245 245 static char *refill(char *);
246 246 static char *cotoken(char *);
247 247 char *skipbl(char *);
248 248 static char *unfill(char *);
249 249 static char *doincl(char *);
250 250 static int equfrm(char *, char *, char *);
251 251 static char *dodef(char *);
252 -static char *control(char *);
252 +void control(char *);
253 253 static struct symtab *stsym(char *);
254 254 static struct symtab *ppsym(char *);
255 255 void pperror(char *fmt, ...);
256 256 void yyerror(char *fmt, ...);
257 257 static void ppwarn(char *fmt, ...);
258 258 struct symtab *lookup(char *, int);
259 259 static struct symtab *slookup(char *, char *, int);
260 260 static char *subst(char *, struct symtab *);
261 261 static char *trmdir(char *);
262 262 static char *copy(char *);
263 263 static char *strdex(char *, int);
264 264 int yywrap(void);
265 265 int main(int argc, char **argav);
266 266
267 267
268 268 #define symsiz 16000
269 269 static struct symtab stab[symsiz];
270 270
271 271 static struct symtab *defloc;
272 272 static struct symtab *udfloc;
273 273 static struct symtab *incloc;
274 274 static struct symtab *ifloc;
275 275 static struct symtab *elsloc;
276 276 static struct symtab *eifloc;
277 277 static struct symtab *elifloc;
278 278 static struct symtab *ifdloc;
279 279 static struct symtab *ifnloc;
280 280 static struct symtab *ysysloc;
281 281 static struct symtab *varloc;
282 282 static struct symtab *lneloc;
283 283 static struct symtab *ulnloc;
284 284 static struct symtab *uflloc;
285 285 static struct symtab *idtloc;
286 286 static struct symtab *pragmaloc;
287 287 static struct symtab *errorloc;
288 288 static int trulvl;
289 289 int flslvl;
290 290 static int elflvl;
291 291 static int elslvl;
292 292
293 293 /*
294 294 * The sun cpp prints a classification token past the
295 295 * "# linenumber filename" lines:
296 296 */
297 297 #define NOINCLUDE "" /* Not related to enter/leave incl. file */
298 298 #define ENTERINCLUDE "1" /* We are just entering an include file */
299 299 #define LEAVEINCLUDE "2" /* We are just leaving an include file */
300 300
301 301 /* ARGSUSED */
302 302 static void
303 303 sayline(what)
304 304 char *what;
305 305 {
306 306 if (pflag==0)
307 307 fprintf(fout,"# %d \"%s\" %s\n", lineno[ifno], fnames[ifno], what);
308 308 }
309 309
310 310 /*
311 311 * data structure guide
312 312 *
313 313 * most of the scanning takes place in the buffer:
314 314 *
315 315 * (low address) (high address)
316 316 * pbeg pbuf pend
317 317 * | <-- BUFFERSIZ chars --> | <-- BUFFERSIZ chars --> |
318 318 * _______________________________________________________________________
319 319 * |_______________________________________________________________________|
320 320 * | | |
321 321 * |<-- waiting -->| |<-- waiting -->
322 322 * | to be |<-- current -->| to be
323 323 * | written | token | scanned
324 324 * | | |
325 325 * outp inp p
326 326 *
327 327 * *outp first char not yet written to output file
328 328 * *inp first char of current token
329 329 * *p first char not yet scanned
330 330 *
331 331 * macro expansion: write from *outp to *inp (chars waiting to be written),
332 332 * ignore from *inp to *p (chars of the macro call), place generated
333 333 * characters in front of *p (in reverse order), update pointers,
334 334 * resume scanning.
335 335 *
336 336 * symbol table pointers point to just beyond the end of macro definitions;
337 337 * the first preceding character is the number of formal parameters.
338 338 * the appearance of a formal in the body of a definition is marked by
339 339 * 2 chars: the char WARN, and a char containing the parameter number.
340 340 * the first char of a definition is preceded by a zero character.
341 341 *
342 342 * when macro expansion attempts to back up over the beginning of the
343 343 * buffer, some characters preceding *pend are saved in a side buffer,
344 344 * the address of the side buffer is put on 'instack', and the rest
345 345 * of the main buffer is moved to the right. the end of the saved buffer
346 346 * is kept in 'endbuf' since there may be nulls in the saved buffer.
347 347 *
348 348 * similar action is taken when an 'include' statement is processed,
349 349 * except that the main buffer must be completely emptied. the array
350 350 * element 'inctop[ifno]' records the last side buffer saved when
351 351 * file 'ifno' was included. these buffers remain dormant while
352 352 * the file is being read, and are reactivated at end-of-file.
353 353 *
354 354 * instack[0 : mactop] holds the addresses of all pending side buffers.
355 355 * instack[inctop[ifno]+1 : mactop-1] holds the addresses of the side
356 356 * buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
357 357 * are dormant, waiting for end-of-file on the current file.
358 358 *
359 359 * space for side buffers is obtained from 'savch' and is never returned.
360 360 * bufstack[0:fretop-1] holds addresses of side buffers which
361 361 * are available for use.
362 362 */
363 363
364 364 static void
365 365 dump() {
366 366 register char *p1;
367 367 if ((p1=outp)==inp || flslvl!=0) return;
368 368 fwrite(p1, inp - p1, 1, fout);
369 369 outp=p1;
370 370 }
371 371
372 372 static char *
373 373 refill(p) register char *p; {
374 374 /*
375 375 * dump buffer. save chars from inp to p. read into buffer at pbuf,
376 376 * contiguous with p. update pointers, return new p.
377 377 */
378 378 register char *np,*op; register int ninbuf;
379 379 dump(); np=pbuf-(p-inp); op=inp;
380 380 if (bob(np+1)) {pperror("token too long"); np=pbeg; p=inp+BUFFERSIZ;}
381 381 macdam += np-inp; outp=inp=np;
382 382 while (op<p) *np++= *op++;
383 383 p=np;
384 384 for (;;) {
385 385 if (mactop>inctop[ifno]) {
386 386 /* retrieve hunk of pushed-back macro text */
387 387 op=instack[--mactop]; np=pbuf;
388 388 do {
389 389 while ((*np++= *op++) != '\0');
390 390 } while (op<endbuf[mactop]); pend=np-1;
391 391 /* make buffer space avail for 'include' processing */
392 392 if (fretop<MAXFRE) bufstack[fretop++]=instack[mactop];
393 393 return(p);
394 394 } else {/* get more text from file(s) */
395 395 maclvl=0;
396 396 if (0<(ninbuf=read(fin,pbuf,BUFFERSIZ))) {
397 397 pend=pbuf+ninbuf; *pend='\0';
398 398 return(p);
399 399 }
400 400 /* end of #include file */
401 401 if (ifno==0) {/* end of input */
402 402 if (plvl > 0) {
403 403 int n=plvl,tlin=lineno[ifno];
404 404 char *tfil=fnames[ifno];
405 405 lineno[ifno]=maclin;
406 406 fnames[ifno]=macfil;
407 407 pperror("%s: unterminated macro call",
408 408 macnam);
409 409 lineno[ifno]=tlin; fnames[ifno]=tfil;
410 410 np=p;
411 411 /*
412 412 * shut off unterminated quoted string
413 413 */
414 414 *np++='\n';
415 415 /* supply missing parens */
416 416 while (--n>=0) *np++=')';
417 417 pend=np; *np='\0';
418 418 if (plvl<0) plvl=0;
419 419 return(p);
420 420 }
421 421 inp=p; dump(); exit(exfail);
422 422 }
423 423 close(fin);
424 424 fin=fins[--ifno];
425 425 dirs[0]=dirnams[ifno];
426 426 sayline(LEAVEINCLUDE);
427 427 }
428 428 }
429 429 }
430 430
431 431 #define BEG 0
432 432 #define LF 1
433 433
434 434 static char *
435 435 cotoken(p) register char *p; {
436 436 register int c,i; char quoc;
437 437 static int state = BEG;
438 438
439 439 if (state!=BEG) goto prevlf;
440 440 for (;;) {
441 441 again:
442 442 while (!isspc(*p++));
443 443 switch (*(inp=p-1)) {
444 444 case 0: {
445 445 if (eob(--p)) {p=refill(p); goto again;}
446 446 else ++p; /* ignore null byte */
447 447 } break;
448 448 case '|': case '&': for (;;) {/* sloscan only */
449 449 if (*p++== *inp) break;
450 450 if (eob(--p)) p=refill(p);
451 451 else break;
452 452 } break;
453 453 case '=': case '!': for (;;) {/* sloscan only */
454 454 if (*p++=='=') break;
455 455 if (eob(--p)) p=refill(p);
456 456 else break;
457 457 } break;
458 458 case '<': case '>': for (;;) {/* sloscan only */
459 459 if (*p++=='=' || p[-2]==p[-1]) break;
460 460 if (eob(--p)) p=refill(p);
461 461 else break;
462 462 } break;
463 463 case '\\': for (;;) {
464 464 if (*p++=='\n') {++lineno[ifno]; break;}
465 465 if (eob(--p)) p=refill(p);
466 466 else {++p; break;}
467 467 } break;
468 468 case '/': for (;;) {
469 469 if (*p++=='*') {/* comment */
470 470 if (!passcom) {inp=p-2; dump(); ++flslvl;}
471 471 for (;;) {
472 472 while (!iscom(*p++));
473 473 if (p[-1]=='*') for (;;) {
474 474 if (*p++=='/') goto endcom;
475 475 if (eob(--p)) {
476 476 if (!passcom) {
477 477 inp=p;
478 478 p=refill(p);
479 479 } else if ((p-inp)>=BUFFERSIZ) {
480 480 /* split long comment */
481 481 inp=p;
482 482 /*
483 483 * last char written
484 484 * is '*'
485 485 */
486 486 p=refill(p);
487 487 /*
488 488 * terminate first part
489 489 */
490 490 putc('/',fout);
491 491 /*
492 492 * and fake start of 2nd
493 493 */
494 494 outp=inp=p-=3;
495 495 *p++='/';
496 496 *p++='*';
497 497 *p++='*';
498 498 } else {
499 499 p=refill(p);
500 500 }
501 501 } else {
502 502 break;
503 503 }
504 504 } else if (p[-1]=='\n') {
505 505 ++lineno[ifno];
506 506 if (!passcom)
507 507 putc('\n',fout);
508 508 } else if (eob(--p)) {
509 509 if (!passcom) {
510 510 inp=p; p=refill(p);
511 511 } else if ((p-inp)>=BUFFERSIZ) {
512 512 /* split long comment */
513 513 inp=p; p=refill(p);
514 514 putc('*',fout); putc('/',fout);
515 515 outp=inp=p-=2;
516 516 *p++='/';
517 517 *p++='*';
518 518 } else {
519 519 p=refill(p);
520 520 }
521 521 } else {
522 522 ++p; /* ignore null byte */
523 523 }
524 524 }
525 525 endcom:
526 526 if (!passcom) {outp=inp=p; --flslvl; goto again;}
527 527 break;
528 528 }
529 529 if (eob(--p)) p=refill(p);
530 530 else break;
531 531 } break;
532 532 case '"': case '\'': {
533 533 quoc=p[-1];
534 534 for (;;) {
535 535 while (!isquo(*p++));
536 536 if (p[-1]==quoc)
537 537 break;
538 538 if (p[-1]=='\n') {
539 539 --p;
540 540 break;
541 541 } /* bare \n terminates quotation */
542 542 if (p[-1]=='\\') {
543 543 for (;;) {
544 544 if (*p++=='\n') {
545 545 ++lineno[ifno];
546 546 break;
547 547 } /* escaped \n ignored */
548 548 if (eob(--p)) {
549 549 p=refill(p);
550 550 } else {
551 551 ++p;
552 552 break;
553 553 }
554 554 }
555 555 } else if (eob(--p)) {
556 556 p=refill(p);
557 557 } else {
558 558 ++p; /* it was a different quote character */
559 559 }
560 560 }
561 561 } break;
562 562 case '\n': {
563 563 ++lineno[ifno]; if (isslo) {state=LF; return(p);}
564 564 prevlf:
565 565 state=BEG;
566 566 for (;;) {
567 567 if (*p++=='#') return(p);
568 568 if (eob(inp= --p)) p=refill(p);
569 569 else goto again;
570 570 }
571 571 }
572 572 /* NOTREACHED */
573 573 case '0': case '1': case '2': case '3': case '4':
574 574 case '5': case '6': case '7': case '8': case '9':
575 575 for (;;) {
576 576 while (isnum(*p++));
577 577 if (eob(--p)) p=refill(p);
578 578 else break;
579 579 } break;
580 580 case 'A': case 'B': case 'C': case 'D': case 'E':
581 581 case 'F': case 'G': case 'H': case 'I': case 'J':
582 582 case 'K': case 'L': case 'M': case 'N': case 'O':
583 583 case 'P': case 'Q': case 'R': case 'S': case 'T':
584 584 case 'U': case 'V': case 'W': case 'X': case 'Y':
585 585 case 'Z': case '_':
586 586 case 'a': case 'b': case 'c': case 'd': case 'e':
587 587 case 'f': case 'g': case 'h': case 'i': case 'j':
588 588 case 'k': case 'l': case 'm': case 'n': case 'o':
589 589 case 'p': case 'q': case 'r': case 's': case 't':
590 590 case 'u': case 'v': case 'w': case 'x': case 'y':
591 591 case 'z':
592 592 #if scw1
593 593 #define tmac1(c,bit) if (!xmac1(c,bit,&)) goto nomac
594 594 #define xmac1(c,bit,op) ((macbit+COFF)[c] op (bit))
595 595 #else
596 596 #define tmac1(c,bit)
597 597 #define xmac1(c,bit,op)
598 598 #endif
599 599
600 600 #if scw2
601 601 #define tmac2(c0,c1,cpos) if (!xmac2(c0,c1,cpos,&)) goto nomac
602 602 #define xmac2(c0,c1,cpos,op)\
603 603 ((macbit+COFF)[(t21+COFF)[c0]+(t22+COFF)[c1]] op (t23+COFF+cpos)[c0])
604 604 #else
605 605 #define tmac2(c0,c1,cpos)
606 606 #define xmac2(c0,c1,cpos,op)
607 607 #endif
608 608
609 609 if (flslvl) goto nomac;
610 610 for (;;) {
611 611 c= p[-1]; tmac1(c,b0);
612 612 i= *p++; if (!isid(i)) goto endid; tmac1(i,b1); tmac2(c,i,0);
613 613 c= *p++; if (!isid(c)) goto endid; tmac1(c,b2); tmac2(i,c,1);
614 614 i= *p++; if (!isid(i)) goto endid; tmac1(i,b3); tmac2(c,i,2);
615 615 c= *p++; if (!isid(c)) goto endid; tmac1(c,b4); tmac2(i,c,3);
616 616 i= *p++; if (!isid(i)) goto endid; tmac1(i,b5); tmac2(c,i,4);
617 617 c= *p++; if (!isid(c)) goto endid; tmac1(c,b6); tmac2(i,c,5);
618 618 i= *p++; if (!isid(i)) goto endid; tmac1(i,b7); tmac2(c,i,6);
619 619 tmac2(i,0,7);
620 620 while (isid(*p++));
621 621 if (eob(--p)) {refill(p); p=inp+1; continue;}
622 622 goto lokid;
623 623 endid:
624 624 if (eob(--p)) {refill(p); p=inp+1; continue;}
625 625 tmac2(p[-1],0,-1+(p-inp));
626 626 lokid:
627 627 slookup(inp,p,0); if (newp) {p=newp; goto again;}
628 628 else break;
629 629 nomac:
630 630 while (isid(*p++));
631 631 if (eob(--p)) {p=refill(p); goto nomac;}
632 632 else break;
633 633 } break;
634 634 } /* end of switch */
635 635
636 636 if (isslo) return(p);
637 637 } /* end of infinite loop */
638 638 }
639 639
640 640 /*
641 641 * XXX: This unconditionally consumes one token (presuming it's blank? that we
642 642 * already consumed it?). That's pretty terrible, but it's also very fragile,
643 643 * and I don't want to change it.
644 644 */
645 645 char *
646 646 skipbl(p) register char *p; {/* get next non-blank token */
647 647 do {
648 648 outp=inp=p;
649 649 p=cotoken(p);
650 650 } while ((toktyp+COFF)[(int)*inp]==BLANK);
651 651 return(p);
652 652 }
653 653
654 654 static char *
655 655 unfill(p) register char *p; {
656 656 /*
657 657 * take <= BUFFERSIZ chars from right end of buffer and put them on instack .
658 658 * slide rest of buffer to the right, update pointers, return new p.
659 659 */
660 660 register char *np,*op; register int d;
661 661 if (mactop>=MAXFRE) {
662 662 pperror("%s: too much pushback",macnam);
663 663 p=inp=pend; dump(); /* begin flushing pushback */
664 664 while (mactop>inctop[ifno]) {p=refill(p); p=inp=pend; dump();}
665 665 }
666 666 if (fretop>0)
667 667 np=bufstack[--fretop];
668 668 else {
669 669 np=savch; savch+=BUFFERSIZ;
670 670 if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
671 671 *savch++='\0';
672 672 }
673 673 instack[mactop]=np; op=pend-BUFFERSIZ; if (op<p) op=p;
674 674 for (;;) {
675 675 while ((*np++= *op++) != '\0');
676 676 if (eob(op))
677 677 break;
678 678 } /* out with old */
679 679 endbuf[mactop++]=np; /* mark end of saved text */
680 680 np=pbuf+BUFFERSIZ;
681 681 op=pend-BUFFERSIZ;
682 682 pend=np;
683 683 if (op<p)
684 684 op=p;
685 685 while (outp<op) *--np= *--op; /* slide over new */
686 686 if (bob(np))
687 687 pperror("token too long");
688 688 d=np-outp; outp+=d; inp+=d; macdam+=d;
689 689 return(p+d);
690 690 }
691 691
692 692 static char *
693 693 doincl(p) register char *p; {
694 694 int filok,inctype;
695 695 register char *cp; char **dirp,*nfil; char filname[BUFFERSIZ];
696 696
697 697 filname[0] = '\0'; /* Make lint quiet */
698 698 p=skipbl(p); cp=filname;
699 699 if (*inp++=='<') {/* special <> syntax */
700 700 inctype=1;
701 701 for (;;) {
702 702 outp=inp=p; p=cotoken(p);
703 703 if (*inp=='\n') {--p; *cp='\0'; break;}
704 704 if (*inp=='>') { *cp='\0'; break;}
705 705 # ifdef gimpel
706 706 if (*inp=='.' && !intss()) *inp='#';
707 707 # endif
708 708 while (inp<p) *cp++= *inp++;
709 709 }
710 710 } else if (inp[-1]=='"') {/* regular "" syntax */
711 711 inctype=0;
712 712 # ifdef gimpel
713 713 while (inp<p) {if (*inp=='.' && !intss()) *inp='#'; *cp++= *inp++;}
714 714 # else
715 715 while (inp<p) *cp++= *inp++;
716 716 # endif
717 717 if (*--cp=='"') *cp='\0';
718 718 } else {pperror("bad include syntax",0); inctype=2;}
719 719 /* flush current file to \n , then write \n */
720 720 ++flslvl; do {outp=inp=p; p=cotoken(p);} while (*inp!='\n'); --flslvl;
721 721 inp=p; dump(); if (inctype==2) return(p);
722 722 /* look for included file */
723 723 if (ifno+1 >=MAXINC) {
724 724 pperror("Unreasonable include nesting",0); return(p);
725 725 }
726 726 if ((nfil=savch)>sbf+SBSIZE-BUFFERSIZ) {
727 727 pperror("no space");
728 728 exit(exfail);
729 729 }
730 730 filok=0;
731 731 for (dirp=dirs+inctype; *dirp; ++dirp) {
732 732 if (filname[0]=='/' || **dirp=='\0') {
733 733 strcpy(nfil,filname);
734 734 } else {
735 735 strcpy(nfil,*dirp);
736 736 # if unix
737 737 strcat(nfil,"/");
738 738 # endif
739 739 strcat(nfil,filname);
740 740 }
741 741 if (0<(fins[ifno+1]=open(nfil, O_RDONLY))) {
742 742 filok=1; fin=fins[++ifno]; break;
743 743 }
744 744 }
745 745 if (filok==0) {
746 746 pperror("Can't find include file %s",filname);
747 747 } else {
748 748 lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
749 749 dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
750 750 sayline(ENTERINCLUDE);
751 751 if (hflag)
752 752 fprintf(stderr, "%s\n", nfil);
753 753 /* save current contents of buffer */
754 754 while (!eob(p)) p=unfill(p);
755 755 inctop[ifno]=mactop;
756 756 }
757 757 return(p);
758 758 }
759 759
760 760 static int
761 761 equfrm(a,p1,p2) register char *a,*p1,*p2; {
762 762 register char c; int flag;
763 763 c= *p2; *p2='\0';
764 764 flag=strcmp(a,p1); *p2=c; return(flag==SAME);
765 765 }
766 766
767 767 static char *
768 768 dodef(p) char *p; {/* process '#define' */
769 769 register char *pin,*psav,*cf;
770 770 char **pf,**qf; int b,c,params; struct symtab *np;
771 771 char *oldval,*oldsavch;
772 772 char *formal[MAXFRM]; /* formal[n] is name of nth formal */
773 773 char formtxt[BUFFERSIZ]; /* space for formal names */
774 774
775 775 formtxt[0] = '\0'; /* Make lint quiet */
776 776
777 777 if (savch>sbf+SBSIZE-BUFFERSIZ) {
778 778 pperror("too much defining");
779 779 return(p);
780 780 }
781 781 oldsavch=savch; /* to reclaim space if redefinition */
782 782 ++flslvl; /* prevent macro expansion during 'define' */
783 783 p=skipbl(p); pin=inp;
784 784 if ((toktyp+COFF)[(int)*pin]!=IDENT) {
785 785 ppwarn("illegal macro name");
786 786 while (*inp!='\n')
787 787 p=skipbl(p);
788 788 return(p);
789 789 }
790 790 np=slookup(pin,p,1);
791 791 if (getenv("CPP_DEBUG_DEFINITIONS") != NULL)
792 792 fprintf(stderr, "*** defining %s at %s:%d\n",
793 793 np->name, fnames[ifno], lineno[ifno]);
794 794 if ((oldval=np->value) != NULL)
795 795 savch=oldsavch; /* was previously defined */
796 796 b=1; cf=pin;
797 797 while (cf<p) {/* update macbit */
798 798 c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
799 799 if (cf!=p) {
800 800 xmac2(c,*cf,-1+(cf-pin),|=);
801 801 } else {
802 802 xmac2(c,0,-1+(cf-pin),|=);
803 803 }
804 804 }
805 805 params=0; outp=inp=p; p=cotoken(p); pin=inp;
806 806 formal[0] = ""; /* Prepare for hack at next line... */
807 807 pf = formal; /* Make gcc/lint quiet, pf only used with params!=0 */
808 808 if (*pin=='(') {/* with parameters; identify the formals */
809 809 cf=formtxt; pf=formal;
810 810 for (;;) {
811 811 p=skipbl(p); pin=inp;
812 812 if (*pin=='\n') {
813 813 --lineno[ifno];
814 814 --p;
815 815 pperror("%s: missing )",np->name);
816 816 break;
817 817 }
818 818 if (*pin==')') break;
819 819 if (*pin==',') continue;
820 820 if ((toktyp+COFF)[(int)*pin]!=IDENT) {
821 821 c= *p;
822 822 *p='\0';
823 823 pperror("bad formal: %s",pin);
824 824 *p=c;
825 825 } else if (pf>= &formal[MAXFRM]) {
826 826 c= *p;
827 827 *p='\0';
828 828 pperror("too many formals: %s",pin);
829 829 *p=c;
830 830 } else {
831 831 *pf++=cf;
832 832 while (pin<p)
833 833 *cf++= *pin++;
834 834 *cf++='\0';
835 835 ++params;
836 836 }
837 837 }
838 838 if (params==0)
839 839 --params; /* #define foo() ... */
840 840 } else if (*pin=='\n') {
841 841 --lineno[ifno];
842 842 --p;
843 843 }
844 844 /*
845 845 * remember beginning of macro body, so that we can
846 846 * warn if a redefinition is different from old value.
847 847 */
848 848 oldsavch=psav=savch;
849 849 for (;;) {/* accumulate definition until linefeed */
850 850 outp=inp=p; p=cotoken(p); pin=inp;
851 851 if (*pin=='\\' && pin[1]=='\n')
852 852 continue; /* ignore escaped lf */
853 853 if (*pin=='\n') break;
854 854 if (params) {
855 855 /* mark the appearance of formals in the definiton */
856 856 if ((toktyp+COFF)[(int)*pin]==IDENT) {
857 857 for (qf=pf; --qf>=formal; ) {
858 858 if (equfrm(*qf,pin,p)) {
859 859 *psav++=qf-formal+1;
860 860 *psav++=WARN;
861 861 pin=p;
862 862 break;
863 863 }
864 864 }
865 865 } else if (*pin=='"' || *pin=='\'') {
866 866 /* inside quotation marks, too */
867 867 char quoc= *pin;
868 868 for (*psav++= *pin++; pin<p && *pin!=quoc; ) {
869 869 while (pin<p && !isid(*pin))
870 870 *psav++= *pin++;
871 871 cf=pin;
872 872 while (cf<p && isid(*cf))
873 873 ++cf;
874 874 for (qf=pf; --qf>=formal; ) {
875 875 if (equfrm(*qf,pin,cf)) {
876 876 *psav++=qf-formal+1;
877 877 *psav++=WARN;
878 878 pin=cf;
879 879 break;
880 880 }
881 881 }
882 882 while (pin<cf)
883 883 *psav++= *pin++;
884 884 }
885 885 }
886 886 }
887 887 while (pin<p) *psav++= *pin++;
888 888 }
889 889 *psav++=params; *psav++='\0';
890 890 if ((cf=oldval)!=NULL) {/* redefinition */
891 891 --cf; /* skip no. of params, which may be zero */
892 892 while (*--cf); /* go back to the beginning */
893 893 if (0!=strcmp(++cf,oldsavch)) {
894 894 /* redefinition different from old */
895 895 --lineno[ifno];
896 896 ppwarn("%s redefined",np->name);
897 897 ++lineno[ifno];
898 898 np->value=psav-1;
899 899 } else {
900 900 psav=oldsavch; /* identical redef.; reclaim space */
↓ open down ↓ |
638 lines elided |
↑ open up ↑ |
901 901 }
902 902 } else {
903 903 np->value=psav-1;
904 904 }
905 905 --flslvl; inp=pin; savch=psav; return(p);
906 906 }
907 907
908 908 #define fasscan() ptrtab=fastab+COFF
909 909 #define sloscan() ptrtab=slotab+COFF
910 910
911 -static char *
911 +void
912 912 control(p) register char *p; {/* find and handle preprocessor control lines */
913 913 register struct symtab *np;
914 914 for (;;) {
915 915 fasscan(); p=cotoken(p); if (*inp=='\n') ++inp; dump();
916 916 sloscan(); p=skipbl(p);
917 917 *--inp=SALT; outp=inp; ++flslvl; np=slookup(inp,p,0); --flslvl;
918 918 if (np==defloc) {/* define */
919 919 if (flslvl==0) {p=dodef(p); continue;}
920 920 } else if (np==incloc) {/* include */
921 921 if (flslvl==0) {p=doincl(p); continue;}
922 922 } else if (np==ifnloc) {/* ifndef */
923 923 ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
924 924 if (flslvl==0 && np->value==0) ++trulvl;
925 925 else ++flslvl;
926 926 } else if (np==ifdloc) {/* ifdef */
927 927 ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
928 928 if (flslvl==0 && np->value!=0) ++trulvl;
929 929 else ++flslvl;
930 930 } else if (np==eifloc) {/* endif */
931 931 if (flslvl) {if (--flslvl==0) sayline(NOINCLUDE);}
932 932 else if (trulvl) --trulvl;
933 933 else pperror("If-less endif",0);
934 934
935 935 if (flslvl == 0)
936 936 elflvl = 0;
937 937 elslvl = 0;
938 938 } else if (np==elifloc) {/* elif */
939 939 if (flslvl == 0)
940 940 elflvl = trulvl;
941 941 if (flslvl) {
942 942 if (elflvl > trulvl) {
943 943 ;
944 944 } else if (--flslvl != 0) {
945 945 ++flslvl;
946 946 } else {
947 947 newp = p;
948 948 if (yyparse()) {
949 949 ++trulvl;
950 950 sayline(NOINCLUDE);
951 951 } else {
952 952 ++flslvl;
953 953 }
954 954 p = newp;
955 955 }
956 956 } else if (trulvl) {
957 957 ++flslvl;
958 958 --trulvl;
959 959 } else
960 960 pperror("If-less elif");
961 961
962 962 } else if (np==elsloc) {/* else */
963 963 if (flslvl) {
964 964 if (elflvl > trulvl)
965 965 ;
966 966 else if (--flslvl!=0) ++flslvl;
967 967 else {++trulvl; sayline(NOINCLUDE);}
968 968 }
969 969 else if (trulvl) {++flslvl; --trulvl;}
970 970 else pperror("If-less else",0);
971 971
972 972 if (elslvl==trulvl+flslvl)
973 973 pperror("Too many #else's");
974 974 elslvl=trulvl+flslvl;
975 975
976 976 } else if (np==udfloc) {/* undefine */
977 977 if (flslvl==0) {
978 978 ++flslvl; p=skipbl(p); slookup(inp,p,DROP); --flslvl;
979 979 }
980 980 } else if (np==ifloc) {/* if */
981 981 newp=p;
982 982 if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
983 983 p=newp;
984 984 } else if (np == idtloc) { /* ident */
985 985 if (pflag == 0)
986 986 while (*inp != '\n') /* pass text */
987 987 p = cotoken(p);
988 988 } else if (np == pragmaloc) { /* pragma */
989 989 while (*inp != '\n') /* pass text */
990 990 p = cotoken(p);
991 991 #ifdef EXIT_ON_ERROR
992 992 } else if (np == errorloc) { /* error */
993 993 if (trulvl > 0) {
994 994 char ebuf[BUFFERSIZ];
995 995
996 996 p = ebuf;
997 997 while (*inp != '\n') {
998 998 if (*inp == '\0')
999 999 if (eob(--inp)) {
1000 1000 inp = refill(inp);
1001 1001 continue;
1002 1002 }
1003 1003 *p++ = *inp++;
1004 1004 if (p >= &ebuf[BUFFERSIZ-1])
1005 1005 break;
1006 1006 }
1007 1007 *p = '\0';
1008 1008 pperror(ebuf);
1009 1009 exit(exfail);
1010 1010 }
1011 1011 #endif
1012 1012 } else if (np==lneloc) {/* line */
1013 1013 if (flslvl==0 && pflag==0) {
1014 1014 outp=inp=p;
1015 1015 *--outp='#';
1016 1016 while (*inp!='\n')
1017 1017 p=cotoken(p);
1018 1018 continue;
1019 1019 }
1020 1020 } else if (*++inp=='\n') {
1021 1021 outp=inp; /* allows blank line after # */
1022 1022 } else {
1023 1023 pperror("undefined control",0);
1024 1024 }
1025 1025 /* flush to lf */
1026 1026 ++flslvl;
1027 1027 while (*inp!='\n') {
1028 1028 outp=inp=p;
1029 1029 p=cotoken(p);
1030 1030 }
1031 1031 --flslvl;
1032 1032 }
1033 1033 }
1034 1034
1035 1035 static struct symtab *
1036 1036 stsym(s) register char *s; {
1037 1037 char buf[BUFFERSIZ]; register char *p;
1038 1038
1039 1039 /* make definition look exactly like end of #define line */
1040 1040 /* copy to avoid running off end of world when param list is at end */
1041 1041 p=buf; while ((*p++= *s++) != '\0');
1042 1042 p=buf; while (isid(*p++)); /* skip first identifier */
1043 1043 if (*--p=='=') {*p++=' '; while (*p++);}
1044 1044 else {s=" 1"; while ((*p++= *s++) != '\0');}
1045 1045 pend=p; *--p='\n';
1046 1046 sloscan(); dodef(buf); return(lastsym);
1047 1047 }
1048 1048
1049 1049 static struct symtab *
1050 1050 ppsym(s) char *s; {/* kluge */
1051 1051 register struct symtab *sp;
1052 1052 cinit=SALT; *savch++=SALT; sp=stsym(s); --sp->name; cinit=0; return(sp);
1053 1053 }
1054 1054
1055 1055 void
1056 1056 verror(char *fmt, va_list args)
1057 1057 {
1058 1058 if (fnames[ifno][0])
1059 1059 fprintf(stderr, "%s: ", fnames[ifno]);
1060 1060 fprintf(stderr, "%d: ",lineno[ifno]);
1061 1061
1062 1062 (void)vfprintf(stderr, fmt, args);
1063 1063 fputc('\n', stderr);
1064 1064 }
1065 1065
1066 1066 /* VARARGS1 */
1067 1067 void
1068 1068 pperror(char *fmt, ...)
1069 1069 {
1070 1070 va_list args;
1071 1071
1072 1072 va_start(args, fmt);
1073 1073 verror(fmt, args);
1074 1074 va_end(args);
1075 1075
1076 1076 exfail = 1;
1077 1077 }
1078 1078
1079 1079 /* VARARGS1 */
1080 1080 void
1081 1081 yyerror(char *fmt, ...)
1082 1082 {
1083 1083 va_list args;
1084 1084
1085 1085 va_start(args, fmt);
1086 1086 verror(fmt, args);
1087 1087 va_end(args);
1088 1088 }
1089 1089
1090 1090 /* VARARGS1 */
1091 1091 static void
1092 1092 ppwarn(char *fmt, ...)
1093 1093 {
1094 1094 va_list args;
1095 1095 int fail = exfail;
1096 1096
1097 1097 va_start(args, fmt);
1098 1098 verror(fmt, args);
1099 1099 va_end(args);
1100 1100
1101 1101 exfail = fail;
1102 1102 }
1103 1103
1104 1104 struct symtab *
1105 1105 lookup(namep, enterf)
1106 1106 char *namep;
1107 1107 int enterf;
1108 1108 {
1109 1109 register char *np, *snp;
1110 1110 register int c, i; int around;
1111 1111 register struct symtab *sp;
1112 1112
1113 1113 /* namep had better not be too long (currently, <=symlen chars) */
1114 1114 np=namep; around=0; i=cinit;
1115 1115 while ((c = *np++) != '\0')
1116 1116 i += i+c;
1117 1117 c=i; /* c=i for register usage on pdp11 */
1118 1118 c %= symsiz;
1119 1119 if (c<0)
1120 1120 c += symsiz;
1121 1121 sp = &stab[c];
1122 1122 while ((snp=sp->name) != NULL) {
1123 1123 np = namep;
1124 1124 while (*snp++ == *np)
1125 1125 if (*np++ == '\0') {
1126 1126 if (enterf==DROP) {
1127 1127 sp->name[0]= DROP;
1128 1128 sp->value=0;
1129 1129 }
1130 1130 return(lastsym=sp);
1131 1131 }
1132 1132 if (--sp < &stab[0]) {
1133 1133 if (around) {
1134 1134 pperror("too many defines", 0);
1135 1135 exit(exfail);
1136 1136 } else {
1137 1137 ++around;
1138 1138 sp = &stab[symsiz-1];
1139 1139 }
1140 1140 }
1141 1141 }
1142 1142 if (enterf>0)
1143 1143 sp->name=namep;
1144 1144 return (lastsym=sp);
1145 1145 }
1146 1146
1147 1147 static struct symtab *
1148 1148 slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{
1149 1149 register char *p3; char c2,c3; struct symtab *np;
1150 1150 c2= *p2; *p2='\0'; /* mark end of token */
1151 1151 if ((p2-p1)>symlen)
1152 1152 p3=p1+symlen;
1153 1153 else
1154 1154 p3=p2;
1155 1155 c3= *p3; *p3='\0'; /* truncate to symlen chars or less */
1156 1156 if (enterf==1)
1157 1157 p1=copy(p1);
1158 1158 np=lookup(p1,enterf); *p3=c3; *p2=c2;
1159 1159 if (np->value!=0 && flslvl==0)
1160 1160 newp=subst(p2,np);
1161 1161 else
1162 1162 newp=0;
1163 1163 return(np);
1164 1164 }
1165 1165
1166 1166 /*
1167 1167 * When a macro substitution must happen, arrange the input stack based on the
1168 1168 * macro definition and any parameters such that the expanded macro is what is
1169 1169 * next read by the preprocessor as if it were input
1170 1170 */
1171 1171 static char *
1172 1172 subst(p,sp) register char *p; struct symtab *sp; {
1173 1173 register char *ca,*vp; int params;
1174 1174 char *actual[MAXFRM]; /* actual[n] is text of nth actual */
1175 1175 char acttxt[BUFFERSIZ]; /* space for actuals */
1176 1176 /* State while pasting, TRAIL is trailing space, INTRA is in the body */
1177 1177 enum { TRAIL, INTRA } state = TRAIL;
1178 1178 int pasted = 0; /* # of character pasted */
1179 1179
1180 1180 if (0==(vp=sp->value)) return(p);
1181 1181 if ((p-macforw)<=macdam) {
1182 1182 if (++maclvl>symsiz && !rflag) {
1183 1183 pperror("%s: macro recursion",sp->name);
1184 1184 return(p);
1185 1185 }
1186 1186 } else {
1187 1187 maclvl=0; /* level decreased */
1188 1188 }
1189 1189 macforw=p; macdam=0; /* new target for decrease in level */
1190 1190 macnam=sp->name;
1191 1191 /* flush all buffered output prior to the expansion */
1192 1192 dump();
1193 1193 if (sp==ulnloc) {
1194 1194 vp=acttxt; *vp++='\0';
1195 1195 sprintf(vp,"%d",lineno[ifno]); while (*vp++);
1196 1196 } else if (sp==uflloc) {
1197 1197 vp=acttxt; *vp++='\0';
1198 1198 sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
1199 1199 }
1200 1200 if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
1201 1201 register char **pa;
1202 1202 char *savp, *savinp, *savoutp;
1203 1203 ca=acttxt; pa=actual;
1204 1204 if (params==0xFF)
1205 1205 params=1; /* #define foo() ... */
1206 1206 sloscan();
1207 1207 ++flslvl; /* no expansion during search for actuals */
1208 1208 plvl= -1;
1209 1209 /*
1210 1210 * Skip any blanks (including \n), until we hit the macro
1211 1211 * arguments.
1212 1212 *
1213 1213 * save our state so we can roll back if none are called.
1214 1214 */
1215 1215 savp = p;
1216 1216 savinp = inp;
1217 1217 savoutp = outp;
1218 1218 do {
1219 1219 p=skipbl(p);
1220 1220 } while (*inp=='\n'); /* skip \n too */
1221 1221
1222 1222 if (*inp=='(') {
1223 1223 maclin=lineno[ifno]; macfil=fnames[ifno];
1224 1224 for (plvl=1; plvl!=0; ) {
1225 1225 *ca++='\0';
1226 1226 for (;;) {
1227 1227 outp=inp=p; p=cotoken(p);
1228 1228 if (*inp=='(') ++plvl;
1229 1229 if (*inp==')' && --plvl==0) {
1230 1230 --params;
1231 1231 break;
1232 1232 }
1233 1233 if (plvl==1 && *inp==',') {
1234 1234 --params;
1235 1235 break;
1236 1236 }
1237 1237 while (inp<p) {
1238 1238 /*
1239 1239 * Sun cpp compatibility.
1240 1240 * Needed for kernel assembler
1241 1241 * preprocessing.
1242 1242 * Replace newlines in actual
1243 1243 * macro parameters by spaces.
1244 1244 * Keep escaped newlines, they
1245 1245 * are assumed to be inside a
1246 1246 * string.
1247 1247 *
1248 1248 * XXX: The above is actually
1249 1249 * false in a couple of ways.
1250 1250 *
1251 1251 * 1) Sun cpp turns newlines
1252 1252 * into spaces, but inserts an
1253 1253 * equal number of newlines
1254 1254 * prior to pasting the body.
1255 1255 *
1256 1256 * 2) Sun does _not_ preserved
1257 1257 * escaped newlines, the \ is
1258 1258 * removed, and the newline
1259 1259 * otherwise treated
1260 1260 * identically to in #1.
1261 1261 */
1262 1262 if (*inp == '\n' &&
1263 1263 inp[-1] != '\\')
1264 1264 *inp = ' ';
1265 1265 *ca++= *inp++;
1266 1266 }
1267 1267 if (ca> &acttxt[BUFFERSIZ])
1268 1268 pperror("%s: actuals too long",
1269 1269 sp->name);
1270 1270 }
1271 1271 if (pa>= &actual[MAXFRM])
1272 1272 ppwarn("%s: argument mismatch" ,
1273 1273 sp->name);
1274 1274 else
1275 1275 *pa++=ca;
1276 1276 }
1277 1277 } else {
1278 1278 /*
1279 1279 * Didn't find any parameters, rollback our state so
1280 1280 * we don't chew more output than necessary
1281 1281 */
1282 1282 p = savp;
1283 1283 inp = savinp;
1284 1284 outp = savoutp;
1285 1285 }
1286 1286 if (params!=0)
1287 1287 ppwarn("%s: argument mismatch", sp->name);
1288 1288 while (--params>=0)
1289 1289 *pa++=""+1; /* null string for missing actuals */
1290 1290 --flslvl; fasscan();
1291 1291 }
1292 1292
1293 1293 for (;;) {/* push definition onto front of input stack */
1294 1294 /*
1295 1295 * Loop until we hit the end of the macro, or a parameter
1296 1296 * placement. Note that we expand the macro into the input
1297 1297 * backwards (so it replays forwards.)
1298 1298 */
1299 1299 while (!iswarn(*--vp)) {
1300 1300 if (bob(p)) {outp=inp=p; p=unfill(p);}
1301 1301
1302 1302 /* Unless we are mid-paste, swallow all spaces */
1303 1303 if (state == TRAIL) {
1304 1304 while (isspace(*vp) && !iswarn(*vp))
1305 1305 vp--;
1306 1306 } else {
1307 1307 /*
1308 1308 * If we're mid-paste, compress spaces to a
1309 1309 * single space
1310 1310 */
1311 1311 while (isspace(*vp)) {
1312 1312 if (!isspace(vp[1])) {
1313 1313 *vp = ' ';
1314 1314 break;
1315 1315 } else {
1316 1316 vp--;
1317 1317 }
1318 1318 }
1319 1319 }
1320 1320 state = INTRA; /* Hit a non-space */
1321 1321
1322 1322 if (iswarn(*vp))
1323 1323 break;
1324 1324 *--p= *vp;
1325 1325 pasted++;
1326 1326 }
1327 1327 if (*vp==warnc) {/* insert actual param */
1328 1328 state = INTRA;
1329 1329 ca=actual[*--vp-1];
1330 1330 while (*--ca) {
1331 1331 if (bob(p)) {outp=inp=p; p=unfill(p);}
1332 1332 *--p= *ca;
1333 1333 pasted++;
1334 1334 }
1335 1335 } else {
1336 1336 /*
1337 1337 * Trim leading spaces, but only those from our pasting
1338 1338 */
1339 1339 while (isspace(*p) && pasted > 0) {
1340 1340 p++;
1341 1341 pasted--;
1342 1342 }
1343 1343 break;
1344 1344 }
1345 1345 }
1346 1346 outp=inp=p;
1347 1347 return(p);
1348 1348 }
1349 1349
1350 1350 static char *
1351 1351 trmdir(s) register char *s; {
1352 1352 register char *p = s;
1353 1353 while (*p++); --p; while (p>s && *--p!='/');
1354 1354 # if unix
1355 1355 if (p==s) *p++='.';
1356 1356 # endif
1357 1357 *p='\0';
1358 1358 return(s);
1359 1359 }
1360 1360
1361 1361 static char *
1362 1362 copy(s) register char *s; {
1363 1363 register char *old;
1364 1364
1365 1365 old = savch; while ((*savch++ = *s++) != '\0');
1366 1366 return(old);
1367 1367 }
1368 1368
1369 1369 static char *
1370 1370 strdex(s,c) char *s,c; {
1371 1371 while (*s) if (*s++==c) return(--s);
1372 1372 return(0);
1373 1373 }
1374 1374
1375 1375 int
1376 1376 yywrap() {
1377 1377 return(1);
1378 1378 }
1379 1379
1380 1380 int
1381 1381 main(argc,argv)
1382 1382 char *argv[];
1383 1383 int argc;
1384 1384 {
1385 1385 register int i,c;
1386 1386 register char *p;
1387 1387 char *tf,**cp2;
1388 1388 char *sysdir = NULL;
1389 1389
1390 1390 fout = stdout; /* Mac OS X is not POSIX compliant (stdout nonconst.) */
1391 1391
1392 1392 p="_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1393 1393 i=0;
1394 1394 while ((c= *p++) != '\0') {
1395 1395 (fastab+COFF)[c] |= IB|NB|SB;
1396 1396 (toktyp+COFF)[c]=IDENT;
1397 1397 #if scw2
1398 1398 /*
1399 1399 * 53 == 63-10; digits rarely appear in identifiers,
1400 1400 * and can never be the first char of an identifier.
1401 1401 * 11 == 53*53/sizeof(macbit) .
1402 1402 */
1403 1403 ++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;
1404 1404 #endif
1405 1405 }
1406 1406 p="0123456789.";
1407 1407 while ((c = *p++) != '\0') {
1408 1408 (fastab+COFF)[c] |= NB|SB;
1409 1409 (toktyp+COFF)[c]=NUMBR;
1410 1410 }
1411 1411 p="\n\"'/\\";
1412 1412 while ((c = *p++) != '\0')
1413 1413 (fastab+COFF)[c] |= SB;
1414 1414 p="\n\"'\\";
1415 1415 while ((c = *p++) != '\0')
1416 1416 (fastab+COFF)[c] |= QB;
1417 1417 p="*\n";
1418 1418 while ((c = *p++)!= '\0')
1419 1419 (fastab+COFF)[c] |= CB;
1420 1420 (fastab+COFF)[(int)warnc] |= WB;
1421 1421 (fastab+COFF)['\0'] |= CB|QB|SB|WB;
1422 1422 for (i=ALFSIZ; --i>=0; )
1423 1423 slotab[i]=fastab[i]|SB;
1424 1424 p=" \t\013\f\r"; /* note no \n; \v not legal for vertical tab? */
1425 1425 while ((c = *p++) != '\0')
1426 1426 (toktyp+COFF)[c]=BLANK;
1427 1427 #if scw2
1428 1428 for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )
1429 1429 if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0)
1430 1430 (t23+COFF)[i]=1;
1431 1431 #endif
1432 1432
1433 1433 # if unix
1434 1434 fnames[ifno=0] = "";
1435 1435 dirs[0]=dirnams[0]= ".";
1436 1436 # endif
1437 1437 # if ibm
1438 1438 fnames[ifno=0] = "";
1439 1439 # endif
1440 1440 # if gimpel
1441 1441 fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);
1442 1442 dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));
1443 1443 # endif
1444 1444 for (i=1; i<argc; i++) {
1445 1445 switch(argv[i][0]) {
1446 1446 case '-':
1447 1447 switch(argv[i][1]) {
1448 1448 case 'P':
1449 1449 pflag++;
1450 1450 continue;
1451 1451 case 'E':
1452 1452 continue;
1453 1453 case 'R':
1454 1454 ++rflag;
1455 1455 continue;
1456 1456 case 'C':
1457 1457 passcom++;
1458 1458 continue;
1459 1459 case 'D':
1460 1460 if (predef>prespc+NPREDEF) {
1461 1461 pperror("too many -D options, "
1462 1462 "ignoring %s", argv[i]);
1463 1463 continue;
1464 1464 }
1465 1465 /* ignore plain "-D" (no argument) */
1466 1466 if (*(argv[i]+2))
1467 1467 *predef++ = argv[i]+2;
1468 1468 continue;
1469 1469 case 'U':
1470 1470 if (prund>punspc+NPREDEF) {
1471 1471 pperror("too many -U options, "
1472 1472 "ignoring %s", argv[i]);
1473 1473 continue;
1474 1474 }
1475 1475 *prund++ = argv[i]+2;
1476 1476 continue;
1477 1477 case 'u':
1478 1478 if (strcmp(argv[i], "-undef") == 0)
1479 1479 nopredef = 1;
1480 1480 else
1481 1481 goto unknown;
1482 1482 continue;
1483 1483 case 'I':
1484 1484 if (nd>=MAXIDIRS)
1485 1485 pperror("excessive -I file "
1486 1486 "(%s) ignored", argv[i]);
1487 1487 else
1488 1488 dirs[nd++] = argv[i]+2;
1489 1489 continue;
1490 1490 case 'T':
1491 1491 symlen = 8;
1492 1492 /* Compatibility with V7 */
1493 1493 continue;
1494 1494 case 'H':
1495 1495 /* Print included filenames */
1496 1496 hflag++;
1497 1497 continue;
1498 1498 case 'Y':
1499 1499 /* Replace system include dir */
1500 1500 sysdir = argv[i]+2;
1501 1501 continue;
1502 1502 case '\0': continue;
1503 1503 default:
1504 1504 unknown:
1505 1505 pperror("unknown flag %s", argv[i]);
1506 1506 continue;
1507 1507 }
1508 1508 default:
1509 1509 if (fin == STDIN_FILENO) {
1510 1510 if (0>(fin=open(argv[i], O_RDONLY))) {
1511 1511 pperror("No source file %s",
1512 1512 argv[i]);
1513 1513 exit(8);
1514 1514 }
1515 1515 fnames[ifno]=copy(argv[i]);
1516 1516 dirs[0]=dirnams[ifno]=trmdir(argv[i]);
1517 1517 /* too dangerous to have file name in same syntactic position
1518 1518 be input or output file depending on file redirections,
1519 1519 so force output to stdout, willy-nilly
1520 1520 [i don't see what the problem is. jfr]
1521 1521 */
1522 1522 } else if (fout==stdout) {
1523 1523 static char _sobuff[BUFSIZ];
1524 1524 if (NULL==(fout=fopen(argv[i], "w"))) {
1525 1525 pperror("Can't create %s",
1526 1526 argv[i]);
1527 1527 exit(8);
1528 1528 } else {
1529 1529 fclose(stdout);
1530 1530 setbuf(fout,_sobuff);
1531 1531 }
1532 1532 } else {
1533 1533 pperror("extraneous name %s", argv[i]);
1534 1534 }
1535 1535 }
1536 1536 }
1537 1537
1538 1538 fins[ifno]=fin;
1539 1539 exfail = 0;
1540 1540 /* after user -I files here are the standard include libraries */
1541 1541 if (sysdir != NULL) {
1542 1542 dirs[nd++] = sysdir;
1543 1543 } else {
1544 1544 # if unix
1545 1545 dirs[nd++] = "/usr/include";
1546 1546 # endif
1547 1547 /* dirs[nd++] = "/compool"; */
1548 1548 }
1549 1549 dirs[nd++] = 0;
1550 1550 defloc=ppsym("define");
1551 1551 udfloc=ppsym("undef");
1552 1552 incloc=ppsym("include");
1553 1553 elsloc=ppsym("else");
1554 1554 eifloc=ppsym("endif");
1555 1555 elifloc=ppsym("elif");
1556 1556 ifdloc=ppsym("ifdef");
1557 1557 ifnloc=ppsym("ifndef");
1558 1558 ifloc=ppsym("if");
1559 1559 lneloc=ppsym("line");
1560 1560 idtloc=ppsym("ident");
1561 1561 pragmaloc=ppsym("pragma");
1562 1562 errorloc=ppsym("error");
1563 1563 for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; )
1564 1564 macbit[i]=0;
1565 1565
1566 1566 if (! nopredef) {
1567 1567 ysysloc=stsym("unix");
1568 1568 ysysloc=stsym("sun");
1569 1569 # if __sparc__
1570 1570 varloc=stsym ("sparc");
1571 1571 # endif
1572 1572 # if __i386__
1573 1573 varloc=stsym ("i386");
1574 1574 # endif
1575 1575 }
1576 1576 ulnloc=stsym ("__LINE__");
1577 1577 uflloc=stsym ("__FILE__");
1578 1578 varloc=stsym ("__BUILTIN_VA_ARG_INCR");
1579 1579
1580 1580 tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;
1581 1581 cp2=prespc;
1582 1582 while (cp2<predef) stsym(*cp2++);
1583 1583 cp2=punspc;
1584 1584 while (cp2<prund) {
1585 1585 if ((p=strdex(*cp2, '=')) != NULL) *p++='\0';
1586 1586 if (strlen(*cp2) > symlen)
1587 1587 (*cp2)[symlen] = '\0';
1588 1588 lookup(*cp2++, DROP);
1589 1589 }
1590 1590 fnames[ifno]=tf;
1591 1591 pbeg=buffer+symlen; pbuf=pbeg+BUFFERSIZ; pend=pbuf+BUFFERSIZ;
1592 1592
1593 1593 trulvl = 0; flslvl = 0;
1594 1594 lineno[0] = 1; sayline(NOINCLUDE);
1595 1595 outp=inp=pend;
1596 1596 control(pend);
1597 1597 return (exfail);
1598 1598 }
↓ open down ↓ |
677 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX