| 1 |
#ifdef RCSID |
| 2 |
static char RCSid[] = |
| 3 |
"$Header: d:/cvsroot/tads/TADS2/TOK.C,v 1.3 1999/07/11 00:46:30 MJRoberts Exp $"; |
| 4 |
#endif |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "os.h" |
| 25 |
#include "std.h" |
| 26 |
#include "mch.h" |
| 27 |
#include "mcm.h" |
| 28 |
#include "tok.h" |
| 29 |
#include "lin.h" |
| 30 |
#include "linf.h" |
| 31 |
#include "dbg.h" |
| 32 |
#include <ctype.h> |
| 33 |
#include <string.h> |
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static char tokmac1[] = ",say(("; |
| 39 |
static char tokmac1s[] = "(say(("; |
| 40 |
static char tokmac2[] = "),nil),\""; |
| 41 |
static char tokmac3[] = "),nil))"; |
| 42 |
static char tokmac4[] = ")"; |
| 43 |
|
| 44 |
|
| 45 |
static int tokdfhsh(char *sym, int len); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static tokdfdef *tok_find_define(tokcxdef *ctx, char *sym, int len) |
| 50 |
{ |
| 51 |
int hsh; |
| 52 |
tokdfdef *df; |
| 53 |
|
| 54 |
|
| 55 |
hsh = tokdfhsh(sym, len); |
| 56 |
|
| 57 |
|
| 58 |
for (df = ctx->tokcxdf[hsh] ; df ; df = df->nxt) |
| 59 |
{ |
| 60 |
|
| 61 |
if (df->len == len && !memcmp(df->nm, sym, (size_t)len)) |
| 62 |
{ |
| 63 |
|
| 64 |
if (len == 8) |
| 65 |
{ |
| 66 |
if (!memcmp(sym, "__FILE__", (size_t)8)) |
| 67 |
{ |
| 68 |
size_t len; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
df->expan[0] = '\''; |
| 75 |
|
| 76 |
|
| 77 |
linnam(ctx->tokcxlin, df->expan+1); |
| 78 |
|
| 79 |
|
| 80 |
len = strlen(df->expan); |
| 81 |
df->expan[len] = '\''; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
df->explen = (int)len + 1; |
| 91 |
|
| 92 |
|
| 93 |
if (df->explen >= OSFNMAX) |
| 94 |
errsig(ctx->tokcxerr, ERR_LONG_FILE_MACRO); |
| 95 |
} |
| 96 |
else if (!memcmp(sym, "__LINE__", (size_t)8)) |
| 97 |
{ |
| 98 |
ulong l; |
| 99 |
|
| 100 |
|
| 101 |
l = linlnum(ctx->tokcxlin); |
| 102 |
|
| 103 |
|
| 104 |
sprintf(df->expan, "%lu", l); |
| 105 |
|
| 106 |
|
| 107 |
df->explen = strlen(df->expan); |
| 108 |
|
| 109 |
|
| 110 |
if (df->explen >= 40) |
| 111 |
errsig(ctx->tokcxerr, ERR_LONG_LINE_MACRO); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
return df; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
void tok_write_defines(tokcxdef *ctx, osfildef *fp, errcxdef *ec) |
| 128 |
{ |
| 129 |
int i; |
| 130 |
tokdfdef **dfp; |
| 131 |
tokdfdef *df; |
| 132 |
char buf[4]; |
| 133 |
|
| 134 |
|
| 135 |
for (i = TOKDFHSHSIZ, dfp = ctx->tokcxdf ; i ; ++dfp, --i) |
| 136 |
{ |
| 137 |
|
| 138 |
for (df = *dfp ; df ; df = df->nxt) |
| 139 |
{ |
| 140 |
|
| 141 |
oswp2(buf, df->len); |
| 142 |
oswp2(buf + 2, df->explen); |
| 143 |
if (osfwb(fp, buf, 4) |
| 144 |
|| osfwb(fp, df->nm, df->len) |
| 145 |
|| (df->explen != 0 && osfwb(fp, df->expan, df->explen))) |
| 146 |
errsig(ec, ERR_WRTGAM); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
oswp2(buf, 0); |
| 151 |
if (osfwb(fp, buf, 4)) errsig(ec, ERR_WRTGAM); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
void tok_read_defines(tokcxdef *ctx, osfildef *fp, errcxdef *ec) |
| 159 |
{ |
| 160 |
int i; |
| 161 |
tokdfdef **dfp; |
| 162 |
tokdfdef *df; |
| 163 |
char buf[4]; |
| 164 |
|
| 165 |
|
| 166 |
for (i = TOKDFHSHSIZ, dfp = ctx->tokcxdf ; i ; ++dfp, --i) |
| 167 |
{ |
| 168 |
|
| 169 |
for (;;) |
| 170 |
{ |
| 171 |
|
| 172 |
if (osfrb(fp, buf, 4)) errsig(ec, ERR_RDGAM); |
| 173 |
if (osrp2(buf) == 0) break; |
| 174 |
|
| 175 |
|
| 176 |
df = (tokdfdef *)mchalo(ec, |
| 177 |
(sizeof(tokdfdef) + osrp2(buf) |
| 178 |
+ osrp2(buf+2) - 1), |
| 179 |
"tok_read_defines"); |
| 180 |
df->explen = osrp2(buf+2); |
| 181 |
df->nm = df->expan + df->explen; |
| 182 |
df->len = osrp2(buf); |
| 183 |
|
| 184 |
|
| 185 |
if (osfrb(fp, df->nm, df->len) |
| 186 |
|| (df->explen != 0 && osfrb(fp, df->expan, df->explen))) |
| 187 |
errsig(ec, ERR_RDGAM); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (tok_find_define(ctx, df->nm, df->len)) |
| 196 |
{ |
| 197 |
|
| 198 |
mchfre(df); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
|
| 203 |
df->nxt = *dfp; |
| 204 |
*dfp = df; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static int tokdfhsh(char *sym, int len) |
| 214 |
{ |
| 215 |
uint hsh; |
| 216 |
|
| 217 |
for (hsh = 0 ; len ; ++sym, --len) |
| 218 |
hsh = (hsh + *sym) & TOKDFHSHMASK; |
| 219 |
return hsh; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
static char *tok_casefold_defsym(tokcxdef *ctx, char *outbuf, |
| 224 |
char *src, int len) |
| 225 |
{ |
| 226 |
if (ctx->tokcxflg & TOKCXCASEFOLD) |
| 227 |
{ |
| 228 |
char *dst; |
| 229 |
int rem; |
| 230 |
|
| 231 |
|
| 232 |
rem = (len > TOKNAMMAX ? TOKNAMMAX : len); |
| 233 |
for (dst = outbuf ; rem > 0 ; ++dst, ++src, --rem) |
| 234 |
*dst = (isupper((uchar)*src) ? tolower((uchar)*src) : *src); |
| 235 |
|
| 236 |
|
| 237 |
return outbuf; |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
|
| 242 |
return src; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void tok_case_fold(tokcxdef *ctx, tokdef *tok) |
| 250 |
{ |
| 251 |
|
| 252 |
if (ctx->tokcxflg & TOKCXCASEFOLD) |
| 253 |
{ |
| 254 |
char *p; |
| 255 |
int len; |
| 256 |
|
| 257 |
|
| 258 |
for (p = tok->toknam, len = tok->toklen ; len != 0 ; ++p, --len) |
| 259 |
{ |
| 260 |
|
| 261 |
if (isupper((uchar)*p)) |
| 262 |
*p = tolower((uchar)*p); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
void tok_add_define_cvtcase(tokcxdef *ctx, char *sym, int len, |
| 269 |
char *expan, int explen) |
| 270 |
{ |
| 271 |
char mysym[TOKNAMMAX]; |
| 272 |
|
| 273 |
|
| 274 |
sym = tok_casefold_defsym(ctx, mysym, sym, len); |
| 275 |
|
| 276 |
|
| 277 |
tok_add_define(ctx, sym, len, expan, explen); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
void tok_add_define(tokcxdef *ctx, char *sym, int len, |
| 282 |
char *expan, int explen) |
| 283 |
{ |
| 284 |
int hsh; |
| 285 |
tokdfdef *df; |
| 286 |
|
| 287 |
|
| 288 |
if (tok_find_define(ctx, sym, len)) |
| 289 |
return; |
| 290 |
|
| 291 |
|
| 292 |
hsh = tokdfhsh(sym, len); |
| 293 |
|
| 294 |
|
| 295 |
df = (tokdfdef *)mchalo(ctx->tokcxerr, |
| 296 |
(sizeof(tokdfdef) + len + explen - 1), |
| 297 |
"tok_add_define"); |
| 298 |
|
| 299 |
|
| 300 |
df->nm = df->expan + explen; |
| 301 |
df->len = len; |
| 302 |
df->explen = explen; |
| 303 |
memcpy(df->expan, expan, explen); |
| 304 |
memcpy(df->nm, sym, len); |
| 305 |
|
| 306 |
|
| 307 |
df->nxt = ctx->tokcxdf[hsh]; |
| 308 |
ctx->tokcxdf[hsh] = df; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
void tok_add_define_num_cvtcase(tokcxdef *ctx, char *sym, int len, int num) |
| 313 |
{ |
| 314 |
char buf[20]; |
| 315 |
|
| 316 |
|
| 317 |
sprintf(buf, "%d", num); |
| 318 |
|
| 319 |
|
| 320 |
tok_add_define_cvtcase(ctx, sym, len, buf, strlen(buf)); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
void tok_del_define(tokcxdef *ctx, char *sym, int len) |
| 325 |
{ |
| 326 |
int hsh; |
| 327 |
tokdfdef *df; |
| 328 |
tokdfdef *prv; |
| 329 |
|
| 330 |
|
| 331 |
hsh = tokdfhsh(sym, len); |
| 332 |
|
| 333 |
|
| 334 |
for (prv = 0, df = ctx->tokcxdf[hsh] ; df ; prv = df, df = df->nxt) |
| 335 |
{ |
| 336 |
|
| 337 |
if (df->len == len && !memcmp(df->nm, sym, (size_t)len)) |
| 338 |
{ |
| 339 |
|
| 340 |
if (prv) |
| 341 |
prv->nxt = df->nxt; |
| 342 |
else |
| 343 |
ctx->tokcxdf[hsh] = df->nxt; |
| 344 |
|
| 345 |
|
| 346 |
mchfre(df); |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
static int tok_scan_defsym(tokcxdef *ctx, char *p, int len) |
| 354 |
{ |
| 355 |
int symlen; |
| 356 |
|
| 357 |
|
| 358 |
if (!(isalpha((uchar)*p) || *p == '_' || *p == '$')) |
| 359 |
{ |
| 360 |
errlog(ctx->tokcxerr, ERR_REQSYM); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
for (symlen = 0 ; len && TOKISSYM(*p) ; ++p, --len, ++symlen) ; |
| 366 |
return symlen; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
static void tokdefine(tokcxdef *ctx, char *p, int len) |
| 371 |
{ |
| 372 |
char *sym; |
| 373 |
int symlen; |
| 374 |
char *expan; |
| 375 |
char mysym[TOKNAMMAX]; |
| 376 |
|
| 377 |
|
| 378 |
sym = p; |
| 379 |
if (!(symlen = tok_scan_defsym(ctx, p, len))) |
| 380 |
return; |
| 381 |
|
| 382 |
|
| 383 |
if (tok_find_define(ctx, sym, symlen)) |
| 384 |
{ |
| 385 |
errlog(ctx->tokcxerr, ERR_DEFREDEF); |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
expan = sym + symlen; |
| 391 |
len -= symlen; |
| 392 |
while (len && t_isspace(*expan)) --len, ++expan; |
| 393 |
|
| 394 |
|
| 395 |
sym = tok_casefold_defsym(ctx, mysym, sym, symlen); |
| 396 |
|
| 397 |
|
| 398 |
tok_add_define(ctx, sym, symlen, expan, len); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
static void tok_update_if_stat(tokcxdef *ctx) |
| 409 |
{ |
| 410 |
int i; |
| 411 |
|
| 412 |
|
| 413 |
for (i = 0 ; i < ctx->tokcxifcnt ; ++i) |
| 414 |
{ |
| 415 |
|
| 416 |
ctx->tokcxifcur = ctx->tokcxif[i]; |
| 417 |
|
| 418 |
|
| 419 |
switch (ctx->tokcxif[i]) |
| 420 |
{ |
| 421 |
case TOKIF_IF_NO: |
| 422 |
case TOKIF_ELSE_NO: |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
return; |
| 428 |
|
| 429 |
default: |
| 430 |
|
| 431 |
break; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
static void tok_ifdef_ifndef(tokcxdef *ctx, char *p, int len, int is_ifdef) |
| 438 |
{ |
| 439 |
int symlen; |
| 440 |
char *sym; |
| 441 |
int stat; |
| 442 |
int found; |
| 443 |
char mysym[TOKNAMMAX]; |
| 444 |
|
| 445 |
|
| 446 |
sym = p; |
| 447 |
if (!(symlen = tok_scan_defsym(ctx, p, len))) |
| 448 |
return; |
| 449 |
|
| 450 |
|
| 451 |
sym = tok_casefold_defsym(ctx, mysym, sym, symlen); |
| 452 |
|
| 453 |
|
| 454 |
found = (tok_find_define(ctx, sym, symlen) != 0); |
| 455 |
|
| 456 |
|
| 457 |
if (!is_ifdef) found = !found; |
| 458 |
|
| 459 |
|
| 460 |
if (found) |
| 461 |
stat = TOKIF_IF_YES; |
| 462 |
else |
| 463 |
stat = TOKIF_IF_NO; |
| 464 |
ctx->tokcxif[ctx->tokcxifcnt] = stat; |
| 465 |
|
| 466 |
|
| 467 |
if (ctx->tokcxifcnt >= TOKIFNEST) |
| 468 |
{ |
| 469 |
errlog(ctx->tokcxerr, ERR_MANYPIF); |
| 470 |
return; |
| 471 |
} |
| 472 |
ctx->tokcxifcnt++; |
| 473 |
|
| 474 |
|
| 475 |
tok_update_if_stat(ctx); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
static void tok_p_error(tokcxdef *ctx, char *p, int len) |
| 480 |
{ |
| 481 |
errlog1(ctx->tokcxerr, ERR_P_ERROR, |
| 482 |
ERRTSTR, errstr(ctx->tokcxerr, p, len)); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
static void tokifdef(tokcxdef *ctx, char *p, int len) |
| 487 |
{ |
| 488 |
tok_ifdef_ifndef(ctx, p, len, TRUE); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
static void tokifndef(tokcxdef *ctx, char *p, int len) |
| 493 |
{ |
| 494 |
tok_ifdef_ifndef(ctx, p, len, FALSE); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
static void tokif(tokcxdef *ctx, char *p, int len) |
| 499 |
{ |
| 500 |
errsig(ctx->tokcxerr, ERR_PIF_NA); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
static void tokelif(tokcxdef *ctx, char *p, int len) |
| 505 |
{ |
| 506 |
errsig(ctx->tokcxerr, ERR_PELIF_NA); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
static void tokelse(tokcxdef *ctx, char *p, int len) |
| 511 |
{ |
| 512 |
int cnt; |
| 513 |
|
| 514 |
|
| 515 |
cnt = ctx->tokcxifcnt; |
| 516 |
if (cnt == 0 || ctx->tokcxif[cnt-1] == TOKIF_ELSE_YES |
| 517 |
|| ctx->tokcxif[cnt-1] == TOKIF_ELSE_NO) |
| 518 |
{ |
| 519 |
errlog(ctx->tokcxerr, ERR_BADPELSE); |
| 520 |
return; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if (ctx->tokcxif[cnt-1] == TOKIF_IF_YES) |
| 525 |
ctx->tokcxif[cnt-1] = TOKIF_ELSE_NO; |
| 526 |
else |
| 527 |
ctx->tokcxif[cnt-1] = TOKIF_ELSE_YES; |
| 528 |
|
| 529 |
|
| 530 |
tok_update_if_stat(ctx); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
static void tokendif(tokcxdef *ctx, char *p, int len) |
| 535 |
{ |
| 536 |
|
| 537 |
if (ctx->tokcxifcnt == 0) |
| 538 |
{ |
| 539 |
errlog(ctx->tokcxerr, ERR_BADENDIF); |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
ctx->tokcxifcnt--; |
| 545 |
|
| 546 |
|
| 547 |
tok_update_if_stat(ctx); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
static void tokundef(tokcxdef *ctx, char *p, int len) |
| 552 |
{ |
| 553 |
char *sym; |
| 554 |
int symlen; |
| 555 |
char mysym[TOKNAMMAX]; |
| 556 |
|
| 557 |
|
| 558 |
sym = p; |
| 559 |
if (!(symlen = tok_scan_defsym(ctx, p, len))) |
| 560 |
return; |
| 561 |
|
| 562 |
|
| 563 |
sym = tok_casefold_defsym(ctx, mysym, sym, symlen); |
| 564 |
|
| 565 |
|
| 566 |
if (!tok_find_define(ctx, sym, symlen)) |
| 567 |
{ |
| 568 |
errlog(ctx->tokcxerr, ERR_PUNDEF); |
| 569 |
return; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
tok_del_define(ctx, sym, symlen); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
static void tokpragma(tokcxdef *ctx, char *p, int len) |
| 578 |
{ |
| 579 |
|
| 580 |
if (len == 0) |
| 581 |
{ |
| 582 |
errlog(ctx->tokcxerr, ERR_PRAGMA); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
if (len > 1 |
| 588 |
&& (*p == 'c' || *p == 'C') |
| 589 |
&& (*(p+1) == '+' || *(p+1) == '-' || t_isspace(*(p+1)))) |
| 590 |
{ |
| 591 |
|
| 592 |
for (++p, --len ; len && t_isspace(*p) ; ++p, --len) ; |
| 593 |
|
| 594 |
|
| 595 |
if (len && *p == '+') |
| 596 |
ctx->tokcxflg |= TOKCXFCMODE; |
| 597 |
else if (len && *p == '-') |
| 598 |
ctx->tokcxflg &= ~TOKCXFCMODE; |
| 599 |
else |
| 600 |
{ |
| 601 |
errlog(ctx->tokcxerr, ERR_PRAGMA); |
| 602 |
return; |
| 603 |
} |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
errlog(ctx->tokcxerr, ERR_PRAGMA); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
static void tokinclude(tokcxdef *ctx, char *p, int len) |
| 613 |
{ |
| 614 |
linfdef *child; |
| 615 |
tokpdef *path; |
| 616 |
char *fname; |
| 617 |
int match; |
| 618 |
int flen; |
| 619 |
linfdef *lin; |
| 620 |
char *q; |
| 621 |
size_t flen2; |
| 622 |
|
| 623 |
|
| 624 |
fname = p + 1; |
| 625 |
path = ctx->tokcxinc; |
| 626 |
|
| 627 |
if (!len) |
| 628 |
{ |
| 629 |
errlog(ctx->tokcxerr, ERR_INCNOFN); |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
switch(*p) |
| 634 |
{ |
| 635 |
case '<': |
| 636 |
match = '>'; |
| 637 |
if (path && path->tokpnxt) path = path->tokpnxt; |
| 638 |
goto find_matching_delim; |
| 639 |
|
| 640 |
case '"': |
| 641 |
match = '"'; |
| 642 |
|
| 643 |
find_matching_delim: |
| 644 |
for (++p, --len ; len && *p != match ; --len, ++p) ; |
| 645 |
if (len == 0 || *p != match) errlog(ctx->tokcxerr, ERR_INCMTCH); |
| 646 |
break; |
| 647 |
|
| 648 |
default: |
| 649 |
errlog(ctx->tokcxerr, ERR_INCSYN); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
flen = p - fname; |
| 654 |
for (q = p, flen2 = 0 ; |
| 655 |
q > fname && *(q-1) != OSPATHCHAR && !strchr(OSPATHALT, *(q-1)) ; |
| 656 |
--q, ++flen2) ; |
| 657 |
|
| 658 |
|
| 659 |
for (lin = ctx->tokcxhdr ; lin ; lin = (linfdef *)lin->linflin.linnxt) |
| 660 |
{ |
| 661 |
char *p = lin->linfnam; |
| 662 |
|
| 663 |
p += strlen(p); |
| 664 |
|
| 665 |
while (p > lin->linfnam && *(p-1) != OSPATHCHAR |
| 666 |
&& !strchr(OSPATHALT, *(p-1))) |
| 667 |
--p; |
| 668 |
if (strlen(p) == flen2 |
| 669 |
&& !memicmp(p, q, flen2)) |
| 670 |
{ |
| 671 |
errlog1(ctx->tokcxerr, ERR_INCRPT, ERRTSTR, |
| 672 |
errstr(ctx->tokcxerr, fname, flen)); |
| 673 |
return; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
child = linfini(ctx->tokcxmem, ctx->tokcxerr, fname, flen, path, TRUE, |
| 679 |
(ctx->tokcxflg & TOKCXFLIN2) != 0); |
| 680 |
|
| 681 |
|
| 682 |
if (!child) errsig1(ctx->tokcxerr, ERR_INCSEAR, |
| 683 |
ERRTSTR, errstr(ctx->tokcxerr, fname, flen)); |
| 684 |
|
| 685 |
|
| 686 |
child->linflin.linnxt = (lindef *)ctx->tokcxhdr; |
| 687 |
ctx->tokcxhdr = child; |
| 688 |
|
| 689 |
|
| 690 |
if (ctx->tokcxdbg) |
| 691 |
{ |
| 692 |
ctx->tokcxdbg->dbgcxlin = &child->linflin; |
| 693 |
child->linflin.linid = ctx->tokcxdbg->dbgcxfid++; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
if (ctx->tokcxflg & TOKCXFCMODE) |
| 698 |
ctx->tokcxlin->linflg |= LINFCMODE; |
| 699 |
else |
| 700 |
ctx->tokcxlin->linflg &= ~LINFCMODE; |
| 701 |
|
| 702 |
child->linflin.linpar = ctx->tokcxlin; |
| 703 |
ctx->tokcxlin = &child->linflin; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
static int tokgetlin(tokcxdef *ctx, int dopound) |
| 708 |
{ |
| 709 |
for (;;) |
| 710 |
{ |
| 711 |
if (linget(ctx->tokcxlin)) |
| 712 |
{ |
| 713 |
|
| 714 |
if (ctx->tokcxlin->linpar) |
| 715 |
{ |
| 716 |
lindef *parent; |
| 717 |
|
| 718 |
parent = ctx->tokcxlin->linpar; |
| 719 |
lincls(ctx->tokcxlin); |
| 720 |
if (!ctx->tokcxdbg) |
| 721 |
mchfre(ctx->tokcxlin); |
| 722 |
ctx->tokcxlin = parent; |
| 723 |
if (parent->linflg & LINFCMODE) |
| 724 |
ctx->tokcxflg |= TOKCXFCMODE; |
| 725 |
else |
| 726 |
ctx->tokcxflg &= ~TOKCXFCMODE; |
| 727 |
continue; |
| 728 |
} |
| 729 |
else |
| 730 |
{ |
| 731 |
|
| 732 |
if (ctx->tokcxifcnt) |
| 733 |
errlog(ctx->tokcxerr, ERR_NOENDIF); |
| 734 |
|
| 735 |
|
| 736 |
return TRUE; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
if (ctx->tokcxlin->linflg & LINFMORE) |
| 742 |
{ |
| 743 |
char *p; |
| 744 |
uint rem; |
| 745 |
int done; |
| 746 |
|
| 747 |
if (!ctx->tokcxbuf) |
| 748 |
{ |
| 749 |
|
| 750 |
ctx->tokcxbuf = (char *)mchalo(ctx->tokcxerr, 1024, |
| 751 |
"tok"); |
| 752 |
ctx->tokcxbsz = 1024; |
| 753 |
} |
| 754 |
ctx->tokcxlen = 0; |
| 755 |
|
| 756 |
for (done = FALSE, p = ctx->tokcxbuf, rem = ctx->tokcxbsz ; |
| 757 |
!done ; ) |
| 758 |
{ |
| 759 |
size_t len = ctx->tokcxlin->linlen; |
| 760 |
|
| 761 |
|
| 762 |
ctx->tokcxlen += len; |
| 763 |
|
| 764 |
|
| 765 |
done = !(ctx->tokcxlin->linflg & LINFMORE); |
| 766 |
if (len + 1 > rem) |
| 767 |
{ |
| 768 |
char *newp; |
| 769 |
|
| 770 |
|
| 771 |
if (ctx->tokcxbsz > (unsigned)0x8000) |
| 772 |
errsig(ctx->tokcxerr, ERR_LONGLIN); |
| 773 |
rem += 4096; |
| 774 |
ctx->tokcxbsz += 4096; |
| 775 |
|
| 776 |
|
| 777 |
newp = (char *)mchalo(ctx->tokcxerr, ctx->tokcxbsz, "tok"); |
| 778 |
memcpy(newp, ctx->tokcxbuf, (size_t)(p - ctx->tokcxbuf)); |
| 779 |
|
| 780 |
|
| 781 |
p = (p - ctx->tokcxbuf) + newp; |
| 782 |
mchfre(ctx->tokcxbuf); |
| 783 |
ctx->tokcxbuf = newp; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
memcpy(p, ctx->tokcxlin->linbuf, len); |
| 788 |
p += len; |
| 789 |
rem -= len; |
| 790 |
|
| 791 |
|
| 792 |
if (!done) |
| 793 |
{ |
| 794 |
if (linget(ctx->tokcxlin)) break; |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
*p = '\0'; |
| 800 |
ctx->tokcxptr = ctx->tokcxbuf; |
| 801 |
} |
| 802 |
else |
| 803 |
{ |
| 804 |
ctx->tokcxptr = ctx->tokcxlin->linbuf; |
| 805 |
ctx->tokcxlen = ctx->tokcxlin->linlen; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
if (dopound && ctx->tokcxlen != 0 && ctx->tokcxptr[0] == '#' |
| 810 |
&& !(ctx->tokcxlin->linflg & LINFNOINC)) |
| 811 |
{ |
| 812 |
char *p; |
| 813 |
int len; |
| 814 |
static struct |
| 815 |
{ |
| 816 |
char *nm; |
| 817 |
int len; |
| 818 |
int ok_in_if; |
| 819 |
void (*fn)(tokcxdef *, char *, int); |
| 820 |
} |
| 821 |
*dirp, dir[] = |
| 822 |
{ |
| 823 |
{ "include", 7, FALSE, tokinclude }, |
| 824 |
{ "pragma", 6, FALSE, tokpragma }, |
| 825 |
{ "define", 6, FALSE, tokdefine }, |
| 826 |
{ "ifdef", 5, TRUE, tokifdef }, |
| 827 |
{ "ifndef", 6, TRUE, tokifndef }, |
| 828 |
{ "if", 2, TRUE, tokif }, |
| 829 |
{ "else", 4, TRUE, tokelse }, |
| 830 |
{ "elif", 4, TRUE, tokelif }, |
| 831 |
{ "endif", 5, TRUE, tokendif }, |
| 832 |
{ "undef", 5, FALSE, tokundef }, |
| 833 |
{ "error", 5, FALSE, tok_p_error } |
| 834 |
}; |
| 835 |
int i; |
| 836 |
|
| 837 |
|
| 838 |
for (len = ctx->tokcxlen - 1, p = &ctx->tokcxptr[1] ; |
| 839 |
len && t_isspace(*p) ; --len, ++p) ; |
| 840 |
|
| 841 |
|
| 842 |
for (dirp = dir, i = sizeof(dir)/sizeof(dir[0]) ; i ; --i, ++dirp) |
| 843 |
{ |
| 844 |
|
| 845 |
if (len >= dirp->len && !memcmp(p, dirp->nm, (size_t)dirp->len) |
| 846 |
&& (len == dirp->len || t_isspace(*(p + dirp->len)))) |
| 847 |
{ |
| 848 |
int cnt; |
| 849 |
int stat; |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
cnt = ctx->tokcxifcnt; |
| 857 |
if (dirp->ok_in_if || cnt == 0 |
| 858 |
|| ((stat = ctx->tokcxifcur) == TOKIF_IF_YES |
| 859 |
|| stat == TOKIF_ELSE_YES)) |
| 860 |
{ |
| 861 |
|
| 862 |
for (p += dirp->len, len -= dirp->len ; |
| 863 |
len && t_isspace(*p) ; |
| 864 |
--len, ++p) ; |
| 865 |
|
| 866 |
|
| 867 |
(*dirp->fn)(ctx, p, len); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
break; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
if (i == 0) |
| 877 |
errlog(ctx->tokcxerr, ERR_PRPDIR); |
| 878 |
|
| 879 |
|
| 880 |
continue; |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
if (ctx->tokcxifcnt != 0) |
| 890 |
{ |
| 891 |
switch(ctx->tokcxifcur) |
| 892 |
{ |
| 893 |
case TOKIF_IF_NO: |
| 894 |
case TOKIF_ELSE_NO: |
| 895 |
|
| 896 |
continue; |
| 897 |
|
| 898 |
default: |
| 899 |
|
| 900 |
break; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
ctx->tokcxlin->linflg &= ~LINFDBG; |
| 905 |
return(FALSE); |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
int toknext(tokcxdef *ctx) |
| 912 |
{ |
| 913 |
char *p; |
| 914 |
tokdef *tok = &ctx->tokcxcur; |
| 915 |
int len; |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
if ((ctx->tokcxflg & TOKCXF_EMBED_PAREN_PRE) != 0) |
| 923 |
{ |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
tok->toktyp = TOKTDSTRING; |
| 931 |
|
| 932 |
|
| 933 |
ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_PRE; |
| 934 |
|
| 935 |
|
| 936 |
return tok->toktyp; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
p = ctx->tokcxptr; |
| 941 |
len = ctx->tokcxlen; |
| 942 |
|
| 943 |
|
| 944 |
do |
| 945 |
{ |
| 946 |
skipblanks: |
| 947 |
|
| 948 |
if (len == 0) |
| 949 |
{ |
| 950 |
|
| 951 |
if (ctx->tokcxmlvl) |
| 952 |
{ |
| 953 |
ctx->tokcxmlvl--; |
| 954 |
p = ctx->tokcxmsav[ctx->tokcxmlvl]; |
| 955 |
len = ctx->tokcxmsvl[ctx->tokcxmlvl]; |
| 956 |
} |
| 957 |
else |
| 958 |
{ |
| 959 |
if (tokgetlin(ctx, TRUE)) |
| 960 |
{ |
| 961 |
tok->toktyp = TOKTEOF; |
| 962 |
goto done; |
| 963 |
} |
| 964 |
p = ctx->tokcxptr; |
| 965 |
len = ctx->tokcxlen; |
| 966 |
} |
| 967 |
} |
| 968 |
while (len && t_isspace(*p)) ++p, --len; |
| 969 |
|
| 970 |
|
| 971 |
if (len >= 2 && *p == '/' && *(p+1) == '/') |
| 972 |
len = 0; |
| 973 |
else if (len >= 2 && *p == '/' && *(p+1) == '*') |
| 974 |
{ |
| 975 |
while (len < 2 || *p != '*' || *(p+1) != '/') |
| 976 |
{ |
| 977 |
if (len != 0) |
| 978 |
++p, --len; |
| 979 |
|
| 980 |
if (len == 0) |
| 981 |
{ |
| 982 |
if (ctx->tokcxmlvl != 0) |
| 983 |
{ |
| 984 |
ctx->tokcxmlvl--; |
| 985 |
p = ctx->tokcxmsav[ctx->tokcxmlvl]; |
| 986 |
len = ctx->tokcxmsvl[ctx->tokcxmlvl]; |
| 987 |
} |
| 988 |
else |
| 989 |
{ |
| 990 |
if (tokgetlin(ctx, FALSE)) |
| 991 |
{ |
| 992 |
ctx->tokcxptr = p; |
| 993 |
tok->toktyp = TOKTEOF; |
| 994 |
goto done; |
| 995 |
} |
| 996 |
p = ctx->tokcxptr; |
| 997 |
len = ctx->tokcxlen; |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
p += 2; |
| 1002 |
len -= 2; |
| 1003 |
goto skipblanks; |
| 1004 |
} |
| 1005 |
} while (len == 0); |
| 1006 |
|
| 1007 |
nexttoken: |
| 1008 |
if (isalpha((uchar)*p) || *p == '_' || *p == '$') |
| 1009 |
{ |
| 1010 |
int l; |
| 1011 |
int hash; |
| 1012 |
char *q; |
| 1013 |
toktdef *tab; |
| 1014 |
int found = FALSE; |
| 1015 |
uchar thischar; |
| 1016 |
tokdfdef *df; |
| 1017 |
|
| 1018 |
for (hash = 0, l = 0, q = tok->toknam ; |
| 1019 |
len != 0 && TOKISSYM(*p) && l < TOKNAMMAX ; |
| 1020 |
(thischar = ((isupper((uchar)*p) |
| 1021 |
&& (ctx->tokcxflg & TOKCXCASEFOLD)) |
| 1022 |
? tolower((uchar)*p) : *p)), |
| 1023 |
(hash = ((hash + thischar) & (TOKHASHSIZE - 1))), |
| 1024 |
(*q++ = thischar), ++p, --len, ++l) ; |
| 1025 |
*q = '\0'; |
| 1026 |
if (len != 0 && TOKISSYM(*p)) |
| 1027 |
{ |
| 1028 |
while (len != 0 && TOKISSYM(*p)) ++p, --len; |
| 1029 |
errlog1(ctx->tokcxerr, ERR_TRUNC, ERRTSTR, |
| 1030 |
errstr(ctx->tokcxerr, tok->toknam, tok->toklen)); |
| 1031 |
} |
| 1032 |
tok->toklen = l; |
| 1033 |
tok->tokhash = hash; |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
if (l == 9 && !memcmp(tok->toknam, |
| 1039 |
((ctx->tokcxflg & TOKCXCASEFOLD) |
| 1040 |
? "__defined" : "__DEFINED"), |
| 1041 |
(size_t)9) |
| 1042 |
&& len > 2 && *p == '(' && TOKISSYM(*(p+1)) |
| 1043 |
&& !isdigit((uchar)*(p+1))) |
| 1044 |
{ |
| 1045 |
int symlen; |
| 1046 |
char mysym[TOKNAMMAX]; |
| 1047 |
|
| 1048 |
|
| 1049 |
++p, --len; |
| 1050 |
for (symlen = 0, q = p ; len && *p != ')' && TOKISSYM(*p) ; |
| 1051 |
++p, --len, ++symlen) ; |
| 1052 |
|
| 1053 |
|
| 1054 |
if (!len || *p != ')') |
| 1055 |
errsig(ctx->tokcxerr, ERR_BADISDEF); |
| 1056 |
++p, --len; |
| 1057 |
|
| 1058 |
|
| 1059 |
q = tok_casefold_defsym(ctx, mysym, q, symlen); |
| 1060 |
|
| 1061 |
|
| 1062 |
tok->toktyp = TOKTNUMBER; |
| 1063 |
tok->tokval = (tok_find_define(ctx, q, symlen) != 0); |
| 1064 |
goto done; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
if ((df = tok_find_define(ctx, tok->toknam, l)) != 0) |
| 1069 |
{ |
| 1070 |
|
| 1071 |
if (ctx->tokcxmlvl >= TOKMACNEST) |
| 1072 |
errsig(ctx->tokcxerr, ERR_MACNEST); |
| 1073 |
ctx->tokcxmsav[ctx->tokcxmlvl] = p; |
| 1074 |
ctx->tokcxmsvl[ctx->tokcxmlvl] = len; |
| 1075 |
ctx->tokcxmlvl++; |
| 1076 |
|
| 1077 |
|
| 1078 |
p = df->expan; |
| 1079 |
len = df->explen; |
| 1080 |
goto nexttoken; |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
for (tab = ctx->tokcxstab ; tab ; tab = tab->toktnxt) |
| 1085 |
{ |
| 1086 |
if ((found = (*tab->toktfsea)(tab, tok->toknam, l, hash, |
| 1087 |
&tok->toksym)) != 0) |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (found && tok->toksym.tokstyp == TOKSTKW) |
| 1092 |
tok->toktyp = tok->toksym.toksval; |
| 1093 |
else |
| 1094 |
{ |
| 1095 |
tok->toktyp = TOKTSYMBOL; |
| 1096 |
if (!found) tok->toksym.tokstyp = TOKSTUNK; |
| 1097 |
} |
| 1098 |
goto done; |
| 1099 |
} |
| 1100 |
else if (isdigit((uchar)*p)) |
| 1101 |
{ |
| 1102 |
long acc = 0; |
| 1103 |
|
| 1104 |
|
| 1105 |
if (*p == '0') |
| 1106 |
{ |
| 1107 |
++p, --len; |
| 1108 |
if (len && (*p == 'x' || *p == 'X')) |
| 1109 |
{ |
| 1110 |
|
| 1111 |
++p, --len; |
| 1112 |
while (len && TOKISHEX(*p)) |
| 1113 |
{ |
| 1114 |
acc = (acc << 4) + TOKHEX2INT(*p); |
| 1115 |
++p, --len; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
else |
| 1119 |
{ |
| 1120 |
|
| 1121 |
while (len && TOKISOCT(*p)) |
| 1122 |
{ |
| 1123 |
acc = (acc << 3) + TOKOCT2INT(*p); |
| 1124 |
++p, --len; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |
| 1128 |
else |
| 1129 |
{ |
| 1130 |
|
| 1131 |
while (len && isdigit((uchar)*p)) |
| 1132 |
{ |
| 1133 |
acc = (acc << 1) + (acc << 3) + TOKDEC2INT(*p); |
| 1134 |
++p, --len; |
| 1135 |
} |
| 1136 |
} |
| 1137 |
tok->tokval = acc; |
| 1138 |
tok->toktyp = TOKTNUMBER; |
| 1139 |
goto done; |
| 1140 |
} |
| 1141 |
else if (*p == '"' || *p == '\'') |
| 1142 |
{ |
| 1143 |
char delim; |
| 1144 |
char *strstart; |
| 1145 |
int warned; |
| 1146 |
|
| 1147 |
delim = *p; |
| 1148 |
--len; |
| 1149 |
strstart = ++p; |
| 1150 |
|
| 1151 |
if (delim == '"' && len >= 2 && *p == '<' && *(p+1) == '<') |
| 1152 |
{ |
| 1153 |
|
| 1154 |
if (ctx->tokcxmlvl >= TOKMACNEST) |
| 1155 |
errsig(ctx->tokcxerr, ERR_MACNEST); |
| 1156 |
ctx->tokcxmsav[ctx->tokcxmlvl] = p + 2; |
| 1157 |
ctx->tokcxmsvl[ctx->tokcxmlvl] = len - 2; |
| 1158 |
ctx->tokcxmlvl++; |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
p = tokmac1s; |
| 1165 |
len = strlen(p); |
| 1166 |
ctx->tokcxflg |= TOKCXFINMAC; |
| 1167 |
goto nexttoken; |
| 1168 |
} |
| 1169 |
tok->toktyp = (delim == '"' ? TOKTDSTRING : TOKTSSTRING); |
| 1170 |
|
| 1171 |
tok->tokofs = (*ctx->tokcxsst)(ctx->tokcxscx); |
| 1172 |
for (warned = FALSE ;; ) |
| 1173 |
{ |
| 1174 |
if (len >= 2 && *p == '\\') |
| 1175 |
{ |
| 1176 |
if (*(p+1) == '"' || *(p+1) == '\'') |
| 1177 |
{ |
| 1178 |
(*ctx->tokcxsad)(ctx->tokcxscx, strstart, |
| 1179 |
(ushort)(p - strstart)); |
| 1180 |
strstart = p + 1; |
| 1181 |
} |
| 1182 |
p += 2; |
| 1183 |
len -= 2; |
| 1184 |
} |
| 1185 |
else if (len == 0 || *p == delim || |
| 1186 |
(delim == '"' && len >= 2 && *p == '<' && *(p+1) == '<' |
| 1187 |
&& !(ctx->tokcxflg & TOKCXFINMAC))) |
| 1188 |
{ |
| 1189 |
(*ctx->tokcxsad)(ctx->tokcxscx, strstart, |
| 1190 |
(ushort)(p - strstart)); |
| 1191 |
if (len == 0) |
| 1192 |
{ |
| 1193 |
if (ctx->tokcxmlvl) |
| 1194 |
{ |
| 1195 |
ctx->tokcxmlvl--; |
| 1196 |
p = ctx->tokcxmsav[ctx->tokcxmlvl]; |
| 1197 |
len = ctx->tokcxmsvl[ctx->tokcxmlvl]; |
| 1198 |
} |
| 1199 |
else |
| 1200 |
(*ctx->tokcxsad)(ctx->tokcxscx, " ", (ushort)1); |
| 1201 |
|
| 1202 |
while (len == 0) |
| 1203 |
{ |
| 1204 |
if (tokgetlin(ctx, FALSE)) |
| 1205 |
errsig(ctx->tokcxerr, ERR_STREOF); |
| 1206 |
p = ctx->tokcxptr; |
| 1207 |
len = ctx->tokcxlen; |
| 1208 |
|
| 1209 |
|
| 1210 |
if (!warned && len && (*p == ';' || *p == '}')) |
| 1211 |
{ |
| 1212 |
errlog(ctx->tokcxerr, ERR_STREND); |
| 1213 |
warned = TRUE; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
while (len && t_isspace(*p)) ++p, --len; |
| 1218 |
} |
| 1219 |
strstart = p; |
| 1220 |
} |
| 1221 |
else break; |
| 1222 |
} |
| 1223 |
else |
| 1224 |
++p, --len; |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
(*ctx->tokcxsend)(ctx->tokcxscx); |
| 1229 |
|
| 1230 |
|
| 1231 |
if (len != 0 && *p == delim) |
| 1232 |
{ |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
++p; |
| 1238 |
--len; |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
if ((ctx->tokcxflg & TOKCXF_EMBED_PAREN_AFT) != 0 |
| 1246 |
&& !(ctx->tokcxflg & TOKCXFINMAC)) |
| 1247 |
{ |
| 1248 |
|
| 1249 |
ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_AFT; |
| 1250 |
|
| 1251 |
|
| 1252 |
if (ctx->tokcxmlvl >= TOKMACNEST) |
| 1253 |
errsig(ctx->tokcxerr, ERR_MACNEST); |
| 1254 |
ctx->tokcxmsav[ctx->tokcxmlvl] = p; |
| 1255 |
ctx->tokcxmsvl[ctx->tokcxmlvl] = len; |
| 1256 |
ctx->tokcxmlvl++; |
| 1257 |
|
| 1258 |
|
| 1259 |
p = tokmac4; |
| 1260 |
len = strlen(p); |
| 1261 |
} |
| 1262 |
} |
| 1263 |
else if (len != 0 && *p == '<') |
| 1264 |
{ |
| 1265 |
|
| 1266 |
if (ctx->tokcxmlvl >= TOKMACNEST) |
| 1267 |
errsig(ctx->tokcxerr, ERR_MACNEST); |
| 1268 |
ctx->tokcxmsav[ctx->tokcxmlvl] = p + 2; |
| 1269 |
ctx->tokcxmsvl[ctx->tokcxmlvl] = len - 2; |
| 1270 |
ctx->tokcxmlvl++; |
| 1271 |
|
| 1272 |
|
| 1273 |
p = tokmac1; |
| 1274 |
len = strlen(p); |
| 1275 |
ctx->tokcxflg |= TOKCXFINMAC; |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
if (!(ctx->tokcxflg & TOKCXF_EMBED_PAREN_AFT)) |
| 1291 |
{ |
| 1292 |
ctx->tokcxflg |= TOKCXF_EMBED_PAREN_PRE; |
| 1293 |
tok->toktyp = TOKTLPAR; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
goto done; |
| 1297 |
} |
| 1298 |
else if (len >= 2 && *p == '>' && *(p+1) == '>' |
| 1299 |
&& (ctx->tokcxflg & TOKCXFINMAC) != 0) |
| 1300 |
{ |
| 1301 |
|
| 1302 |
ctx->tokcxflg &= ~TOKCXFINMAC; |
| 1303 |
p += 2; |
| 1304 |
len -= 2; |
| 1305 |
|
| 1306 |
|
| 1307 |
if (ctx->tokcxmlvl >= TOKMACNEST) |
| 1308 |
errsig(ctx->tokcxerr, ERR_MACNEST); |
| 1309 |
ctx->tokcxmsav[ctx->tokcxmlvl] = p; |
| 1310 |
ctx->tokcxmsvl[ctx->tokcxmlvl] = len; |
| 1311 |
ctx->tokcxmlvl++; |
| 1312 |
|
| 1313 |
if (*p == '"') |
| 1314 |
{ |
| 1315 |
++(ctx->tokcxmsav[ctx->tokcxmlvl - 1]); |
| 1316 |
--(ctx->tokcxmsvl[ctx->tokcxmlvl - 1]); |
| 1317 |
p = tokmac3; |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_AFT; |
| 1324 |
} |
| 1325 |
else |
| 1326 |
{ |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
ctx->tokcxflg |= TOKCXF_EMBED_PAREN_AFT; |
| 1334 |
p = tokmac2; |
| 1335 |
} |
| 1336 |
|
| 1337 |
len = strlen(p); |
| 1338 |
goto nexttoken; |
| 1339 |
} |
| 1340 |
else |
| 1341 |
{ |
| 1342 |
tokscdef *sc; |
| 1343 |
|
| 1344 |
for (sc = ctx->tokcxsc[ctx->tokcxinx[(uchar)*p]] ; sc ; |
| 1345 |
sc = sc->tokscnxt) |
| 1346 |
{ |
| 1347 |
if (toksceq(sc->tokscstr, p, sc->toksclen, len)) |
| 1348 |
{ |
| 1349 |
tok->toktyp = sc->toksctyp; |
| 1350 |
p += sc->toksclen; |
| 1351 |
len -= sc->toksclen; |
| 1352 |
goto done; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
errsig(ctx->tokcxerr, ERR_INVTOK); |
| 1356 |
} |
| 1357 |
|
| 1358 |
done: |
| 1359 |
ctx->tokcxptr = p; |
| 1360 |
ctx->tokcxlen = len; |
| 1361 |
return(tok->toktyp); |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
void toktlini(errcxdef *errctx, toktldef *toktab, uchar *mem, uint siz) |
| 1366 |
{ |
| 1367 |
CLRSTRUCT(*toktab); |
| 1368 |
|
| 1369 |
|
| 1370 |
toktab->toktlsc.toktfadd = toktladd; |
| 1371 |
toktab->toktlsc.toktfsea = toktlsea; |
| 1372 |
toktab->toktlsc.toktfeach = toktleach; |
| 1373 |
toktab->toktlsc.toktfset = toktlset; |
| 1374 |
toktab->toktlsc.tokterr = errctx; |
| 1375 |
|
| 1376 |
|
| 1377 |
toktab->toktlptr = mem; |
| 1378 |
toktab->toktlnxt = mem; |
| 1379 |
toktab->toktlsiz = siz; |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
void toktladd(toktdef *toktab1, char *name, int namel, |
| 1384 |
int typ, int val, int hash) |
| 1385 |
{ |
| 1386 |
uint siz = sizeof(toks1def) + namel; |
| 1387 |
toksdef *sym; |
| 1388 |
toktldef *toktab = (toktldef *)toktab1; |
| 1389 |
|
| 1390 |
VARUSED(hash); |
| 1391 |
|
| 1392 |
if (toktab->toktlsiz < siz) |
| 1393 |
errsig(toktab->toktlsc.tokterr, ERR_NOLCLSY); |
| 1394 |
|
| 1395 |
sym = (toksdef *)toktab->toktlnxt; |
| 1396 |
siz = osrndsz(siz); |
| 1397 |
toktab->toktlnxt += siz; |
| 1398 |
if (siz > toktab->toktlsiz) toktab->toktlsiz = 0; |
| 1399 |
else toktab->toktlsiz -= siz; |
| 1400 |
|
| 1401 |
|
| 1402 |
sym->toksval = val; |
| 1403 |
sym->tokslen = namel; |
| 1404 |
sym->tokstyp = typ; |
| 1405 |
sym->toksfr = 0; |
| 1406 |
memcpy(sym->toksnam, name, (size_t)(namel + 1)); |
| 1407 |
|
| 1408 |
|
| 1409 |
++(toktab->toktlcnt); |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
void toktldel(toktldef *tab) |
| 1414 |
{ |
| 1415 |
tab->toktlcnt = 0; |
| 1416 |
tab->toktlsiz += tab->toktlnxt - tab->toktlptr; |
| 1417 |
tab->toktlnxt = tab->toktlptr; |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
void toktleach(toktdef *tab1, |
| 1422 |
void (*cb)(void *ctx, toksdef *sym), void *ctx) |
| 1423 |
{ |
| 1424 |
toksdef *p; |
| 1425 |
uint cnt; |
| 1426 |
toktldef *tab = (toktldef *)tab1; |
| 1427 |
|
| 1428 |
for (p = (toksdef *)tab->toktlptr, cnt = tab->toktlcnt ; cnt ; --cnt ) |
| 1429 |
{ |
| 1430 |
(*cb)(ctx, p); |
| 1431 |
p = (toksdef *)(((uchar *)p) |
| 1432 |
+ osrndsz(p->tokslen + sizeof(toks1def))); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
int toktlsea(toktdef *tab1, char *name, int namel, int hash, toksdef *ret) |
| 1438 |
{ |
| 1439 |
toksdef *p; |
| 1440 |
uint cnt; |
| 1441 |
toktldef *tab = (toktldef *)tab1; |
| 1442 |
|
| 1443 |
VARUSED(hash); |
| 1444 |
|
| 1445 |
for (p = (toksdef *)tab->toktlptr, cnt = tab->toktlcnt ; cnt ; --cnt ) |
| 1446 |
{ |
| 1447 |
if (p->tokslen == namel && !memcmp(p->toksnam, name, (size_t)namel)) |
| 1448 |
{ |
| 1449 |
memcpy(ret, p, (size_t)(sizeof(toks1def) + namel)); |
| 1450 |
return(TRUE); |
| 1451 |
} |
| 1452 |
|
| 1453 |
p = (toksdef *)(((uchar *)p) |
| 1454 |
+ osrndsz(p->tokslen + sizeof(toks1def))); |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
return(FALSE); |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
void toktlset(toktdef *tab1, toksdef *newsym) |
| 1463 |
{ |
| 1464 |
toksdef *p; |
| 1465 |
uint cnt; |
| 1466 |
toktldef *tab = (toktldef *)tab1; |
| 1467 |
|
| 1468 |
for (p = (toksdef *)tab->toktlptr, cnt = tab->toktlcnt ; cnt ; --cnt ) |
| 1469 |
{ |
| 1470 |
if (p->tokslen == newsym->tokslen |
| 1471 |
&& !memcmp(p->toksnam, newsym->toksnam, (size_t)newsym->tokslen)) |
| 1472 |
{ |
| 1473 |
p->toksval = newsym->toksval; |
| 1474 |
p->tokstyp = newsym->tokstyp; |
| 1475 |
return; |
| 1476 |
} |
| 1477 |
|
| 1478 |
p = (toksdef *)(((uchar *)p) |
| 1479 |
+ osrndsz(p->tokslen + sizeof(toks1def))); |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
tokcxdef *tokcxini(errcxdef *errctx, mcmcxdef *mcmctx, tokldef *sctab) |
| 1484 |
{ |
| 1485 |
int i; |
| 1486 |
int cnt; |
| 1487 |
tokldef *p; |
| 1488 |
uchar c; |
| 1489 |
uchar index[256]; |
| 1490 |
tokcxdef *ret; |
| 1491 |
tokscdef *sc; |
| 1492 |
ushort siz; |
| 1493 |
|
| 1494 |
|
| 1495 |
memset(index, 0, (size_t)sizeof(index)); |
| 1496 |
for (i = cnt = 0, p = sctab ; (c = p->toklstr[0]) != 0 ; ++cnt, ++p) |
| 1497 |
if (!index[c]) index[c] = ++i; |
| 1498 |
|
| 1499 |
|
| 1500 |
siz = sizeof(tokcxdef) + (i * sizeof(tokscdef *)) |
| 1501 |
+ ((cnt + 1) * sizeof(tokscdef)); |
| 1502 |
ret = (tokcxdef *)mchalo(errctx, siz, "tokcxini"); |
| 1503 |
memset(ret, 0, (size_t)siz); |
| 1504 |
|
| 1505 |
|
| 1506 |
memcpy(ret->tokcxinx, index, sizeof(ret->tokcxinx)); |
| 1507 |
ret->tokcxerr = errctx; |
| 1508 |
ret->tokcxmem = mcmctx; |
| 1509 |
|
| 1510 |
|
| 1511 |
ret->tokcxifcur = TOKIF_IF_YES; |
| 1512 |
|
| 1513 |
|
| 1514 |
ret->tokcxptr = "\000"; |
| 1515 |
|
| 1516 |
|
| 1517 |
sc = (tokscdef *)&ret->tokcxsc[i+1]; |
| 1518 |
|
| 1519 |
|
| 1520 |
for (p = sctab ; (c = p->toklstr[0]) != 0 ; ++p, ++sc) |
| 1521 |
{ |
| 1522 |
size_t len; |
| 1523 |
|
| 1524 |
sc->toksctyp = p->tokltyp; |
| 1525 |
len = sc->toksclen = strlen(p->toklstr); |
| 1526 |
memcpy(sc->tokscstr, p->toklstr, len); |
| 1527 |
sc->tokscnxt = ret->tokcxsc[index[c]]; |
| 1528 |
ret->tokcxsc[index[c]] = sc; |
| 1529 |
} |
| 1530 |
|
| 1531 |
return(ret); |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
void tokaddinc(tokcxdef *ctx, char *path, int pathlen) |
| 1536 |
{ |
| 1537 |
tokpdef *newpath; |
| 1538 |
tokpdef *last; |
| 1539 |
|
| 1540 |
|
| 1541 |
for (last = ctx->tokcxinc ; last && last->tokpnxt ; |
| 1542 |
last = last->tokpnxt) ; |
| 1543 |
|
| 1544 |
|
| 1545 |
newpath = (tokpdef *)mchalo(ctx->tokcxerr, |
| 1546 |
(sizeof(tokpdef) + pathlen - 1), |
| 1547 |
"tokaddinc"); |
| 1548 |
newpath->tokplen = pathlen; |
| 1549 |
newpath->tokpnxt = (tokpdef *)0; |
| 1550 |
memcpy(newpath->tokpdir, path, (size_t)pathlen); |
| 1551 |
|
| 1552 |
|
| 1553 |
if (last) |
| 1554 |
last->tokpnxt = newpath; |
| 1555 |
else |
| 1556 |
ctx->tokcxinc = newpath; |
| 1557 |
} |
| 1558 |
|