Debian Codegraph @

terps/tads/tads2/tok.c

raw symbols SCIP
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 * Copyright (c) 1987, 1988 by Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 tok - tokenizer
15 Function
16 Tokenizes an input stream
17 Notes
18 None
19 Modified
20 04/11/99 CNebel - Enforce identifier length limits.
21 08/14/91 MJRoberts - created
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 /* special temporary buffers for <<expr>> macro expansion */
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 /* forward definition of static functions */
45 static int tokdfhsh(char *sym, int len);
46
47
48 /* find a #define symbol */
49 static tokdfdef *tok_find_define(tokcxdef *ctx, char *sym, int len)
50 {
51 int hsh;
52 tokdfdef *df;
53
54 /* find the appropriate chain the hash table */
55 hsh = tokdfhsh(sym, len);
56
57 /* search the chain for this symbol */
58 for (df = ctx->tokcxdf[hsh] ; df ; df = df->nxt)
59 {
60 /* if this one matches, return it */
61 if (df->len == len && !memcmp(df->nm, sym, (size_t)len))
62 {
63 /* fix it up if it's the special __FILE__ or __LINE__ symbol */
64 if (len == 8)
65 {
66 if (!memcmp(sym, "__FILE__", (size_t)8))
67 {
68 size_t len;
69
70 /*
71 * put in the opening single quote, since we want
72 * the expanded result to be a string
73 */
74 df->expan[0] = '\'';
75
76 /* get the name */
77 linnam(ctx->tokcxlin, df->expan+1);
78
79 /* get the length, and add the closing quote */
80 len = strlen(df->expan);
81 df->expan[len] = '\'';
82
83 /*
84 * set the length of the expansion, including the
85 * quotes (the first quote was measured in the
86 * length originally, but the second quote hasn't
87 * been counted yet, so add one to our original
88 * length)
89 */
90 df->explen = (int)len + 1;
91
92 /* if the expansion is too long, it's an error */
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 /* get the line number */
101 l = linlnum(ctx->tokcxlin);
102
103 /* convert it to a textual format for the expansion */
104 sprintf(df->expan, "%lu", l);
105
106 /* set the expanded value's length */
107 df->explen = strlen(df->expan);
108
109 /* make sure the expansion isn't too long */
110 if (df->explen >= 40)
111 errsig(ctx->tokcxerr, ERR_LONG_LINE_MACRO);
112 }
113 }
114
115 /* return it */
116 return df;
117 }
118 }
119
120 /* didn't find anything */
121 return 0;
122 }
123
124 /*
125 * Write preprocessor state to a file
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 /* write each element of the hash chains */
135 for (i = TOKDFHSHSIZ, dfp = ctx->tokcxdf ; i ; ++dfp, --i)
136 {
137 /* write each entry in this hash chain */
138 for (df = *dfp ; df ; df = df->nxt)
139 {
140 /* write this entry */
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 /* write a zero-length entry to indicate the end of this chain */
150 oswp2(buf, 0);
151 if (osfwb(fp, buf, 4)) errsig(ec, ERR_WRTGAM);
152 }
153 }
154
155 /*
156 * Read preprocessor state from a file
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 /* write each element of the hash chains */
166 for (i = TOKDFHSHSIZ, dfp = ctx->tokcxdf ; i ; ++dfp, --i)
167 {
168 /* read this hash chain */
169 for (;;)
170 {
171 /* read the next entry's header, and stop if this is the end */
172 if (osfrb(fp, buf, 4)) errsig(ec, ERR_RDGAM);
173 if (osrp2(buf) == 0) break;
174
175 /* set up a new symbol of the appropriate size */
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 /* read the rest of the symbol */
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 * If a symbol with this name already exists in the table,
191 * discard the new one -- the symbols defined by -D and the
192 * current set of built-in symbols takes precedence over the
193 * set loaded from the file.
194 */
195 if (tok_find_define(ctx, df->nm, df->len))
196 {
197 /* simply discard this symbol */
198 mchfre(df);
199 }
200 else
201 {
202 /* link it into this hash chain */
203 df->nxt = *dfp;
204 *dfp = df;
205 }
206 }
207 }
208 }
209
210
211
212 /* compute a #define symbol's hash value */
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 /* convert a #define symbol to lower case if folding case */
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 /* make a lower-case copy of the symbol */
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 /* use the lower-case copy instead of the original */
237 return outbuf;
238 }
239 else
240 {
241 /* return the original unchanged */
242 return src;
243 }
244 }
245
246 /*
247 * convert a token to lower-case if we're folding case
248 */
249 void tok_case_fold(tokcxdef *ctx, tokdef *tok)
250 {
251 /* if we're in case-insensitive mode, convert the token to lower-case */
252 if (ctx->tokcxflg & TOKCXCASEFOLD)
253 {
254 char *p;
255 int len;
256
257 /* convert each character in the token to lower-case */
258 for (p = tok->toknam, len = tok->toklen ; len != 0 ; ++p, --len)
259 {
260 /* if this character is upper-case, convert it to lower-case */
261 if (isupper((uchar)*p))
262 *p = tolower((uchar)*p);
263 }
264 }
265 }
266
267 /* add a symbol to the #define symbol table, folding case if necessary */
268 void tok_add_define_cvtcase(tokcxdef *ctx, char *sym, int len,
269 char *expan, int explen)
270 {
271 char mysym[TOKNAMMAX];
272
273 /* convert to lower-case if necessary */
274 sym = tok_casefold_defsym(ctx, mysym, sym, len);
275
276 /* add the symbol */
277 tok_add_define(ctx, sym, len, expan, explen);
278 }
279
280 /* add a symbol to the #define symbol table */
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 /* if it's already defined, ignore it */
288 if (tok_find_define(ctx, sym, len))
289 return;
290
291 /* find the appropriate entry in the hash table */
292 hsh = tokdfhsh(sym, len);
293
294 /* allocate space for the symbol */
295 df = (tokdfdef *)mchalo(ctx->tokcxerr,
296 (sizeof(tokdfdef) + len + explen - 1),
297 "tok_add_define");
298
299 /* set up the new symbol */
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 /* link it into the hash chain */
307 df->nxt = ctx->tokcxdf[hsh];
308 ctx->tokcxdf[hsh] = df;
309 }
310
311 /* add a #define symbol with a numeric value */
312 void tok_add_define_num_cvtcase(tokcxdef *ctx, char *sym, int len, int num)
313 {
314 char buf[20];
315
316 /* convert the value to a string */
317 sprintf(buf, "%d", num);
318
319 /* add the text value */
320 tok_add_define_cvtcase(ctx, sym, len, buf, strlen(buf));
321 }
322
323 /* undefine a #define symbol */
324 void tok_del_define(tokcxdef *ctx, char *sym, int len)
325 {
326 int hsh;
327 tokdfdef *df;
328 tokdfdef *prv;
329
330 /* find the appropriate chain the hash table */
331 hsh = tokdfhsh(sym, len);
332
333 /* search the chain for this symbol */
334 for (prv = 0, df = ctx->tokcxdf[hsh] ; df ; prv = df, df = df->nxt)
335 {
336 /* if this one matches, delete it */
337 if (df->len == len && !memcmp(df->nm, sym, (size_t)len))
338 {
339 /* unlink it from the chain */
340 if (prv)
341 prv->nxt = df->nxt;
342 else
343 ctx->tokcxdf[hsh] = df->nxt;
344
345 /* delete this symbol, and we're done */
346 mchfre(df);
347 break;
348 }
349 }
350 }
351
352 /* scan a #define symbol to see how long it is */
353 static int tok_scan_defsym(tokcxdef *ctx, char *p, int len)
354 {
355 int symlen;
356
357 /* make sure it's a valid symbol */
358 if (!(isalpha((uchar)*p) || *p == '_' || *p == '$'))
359 {
360 errlog(ctx->tokcxerr, ERR_REQSYM);
361 return 0;
362 }
363
364 /* count characters as long as we have valid symbol characters */
365 for (symlen = 0 ; len && TOKISSYM(*p) ; ++p, --len, ++symlen) ;
366 return symlen;
367 }
368
369 /* process a #define */
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 /* get the symbol */
378 sym = p;
379 if (!(symlen = tok_scan_defsym(ctx, p, len)))
380 return;
381
382 /* if it's already in the table, log an error */
383 if (tok_find_define(ctx, sym, symlen))
384 {
385 errlog(ctx->tokcxerr, ERR_DEFREDEF);
386 return;
387 }
388
389 /* skip whitespace following the symbol */
390 expan = sym + symlen;
391 len -= symlen;
392 while (len && t_isspace(*expan)) --len, ++expan;
393
394 /* if we're folding case, convert the symbol to lower case */
395 sym = tok_casefold_defsym(ctx, mysym, sym, symlen);
396
397 /* define the symbol */
398 tok_add_define(ctx, sym, symlen, expan, len);
399 }
400
401 /*
402 * Update the #if status for the current nesting. Any enclosing
403 * negative #if will override everything inside, so we need to look
404 * through the nesting from the outside in until we either determine
405 * that everything is affirmative or we find a negative anywhere in the
406 * nesting.
407 */
408 static void tok_update_if_stat(tokcxdef *ctx)
409 {
410 int i;
411
412 /* look through nesting from the outermost level */
413 for (i = 0 ; i < ctx->tokcxifcnt ; ++i)
414 {
415 /* assume this level will apply to everything inside */
416 ctx->tokcxifcur = ctx->tokcxif[i];
417
418 /* if this level is off, everything inside is off */
419 switch (ctx->tokcxif[i])
420 {
421 case TOKIF_IF_NO:
422 case TOKIF_ELSE_NO:
423 /*
424 * this level is off, hence everything inside is off -- stop
425 * here with the current (negative) determination
426 */
427 return;
428
429 default:
430 /* so far we're in the "on" section, so keep looking */
431 break;
432 }
433 }
434 }
435
436 /* process an #ifdef or a #ifndef */
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 /* get the symbol */
446 sym = p;
447 if (!(symlen = tok_scan_defsym(ctx, p, len)))
448 return;
449
450 /* if we're folding case, convert the symbol to lower case */
451 sym = tok_casefold_defsym(ctx, mysym, sym, symlen);
452
453 /* see if we can find it in the table, and set the status accordingly */
454 found = (tok_find_define(ctx, sym, symlen) != 0);
455
456 /* invert the test if this is an ifndef */
457 if (!is_ifdef) found = !found;
458
459 /* set the #if status accordingly */
460 if (found)
461 stat = TOKIF_IF_YES;
462 else
463 stat = TOKIF_IF_NO;
464 ctx->tokcxif[ctx->tokcxifcnt] = stat;
465
466 /* allocate a new #if level (making sure we have room) */
467 if (ctx->tokcxifcnt >= TOKIFNEST)
468 {
469 errlog(ctx->tokcxerr, ERR_MANYPIF);
470 return;
471 }
472 ctx->tokcxifcnt++;
473
474 /* update the current status */
475 tok_update_if_stat(ctx);
476 }
477
478 /* process a #error */
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 /* process a #ifdef */
486 static void tokifdef(tokcxdef *ctx, char *p, int len)
487 {
488 tok_ifdef_ifndef(ctx, p, len, TRUE);
489 }
490
491 /* process a #ifndef */
492 static void tokifndef(tokcxdef *ctx, char *p, int len)
493 {
494 tok_ifdef_ifndef(ctx, p, len, FALSE);
495 }
496
497 /* process a #if */
498 static void tokif(tokcxdef *ctx, char *p, int len)
499 {
500 errsig(ctx->tokcxerr, ERR_PIF_NA);
501 }
502
503 /* process a #elif */
504 static void tokelif(tokcxdef *ctx, char *p, int len)
505 {
506 errsig(ctx->tokcxerr, ERR_PELIF_NA);
507 }
508
509 /* process a #else */
510 static void tokelse(tokcxdef *ctx, char *p, int len)
511 {
512 int cnt;
513
514 /* if we're not expecting #else, it's an error */
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 /* switch to the appropriate #else state (opposite the #if state) */
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 /* update the current status */
530 tok_update_if_stat(ctx);
531 }
532
533 /* process a #endif */
534 static void tokendif(tokcxdef *ctx, char *p, int len)
535 {
536 /* if we're not expecting #endif, it's an error */
537 if (ctx->tokcxifcnt == 0)
538 {
539 errlog(ctx->tokcxerr, ERR_BADENDIF);
540 return;
541 }
542
543 /* remove the #if level */
544 ctx->tokcxifcnt--;
545
546 /* update the current status */
547 tok_update_if_stat(ctx);
548 }
549
550 /* process a #undef */
551 static void tokundef(tokcxdef *ctx, char *p, int len)
552 {
553 char *sym;
554 int symlen;
555 char mysym[TOKNAMMAX];
556
557 /* get the symbol */
558 sym = p;
559 if (!(symlen = tok_scan_defsym(ctx, p, len)))
560 return;
561
562 /* if we're folding case, convert the symbol to lower case */
563 sym = tok_casefold_defsym(ctx, mysym, sym, symlen);
564
565 /* if it's not defined, log a warning */
566 if (!tok_find_define(ctx, sym, symlen))
567 {
568 errlog(ctx->tokcxerr, ERR_PUNDEF);
569 return;
570 }
571
572 /* undefine the symbol */
573 tok_del_define(ctx, sym, symlen);
574 }
575
576 /* process a #pragma directive */
577 static void tokpragma(tokcxdef *ctx, char *p, int len)
578 {
579 /* ignore empty pragmas */
580 if (len == 0)
581 {
582 errlog(ctx->tokcxerr, ERR_PRAGMA);
583 return;
584 }
585
586 /* see what we have */
587 if (len > 1
588 && (*p == 'c' || *p == 'C')
589 && (*(p+1) == '+' || *(p+1) == '-' || t_isspace(*(p+1))))
590 {
591 /* skip spaces after the 'C', if any */
592 for (++p, --len ; len && t_isspace(*p) ; ++p, --len) ;
593
594 /* look for the + or - flag */
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 /* process a #include directive */
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 /* find the filename portion */
624 fname = p + 1; /* remember start of filename */
625 path = ctx->tokcxinc; /* start with first path entry */
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; /* skip 1st path */
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; /* compute length of filename */
654 for (q = p, flen2 = 0 ;
655 q > fname && *(q-1) != OSPATHCHAR && !strchr(OSPATHALT, *(q-1)) ;
656 --q, ++flen2) ;
657
658 /* check to see if this file has already been included */
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 /* initialize the line source */
678 child = linfini(ctx->tokcxmem, ctx->tokcxerr, fname, flen, path, TRUE,
679 (ctx->tokcxflg & TOKCXFLIN2) != 0);
680
681 /* if not found, signal an error */
682 if (!child) errsig1(ctx->tokcxerr, ERR_INCSEAR,
683 ERRTSTR, errstr(ctx->tokcxerr, fname, flen));
684
685 /* link into tokenizer list of line records */
686 child->linflin.linnxt = (lindef *)ctx->tokcxhdr;
687 ctx->tokcxhdr = child;
688
689 /* if we're tracking sources for debugging, add into the chain */
690 if (ctx->tokcxdbg)
691 {
692 ctx->tokcxdbg->dbgcxlin = &child->linflin;
693 child->linflin.linid = ctx->tokcxdbg->dbgcxfid++;
694 }
695
696 /* remember my C-mode setting */
697 if (ctx->tokcxflg & TOKCXFCMODE)
698 ctx->tokcxlin->linflg |= LINFCMODE;
699 else
700 ctx->tokcxlin->linflg &= ~LINFCMODE;
701
702 child->linflin.linpar = ctx->tokcxlin; /* remember parent line source */
703 ctx->tokcxlin = &child->linflin; /* make the child the current source */
704 }
705
706 /* get a new line from line source, processing '#' directives */
707 static int tokgetlin(tokcxdef *ctx, int dopound)
708 {
709 for (;;)
710 {
711 if (linget(ctx->tokcxlin))
712 {
713 /* at eof in current source; resume parent if there is one */
714 if (ctx->tokcxlin->linpar)
715 {
716 lindef *parent;
717
718 parent = ctx->tokcxlin->linpar; /* remember parent */
719 lincls(ctx->tokcxlin); /* close included file */
720 if (!ctx->tokcxdbg) /* if no debug context... */
721 mchfre(ctx->tokcxlin); /* free line source */
722 ctx->tokcxlin = parent; /* reset to parent line source */
723 if (parent->linflg & LINFCMODE)
724 ctx->tokcxflg |= TOKCXFCMODE;
725 else
726 ctx->tokcxflg &= ~TOKCXFCMODE;
727 continue; /* back for another attempt */
728 }
729 else
730 {
731 /* check for outstanding #if/#ifdef */
732 if (ctx->tokcxifcnt)
733 errlog(ctx->tokcxerr, ERR_NOENDIF);
734
735 /* return end-of-file indication */
736 return TRUE;
737 }
738 }
739
740 /* if this is a multi-segment line, copy it into our own buffer */
741 if (ctx->tokcxlin->linflg & LINFMORE)
742 {
743 char *p;
744 uint rem;
745 int done;
746
747 if (!ctx->tokcxbuf)
748 {
749 /* allocate 1k as a default buffer */
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 /* add the current segment's length into line length */
762 ctx->tokcxlen += len;
763
764 /* we're done after this piece if the last fetch was all */
765 done = !(ctx->tokcxlin->linflg & LINFMORE);
766 if (len + 1 > rem)
767 {
768 char *newp;
769
770 /* increase the size of the buffer */
771 if (ctx->tokcxbsz > (unsigned)0x8000)
772 errsig(ctx->tokcxerr, ERR_LONGLIN);
773 rem += 4096;
774 ctx->tokcxbsz += 4096;
775
776 /* allocate a new buffer and copy line into it */
777 newp = (char *)mchalo(ctx->tokcxerr, ctx->tokcxbsz, "tok");
778 memcpy(newp, ctx->tokcxbuf, (size_t)(p - ctx->tokcxbuf));
779
780 /* free the original buffer, and use the new one */
781 p = (p - ctx->tokcxbuf) + newp;
782 mchfre(ctx->tokcxbuf);
783 ctx->tokcxbuf = newp;
784 }
785
786 /* add the line to the buffer */
787 memcpy(p, ctx->tokcxlin->linbuf, len);
788 p += len;
789 rem -= len;
790
791 /* get the next piece of the line if there is one */
792 if (!done)
793 {
794 if (linget(ctx->tokcxlin)) break;
795 }
796 }
797
798 /* null-terminate the buffer, and use it for input */
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 /* check for preprocessor directives */
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 /* scan off spaces between '#' and directive */
838 for (len = ctx->tokcxlen - 1, p = &ctx->tokcxptr[1] ;
839 len && t_isspace(*p) ; --len, ++p) ;
840
841 /* find and process the directive */
842 for (dirp = dir, i = sizeof(dir)/sizeof(dir[0]) ; i ; --i, ++dirp)
843 {
844 /* compare this directive; if it wins, call its function */
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 * if we're not in a #if's false part, or if the
853 * directive is processed even in #if false parts,
854 * process the line, otherwise skip it
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 /* skip whitespace following the directive */
862 for (p += dirp->len, len -= dirp->len ;
863 len && t_isspace(*p) ;
864 --len, ++p) ;
865
866 /* invoke the function to process this directive */
867 (*dirp->fn)(ctx, p, len);
868 }
869
870 /* there's no need to look at more directives */
871 break;
872 }
873 }
874
875 /* if we didn't find anything, flag the error */
876 if (i == 0)
877 errlog(ctx->tokcxerr, ERR_PRPDIR);
878
879 /* ignore this line */
880 continue;
881 }
882 else
883 {
884 /*
885 * Check the #if level. If we're in an #if, and we're to
886 * ignore lines (because of a false condition or an #else
887 * part for a true condition), skip this line.
888 */
889 if (ctx->tokcxifcnt != 0)
890 {
891 switch(ctx->tokcxifcur)
892 {
893 case TOKIF_IF_NO:
894 case TOKIF_ELSE_NO:
895 /* ignore this line */
896 continue;
897
898 default:
899 /* we're in a true part - keep the line */
900 break;
901 }
902 }
903
904 ctx->tokcxlin->linflg &= ~LINFDBG; /* no debug record yet */
905 return(FALSE); /* return the line we found */
906 }
907 }
908 }
909
910 /* get the next token, removing it from the input stream */
911 int toknext(tokcxdef *ctx)
912 {
913 char *p;
914 tokdef *tok = &ctx->tokcxcur;
915 int len;
916
917 /*
918 * Check for the special case that we pushed an open paren prior to
919 * a string containing an embedded expression. If this is the case,
920 * immediately return the string we previously parsed.
921 */
922 if ((ctx->tokcxflg & TOKCXF_EMBED_PAREN_PRE) != 0)
923 {
924 /*
925 * convert the token to a string - note that the offset
926 * information for the string is already in the current token
927 * structure, since we set everything up for it on the previous
928 * call where we actually parsed the beginning of the string
929 */
930 tok->toktyp = TOKTDSTRING;
931
932 /* clear the special flag - we've now consumed the pushed string */
933 ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_PRE;
934
935 /* immediately return the string */
936 return tok->toktyp;
937 }
938
939 /* set up at the current scanning position */
940 p = ctx->tokcxptr;
941 len = ctx->tokcxlen;
942
943 /* scan off whitespace and comments until we find something */
944 do
945 {
946 skipblanks:
947 /* if there's nothing on this line, get the next one */
948 if (len == 0)
949 {
950 /* if we're in a macro expansion, continue after it */
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; /* scan off whitespace */
969
970 /* check for comments, and remove if present */
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 * check for the special defined() preprocessor operator
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 /* find the matching ')', allowing only symbolic characters */
1049 ++p, --len;
1050 for (symlen = 0, q = p ; len && *p != ')' && TOKISSYM(*p) ;
1051 ++p, --len, ++symlen) ;
1052
1053 /* make sure we found the closing paren */
1054 if (!len || *p != ')')
1055 errsig(ctx->tokcxerr, ERR_BADISDEF);
1056 ++p, --len;
1057
1058 /* if we're folding case, convert the symbol to lower case */
1059 q = tok_casefold_defsym(ctx, mysym, q, symlen);
1060
1061 /* check to see if it's defined */
1062 tok->toktyp = TOKTNUMBER;
1063 tok->tokval = (tok_find_define(ctx, q, symlen) != 0);
1064 goto done;
1065 }
1066
1067 /* substitute the preprocessor #define, if any */
1068 if ((df = tok_find_define(ctx, tok->toknam, l)) != 0)
1069 {
1070 /* save the current parsing position */
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 /* point to the token's expansion and keep going */
1078 p = df->expan;
1079 len = df->explen;
1080 goto nexttoken;
1081 }
1082
1083 /* look up in symbol table(s), if any */
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 /* check for octal/hex */
1105 if (*p == '0')
1106 {
1107 ++p, --len;
1108 if (len && (*p == 'x' || *p == 'X'))
1109 {
1110 /* hex */
1111 ++p, --len;
1112 while (len && TOKISHEX(*p))
1113 {
1114 acc = (acc << 4) + TOKHEX2INT(*p);
1115 ++p, --len;
1116 }
1117 }
1118 else
1119 {
1120 /* octal */
1121 while (len && TOKISOCT(*p))
1122 {
1123 acc = (acc << 3) + TOKOCT2INT(*p);
1124 ++p, --len;
1125 }
1126 }
1127 }
1128 else
1129 {
1130 /* decimal */
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; /* closing delimiter we're looking for */
1144 char *strstart; /* pointer to start of string */
1145 int warned;
1146
1147 delim = *p;
1148 --len;
1149 strstart = ++p;
1150
1151 if (delim == '"' && len >= 2 && *p == '<' && *(p+1) == '<')
1152 {
1153 /* save the current parsing position */
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 * read from the special "<<" expansion string - use the
1162 * version for a "<<" at the very beginning of the string
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); /* start the string */
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 /* warn if it looks like the end of an object */
1210 if (!warned && len && (*p == ';' || *p == '}'))
1211 {
1212 errlog(ctx->tokcxerr, ERR_STREND);
1213 warned = TRUE; /* warn only once per string */
1214 }
1215
1216 /* scan past whitespace at start of line */
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 /* end the string */
1228 (*ctx->tokcxsend)(ctx->tokcxscx);
1229
1230 /* check to see how it ended */
1231 if (len != 0 && *p == delim)
1232 {
1233 /*
1234 * We ended with the matching delimiter. Move past the
1235 * closing delimiter.
1236 */
1237 ++p;
1238 --len;
1239
1240 /*
1241 * If we have a pending close paren we need to put in
1242 * because of an embedded expression that occurred earlier
1243 * in the string, parse the macro to provide the paren.
1244 */
1245 if ((ctx->tokcxflg & TOKCXF_EMBED_PAREN_AFT) != 0
1246 && !(ctx->tokcxflg & TOKCXFINMAC))
1247 {
1248 /* clear the flag */
1249 ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_AFT;
1250
1251 /* push the current parsing position */
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 /* parse the macro */
1259 p = tokmac4;
1260 len = strlen(p);
1261 }
1262 }
1263 else if (len != 0 && *p == '<')
1264 {
1265 /* save the current parsing position */
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 /* read from the "<<" expansion */
1273 p = tokmac1;
1274 len = strlen(p);
1275 ctx->tokcxflg |= TOKCXFINMAC;
1276
1277 /*
1278 * Set the special push-a-paren flag: we'll return an open
1279 * paren now, so that we have an open paren before the
1280 * string, and then on the next call to toknext() we'll
1281 * immediately return the string we've already parsed here.
1282 * This will ensure that everything in the string is
1283 * properly grouped together as a single indivisible
1284 * expression.
1285 *
1286 * Note that we only need to do this for the first embedded
1287 * expression in a string. Once we have a close paren
1288 * pending, we don't need more open parens.
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 /* skip the ">>" */
1302 ctx->tokcxflg &= ~TOKCXFINMAC;
1303 p += 2;
1304 len -= 2;
1305
1306 /* save the current parsing position */
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 * we won't need an extra closing paren now, since tokmac3
1321 * provides it
1322 */
1323 ctx->tokcxflg &= ~TOKCXF_EMBED_PAREN_AFT;
1324 }
1325 else
1326 {
1327 /*
1328 * The string is continuing. Set a flag to note that we
1329 * need to provide a close paren after the end of the
1330 * string, and parse the glue (tokmac2) that goes between
1331 * the expression and the resumption of the string.
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 /* initialize a linear symbol table */
1365 void toktlini(errcxdef *errctx, toktldef *toktab, uchar *mem, uint siz)
1366 {
1367 CLRSTRUCT(*toktab);
1368
1369 /* initialize superclass data */
1370 toktab->toktlsc.toktfadd = toktladd; /* set add-symbol method */
1371 toktab->toktlsc.toktfsea = toktlsea; /* set search-table method */
1372 toktab->toktlsc.toktfeach = toktleach; /* set 'each' method */
1373 toktab->toktlsc.toktfset = toktlset; /* set 'update' method */
1374 toktab->toktlsc.tokterr = errctx; /* set error handling context */
1375
1376 /* initialize class data */
1377 toktab->toktlptr = mem;
1378 toktab->toktlnxt = mem;
1379 toktab->toktlsiz = siz;
1380 }
1381
1382 /* add a symbol to a linear symbol table */
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 /* set up symbol */
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 /* indicate there's one more symbol in the table */
1409 ++(toktab->toktlcnt);
1410 }
1411
1412 /* delete all symbols from a linear symbol table */
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 /* call a function for every symbol in a linear symbol table */
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 /* search a linear symbol table */
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 /* nothing found - indicate by returning FALSE */
1458 return(FALSE);
1459 }
1460
1461 /* update a symbol in a linear symbol table */
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 /* set up index table: finds tokcxsc entry from character value */
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 /* allocate memory for table plus the tokscdef's */
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 /* copy the index, set up fixed part */
1506 memcpy(ret->tokcxinx, index, sizeof(ret->tokcxinx));
1507 ret->tokcxerr = errctx;
1508 ret->tokcxmem = mcmctx;
1509
1510 /* start out without an #if */
1511 ret->tokcxifcur = TOKIF_IF_YES;
1512
1513 /* force the first toknext() to read a line */
1514 ret->tokcxptr = "\000";
1515
1516 /* figure where the tokscdef's go (right after sc pointer array) */
1517 sc = (tokscdef *)&ret->tokcxsc[i+1];
1518
1519 /* set up the individual tokscdef entries, and link into lists */
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 /* add an include path to a tokdef */
1535 void tokaddinc(tokcxdef *ctx, char *path, int pathlen)
1536 {
1537 tokpdef *newpath;
1538 tokpdef *last;
1539
1540 /* find the tail of the include path list, if any */
1541 for (last = ctx->tokcxinc ; last && last->tokpnxt ;
1542 last = last->tokpnxt) ;
1543
1544 /* allocate storage for and set up a new path structure */
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 /* link in at end of list (if no list yet, newpath becomes first entry) */
1553 if (last)
1554 last->tokpnxt = newpath;
1555 else
1556 ctx->tokcxinc = newpath;
1557 }
1558