| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#ifdef pdp11 |
| 43 |
#include <sys/types.h> |
| 44 |
#include <sys/file.h> |
| 45 |
#else |
| 46 |
#include "config.h" |
| 47 |
#endif |
| 48 |
|
| 49 |
#include <sys/stat.h> |
| 50 |
|
| 51 |
#include <fcntl.h> |
| 52 |
#if defined(HAVE_UNISTD_H) || defined(pdp11) |
| 53 |
#include <unistd.h> |
| 54 |
#endif |
| 55 |
#include <stdio.h> |
| 56 |
#include <stdarg.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <string.h> |
| 59 |
#include <time.h> |
| 60 |
|
| 61 |
#ifndef pdp11 |
| 62 |
#include "compat.h" |
| 63 |
#endif |
| 64 |
#include "cpp.h" |
| 65 |
|
| 66 |
#ifdef pdp11 |
| 67 |
#define VERSSTR "Portable C Compiler 1.2.0.DEVEL 20190327" |
| 68 |
#endif |
| 69 |
|
| 70 |
#ifndef S_ISDIR |
| 71 |
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static int counter, didexpand; |
| 81 |
|
| 82 |
|
| 83 |
int tflag; |
| 84 |
#ifdef PCC_DEBUG |
| 85 |
int dflag; |
| 86 |
static void prline(const usch *s); |
| 87 |
static void prrep(mvtyp); |
| 88 |
#define DPRINT(x) if (dflag) printf x |
| 89 |
#else |
| 90 |
#define DPRINT(x) |
| 91 |
#endif |
| 92 |
#define PUTOB(ob, ch) (ob->cptr == ob->bsz ? \ |
| 93 |
(putob(ob, ch), 1) : (ob->buf[ob->cptr++] = ch)) |
| 94 |
#define cunput(x) *--inp = x |
| 95 |
|
| 96 |
static int istty; |
| 97 |
int Aflag, Cflag, Eflag, Mflag, dMflag, Pflag, MPflag, MMDflag; |
| 98 |
char *Mfile, *MPfile; |
| 99 |
char *Mxfile; |
| 100 |
int warnings, Mxlen, skpows, readinc; |
| 101 |
usch pbbeg[MINBUF], *pbinp = pbbeg, *pbend = pbbeg + MINBUF; |
| 102 |
|
| 103 |
static void macstr(const usch *s); |
| 104 |
int lckmacbuf; |
| 105 |
#if LIBVMF |
| 106 |
struct vspace ibspc, macspc; |
| 107 |
struct vseg *macvseg; |
| 108 |
#endif |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#if LIBVMF == 0 |
| 116 |
char **macptr; |
| 117 |
int nmacptr; |
| 118 |
#endif |
| 119 |
usch *mbeg, *minp, *mend; |
| 120 |
|
| 121 |
|
| 122 |
struct incs { |
| 123 |
struct incs *next; |
| 124 |
usch *dir; |
| 125 |
dev_t dev; |
| 126 |
ino_t ino; |
| 127 |
} *incdir[2]; |
| 128 |
|
| 129 |
static struct symtab *filloc; |
| 130 |
static struct symtab *linloc; |
| 131 |
static struct symtab *pragloc; |
| 132 |
static struct symtab *defloc; |
| 133 |
static struct symtab *ctrloc; |
| 134 |
int trulvl; |
| 135 |
int flslvl; |
| 136 |
int elflvl; |
| 137 |
int elslvl; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
struct blok { |
| 166 |
int nidx; |
| 167 |
struct symtab *sp; |
| 168 |
}; |
| 169 |
struct blok *blokx[L2MAX]; |
| 170 |
#define BLKPTR(x) ((x) & ((CPPBUF/sizeof(struct blok))-1)) |
| 171 |
#define BLKBUF(x) ((x) / (CPPBUF/sizeof(struct blok))) |
| 172 |
int blkidp; |
| 173 |
|
| 174 |
static struct iobuf *readargs(struct iobuf *, struct symtab *, const usch **); |
| 175 |
static struct iobuf *exparg(int, struct iobuf *, struct iobuf *, int); |
| 176 |
static struct iobuf *subarg(struct symtab *sp, const usch **args, int, int); |
| 177 |
static void usage(void); |
| 178 |
static void addidir(char *idir, struct incs **ww); |
| 179 |
static void vsheap(struct iobuf *, const char *, va_list); |
| 180 |
static int skipws(struct iobuf *ib); |
| 181 |
static int getyp(usch *s); |
| 182 |
static void macsav(int ch); |
| 183 |
static void fstrstr(struct iobuf *ib, struct iobuf *ob); |
| 184 |
static usch *chkfile(const usch *n1, const usch *n2); |
| 185 |
static usch *addname(const usch *str); |
| 186 |
static void *addblock(int sz); |
| 187 |
|
| 188 |
int |
| 189 |
main(int argc, char **argv) |
| 190 |
{ |
| 191 |
struct includ bic; |
| 192 |
struct iobuf *fb = getobuf(BNORMAL); |
| 193 |
register int ch; |
| 194 |
register const usch *fn1, *fn2; |
| 195 |
char *a; |
| 196 |
|
| 197 |
#ifdef TIMING |
| 198 |
struct timeval t1, t2; |
| 199 |
|
| 200 |
(void)gettimeofday(&t1, NULL); |
| 201 |
#endif |
| 202 |
|
| 203 |
#if LIBVMF |
| 204 |
if (vminit(NVMPGS)) |
| 205 |
error("vminit"); |
| 206 |
if (vmopen(&ibspc, NULL) < 0) |
| 207 |
error("vmopen ibspc"); |
| 208 |
if (vmopen(&macspc, NULL) < 0) |
| 209 |
error("vmopen macspc"); |
| 210 |
#endif |
| 211 |
|
| 212 |
while ((ch = getopt(argc, argv, "ACD:d:EI:i:MPS:tU:Vvx:")) != -1) { |
| 213 |
switch (ch) { |
| 214 |
case 'A': |
| 215 |
Aflag++; |
| 216 |
break; |
| 217 |
|
| 218 |
case 'C': |
| 219 |
Cflag++; |
| 220 |
break; |
| 221 |
|
| 222 |
case 'E': |
| 223 |
Eflag++; |
| 224 |
break; |
| 225 |
|
| 226 |
case 'D': |
| 227 |
if ((a = strchr(optarg, '=')) != NULL) |
| 228 |
*a = ' '; |
| 229 |
bsheap(fb, "#define %s%s", optarg, a ? "\n" : " 1\n"); |
| 230 |
break; |
| 231 |
|
| 232 |
case 'i': |
| 233 |
bsheap(fb, "#include \"%s\"\n", optarg); |
| 234 |
break; |
| 235 |
|
| 236 |
case 'U': |
| 237 |
bsheap(fb, "#undef %s\n", optarg); |
| 238 |
break; |
| 239 |
|
| 240 |
case 'd': |
| 241 |
while (*optarg) { |
| 242 |
switch(*optarg) { |
| 243 |
case 'M': |
| 244 |
dMflag = 1; |
| 245 |
Mflag = 1; |
| 246 |
break; |
| 247 |
|
| 248 |
default: |
| 249 |
break; |
| 250 |
} |
| 251 |
optarg++; |
| 252 |
} |
| 253 |
break; |
| 254 |
|
| 255 |
case 'I': |
| 256 |
case 'S': |
| 257 |
addidir(optarg, &incdir[ch == 'I' ? INCINC : SYSINC]); |
| 258 |
break; |
| 259 |
|
| 260 |
case 'M': |
| 261 |
Mflag++; |
| 262 |
break; |
| 263 |
|
| 264 |
case 'P': |
| 265 |
Pflag++; |
| 266 |
break; |
| 267 |
|
| 268 |
case 't': |
| 269 |
tflag = 1; |
| 270 |
break; |
| 271 |
|
| 272 |
#ifdef PCC_DEBUG |
| 273 |
case 'V': |
| 274 |
dflag++; |
| 275 |
break; |
| 276 |
#endif |
| 277 |
case 'v': |
| 278 |
fprintf(stderr, "PCC preprocessor version "VERSSTR"\n"); |
| 279 |
break; |
| 280 |
|
| 281 |
case 'x': |
| 282 |
if (strcmp(optarg, "MMD") == 0) { |
| 283 |
MMDflag++; |
| 284 |
} else if (strcmp(optarg, "MP") == 0) { |
| 285 |
MPflag++; |
| 286 |
} else if (strncmp(optarg, "MT,", 3) == 0 || |
| 287 |
strncmp(optarg, "MQ,", 3) == 0) { |
| 288 |
int l = (int)strlen(optarg+3) + 2; |
| 289 |
char *cp, *up; |
| 290 |
|
| 291 |
if (optarg[1] == 'Q') |
| 292 |
for (cp = optarg+3; *cp; cp++) |
| 293 |
if (*cp == '$') |
| 294 |
l++; |
| 295 |
Mxlen += l; |
| 296 |
Mxfile = cp = realloc(Mxfile, Mxlen); |
| 297 |
for (up = Mxfile; *up; up++) |
| 298 |
; |
| 299 |
if (up != Mxfile) |
| 300 |
*up++ = ' '; |
| 301 |
for (cp = optarg+3; *cp; cp++) { |
| 302 |
*up++ = *cp; |
| 303 |
if (optarg[1] == 'Q' && *cp == '$') |
| 304 |
*up++ = *cp; |
| 305 |
} |
| 306 |
*up = 0; |
| 307 |
} else |
| 308 |
usage(); |
| 309 |
break; |
| 310 |
|
| 311 |
case '?': |
| 312 |
default: |
| 313 |
usage(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
argc -= optind; |
| 318 |
argv += optind; |
| 319 |
|
| 320 |
filloc = lookup((const usch *)"__FILE__", ENTER); |
| 321 |
linloc = lookup((const usch *)"__LINE__", ENTER); |
| 322 |
pragloc = lookup((const usch *)"_Pragma", ENTER); |
| 323 |
defloc = lookup((const usch *)"defined", ENTER); |
| 324 |
ctrloc = lookup((const usch *)"__COUNTER__", ENTER); |
| 325 |
|
| 326 |
#if LIBVMF |
| 327 |
macvseg = vmmapseg(&macspc, 0); |
| 328 |
vmlock(macvseg); |
| 329 |
mbeg = minp = macvseg->s_cinfo; |
| 330 |
mend = mbeg + BYTESPERSEG; |
| 331 |
#else |
| 332 |
macptr = xmalloc((nmacptr = 10) * sizeof(char **)); |
| 333 |
memset(macptr, 0, nmacptr * sizeof(char **)); |
| 334 |
macptr[0] = xmalloc(CPPBUF); |
| 335 |
mbeg = minp = macptr[0]; |
| 336 |
mend = mbeg + CPPBUF; |
| 337 |
#endif |
| 338 |
|
| 339 |
#ifdef pdp11 |
| 340 |
|
| 341 |
bsheap(fb, "#define __BSD2_11__ 1\n"); |
| 342 |
bsheap(fb, "#define BSD2_11 1\n"); |
| 343 |
bsheap(fb, "#define __pdp11__ 1\n"); |
| 344 |
bsheap(fb, "#define pdp11 1\n"); |
| 345 |
bsheap(fb, "#define unix 1\n"); |
| 346 |
addidir("/usr/include", &incdir[SYSINC]); |
| 347 |
if (tflag == 0) |
| 348 |
bsheap(fb, "#define __STDC__ 1\n"); |
| 349 |
#endif |
| 350 |
|
| 351 |
macsav(0); |
| 352 |
filloc->valoff = linloc->valoff = pragloc->valoff = |
| 353 |
ctrloc->valoff = defloc->valoff = 1; |
| 354 |
macstr((const usch *)"defined"); |
| 355 |
macsav(0); |
| 356 |
|
| 357 |
filloc->type = FILLOC; |
| 358 |
linloc->type = LINLOC; |
| 359 |
pragloc->type = PRAGLOC; |
| 360 |
defloc->type = DEFLOC; |
| 361 |
ctrloc->type = CTRLOC; |
| 362 |
|
| 363 |
if (Mflag && !dMflag) { |
| 364 |
char *c; |
| 365 |
|
| 366 |
if (argc < 1) |
| 367 |
error("-M and no infile"); |
| 368 |
if ((c = strrchr(argv[0], '/')) == NULL) |
| 369 |
c = argv[0]; |
| 370 |
else |
| 371 |
c++; |
| 372 |
Mfile = (char *)addname((usch *)c); |
| 373 |
if (MPflag) |
| 374 |
MPfile = (char *)addname((usch *)c); |
| 375 |
if (Mxfile) |
| 376 |
Mfile = Mxfile; |
| 377 |
if ((c = strrchr(Mfile, '.')) == NULL) |
| 378 |
error("-M and no extension: "); |
| 379 |
c[1] = 'o'; |
| 380 |
c[2] = 0; |
| 381 |
} |
| 382 |
|
| 383 |
if (argc == 2) { |
| 384 |
close(1); |
| 385 |
if (open(argv[1], O_WRONLY|O_CREAT, 0600) < 0) |
| 386 |
error("Can't creat %s", argv[1]); |
| 387 |
} |
| 388 |
istty = isatty(1); |
| 389 |
|
| 390 |
if (argc && strcmp(argv[0], "-")) { |
| 391 |
fn1 = fn2 = (usch *)argv[0]; |
| 392 |
} else { |
| 393 |
fn1 = NULL; |
| 394 |
fn2 = (const usch *)""; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
if (dMflag) |
| 399 |
write(1, fb->buf, fb->cptr); |
| 400 |
fb->buf[fb->cptr] = 0; |
| 401 |
memset(&bic, 0, sizeof(bic)); |
| 402 |
bic.fname = bic.orgfn = (const usch *)"<command line>"; |
| 403 |
bic.lineno = 1; |
| 404 |
bic.infil = -1; |
| 405 |
fb->bsz = fb->cptr; |
| 406 |
fb->cptr = 0; |
| 407 |
pbeg = outp = inp = fb->buf; |
| 408 |
pend = pbeg + fb->bsz; |
| 409 |
ifiles = &bic; |
| 410 |
fastscan(); |
| 411 |
bufree(fb); |
| 412 |
ifiles = NULL; |
| 413 |
|
| 414 |
|
| 415 |
pushfile(fn1, fn2, 0, NULL); |
| 416 |
|
| 417 |
if (Mflag == 0) { |
| 418 |
if (skpows) |
| 419 |
*pbinp++ = '\n'; |
| 420 |
write(1, pbbeg, pbinp - pbbeg); |
| 421 |
} |
| 422 |
#ifdef TIMING |
| 423 |
(void)gettimeofday(&t2, NULL); |
| 424 |
t2.tv_sec -= t1.tv_sec; |
| 425 |
t2.tv_usec -= t1.tv_usec; |
| 426 |
if (t2.tv_usec < 0) { |
| 427 |
t2.tv_usec += 1000000; |
| 428 |
t2.tv_sec -= 1; |
| 429 |
} |
| 430 |
fprintf(stderr, "cpp total time: %ld s %ld us\n", |
| 431 |
(long)t2.tv_sec, (long)t2.tv_usec); |
| 432 |
#endif |
| 433 |
if (Eflag && warnings > 0) |
| 434 |
return 2; |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
void |
| 443 |
putob(register struct iobuf *ob, register int ch) |
| 444 |
{ |
| 445 |
if (ob->cptr == ob->bsz) { |
| 446 |
int sz = ob->bsz; |
| 447 |
switch (ob->type) { |
| 448 |
case BNORMAL: |
| 449 |
ob->buf = xrealloc(ob->buf, sz + CPPBUF+1); |
| 450 |
|
| 451 |
ob->bsz = sz + CPPBUF; |
| 452 |
break; |
| 453 |
case BMAC: |
| 454 |
macsav(ch); |
| 455 |
return; |
| 456 |
case BINBUF: |
| 457 |
case BUTBUF: |
| 458 |
error("putob %d", ob->type); |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
ob->buf[ob->cptr++] = ch; |
| 463 |
} |
| 464 |
|
| 465 |
static struct iobuf *ioblnk; |
| 466 |
|
| 467 |
static struct iobuf * |
| 468 |
giob(int typ, register const usch *bp, int bsz) |
| 469 |
{ |
| 470 |
register struct iobuf *iob; |
| 471 |
|
| 472 |
if (ioblnk) { |
| 473 |
iob = ioblnk; |
| 474 |
ioblnk = (void *)iob->buf; |
| 475 |
} else |
| 476 |
iob = addblock(sizeof(*iob)); |
| 477 |
|
| 478 |
if (bp == NULL) |
| 479 |
bp = xmalloc(bsz); |
| 480 |
iob->buf = (usch *)bp; |
| 481 |
iob->cptr = 0; |
| 482 |
iob->bsz = bsz; |
| 483 |
iob->ro = 0; |
| 484 |
iob->type = typ; |
| 485 |
return iob; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
int nbufused; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
struct iobuf * |
| 494 |
getobuf(register int type) |
| 495 |
{ |
| 496 |
register struct iobuf *iob = 0; |
| 497 |
|
| 498 |
switch (type) { |
| 499 |
case BMAC: |
| 500 |
#if LIBVMF |
| 501 |
iob = giob(BMAC, (usch *)macvseg->s_cinfo, BYTESPERSEG); |
| 502 |
#else |
| 503 |
iob = giob(BMAC, NULL, CPPBUF); |
| 504 |
#endif |
| 505 |
break; |
| 506 |
case BNORMAL: |
| 507 |
nbufused++; |
| 508 |
iob = giob(BNORMAL, NULL, CPPBUF); |
| 509 |
iob->bsz = CPPBUF-1; |
| 510 |
break; |
| 511 |
case BINBUF: |
| 512 |
#if LIBVMF |
| 513 |
iob = giob(BINBUF, (usch *)ifiles->vseg->s_cinfo, BYTESPERSEG); |
| 514 |
#else |
| 515 |
iob = giob(BINBUF, NULL, CPPBUF); |
| 516 |
#endif |
| 517 |
break; |
| 518 |
default: |
| 519 |
error("getobuf"); |
| 520 |
} |
| 521 |
return iob; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static struct iobuf * |
| 528 |
mkrobuf(register const usch *s) |
| 529 |
{ |
| 530 |
register struct iobuf *iob; |
| 531 |
|
| 532 |
nbufused++; |
| 533 |
iob = giob(BNORMAL, s, strlen((char *)s)); |
| 534 |
iob->ro = 1; |
| 535 |
DPRINT(("mkrobuf %s\n", s)); |
| 536 |
return iob; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
struct iobuf * |
| 543 |
buftobuf(register struct iobuf *in, register struct iobuf *iob) |
| 544 |
{ |
| 545 |
register int cp; |
| 546 |
|
| 547 |
DPRINT(("buftobuf in %p out %p instr %s\n", in, iob, in->buf)); |
| 548 |
if (iob == NULL) |
| 549 |
iob = getobuf(BNORMAL); |
| 550 |
for (cp = 0; cp < in->cptr; cp++) |
| 551 |
putob(iob, in->buf[cp]); |
| 552 |
return iob; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
struct iobuf * |
| 559 |
strtobuf(register const usch *str, register struct iobuf *iob) |
| 560 |
{ |
| 561 |
if (iob == NULL) |
| 562 |
iob = getobuf(BNORMAL); |
| 563 |
DPRINT(("strtobuf iob %p buf %p str %s\n", iob, iob->buf, str)); |
| 564 |
do { |
| 565 |
PUTOB(iob, *str); |
| 566 |
} while (*str++); |
| 567 |
iob->cptr--; |
| 568 |
return iob; |
| 569 |
} |
| 570 |
|
| 571 |
static void |
| 572 |
macsav(int ch) |
| 573 |
{ |
| 574 |
#if LIBVMF |
| 575 |
if (minp == mend) { |
| 576 |
|
| 577 |
lckmacbuf++; |
| 578 |
vmmodify(macvseg); |
| 579 |
vmunlock(macvseg); |
| 580 |
macvseg = vmmapseg(&macspc, lckmacbuf); |
| 581 |
vmlock(macvseg); |
| 582 |
minp = mbeg = macvseg->s_cinfo; |
| 583 |
mend = mbeg + BYTESPERSEG; |
| 584 |
} |
| 585 |
#else |
| 586 |
if (minp == mend) { |
| 587 |
if (++lckmacbuf == nmacptr) { |
| 588 |
macptr = xrealloc(macptr, (nmacptr + 10) * sizeof(char **)); |
| 589 |
memset(macptr+nmacptr, 0, 10 * sizeof(char **)); |
| 590 |
nmacptr += 10; |
| 591 |
} |
| 592 |
|
| 593 |
if ((mbeg = macptr[lckmacbuf]) == NULL) |
| 594 |
mbeg = macptr[lckmacbuf] = xmalloc(CPPBUF); |
| 595 |
mend = mbeg + CPPBUF; |
| 596 |
minp = mbeg; |
| 597 |
} |
| 598 |
#endif |
| 599 |
*minp++ = ch; |
| 600 |
} |
| 601 |
|
| 602 |
static void |
| 603 |
macstr(register const usch *s) |
| 604 |
{ |
| 605 |
while (*s != 0) { |
| 606 |
if (minp == mend) |
| 607 |
macsav(*s++); |
| 608 |
else |
| 609 |
*minp++ = *s++; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
static int |
| 614 |
macget(register mvtyp a) |
| 615 |
{ |
| 616 |
#if LIBVMF |
| 617 |
register struct vseg *vseg = vmmapseg(&macspc, VALBUF(a)); |
| 618 |
return vseg->s_cinfo[VALPTR(a)]; |
| 619 |
#else |
| 620 |
return macptr[VALBUF(a)][VALPTR(a)]; |
| 621 |
#endif |
| 622 |
} |
| 623 |
|
| 624 |
static char * |
| 625 |
macmapin(int seg) |
| 626 |
{ |
| 627 |
#if LIBVMF |
| 628 |
struct vseg *vseg= vmmapseg(&macspc, seg); |
| 629 |
return vseg->s_cinfo; |
| 630 |
#else |
| 631 |
return macptr[seg]; |
| 632 |
#endif |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
static struct iobuf * |
| 639 |
macrepbuf(struct symtab *sp) |
| 640 |
{ |
| 641 |
register struct iobuf *ob; |
| 642 |
int ch; |
| 643 |
register usch *from, *to, *fend, *tend; |
| 644 |
int cvoff = VALBUF(sp->valoff); |
| 645 |
|
| 646 |
|
| 647 |
ob = getobuf(BNORMAL); |
| 648 |
to = ob->buf; |
| 649 |
tend = to + ob->bsz; |
| 650 |
|
| 651 |
from = macmapin(cvoff++); |
| 652 |
fend = from + MINBUF; |
| 653 |
from += VALPTR(sp->valoff); |
| 654 |
|
| 655 |
while ((ch = (*to++ = *from++)) != 0) { |
| 656 |
iloop: if (from == fend) { |
| 657 |
from = macmapin(cvoff++); |
| 658 |
fend = from + MINBUF; |
| 659 |
} |
| 660 |
if (to == tend) { |
| 661 |
ob->cptr = to - ob->buf; |
| 662 |
putob(ob, 0); |
| 663 |
ob->cptr--; |
| 664 |
to = ob->buf + ob->cptr; |
| 665 |
tend = ob->buf + ob->bsz; |
| 666 |
} |
| 667 |
if (ch == WARN) { |
| 668 |
ch = (*to++ = *from++); |
| 669 |
goto iloop; |
| 670 |
} |
| 671 |
} |
| 672 |
ob->cptr = 0; |
| 673 |
return ob; |
| 674 |
} |
| 675 |
|
| 676 |
void |
| 677 |
bufree(register struct iobuf *iob) |
| 678 |
{ |
| 679 |
if (iob->type == BNORMAL) |
| 680 |
nbufused--; |
| 681 |
if (iob->ro == 0) |
| 682 |
free(iob->buf); |
| 683 |
iob->buf = (void *)ioblnk; |
| 684 |
ioblnk = iob; |
| 685 |
} |
| 686 |
|
| 687 |
static void |
| 688 |
addidir(register char *idir, register struct incs **ww) |
| 689 |
{ |
| 690 |
register struct incs *w; |
| 691 |
struct stat st; |
| 692 |
|
| 693 |
if (stat(idir, &st) == -1 || !S_ISDIR(st.st_mode)) |
| 694 |
return; |
| 695 |
if (*ww != NULL) { |
| 696 |
for (w = *ww; w->next; w = w->next) { |
| 697 |
#ifdef _WIN32 |
| 698 |
if (strcmp(w->dir, idir) == 0) |
| 699 |
return; |
| 700 |
#else |
| 701 |
if (w->dev == st.st_dev && w->ino == st.st_ino) |
| 702 |
return; |
| 703 |
#endif |
| 704 |
} |
| 705 |
#ifdef _WIN32 |
| 706 |
if (strcmp(w->dir, idir) == 0) |
| 707 |
return; |
| 708 |
#else |
| 709 |
if (w->dev == st.st_dev && w->ino == st.st_ino) |
| 710 |
return; |
| 711 |
#endif |
| 712 |
ww = &w->next; |
| 713 |
} |
| 714 |
if ((w = calloc(sizeof(struct incs), 1)) == NULL) |
| 715 |
error("couldn't add path %s", idir); |
| 716 |
w->dir = (usch *)idir; |
| 717 |
w->dev = st.st_dev; |
| 718 |
w->ino = st.st_ino; |
| 719 |
*ww = w; |
| 720 |
} |
| 721 |
|
| 722 |
void |
| 723 |
line(void) |
| 724 |
{ |
| 725 |
register struct iobuf *ob; |
| 726 |
register int x, n, ln = 0, oidx; |
| 727 |
|
| 728 |
oidx = ifiles->idx; |
| 729 |
|
| 730 |
x = 0; |
| 731 |
if (yylex() == NUMBER && |
| 732 |
(yynode.nd_val >= 1 || yynode.nd_val <= 2147483647L)) { |
| 733 |
ln = yynode.nd_val; |
| 734 |
escln = 0; |
| 735 |
|
| 736 |
if ((x = yylex()) == STRING) { |
| 737 |
ob = yynode.nd_ob; |
| 738 |
ob->buf[--ob->cptr] = 0; |
| 739 |
if (strcmp((char *)ifiles->fname, (char *)ob->buf+1)) |
| 740 |
ifiles->fname = addname(ob->buf+1); |
| 741 |
bufree(ob); |
| 742 |
|
| 743 |
if ((x = yylex()) == NUMBER && |
| 744 |
((n = yynode.nd_val) >= 0 || n <= 9)) { |
| 745 |
if (n == 3) |
| 746 |
ifiles->idx = SYSINC; |
| 747 |
x = yylex(); |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if (x != WARN) |
| 753 |
error("bad #line"); |
| 754 |
|
| 755 |
ifiles->lineno = ln; |
| 756 |
prtline(1); |
| 757 |
ifiles->idx = oidx; |
| 758 |
ifiles->lineno--; |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
#ifdef MACHOABI |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
static int |
| 773 |
fsrch_macos_framework(const usch *fn, const usch *dir) |
| 774 |
{ |
| 775 |
struct iobuf *ob; |
| 776 |
usch *s = (usch *)strchr((const char*)fn, '/'); |
| 777 |
usch *p, *q, *nm; |
| 778 |
int len = s - fn; |
| 779 |
|
| 780 |
if (s == NULL) |
| 781 |
return 0; |
| 782 |
|
| 783 |
nm = addname(dir); |
| 784 |
p = addname(fn); |
| 785 |
*(p + len) = 0; |
| 786 |
|
| 787 |
q = (usch *)strstr((const char *)nm, (const char *)p); |
| 788 |
if (q != NULL) { |
| 789 |
*q = 0; |
| 790 |
return fsrch_macos_framework(fn, nm); |
| 791 |
} |
| 792 |
free(p); |
| 793 |
|
| 794 |
p = nm + strlen((char *)nm) - 1; |
| 795 |
while (*p == '/') |
| 796 |
p--; |
| 797 |
while (*p != '/') |
| 798 |
p--; |
| 799 |
++p; |
| 800 |
|
| 801 |
ob = bsheap(NULL, "%s/Frameworks/%s.framework/Headers%s", nm, fn, s); |
| 802 |
free(nm); |
| 803 |
nm = addname(ob->buf); |
| 804 |
bufree(ob); |
| 805 |
pushfile(nm, fn, SYSINC, NULL); |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
#endif |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
static int |
| 817 |
fsrch(const usch *fn, int idx, register struct incs *w) |
| 818 |
{ |
| 819 |
register usch *res; |
| 820 |
register int i; |
| 821 |
|
| 822 |
for (i = idx; i < 2; i++) { |
| 823 |
if (i > idx) |
| 824 |
w = incdir[i]; |
| 825 |
for (; w; w = w->next) { |
| 826 |
if ((res = chkfile(fn, w->dir)) != NULL) { |
| 827 |
pushfile(res, fn, i, w->next); |
| 828 |
return 1; |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
#ifdef MACHOABI |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
{ |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
usch *p, *dir = addname(ifiles->orgfn); |
| 843 |
if ((p = (usch *)strrchr((char *)dir, '/')) != NULL) { |
| 844 |
p[1] = 0; |
| 845 |
if (fsrch_macos_framework(fn, dir) == 1) |
| 846 |
return 1; |
| 847 |
} |
| 848 |
|
| 849 |
if (fsrch_macos_framework(fn, |
| 850 |
(const usch *)"/Library/Frameworks/") == 1) |
| 851 |
return 1; |
| 852 |
|
| 853 |
if (fsrch_macos_framework(fn, |
| 854 |
(const usch *)"/System/Library/Frameworks/") == 1) |
| 855 |
return 1; |
| 856 |
} |
| 857 |
#endif |
| 858 |
|
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
static void |
| 863 |
prem(void) |
| 864 |
{ |
| 865 |
error("premature EOF"); |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
static usch * |
| 873 |
chkfile(register const usch *n1, register const usch *n2) |
| 874 |
{ |
| 875 |
register struct iobuf *ob; |
| 876 |
register usch *res = NULL; |
| 877 |
|
| 878 |
if (n2 != NULL) { |
| 879 |
ob = bsheap(NULL, "%s/%s", n2, n1); |
| 880 |
} else |
| 881 |
ob = strtobuf(n1, NULL); |
| 882 |
if (access((char *)ob->buf, R_OK) == 0) |
| 883 |
res = addname(ob->buf); |
| 884 |
bufree(ob); |
| 885 |
return res; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
void |
| 894 |
include(void) |
| 895 |
{ |
| 896 |
register struct iobuf *ob; |
| 897 |
register usch *fn, *nm = NULL; |
| 898 |
|
| 899 |
if (flslvl) |
| 900 |
return; |
| 901 |
|
| 902 |
readinc++; |
| 903 |
if (yylex() != STRING) |
| 904 |
error("bad #include"); |
| 905 |
readinc = 0; |
| 906 |
ob = yynode.nd_ob; |
| 907 |
ob->buf[ob->cptr-1] = 0; |
| 908 |
|
| 909 |
if (ob->buf[1] == '/') { |
| 910 |
if ((fn = chkfile(&ob->buf[1], 0)) != NULL) |
| 911 |
goto okret; |
| 912 |
} |
| 913 |
if (ob->buf[0] == '\"') { |
| 914 |
if ((nm = (usch *)strrchr((char *)ifiles->orgfn, '/'))) { |
| 915 |
*nm = 0; |
| 916 |
fn = chkfile(&ob->buf[1], ifiles->orgfn); |
| 917 |
*nm = '/'; |
| 918 |
} else |
| 919 |
fn = chkfile(&ob->buf[1], 0); |
| 920 |
if (fn != NULL) |
| 921 |
goto okret; |
| 922 |
} |
| 923 |
fn = addname(&ob->buf[1]); |
| 924 |
bufree(ob); |
| 925 |
if (fsrch(fn, 0, incdir[0])) |
| 926 |
goto prt; |
| 927 |
|
| 928 |
error("cannot find '%s'", fn); |
| 929 |
|
| 930 |
|
| 931 |
okret: bufree(ob); |
| 932 |
pushfile(fn, fn, 0, NULL); |
| 933 |
prt: prtline(1); |
| 934 |
} |
| 935 |
|
| 936 |
void |
| 937 |
include_next(void) |
| 938 |
{ |
| 939 |
register struct iobuf *ob; |
| 940 |
register usch *fn; |
| 941 |
|
| 942 |
if (flslvl) |
| 943 |
return; |
| 944 |
|
| 945 |
readinc++; |
| 946 |
if (yylex() != STRING) |
| 947 |
error("bad #include_next"); |
| 948 |
readinc = 0; |
| 949 |
ob = yynode.nd_ob; |
| 950 |
ob->buf[ob->cptr-1] = 0; |
| 951 |
|
| 952 |
fn = addname(&ob->buf[1]); |
| 953 |
bufree(ob); |
| 954 |
if (fsrch(fn, ifiles->idx, ifiles->incs) == 0) |
| 955 |
error("cannot find '%s'", &ob->buf[1]); |
| 956 |
|
| 957 |
prtline(1); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
static int |
| 964 |
cmprepl(mvtyp oin, mvtyp nin) |
| 965 |
{ |
| 966 |
register int o, n; |
| 967 |
|
| 968 |
for (; ; oin++, nin++) { |
| 969 |
|
| 970 |
o = macget(oin); |
| 971 |
n = macget(nin); |
| 972 |
if (o == '/' && macget(oin+1) == '*') { |
| 973 |
oin+=2; |
| 974 |
while (macget(oin) != '*' || macget(oin+1) != '/') |
| 975 |
oin++; |
| 976 |
oin += 2; |
| 977 |
} |
| 978 |
if (n == '/' && macget(nin+1) == '*') { |
| 979 |
nin+=2; |
| 980 |
while (macget(nin) != '*' || macget(nin+1) != '/') |
| 981 |
nin++; |
| 982 |
nin += 2; |
| 983 |
} |
| 984 |
while ((o = macget(oin)) == ' ' || o == '\t') |
| 985 |
oin++; |
| 986 |
while ((n = macget(nin)) == ' ' || n == '\t') |
| 987 |
nin++; |
| 988 |
if (o != n) |
| 989 |
return 1; |
| 990 |
if (o == 0) |
| 991 |
break; |
| 992 |
} |
| 993 |
return 0; |
| 994 |
} |
| 995 |
|
| 996 |
static int |
| 997 |
isell(void) |
| 998 |
{ |
| 999 |
if (cinput() != '.' || cinput() != '.') |
| 1000 |
return 0; |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
static int |
| 1005 |
skipwscmnt(struct iobuf *ib) |
| 1006 |
{ |
| 1007 |
|
| 1008 |
return skipws(ib); |
| 1009 |
} |
| 1010 |
|
| 1011 |
static int |
| 1012 |
findarg(register usch *s, register struct iobuf *ab, int *arg, int narg) |
| 1013 |
{ |
| 1014 |
register int i; |
| 1015 |
|
| 1016 |
for (i = 0; i < narg; i++) |
| 1017 |
if (strcmp((char *)s, (char *)ab->buf + arg[i]) == 0) |
| 1018 |
return i; |
| 1019 |
return -1; |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
void |
| 1028 |
define(void) |
| 1029 |
{ |
| 1030 |
extern int incmnt; |
| 1031 |
register struct iobuf *ab; |
| 1032 |
register struct symtab *np; |
| 1033 |
usch cc[2], *vararg, *dp; |
| 1034 |
int arg[MAXARGS+1]; |
| 1035 |
register int c, i, redef, oCflag, t; |
| 1036 |
int type, narg, begpos, needws; |
| 1037 |
int wascon; |
| 1038 |
|
| 1039 |
if (flslvl) |
| 1040 |
return; |
| 1041 |
|
| 1042 |
oCflag = Cflag, Cflag = 0; |
| 1043 |
if (!ISID0(c = skipws(0))) |
| 1044 |
goto bad; |
| 1045 |
|
| 1046 |
dp = readid(c); |
| 1047 |
np = lookup(dp, ENTER); |
| 1048 |
if (np->valoff) { |
| 1049 |
redef = 1; |
| 1050 |
} else { |
| 1051 |
np->namep = addname(dp); |
| 1052 |
redef = 0; |
| 1053 |
} |
| 1054 |
|
| 1055 |
type = OBJCT; |
| 1056 |
narg = 0; |
| 1057 |
ab = getobuf(BNORMAL); |
| 1058 |
vararg = NULL; |
| 1059 |
if ((c = cinput()) == '(') { |
| 1060 |
type = FUNLIKE; |
| 1061 |
|
| 1062 |
c = skipws(0); |
| 1063 |
for (;;) { |
| 1064 |
switch (c) { |
| 1065 |
case ')': |
| 1066 |
break; |
| 1067 |
case '.': |
| 1068 |
if (isell() == 0 || (c = skipws(0)) != ')') |
| 1069 |
goto bad; |
| 1070 |
vararg = (usch *)"__VA_ARGS__"; |
| 1071 |
break; |
| 1072 |
default: |
| 1073 |
if (!ISID0(c)) |
| 1074 |
goto bad; |
| 1075 |
|
| 1076 |
dp = bufid(c, ab); |
| 1077 |
|
| 1078 |
if (findarg(dp, ab, arg, narg) >= 0) |
| 1079 |
error("Duplicate parameter \"%s\"", dp); |
| 1080 |
if (narg == MAXARGS) |
| 1081 |
error("Too many macro args"); |
| 1082 |
putob(ab, 0); |
| 1083 |
arg[narg++] = (int)(dp - ab->buf); |
| 1084 |
switch ((c = skipws(0))) { |
| 1085 |
case ',': break; |
| 1086 |
case ')': continue; |
| 1087 |
case '.': |
| 1088 |
if (isell() == 0 || skipws(0) != ')') |
| 1089 |
goto bad; |
| 1090 |
vararg = ab->buf + arg[--narg]; |
| 1091 |
c = ')'; |
| 1092 |
continue; |
| 1093 |
default: |
| 1094 |
goto bad; |
| 1095 |
} |
| 1096 |
c = skipws(0); |
| 1097 |
} |
| 1098 |
if (c == ')') |
| 1099 |
break; |
| 1100 |
} |
| 1101 |
c = skipws(0); |
| 1102 |
} else if (c == '\n') { |
| 1103 |
|
| 1104 |
; |
| 1105 |
} else if (c == 0) { |
| 1106 |
prem(); |
| 1107 |
} else if (!ISWS(c)) |
| 1108 |
goto bad; |
| 1109 |
|
| 1110 |
if (tflag == 0) |
| 1111 |
Cflag = oCflag; |
| 1112 |
else |
| 1113 |
Cflag = 1; |
| 1114 |
|
| 1115 |
begpos = MKVAL(lckmacbuf, minp - mbeg); |
| 1116 |
if (ISWS(c)) |
| 1117 |
c = skipwscmnt(0); |
| 1118 |
|
| 1119 |
|
| 1120 |
wascon = needws = 0; |
| 1121 |
while (c != '\n') { |
| 1122 |
incmnt++; |
| 1123 |
cc[0] = c, cc[1] = cinput(); |
| 1124 |
incmnt--; |
| 1125 |
t = getyp(cc); |
| 1126 |
cunput(cc[1]); |
| 1127 |
|
| 1128 |
switch (t) { |
| 1129 |
case ' ': |
| 1130 |
case '\t': |
| 1131 |
needws++; |
| 1132 |
while ((c = cinput()) == ' ' || c == '\t') |
| 1133 |
; |
| 1134 |
continue; |
| 1135 |
|
| 1136 |
case '#': |
| 1137 |
if (cc[1] == '#') { |
| 1138 |
|
| 1139 |
(void)cinput(); |
| 1140 |
needws = 0; |
| 1141 |
macsav(CONC); |
| 1142 |
if (ISID0(c = skipws(0)) && type == FUNLIKE) |
| 1143 |
wascon = 1; |
| 1144 |
if (c == '\n') |
| 1145 |
goto bad; |
| 1146 |
continue; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (needws) |
| 1150 |
macsav(' '), needws = 0; |
| 1151 |
if (type == OBJCT) { |
| 1152 |
|
| 1153 |
macsav('#'); |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
macsav(SNUFF); |
| 1159 |
c = skipws(0); |
| 1160 |
if (!ISID0(c)) |
| 1161 |
goto bad; |
| 1162 |
dp = readid(c); |
| 1163 |
if (vararg && strcmp((char *)dp, (char *)vararg) == 0) { |
| 1164 |
macsav(WARN); |
| 1165 |
macsav(C99ARG); |
| 1166 |
macsav(SNUFF); |
| 1167 |
break; |
| 1168 |
|
| 1169 |
} |
| 1170 |
if ((i = findarg(dp, ab, arg, narg)) < 0) |
| 1171 |
goto bad; |
| 1172 |
macsav(WARN); |
| 1173 |
macsav(i); |
| 1174 |
macsav(SNUFF); |
| 1175 |
break; |
| 1176 |
|
| 1177 |
case CMNT: |
| 1178 |
if (needws) |
| 1179 |
macsav(' '), needws = 0; |
| 1180 |
if (oCflag) |
| 1181 |
macsav(c); |
| 1182 |
c = cinput(); |
| 1183 |
if (c == '/') { |
| 1184 |
do { |
| 1185 |
if (oCflag) |
| 1186 |
macsav(c); |
| 1187 |
c = cinput(); |
| 1188 |
} while (c && c != '\n'); |
| 1189 |
if (c == 0) |
| 1190 |
goto bad; |
| 1191 |
continue; |
| 1192 |
} else { |
| 1193 |
if (oCflag) |
| 1194 |
macsav(c); |
| 1195 |
for (;;) { |
| 1196 |
c = cinput(); |
| 1197 |
if (oCflag) |
| 1198 |
macsav(c); |
| 1199 |
back: if (c == '*') { |
| 1200 |
c = cinput(); |
| 1201 |
if (oCflag) |
| 1202 |
macsav(c); |
| 1203 |
if (c == '/') |
| 1204 |
break; |
| 1205 |
if (c == '*') |
| 1206 |
goto back; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
break; |
| 1211 |
|
| 1212 |
case NUMBER: |
| 1213 |
if (needws) |
| 1214 |
macsav(' '), needws = 0; |
| 1215 |
if (c == '.') |
| 1216 |
macsav(c), c = cinput(); |
| 1217 |
for (;;) { |
| 1218 |
macsav(i = c), c = cinput(); |
| 1219 |
if (c == '-' || c == '+') { |
| 1220 |
if ((i & 0337) != 'E' && i != 'P') |
| 1221 |
break; |
| 1222 |
} else if ((c != '.') && ((ISID(c)) == 0)) |
| 1223 |
break; |
| 1224 |
} |
| 1225 |
continue; |
| 1226 |
|
| 1227 |
case STRING: |
| 1228 |
if (needws) |
| 1229 |
macsav(' '), needws = 0; |
| 1230 |
if (tflag) { |
| 1231 |
macsav(c); |
| 1232 |
} else { |
| 1233 |
extern int instr; |
| 1234 |
int bc; |
| 1235 |
while (c != '\"' && c != '\'') |
| 1236 |
macsav(c), c = cinput(); |
| 1237 |
|
| 1238 |
bc = c; |
| 1239 |
instr = 1; |
| 1240 |
macsav(c), c = cinput(); |
| 1241 |
while (c != bc) { |
| 1242 |
if (c == '\\') |
| 1243 |
macsav(c), c = cinput(); |
| 1244 |
else if (c == '\n') |
| 1245 |
goto bad; |
| 1246 |
macsav(c), c = cinput(); |
| 1247 |
} |
| 1248 |
macsav(c); |
| 1249 |
instr = 0; |
| 1250 |
} |
| 1251 |
break; |
| 1252 |
|
| 1253 |
case IDENT: |
| 1254 |
if (needws) |
| 1255 |
macsav(' '), needws = 0; |
| 1256 |
dp = readid(c); |
| 1257 |
if (type == OBJCT) { |
| 1258 |
macstr(dp); |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
if (vararg && strcmp((char *)dp, (char *)vararg) == 0) { |
| 1262 |
macsav(WARN); |
| 1263 |
macsav(wascon ? GCCARG : C99ARG); |
| 1264 |
break; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
if ((i = findarg(dp, ab, arg, narg)) < 0) { |
| 1269 |
macstr(dp); |
| 1270 |
break; |
| 1271 |
} |
| 1272 |
macsav(WARN); |
| 1273 |
macsav(i); |
| 1274 |
break; |
| 1275 |
|
| 1276 |
case 0: |
| 1277 |
goto bad; |
| 1278 |
|
| 1279 |
default: |
| 1280 |
if (needws) |
| 1281 |
macsav(' '), needws = 0; |
| 1282 |
macsav(c); |
| 1283 |
break; |
| 1284 |
} |
| 1285 |
wascon = 0; |
| 1286 |
c = cinput(); |
| 1287 |
} |
| 1288 |
cunput(c); |
| 1289 |
|
| 1290 |
|
| 1291 |
Cflag = oCflag; |
| 1292 |
|
| 1293 |
macsav(0); |
| 1294 |
if (vararg) |
| 1295 |
type = VARG; |
| 1296 |
|
| 1297 |
if (macget(begpos) == CONC) |
| 1298 |
goto bad; |
| 1299 |
|
| 1300 |
if (redef && ifiles->idx != SYSINC) { |
| 1301 |
if (cmprepl(np->valoff, begpos) || |
| 1302 |
np->type != type || np->narg != narg) { |
| 1303 |
np->valoff = begpos; |
| 1304 |
warning("%s redefined (previously defined at \"%s\" line %d)", |
| 1305 |
np->namep, np->file, np->line); |
| 1306 |
} |
| 1307 |
} else |
| 1308 |
np->valoff = begpos; |
| 1309 |
np->type = type; |
| 1310 |
np->narg = narg; |
| 1311 |
np->wraps = VALBUF(begpos) != lckmacbuf; |
| 1312 |
|
| 1313 |
#ifdef PCC_DEBUG |
| 1314 |
if (dflag) { |
| 1315 |
printf("!define %s: ", np->namep); |
| 1316 |
if (type == OBJCT) |
| 1317 |
printf("[object]"); |
| 1318 |
else if (type == VARG) |
| 1319 |
printf("[VARG%d]", narg); |
| 1320 |
else |
| 1321 |
printf("[%d]", narg); |
| 1322 |
putchar('\''); |
| 1323 |
prrep(np->valoff); |
| 1324 |
printf("\'\n"); |
| 1325 |
printf("%s: link %d off %d\n", np->namep, VALBUF(np->valoff), VALPTR(np->valoff)); |
| 1326 |
} |
| 1327 |
#endif |
| 1328 |
bufree(ab); |
| 1329 |
return; |
| 1330 |
|
| 1331 |
bad: error("bad #define"); |
| 1332 |
} |
| 1333 |
|
| 1334 |
void |
| 1335 |
warning(const char *fmt, ...) |
| 1336 |
{ |
| 1337 |
va_list ap; |
| 1338 |
|
| 1339 |
if (ifiles != NULL) |
| 1340 |
fprintf(stderr, "%s:%d: warning: ", |
| 1341 |
ifiles->fname, ifiles->lineno); |
| 1342 |
|
| 1343 |
va_start(ap,fmt); |
| 1344 |
vfprintf(stderr, fmt, ap); |
| 1345 |
va_end(ap); |
| 1346 |
fputc('\n', stderr); |
| 1347 |
|
| 1348 |
warnings++; |
| 1349 |
} |
| 1350 |
|
| 1351 |
void |
| 1352 |
error(const char *fmt, ...) |
| 1353 |
{ |
| 1354 |
va_list ap; |
| 1355 |
|
| 1356 |
write(1, pbbeg, pbinp-pbbeg); |
| 1357 |
pbinp = pbbeg; |
| 1358 |
if (ifiles != NULL) |
| 1359 |
fprintf(stderr, "%s:%d: error: ", |
| 1360 |
ifiles->fname, ifiles->lineno); |
| 1361 |
|
| 1362 |
va_start(ap, fmt); |
| 1363 |
vfprintf(stderr, fmt, ap); |
| 1364 |
va_end(ap); |
| 1365 |
fputc('\n', stderr); |
| 1366 |
exit(1); |
| 1367 |
} |
| 1368 |
|
| 1369 |
static int |
| 1370 |
pragwin(register struct iobuf *ib) |
| 1371 |
{ |
| 1372 |
return ib ? ib->buf[ib->cptr++] : cinput(); |
| 1373 |
} |
| 1374 |
|
| 1375 |
static int |
| 1376 |
skipws(struct iobuf *ib) |
| 1377 |
{ |
| 1378 |
register int t; |
| 1379 |
|
| 1380 |
while ((t = pragwin(ib)) == ' ' || t == '\t') |
| 1381 |
; |
| 1382 |
return t; |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
static void |
| 1390 |
pragoper(register struct iobuf *ib) |
| 1391 |
{ |
| 1392 |
register int t; |
| 1393 |
|
| 1394 |
if (skipws(ib) != '(' || ((t = skipws(ib)) != '\"' && t != 'L')) |
| 1395 |
goto err; |
| 1396 |
if (t == 'L' && (t = pragwin(ib)) != '\"') |
| 1397 |
goto err; |
| 1398 |
putstr((usch *)"\n#pragma "); |
| 1399 |
while ((t = pragwin(ib)) != '\"') { |
| 1400 |
if (t == BLKID || t == BLKID2) { |
| 1401 |
pragwin(ib); |
| 1402 |
if (t == BLKID2) |
| 1403 |
pragwin(ib); |
| 1404 |
continue; |
| 1405 |
} |
| 1406 |
if (t == '\"') |
| 1407 |
continue; |
| 1408 |
if (t == '\\') { |
| 1409 |
if ((t = pragwin(ib)) != '\"' && t != '\\') |
| 1410 |
putch('\\'); |
| 1411 |
} |
| 1412 |
putch(t); |
| 1413 |
} |
| 1414 |
prtline(1); |
| 1415 |
if (skipws(ib) == ')') |
| 1416 |
return; |
| 1417 |
|
| 1418 |
err: error("_Pragma() syntax error"); |
| 1419 |
} |
| 1420 |
|
| 1421 |
#ifdef PCC_DEBUG |
| 1422 |
static void |
| 1423 |
prblocker(char *s, int id) |
| 1424 |
{ |
| 1425 |
printf("%s (blocker): ", s); |
| 1426 |
|
| 1427 |
for (; id; id = blokx[BLKBUF(id)][BLKPTR(id)].nidx) |
| 1428 |
printf("%s ", blokx[BLKBUF(id)][BLKPTR(id)].sp->namep); |
| 1429 |
printf("\n"); |
| 1430 |
|
| 1431 |
} |
| 1432 |
#else |
| 1433 |
#define prblocker(x,y) |
| 1434 |
#endif |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
static int |
| 1440 |
expok(struct symtab *sp, register int id) |
| 1441 |
{ |
| 1442 |
|
| 1443 |
if (id == 0) |
| 1444 |
return 1; |
| 1445 |
#ifdef PCC_DEBUG |
| 1446 |
if (dflag) |
| 1447 |
prblocker("expok", id); |
| 1448 |
#endif |
| 1449 |
for (; id; id = blokx[BLKBUF(id)][BLKPTR(id)].nidx) { |
| 1450 |
if (blokx[BLKBUF(id)][BLKPTR(id)].sp == sp) |
| 1451 |
return 0; |
| 1452 |
} |
| 1453 |
return 1; |
| 1454 |
} |
| 1455 |
|
| 1456 |
#define expokb(s,l) expok(s,l) |
| 1457 |
|
| 1458 |
static int |
| 1459 |
blkget(struct symtab *sp, int id) |
| 1460 |
{ |
| 1461 |
register int upper = BLKBUF(blkidp); |
| 1462 |
register int off = BLKPTR(blkidp); |
| 1463 |
|
| 1464 |
if (upper == L2MAX) |
| 1465 |
error("too complex macro"); |
| 1466 |
if (blokx[upper] == NULL) |
| 1467 |
blokx[upper] = xmalloc(CPPBUF); |
| 1468 |
blokx[upper][off].sp = sp; |
| 1469 |
blokx[upper][off].nidx = id; |
| 1470 |
return blkidp++; |
| 1471 |
} |
| 1472 |
|
| 1473 |
static int |
| 1474 |
mergeadd(register int l, register int m) |
| 1475 |
{ |
| 1476 |
|
| 1477 |
DPRINT(("mergeadd: %d %d\n", l, m)); |
| 1478 |
#ifdef PCC_DEBUG |
| 1479 |
if (dflag > 1) { |
| 1480 |
prblocker("mergeadd", l); |
| 1481 |
if (m) prblocker("mergeadd", m); |
| 1482 |
} |
| 1483 |
#endif |
| 1484 |
if (l == 0) |
| 1485 |
return m; |
| 1486 |
if (m == 0 || l == m) |
| 1487 |
return l; |
| 1488 |
|
| 1489 |
for (; m; m = blokx[BLKBUF(m)][BLKPTR(m)].nidx) |
| 1490 |
l = blkget(blokx[BLKBUF(m)][BLKPTR(m)].sp, l); |
| 1491 |
|
| 1492 |
DPRINT(("mergeadd return: %d ", l)); |
| 1493 |
#ifdef PCC_DEBUG |
| 1494 |
if (dflag) |
| 1495 |
prblocker("mergeadd", l); |
| 1496 |
#endif |
| 1497 |
return l; |
| 1498 |
} |
| 1499 |
|
| 1500 |
static void |
| 1501 |
storeblk(register int l, register struct iobuf *ob) |
| 1502 |
{ |
| 1503 |
DPRINT(("storeblk: %d\n", l)); |
| 1504 |
if (l == 0) |
| 1505 |
return; |
| 1506 |
if (l > 255) { |
| 1507 |
putob(ob, BLKID2); |
| 1508 |
putob(ob, l >> 8); |
| 1509 |
} else |
| 1510 |
putob(ob, BLKID); |
| 1511 |
putob(ob, l & 255); |
| 1512 |
} |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
static struct iobuf * |
| 1518 |
unfname(void) |
| 1519 |
{ |
| 1520 |
register struct iobuf *ob = getobuf(BNORMAL); |
| 1521 |
register const usch *bp = ifiles->fname; |
| 1522 |
|
| 1523 |
putob(ob, '\"'); |
| 1524 |
for (; *bp; bp++) { |
| 1525 |
if (*bp == '\"' || *bp == '\'' || *bp == '\\') |
| 1526 |
putob(ob, '\\'); |
| 1527 |
putob(ob, *bp); |
| 1528 |
} |
| 1529 |
putob(ob, '\"'); |
| 1530 |
return ob; |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
static void |
| 1538 |
fstrnum(struct iobuf *ib, register struct iobuf *ob) |
| 1539 |
{ |
| 1540 |
register usch *s = ib->buf+ib->cptr; |
| 1541 |
register int c2; |
| 1542 |
|
| 1543 |
if (*s == '.') { |
| 1544 |
|
| 1545 |
putob(ob, *s++); |
| 1546 |
} |
| 1547 |
for (;;) { |
| 1548 |
putob(ob, *s++); |
| 1549 |
if ((c2 = (*s & 0337)) == 'E' || c2 == 'P') { |
| 1550 |
if (s[1] != '-' && s[1] != '+') |
| 1551 |
break; |
| 1552 |
putob(ob, *s++); |
| 1553 |
} else if ((*s != '.') && ((ISID(*s)) == 0)) |
| 1554 |
break; |
| 1555 |
} |
| 1556 |
ib->cptr = (int)(s - ib->buf); |
| 1557 |
} |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
static void |
| 1564 |
fstrstr(struct iobuf *ib, struct iobuf *ob) |
| 1565 |
{ |
| 1566 |
register usch *p, *q; |
| 1567 |
register int ch; |
| 1568 |
|
| 1569 |
q = p = ib ? ib->buf+ib->cptr : inp; |
| 1570 |
if (*p == 'L' || (*p|040) == 'u') |
| 1571 |
p++; |
| 1572 |
if (*p == '8') |
| 1573 |
p++; |
| 1574 |
ch = *p++; |
| 1575 |
for (;;) { |
| 1576 |
while (ISESTR(*p++) == 0) |
| 1577 |
; |
| 1578 |
if (*--p == 0 && ib == NULL) { |
| 1579 |
|
| 1580 |
strtobuf(inp, ob); |
| 1581 |
inp = p; |
| 1582 |
inpbuf(); |
| 1583 |
q = p = inp; |
| 1584 |
p--; |
| 1585 |
} else if (*p == '\\') { |
| 1586 |
p++; |
| 1587 |
} else if (*p == '\n' && ib == NULL) { |
| 1588 |
warning("unterminated literal"); |
| 1589 |
p--; |
| 1590 |
break; |
| 1591 |
} else if (ch == *p) |
| 1592 |
break; |
| 1593 |
p++; |
| 1594 |
} |
| 1595 |
++p; |
| 1596 |
while (q < p) |
| 1597 |
putob(ob, *q++); |
| 1598 |
if (ib == 0) inp = p; else ib->cptr = p - ib->buf; |
| 1599 |
} |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
static void |
| 1605 |
fcmnt(struct iobuf *ib, register struct iobuf *ob) |
| 1606 |
{ |
| 1607 |
register usch *s = ib->buf+ib->cptr; |
| 1608 |
|
| 1609 |
putob(ob, *s++); |
| 1610 |
putob(ob, *s++); |
| 1611 |
for (;;s++) { |
| 1612 |
putob(ob, *s); |
| 1613 |
if (s[-1] == '*' && *s == '/') |
| 1614 |
break; |
| 1615 |
} |
| 1616 |
ib->cptr = (int)(s - ib->buf + 1); |
| 1617 |
} |
| 1618 |
|
| 1619 |
static int |
| 1620 |
getyp(register usch *s) |
| 1621 |
{ |
| 1622 |
|
| 1623 |
if (ISID0(*s)) return IDENT; |
| 1624 |
if ((*s == 'L' || *s == 'U' || *s == 'u') && |
| 1625 |
(s[1] == '\'' || s[1] == '\"')) return STRING; |
| 1626 |
if (s[0] == 'u' && s[1] == '8' && s[2] == '\"') return STRING; |
| 1627 |
if (s[0] == '\'' || s[0] == '\"') return STRING; |
| 1628 |
if (ISDIGIT(*s)) return NUMBER; |
| 1629 |
if (*s == '.' && (ISDIGIT(s[1]))) return NUMBER; |
| 1630 |
if (*s == '/' && (s[1] == '/' || s[1] == '*')) return CMNT; |
| 1631 |
return *s; |
| 1632 |
|
| 1633 |
} |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
static struct symtab * |
| 1642 |
loopover(register struct iobuf *ib, register struct iobuf *ob) |
| 1643 |
{ |
| 1644 |
struct iobuf *xb, *xob; |
| 1645 |
struct symtab *sp; |
| 1646 |
register usch *cp; |
| 1647 |
int l, c, t, cn; |
| 1648 |
|
| 1649 |
ib->cptr = 0; |
| 1650 |
#ifdef PCC_DEBUG |
| 1651 |
if (dflag) { |
| 1652 |
printf("loopover: '"); |
| 1653 |
prline(ib->buf+ib->cptr); |
| 1654 |
printf("'\n"); |
| 1655 |
} |
| 1656 |
#endif |
| 1657 |
|
| 1658 |
xb = getobuf(BNORMAL); |
| 1659 |
while ((c = ib->buf[ib->cptr])) { |
| 1660 |
switch (t = getyp(ib->buf+ib->cptr)) { |
| 1661 |
case CMNT: |
| 1662 |
fcmnt(ib, ob); |
| 1663 |
continue; |
| 1664 |
case NUMBER: |
| 1665 |
fstrnum(ib, ob); |
| 1666 |
continue; |
| 1667 |
case STRING: |
| 1668 |
xb->cptr = 0; |
| 1669 |
fstrstr(ib, xb); |
| 1670 |
xb->buf[xb->cptr] = 0; |
| 1671 |
for (cp = xb->buf; *cp; cp++) { |
| 1672 |
if (*cp <= BLKID2 && *cp > 0) { |
| 1673 |
if (*cp == BLKID) |
| 1674 |
cp++; |
| 1675 |
if (*cp == BLKID2) |
| 1676 |
cp++, cp++; |
| 1677 |
continue; |
| 1678 |
} |
| 1679 |
putob(ob, *cp); |
| 1680 |
} |
| 1681 |
continue; |
| 1682 |
case BLKID: |
| 1683 |
case BLKID2: |
| 1684 |
l = (unsigned char)ib->buf[++ib->cptr]; |
| 1685 |
if (t == BLKID2) |
| 1686 |
l = (l << 8) | (unsigned char)ib->buf[++ib->cptr]; |
| 1687 |
ib->cptr++; |
| 1688 |
|
| 1689 |
case IDENT: |
| 1690 |
if (t == IDENT) |
| 1691 |
l = 0; |
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
for (cn = ib->cptr; |
| 1702 |
ISID(ib->buf[ib->cptr]); ib->cptr++) |
| 1703 |
; |
| 1704 |
if ((sp = lookup(cn+ib->buf, FIND)) == NULL) { |
| 1705 |
sstr: for (; cn < ib->cptr; cn++) |
| 1706 |
putob(ob, ib->buf[cn]); |
| 1707 |
continue; |
| 1708 |
} |
| 1709 |
if (expok(sp, l) == 0) { |
| 1710 |
|
| 1711 |
goto sstr; |
| 1712 |
} else { |
| 1713 |
if (sp->type != OBJCT) { |
| 1714 |
cn = ib->cptr; |
| 1715 |
while (ISWS(ib->buf[ib->cptr])) |
| 1716 |
ib->cptr++; |
| 1717 |
if (ib->buf[ib->cptr] == 0) { |
| 1718 |
bufree(xb); |
| 1719 |
return sp; |
| 1720 |
} |
| 1721 |
ib->cptr = cn; |
| 1722 |
} |
| 1723 |
newmac: if ((xob = submac(sp, 1, ib, 0)) == NULL) { |
| 1724 |
strtobuf((usch *)sp->namep, ob); |
| 1725 |
} else { |
| 1726 |
sp = loopover(xob, ob); |
| 1727 |
bufree(xob); |
| 1728 |
if (sp != NULL) |
| 1729 |
goto newmac; |
| 1730 |
} |
| 1731 |
} |
| 1732 |
continue; |
| 1733 |
default: |
| 1734 |
putob(ob, c); |
| 1735 |
} |
| 1736 |
|
| 1737 |
ib->cptr++; |
| 1738 |
} |
| 1739 |
|
| 1740 |
bufree(xb); |
| 1741 |
DPRINT(("loopover return 0\n")); |
| 1742 |
return 0; |
| 1743 |
} |
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
struct iobuf * |
| 1753 |
kfind(struct symtab *sp) |
| 1754 |
{ |
| 1755 |
register struct iobuf *ib, *ob, *outb, *ab; |
| 1756 |
const usch *argary[MAXARGS+1]; |
| 1757 |
int c, n = 0; |
| 1758 |
int l; |
| 1759 |
|
| 1760 |
blkidp = 1; |
| 1761 |
outb = NULL; |
| 1762 |
DPRINT(("%d:enter kfind(%s)\n",0,sp->namep)); |
| 1763 |
switch ((unsigned int)sp->type) { |
| 1764 |
case FILLOC: |
| 1765 |
ob = unfname(); |
| 1766 |
return ob; |
| 1767 |
|
| 1768 |
case LINLOC: |
| 1769 |
return bsheap(NULL, "%d", ifiles->lineno); |
| 1770 |
|
| 1771 |
case PRAGLOC: |
| 1772 |
pragoper(NULL); |
| 1773 |
return getobuf(BNORMAL); |
| 1774 |
|
| 1775 |
case DEFLOC: |
| 1776 |
case OBJCT: |
| 1777 |
l = blkget(sp, 0); |
| 1778 |
ib = macrepbuf(sp); |
| 1779 |
ob = getobuf(BNORMAL); |
| 1780 |
ob = exparg(1, ib, ob, l); |
| 1781 |
bufree(ib); |
| 1782 |
break; |
| 1783 |
|
| 1784 |
case CTRLOC: |
| 1785 |
return bsheap(NULL, "%d", counter++); |
| 1786 |
|
| 1787 |
default: |
| 1788 |
|
| 1789 |
while (ISWSNL(c = cinput())) |
| 1790 |
if (c == '\n') |
| 1791 |
n++; |
| 1792 |
if (c != '(') { |
| 1793 |
putstr(sp->namep); |
| 1794 |
if (n == 0) |
| 1795 |
putch(' '); |
| 1796 |
else for (ifiles->lineno += n; n; n--) |
| 1797 |
putch('\n'); |
| 1798 |
cunput(c); |
| 1799 |
return 0; |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
again: if ((ab = readargs(NULL, sp, argary)) == 0) |
| 1804 |
error("readargs"); |
| 1805 |
|
| 1806 |
l = blkget(sp, 0); |
| 1807 |
ib = subarg(sp, argary, 1, l); |
| 1808 |
bufree(ab); |
| 1809 |
ob = getobuf(BNORMAL); |
| 1810 |
ob = exparg(1, ib, ob, l); |
| 1811 |
bufree(ib); |
| 1812 |
break; |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
putob(ob, 0); |
| 1820 |
|
| 1821 |
if (outb == NULL) |
| 1822 |
outb = getobuf(BNORMAL); |
| 1823 |
|
| 1824 |
if ((sp = loopover(ob, outb))) { |
| 1825 |
|
| 1826 |
while (ISWSNL(c = cinput())) |
| 1827 |
if (c == '\n') |
| 1828 |
n++; |
| 1829 |
if (c == '(') { |
| 1830 |
bufree(ob); |
| 1831 |
goto again; |
| 1832 |
} |
| 1833 |
cunput(c); |
| 1834 |
strtobuf((usch *)sp->namep, outb); |
| 1835 |
} |
| 1836 |
bufree(ob); |
| 1837 |
|
| 1838 |
for (ifiles->lineno += n; n; n--) |
| 1839 |
putob(outb, '\n'); |
| 1840 |
if (nbufused != 1) |
| 1841 |
error("lost buffer"); |
| 1842 |
return outb; |
| 1843 |
} |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
struct iobuf * |
| 1852 |
submac(struct symtab *sp, int lvl, register struct iobuf *ib, int l) |
| 1853 |
{ |
| 1854 |
int bl; |
| 1855 |
register struct iobuf *ob, *ab; |
| 1856 |
const usch *argary[MAXARGS+1]; |
| 1857 |
int cn; |
| 1858 |
|
| 1859 |
DPRINT(("%d:submac: trying '%s'\n", lvl, sp->namep)); |
| 1860 |
switch ((unsigned int)sp->type) { |
| 1861 |
case FILLOC: |
| 1862 |
ob = unfname(); |
| 1863 |
break; |
| 1864 |
case LINLOC: |
| 1865 |
ob = bsheap(NULL, "%d", ifiles->lineno); |
| 1866 |
break; |
| 1867 |
case PRAGLOC: |
| 1868 |
pragoper(ib); |
| 1869 |
ob = getobuf(BNORMAL); |
| 1870 |
break; |
| 1871 |
case DEFLOC: |
| 1872 |
case OBJCT: |
| 1873 |
bl = blkget(sp, l); |
| 1874 |
ib = macrepbuf(sp); |
| 1875 |
ob = getobuf(BNORMAL); |
| 1876 |
DPRINT(("%d:submac: calling exparg\n", lvl)); |
| 1877 |
ob = exparg(lvl+1, ib, ob, bl); |
| 1878 |
bufree(ib); |
| 1879 |
DPRINT(("%d:submac: return exparg\n", lvl)); |
| 1880 |
break; |
| 1881 |
case CTRLOC: |
| 1882 |
ob = bsheap(NULL, "%d", counter++); |
| 1883 |
break; |
| 1884 |
default: |
| 1885 |
cn = ib->cptr; |
| 1886 |
while (ISWSNL(ib->buf[ib->cptr])) |
| 1887 |
ib->cptr++; |
| 1888 |
if (ib->buf[ib->cptr] != '(') { |
| 1889 |
ib->cptr = cn; |
| 1890 |
return 0; |
| 1891 |
} |
| 1892 |
cn = ib->cptr++; |
| 1893 |
if ((ab = readargs(ib, sp, argary)) == 0) { |
| 1894 |
|
| 1895 |
ib->cptr = cn; |
| 1896 |
return 0; |
| 1897 |
} |
| 1898 |
bl = blkget(sp, l); |
| 1899 |
ib = subarg(sp, argary, lvl+1, bl); |
| 1900 |
bufree(ab); |
| 1901 |
|
| 1902 |
ob = getobuf(BNORMAL); |
| 1903 |
DPRINT(("%d:submac(: calling exparg\n", lvl)); |
| 1904 |
ob = exparg(lvl+1, ib, ob, bl); |
| 1905 |
bufree(ib); |
| 1906 |
DPRINT(("%d:submac(: return exparg\n", lvl)); |
| 1907 |
break; |
| 1908 |
} |
| 1909 |
putob(ob, 0); |
| 1910 |
ob->cptr--; |
| 1911 |
|
| 1912 |
return ob; |
| 1913 |
} |
| 1914 |
|
| 1915 |
static int |
| 1916 |
skpws(void) |
| 1917 |
{ |
| 1918 |
register int c; |
| 1919 |
while ((c = cinput()) == ' ' || c == '\t') |
| 1920 |
; |
| 1921 |
return c; |
| 1922 |
} |
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
struct iobuf * |
| 1929 |
readargs(register struct iobuf *in, struct symtab *sp, const usch **args) |
| 1930 |
{ |
| 1931 |
usch *opbeg, *opend, *oinp; |
| 1932 |
register struct iobuf *ab; |
| 1933 |
register int c, infil, i, j, plev, narg, ellips = 0; |
| 1934 |
int argary[MAXARGS+1]; |
| 1935 |
|
| 1936 |
DPRINT(("readargs: in %p\n", in)); |
| 1937 |
narg = sp->narg; |
| 1938 |
ellips = sp->type == VARG; |
| 1939 |
|
| 1940 |
#ifdef __GNUC__ |
| 1941 |
opbeg = opend = oinp = 0; |
| 1942 |
#endif |
| 1943 |
|
| 1944 |
infil = ifiles->infil; |
| 1945 |
if (in) { |
| 1946 |
oinp = inp; |
| 1947 |
opend = pend; |
| 1948 |
opbeg = pbeg; |
| 1949 |
ifiles->infil = -1; |
| 1950 |
pbeg = in->buf; |
| 1951 |
inp = pbeg + in->cptr; |
| 1952 |
pend = pbeg + in->bsz; |
| 1953 |
} |
| 1954 |
|
| 1955 |
#ifdef PCC_DEBUG |
| 1956 |
if (dflag > 1) { |
| 1957 |
printf("narg %d varg %d: ", narg, ellips); |
| 1958 |
prrep(sp->valoff); |
| 1959 |
printf("\n"); |
| 1960 |
} |
| 1961 |
#endif |
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
ab = getobuf(BNORMAL); |
| 1967 |
c = '('; |
| 1968 |
for (i = 0; i < narg && c != ')'; i++) { |
| 1969 |
argary[i] = ab->cptr; |
| 1970 |
plev = 0; |
| 1971 |
|
| 1972 |
c = skpws(); |
| 1973 |
for (;;) { |
| 1974 |
if (plev == 0 && (c == ')' || c == ',')) |
| 1975 |
break; |
| 1976 |
if (c == '(') plev++; |
| 1977 |
if (c == ')') plev--; |
| 1978 |
switch (c) { |
| 1979 |
case 0: |
| 1980 |
if (in) { |
| 1981 |
in->cptr = inp - pbeg; |
| 1982 |
pend = opend; |
| 1983 |
inp = oinp; |
| 1984 |
pbeg = opbeg; |
| 1985 |
in = NULL; |
| 1986 |
} else |
| 1987 |
error("eof in macro"); |
| 1988 |
break; |
| 1989 |
case BLKID2: |
| 1990 |
case BLKID: |
| 1991 |
putob(ab, c); |
| 1992 |
putob(ab, *inp++); |
| 1993 |
if (c == BLKID2) |
| 1994 |
putob(ab, *inp++); |
| 1995 |
break; |
| 1996 |
case '/': |
| 1997 |
if ((c = cinput()) == '*' || c == '/') |
| 1998 |
Ccmnt2(ab, c); |
| 1999 |
else { |
| 2000 |
putob(ab, '/'); |
| 2001 |
cunput(c); |
| 2002 |
} |
| 2003 |
break; |
| 2004 |
case '\n': |
| 2005 |
escln++; |
| 2006 |
c = skpws(); |
| 2007 |
if (c == '#') { |
| 2008 |
ppdir(); |
| 2009 |
} else { |
| 2010 |
|
| 2011 |
if (argary[i] != ab->cptr) |
| 2012 |
putob(ab, ' '); |
| 2013 |
continue; |
| 2014 |
} |
| 2015 |
break; |
| 2016 |
case '\"': |
| 2017 |
case '\'': |
| 2018 |
*--inp = c; |
| 2019 |
fstrstr(NULL, ab); |
| 2020 |
break; |
| 2021 |
default: |
| 2022 |
if (ISID0(c)) { |
| 2023 |
bufid(c, ab); |
| 2024 |
} else |
| 2025 |
putob(ab, c); |
| 2026 |
break; |
| 2027 |
} |
| 2028 |
c = cinput(); |
| 2029 |
} |
| 2030 |
|
| 2031 |
while (argary[i] < ab->cptr && ISWSNL(ab->buf[ab->cptr-1])) |
| 2032 |
ab->cptr--; |
| 2033 |
putob(ab, '\0'); |
| 2034 |
#ifdef PCC_DEBUG |
| 2035 |
if (dflag) { |
| 2036 |
printf("readargs: save arg %d '", i); |
| 2037 |
prline(ab->buf+argary[i]); |
| 2038 |
printf("'\n"); |
| 2039 |
} |
| 2040 |
#endif |
| 2041 |
} |
| 2042 |
|
| 2043 |
|
| 2044 |
argary[i] = ab->cptr; |
| 2045 |
putob(ab, 0); |
| 2046 |
ab->cptr--; |
| 2047 |
if (ellips && c != ')') { |
| 2048 |
plev = 0; |
| 2049 |
c = skpws(); |
| 2050 |
for (;;) { |
| 2051 |
if ((plev == 0 && c == ')') || c == 0) |
| 2052 |
break; |
| 2053 |
if (c == '(') plev++; |
| 2054 |
if (c == ')') plev--; |
| 2055 |
if (c == '\"' || c == '\'') { |
| 2056 |
*--inp = c; |
| 2057 |
fstrstr(NULL, ab); |
| 2058 |
} else |
| 2059 |
putob(ab, c); |
| 2060 |
if ((c = cinput()) == '\n') |
| 2061 |
escln++, c = ' '; |
| 2062 |
} |
| 2063 |
if (c == 0) |
| 2064 |
error("unterminated macro invocation"); |
| 2065 |
while (argary[i] < ab->cptr && ISWSNL(ab->buf[ab->cptr-1])) |
| 2066 |
ab->cptr--; |
| 2067 |
putob(ab, '\0'); |
| 2068 |
#ifdef PCC_DEBUG |
| 2069 |
if (dflag) { |
| 2070 |
printf("readargs: vararg arg %d '", i); |
| 2071 |
prline(ab->buf+argary[i]); |
| 2072 |
printf("'\n"); |
| 2073 |
} |
| 2074 |
#endif |
| 2075 |
} |
| 2076 |
if (ellips) |
| 2077 |
i++; |
| 2078 |
if (narg == 0 && ellips == 0) |
| 2079 |
c = skpws(); |
| 2080 |
|
| 2081 |
if (c != ')' || (i != narg && ellips == 0) || (i < narg && ellips == 1)) |
| 2082 |
error("wrong arg count"); |
| 2083 |
for (j = 0; j < i; j++) |
| 2084 |
args[j] = ab->buf + argary[j]; |
| 2085 |
|
| 2086 |
ifiles->infil = infil; |
| 2087 |
if (in) { |
| 2088 |
in->cptr = inp - pbeg; |
| 2089 |
inp = oinp; |
| 2090 |
pend = opend; |
| 2091 |
pbeg = opbeg; |
| 2092 |
} |
| 2093 |
return ab; |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
static void |
| 2100 |
escstr(register const usch *bp, register struct iobuf *ob) |
| 2101 |
{ |
| 2102 |
register int instr = 0; |
| 2103 |
|
| 2104 |
while (*bp) { |
| 2105 |
if (!instr && ISWS(*bp)) { |
| 2106 |
while (ISWS(*bp)) |
| 2107 |
bp++; |
| 2108 |
putob(ob, ' '); |
| 2109 |
} |
| 2110 |
|
| 2111 |
if (*bp == '\'' || *bp == '"') { |
| 2112 |
instr ^= 1; |
| 2113 |
if (*bp == '"') |
| 2114 |
putob(ob, '\\'); |
| 2115 |
} |
| 2116 |
if (instr && *bp == '\\') { |
| 2117 |
putob(ob, *bp); |
| 2118 |
if (bp[1] == '\"') |
| 2119 |
putob(ob, *bp), putob(ob, *bp++); |
| 2120 |
} |
| 2121 |
putob(ob, *bp); |
| 2122 |
bp++; |
| 2123 |
} |
| 2124 |
} |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
struct iobuf * |
| 2133 |
subarg(struct symtab *nl, const usch **args, int lvl, int l) |
| 2134 |
{ |
| 2135 |
int lw; |
| 2136 |
register struct iobuf *ob, *cb, *nb, *vb; |
| 2137 |
int narg, snuff, c2; |
| 2138 |
const usch *sp, *bp, *ap, *vp; |
| 2139 |
|
| 2140 |
DPRINT(("%d:subarg '%s'\n", lvl, nl->namep)); |
| 2141 |
ob = getobuf(BNORMAL); |
| 2142 |
vb = macrepbuf(nl); |
| 2143 |
vp = vb->buf; |
| 2144 |
narg = nl->narg; |
| 2145 |
|
| 2146 |
sp = vp; |
| 2147 |
snuff = 0; |
| 2148 |
#ifdef PCC_DEBUG |
| 2149 |
if (dflag>1) { |
| 2150 |
printf("%d:subarg ARGlist for %s: '", lvl, nl->namep); |
| 2151 |
prrep(nl->valoff); |
| 2152 |
printf("'\n"); |
| 2153 |
prblocker("subarg", l); |
| 2154 |
} |
| 2155 |
#endif |
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
while (*sp) { |
| 2162 |
if (*sp == SNUFF) |
| 2163 |
putob(ob, '\"'), snuff ^= 1; |
| 2164 |
else if (*sp == CONC) |
| 2165 |
; |
| 2166 |
else if (*sp == WARN) { |
| 2167 |
|
| 2168 |
if (sp[1] == (usch)C99ARG) { |
| 2169 |
bp = ap = args[narg]; |
| 2170 |
sp++; |
| 2171 |
#ifdef GCC_COMPAT |
| 2172 |
} else if (sp[1] == (usch)GCCARG) { |
| 2173 |
|
| 2174 |
ap = args[narg]; |
| 2175 |
if (ap[0] == 0) |
| 2176 |
ap = (const usch *)"0"; |
| 2177 |
bp = ap; |
| 2178 |
sp++; |
| 2179 |
#endif |
| 2180 |
} else |
| 2181 |
bp = ap = args[(unsigned char)*++sp]; |
| 2182 |
#ifdef PCC_DEBUG |
| 2183 |
if (dflag>1){ |
| 2184 |
printf("%d:subarg GOTwarn; arglist '", lvl); |
| 2185 |
prline(bp); |
| 2186 |
printf("'\n"); |
| 2187 |
} |
| 2188 |
#endif |
| 2189 |
c2 = (sp-2 < vp ? 0 : sp[-2]); |
| 2190 |
if (c2 != CONC && !snuff && sp[1] != CONC) { |
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
lw = l ? blokx[BLKBUF(l)][BLKPTR(l)].nidx : 0; |
| 2199 |
nb = mkrobuf(bp); |
| 2200 |
DPRINT(("%d:subarg: calling exparg\n", lvl)); |
| 2201 |
do { |
| 2202 |
cb = nb; |
| 2203 |
cb->cptr = 0; |
| 2204 |
didexpand = 0; |
| 2205 |
nb = getobuf(BNORMAL); |
| 2206 |
nb = exparg(lvl+1, cb, nb, lw); |
| 2207 |
bufree(cb); |
| 2208 |
} while (didexpand); |
| 2209 |
DPRINT(("%d:subarg: return exparg\n", lvl)); |
| 2210 |
strtobuf(nb->buf, ob); |
| 2211 |
bufree(nb); |
| 2212 |
} else { |
| 2213 |
if (snuff) |
| 2214 |
escstr(bp, ob); |
| 2215 |
else |
| 2216 |
strtobuf(bp, ob); |
| 2217 |
} |
| 2218 |
} else if (ISID0(*sp)) { |
| 2219 |
if (lookup(sp, FIND)) |
| 2220 |
storeblk(l, ob); |
| 2221 |
while (ISID(*sp)) |
| 2222 |
putob(ob, *sp++); |
| 2223 |
sp--; |
| 2224 |
} else |
| 2225 |
putob(ob, *sp); |
| 2226 |
sp++; |
| 2227 |
} |
| 2228 |
putob(ob, 0); |
| 2229 |
ob->cptr = 0; |
| 2230 |
DPRINT(("%d:subarg retline %s\n", lvl, ob->buf)); |
| 2231 |
bufree(vb); |
| 2232 |
return ob; |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
struct iobuf * |
| 2242 |
exparg(int lvl, register struct iobuf *ib, register struct iobuf *ob, int l) |
| 2243 |
{ |
| 2244 |
struct iobuf *nob, *tb; |
| 2245 |
struct symtab *nl; |
| 2246 |
int c, m; |
| 2247 |
register usch *cp; |
| 2248 |
|
| 2249 |
DPRINT(("%d:exparg: entry ib %s\n", lvl, ib->buf+ib->cptr)); |
| 2250 |
#ifdef PCC_DEBUG |
| 2251 |
if (dflag > 1) { |
| 2252 |
printf("exparg entry: full "); |
| 2253 |
prline(ib->buf+ib->cptr); |
| 2254 |
printf("\n"); |
| 2255 |
prblocker("exparg", l); |
| 2256 |
} |
| 2257 |
#endif |
| 2258 |
|
| 2259 |
while ((c = getyp(ib->buf+ib->cptr)) != 0) { |
| 2260 |
switch (c) { |
| 2261 |
|
| 2262 |
case CMNT: |
| 2263 |
fcmnt(ib, ob); |
| 2264 |
break; |
| 2265 |
case NUMBER: |
| 2266 |
fstrnum(ib, ob); |
| 2267 |
break; |
| 2268 |
case STRING: |
| 2269 |
fstrstr(ib, ob); |
| 2270 |
break; |
| 2271 |
case BLKID2: |
| 2272 |
case BLKID: |
| 2273 |
m = (unsigned char)ib->buf[++ib->cptr]; |
| 2274 |
if (c == BLKID2) |
| 2275 |
m = (m << 8) | (unsigned char)ib->buf[++ib->cptr]; |
| 2276 |
ib->cptr++; |
| 2277 |
|
| 2278 |
case IDENT: |
| 2279 |
if (c == IDENT) |
| 2280 |
m = 0; |
| 2281 |
tb = getobuf(BNORMAL); |
| 2282 |
cp = ib->buf+ib->cptr; |
| 2283 |
for (; ISID(*cp) || *cp == BLKID || *cp == BLKID2; cp++) { |
| 2284 |
if (*cp == BLKID || *cp == BLKID2) { |
| 2285 |
|
| 2286 |
cp++; |
| 2287 |
if (cp[-1] == BLKID2) |
| 2288 |
cp++; |
| 2289 |
} else |
| 2290 |
putob(tb, *cp); |
| 2291 |
} |
| 2292 |
tb->buf[tb->cptr] = 0; |
| 2293 |
ib->cptr = (int)(cp - ib->buf); |
| 2294 |
|
| 2295 |
|
| 2296 |
if ((nl = lookup(tb->buf, FIND)) == NULL) { |
| 2297 |
buftobuf(tb, ob); |
| 2298 |
} else if (expokb(nl, l) && expok(nl, m) && |
| 2299 |
(nob = submac(nl, lvl+1, ib, l))) { |
| 2300 |
didexpand = 1; |
| 2301 |
if (nob->buf[0] == '-' || nob->buf[0] == '+') |
| 2302 |
putob(ob, ' '); |
| 2303 |
strtobuf(nob->buf, ob); |
| 2304 |
if (ob->cptr > 0 && |
| 2305 |
(ob->buf[ob->cptr-1] == '-' || |
| 2306 |
ob->buf[ob->cptr-1] == '+')) |
| 2307 |
putob(ob, ' '); |
| 2308 |
bufree(nob); |
| 2309 |
} else { |
| 2310 |
storeblk(mergeadd(l, m), ob); |
| 2311 |
buftobuf(tb, ob); |
| 2312 |
} |
| 2313 |
bufree(tb); |
| 2314 |
break; |
| 2315 |
|
| 2316 |
default: |
| 2317 |
PUTOB(ob, c); |
| 2318 |
ib->cptr++; |
| 2319 |
break; |
| 2320 |
} |
| 2321 |
} |
| 2322 |
putob(ob, 0); |
| 2323 |
ob->cptr--; |
| 2324 |
DPRINT(("%d:exparg return: ob %s\n", lvl, ob->buf)); |
| 2325 |
#ifdef PCC_DEBUG |
| 2326 |
if (dflag > 1) { |
| 2327 |
printf("%d:exparg: full ", lvl); |
| 2328 |
prline(ob->buf); |
| 2329 |
printf("\n"); |
| 2330 |
prblocker("exparg", l); |
| 2331 |
} |
| 2332 |
#endif |
| 2333 |
return ob; |
| 2334 |
} |
| 2335 |
|
| 2336 |
#ifdef PCC_DEBUG |
| 2337 |
|
| 2338 |
static void |
| 2339 |
blkprint(int l) |
| 2340 |
{ |
| 2341 |
printf("<BLKID%s(", l > 255 ? "2" : ""); |
| 2342 |
for (; l; l = blokx[BLKBUF(l)][BLKPTR(l)].nidx) |
| 2343 |
printf("%s ", blokx[BLKBUF(l)][BLKPTR(l)].sp->namep); |
| 2344 |
printf(")>"); |
| 2345 |
} |
| 2346 |
|
| 2347 |
static void |
| 2348 |
prrep(mvtyp ptr) |
| 2349 |
{ |
| 2350 |
int s; |
| 2351 |
|
| 2352 |
while ((s = macget(ptr++))) { |
| 2353 |
switch (s) { |
| 2354 |
case WARN: |
| 2355 |
s = macget(ptr++); |
| 2356 |
if (s == (usch)C99ARG) printf("<C99ARG>"); |
| 2357 |
else if (s == (usch)GCCARG) printf("<GCCARG>"); |
| 2358 |
else printf("<ARG(%d)>", s); |
| 2359 |
break; |
| 2360 |
case CONC: printf("<CONC>"); break; |
| 2361 |
case SNUFF: printf("<SNUFF>"); break; |
| 2362 |
case BLKID: blkprint(macget(ptr++)); break; |
| 2363 |
case BLKID2: blkprint(macget(ptr) << 8 | macget(ptr+1)); ptr += 2; break; |
| 2364 |
default: printf("%c", s); break; |
| 2365 |
} |
| 2366 |
} |
| 2367 |
} |
| 2368 |
|
| 2369 |
static void |
| 2370 |
prline(const usch *s) |
| 2371 |
{ |
| 2372 |
while (*s) { |
| 2373 |
switch (*s) { |
| 2374 |
case BLKID: blkprint((unsigned char)*++s); break; |
| 2375 |
case BLKID2: blkprint((unsigned char)s[1] << 8 | (unsigned char)s[2]); s += 2; break; |
| 2376 |
case WARN: printf("<WARN>"); break; |
| 2377 |
case CONC: printf("<CONC>"); break; |
| 2378 |
case SNUFF: printf("<SNUFF>"); break; |
| 2379 |
case '\n': printf("<NL>"); break; |
| 2380 |
default: |
| 2381 |
if ((unsigned char)*s > 0x7f) |
| 2382 |
printf("<0x%x>", *s); |
| 2383 |
else |
| 2384 |
printf("%c", *s); |
| 2385 |
break; |
| 2386 |
} |
| 2387 |
s++; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
#endif |
| 2391 |
|
| 2392 |
void |
| 2393 |
cntline(void) |
| 2394 |
{ |
| 2395 |
if (skpows < 10) |
| 2396 |
for (; skpows > 0; skpows--) { |
| 2397 |
if (pbinp == pbend) { |
| 2398 |
if (Mflag == 0) |
| 2399 |
(void)write(1, pbbeg, pbinp - pbbeg); |
| 2400 |
pbinp = pbbeg; |
| 2401 |
} |
| 2402 |
*pbinp++ = '\n'; |
| 2403 |
} |
| 2404 |
else |
| 2405 |
prtline(1); |
| 2406 |
skpows = 0; |
| 2407 |
} |
| 2408 |
|
| 2409 |
void |
| 2410 |
putch(register int ch) |
| 2411 |
{ |
| 2412 |
if (skpows) { |
| 2413 |
if (ch == '\n') |
| 2414 |
skpows++; |
| 2415 |
if (ISWSNL(ch)) |
| 2416 |
return; |
| 2417 |
cntline(); |
| 2418 |
} else if (ch == '\n' && tflag == 0) { |
| 2419 |
skpows = 1; |
| 2420 |
return; |
| 2421 |
} |
| 2422 |
if (pbinp == pbend) { |
| 2423 |
if (Mflag == 0) |
| 2424 |
(void)write(1, pbbeg, pbinp - pbbeg); |
| 2425 |
pbinp = pbbeg; |
| 2426 |
} |
| 2427 |
*pbinp++ = ch; |
| 2428 |
if (ch == '\n' && istty && Mflag == 0) |
| 2429 |
(void)write(1, pbbeg, pbinp - pbbeg), pbinp = pbbeg; |
| 2430 |
} |
| 2431 |
|
| 2432 |
void |
| 2433 |
putstr(const usch *s) |
| 2434 |
{ |
| 2435 |
if (skpows) |
| 2436 |
cntline(); |
| 2437 |
while (*s) { |
| 2438 |
if (pbinp == pbend) { |
| 2439 |
if (Mflag == 0) |
| 2440 |
(void)write(1, pbbeg, pbinp - pbbeg); |
| 2441 |
pbinp = pbbeg; |
| 2442 |
} |
| 2443 |
*pbinp++ = *s++; |
| 2444 |
} |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
static void |
| 2451 |
num2str(struct iobuf *ob, register int num) |
| 2452 |
{ |
| 2453 |
static usch buf[12]; |
| 2454 |
register usch *b = buf; |
| 2455 |
register int m = 0; |
| 2456 |
|
| 2457 |
if (num < 0) |
| 2458 |
num = -num, m = 1; |
| 2459 |
do { |
| 2460 |
*b++ = (usch)(num % 10 + '0'); |
| 2461 |
num /= 10; |
| 2462 |
} while (num); |
| 2463 |
if (m) |
| 2464 |
*b++ = '-'; |
| 2465 |
while (b > buf) |
| 2466 |
putob(ob, *--b); |
| 2467 |
} |
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
static void |
| 2474 |
vsheap(register struct iobuf *ob, register const char *fmt, va_list ap) |
| 2475 |
{ |
| 2476 |
for (; *fmt; fmt++) { |
| 2477 |
if (*fmt == '%') { |
| 2478 |
fmt++; |
| 2479 |
switch (*fmt) { |
| 2480 |
case 's': |
| 2481 |
strtobuf(va_arg(ap, usch *), ob); |
| 2482 |
break; |
| 2483 |
case 'd': |
| 2484 |
num2str(ob, va_arg(ap, int)); |
| 2485 |
break; |
| 2486 |
case 'c': |
| 2487 |
putob(ob, va_arg(ap, int)); |
| 2488 |
break; |
| 2489 |
default: |
| 2490 |
error("bad sheap"); |
| 2491 |
} |
| 2492 |
} else |
| 2493 |
putob(ob, *fmt); |
| 2494 |
} |
| 2495 |
putob(ob, 0); |
| 2496 |
ob->cptr--; |
| 2497 |
} |
| 2498 |
|
| 2499 |
struct iobuf * |
| 2500 |
bsheap(register struct iobuf *ob, const char *fmt, ...) |
| 2501 |
{ |
| 2502 |
va_list ap; |
| 2503 |
|
| 2504 |
if (ob == NULL) |
| 2505 |
ob = getobuf(BNORMAL); |
| 2506 |
|
| 2507 |
va_start(ap, fmt); |
| 2508 |
vsheap(ob, fmt, ap); |
| 2509 |
va_end(ap); |
| 2510 |
|
| 2511 |
return ob; |
| 2512 |
} |
| 2513 |
|
| 2514 |
static void |
| 2515 |
usage(void) |
| 2516 |
{ |
| 2517 |
error("Usage: cpp [-Cdt] [-Dvar=val] [-Uvar] [-Ipath] [-Spath]"); |
| 2518 |
} |
| 2519 |
|
| 2520 |
#ifdef notyet |
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
#endif |
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
struct tree { |
| 2538 |
int bitno; |
| 2539 |
struct tree *lr[2]; |
| 2540 |
}; |
| 2541 |
|
| 2542 |
#define BITNO(x) ((x) & ~(LEFT_IS_LEAF|RIGHT_IS_LEAF)) |
| 2543 |
#ifdef pdp11 |
| 2544 |
#define LEFT_IS_LEAF 0x8000 |
| 2545 |
#define RIGHT_IS_LEAF 0x4000 |
| 2546 |
#else |
| 2547 |
#define LEFT_IS_LEAF 0x80000000 |
| 2548 |
#define RIGHT_IS_LEAF 0x40000000 |
| 2549 |
#endif |
| 2550 |
#define IS_LEFT_LEAF(x) (((x) & LEFT_IS_LEAF) != 0) |
| 2551 |
#define IS_RIGHT_LEAF(x) (((x) & RIGHT_IS_LEAF) != 0) |
| 2552 |
#define P_BIT(key, bit) (key[bit >> 3] >> (bit & 7)) & 1 |
| 2553 |
#define CHECKBITS 8 |
| 2554 |
|
| 2555 |
static struct tree *sympole; |
| 2556 |
static int numsyms; |
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
static struct symtab * |
| 2562 |
getsymtab(const usch *str) |
| 2563 |
{ |
| 2564 |
register struct symtab *sp; |
| 2565 |
|
| 2566 |
sp = addblock(sizeof(*sp)); |
| 2567 |
|
| 2568 |
sp->namep = str; |
| 2569 |
sp->valoff = 0; |
| 2570 |
sp->file = ifiles ? ifiles->orgfn : (const usch *)"<initial>"; |
| 2571 |
sp->line = ifiles ? ifiles->lineno : 0; |
| 2572 |
return sp; |
| 2573 |
} |
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
struct symtab * |
| 2580 |
lookup(register const usch *key, int enterf) |
| 2581 |
{ |
| 2582 |
struct symtab *sp; |
| 2583 |
register struct tree *w, *new, *last; |
| 2584 |
int len, cix, bit, fbit, svbit, ix, bitno; |
| 2585 |
const usch *k, *m; |
| 2586 |
|
| 2587 |
|
| 2588 |
for (k = key, len = 0; ISID(*k); k++, len++) |
| 2589 |
; |
| 2590 |
|
| 2591 |
switch (numsyms) { |
| 2592 |
case 0: |
| 2593 |
if (enterf != ENTER) |
| 2594 |
return NULL; |
| 2595 |
sympole = (struct tree *)getsymtab(key); |
| 2596 |
numsyms++; |
| 2597 |
return (struct symtab *)sympole; |
| 2598 |
|
| 2599 |
case 1: |
| 2600 |
w = sympole; |
| 2601 |
svbit = 0; |
| 2602 |
break; |
| 2603 |
|
| 2604 |
default: |
| 2605 |
w = sympole; |
| 2606 |
bitno = len * CHECKBITS; |
| 2607 |
for (;;) { |
| 2608 |
bit = BITNO(w->bitno); |
| 2609 |
fbit = bit >= bitno ? 0 : P_BIT(key, bit); |
| 2610 |
svbit = fbit ? IS_RIGHT_LEAF(w->bitno) : |
| 2611 |
IS_LEFT_LEAF(w->bitno); |
| 2612 |
w = w->lr[fbit]; |
| 2613 |
if (svbit) |
| 2614 |
break; |
| 2615 |
} |
| 2616 |
} |
| 2617 |
|
| 2618 |
sp = (struct symtab *)w; |
| 2619 |
|
| 2620 |
m = sp->namep; |
| 2621 |
k = key; |
| 2622 |
|
| 2623 |
|
| 2624 |
for (cix = 0; *m && ISID(*k) && *m == *k; m++, k++, cix += CHECKBITS) |
| 2625 |
; |
| 2626 |
if (*m == 0 && ISID(*k) == 0) { |
| 2627 |
if (enterf != ENTER && sp->valoff == 0) |
| 2628 |
return NULL; |
| 2629 |
return sp; |
| 2630 |
} |
| 2631 |
|
| 2632 |
if (enterf != ENTER) |
| 2633 |
return NULL; |
| 2634 |
|
| 2635 |
ix = *m ^ *k; |
| 2636 |
while ((ix & 1) == 0) |
| 2637 |
ix >>= 1, cix++; |
| 2638 |
|
| 2639 |
|
| 2640 |
new = addblock(sizeof(*new)); |
| 2641 |
bit = P_BIT(key, cix); |
| 2642 |
new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF); |
| 2643 |
new->lr[bit] = (struct tree *)getsymtab(key); |
| 2644 |
|
| 2645 |
if (numsyms++ == 1) { |
| 2646 |
new->lr[!bit] = sympole; |
| 2647 |
new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF); |
| 2648 |
sympole = new; |
| 2649 |
return (struct symtab *)new->lr[bit]; |
| 2650 |
} |
| 2651 |
|
| 2652 |
w = sympole; |
| 2653 |
last = NULL; |
| 2654 |
for (;;) { |
| 2655 |
fbit = w->bitno; |
| 2656 |
bitno = BITNO(w->bitno); |
| 2657 |
if (bitno == cix) |
| 2658 |
error("bitno == cix"); |
| 2659 |
if (bitno > cix) |
| 2660 |
break; |
| 2661 |
svbit = P_BIT(key, bitno); |
| 2662 |
last = w; |
| 2663 |
w = w->lr[svbit]; |
| 2664 |
if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF)) |
| 2665 |
break; |
| 2666 |
} |
| 2667 |
|
| 2668 |
new->lr[!bit] = w; |
| 2669 |
if (last == NULL) { |
| 2670 |
sympole = new; |
| 2671 |
} else { |
| 2672 |
last->lr[svbit] = new; |
| 2673 |
last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF); |
| 2674 |
} |
| 2675 |
if (bitno < cix) |
| 2676 |
new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF); |
| 2677 |
return (struct symtab *)new->lr[bit]; |
| 2678 |
} |
| 2679 |
|
| 2680 |
void * |
| 2681 |
xmalloc(int sz) |
| 2682 |
{ |
| 2683 |
register usch *rv; |
| 2684 |
|
| 2685 |
if ((rv = (void *)malloc(sz)) == NULL) |
| 2686 |
error("xmalloc: out of mem"); |
| 2687 |
return rv; |
| 2688 |
} |
| 2689 |
|
| 2690 |
void * |
| 2691 |
xrealloc(void *p, int sz) |
| 2692 |
{ |
| 2693 |
register usch *rv; |
| 2694 |
|
| 2695 |
if ((rv = (void *)realloc(p, sz)) == NULL) |
| 2696 |
error("xrealloc: out of mem"); |
| 2697 |
return rv; |
| 2698 |
} |
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
static usch * |
| 2704 |
addname(const usch *str) |
| 2705 |
{ |
| 2706 |
static usch *nbase; |
| 2707 |
static int nsz; |
| 2708 |
const usch *w = str; |
| 2709 |
usch *p; |
| 2710 |
int len; |
| 2711 |
|
| 2712 |
while (*w++) |
| 2713 |
; |
| 2714 |
len = w - str; |
| 2715 |
if (len > nsz) { |
| 2716 |
nbase = xmalloc(MINBUF); |
| 2717 |
nsz = MINBUF; |
| 2718 |
} |
| 2719 |
nsz -= len; |
| 2720 |
p = nbase; |
| 2721 |
while ((*nbase++ = *str++)) |
| 2722 |
; |
| 2723 |
return p; |
| 2724 |
} |
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
|
| 2729 |
static void * |
| 2730 |
addblock(register int sz) |
| 2731 |
{ |
| 2732 |
static usch *nbase; |
| 2733 |
static int nsz; |
| 2734 |
register usch *str; |
| 2735 |
|
| 2736 |
|
| 2737 |
sz = (sz + sizeof(int *)-1) & ~(sizeof(int *)-1); |
| 2738 |
|
| 2739 |
if (nsz < sz) { |
| 2740 |
nbase = xmalloc(MINBUF); |
| 2741 |
nsz = MINBUF; |
| 2742 |
} |
| 2743 |
str = nbase; |
| 2744 |
nsz -= sz; |
| 2745 |
nbase += sz; |
| 2746 |
return str; |
| 2747 |
} |
| 2748 |
|