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