[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / m4

Annotation of src/usr.bin/m4/main.c, Revision 1.54

1.54    ! espie       1: /*     $OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $ */
1.7       deraadt     2: /*     $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $    */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Ozan Yigit at York University.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: static char copyright[] =
                     42: "@(#) Copyright (c) 1989, 1993\n\
                     43:        The Regents of the University of California.  All rights reserved.\n";
                     44: #endif /* not lint */
                     45:
                     46: #ifndef lint
                     47: #if 0
                     48: static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 6/6/93";
                     49: #else
1.54    ! espie      50: static char rcsid[] = "$OpenBSD: main.c,v 1.53 2002/04/26 16:15:16 espie Exp $";
1.1       deraadt    51: #endif
                     52: #endif /* not lint */
                     53:
                     54: /*
                     55:  * main.c
                     56:  * Facility: m4 macro processor
                     57:  * by: oz
                     58:  */
                     59:
                     60: #include <sys/types.h>
1.21      espie      61: #include <assert.h>
1.1       deraadt    62: #include <signal.h>
                     63: #include <errno.h>
                     64: #include <unistd.h>
                     65: #include <stdio.h>
                     66: #include <ctype.h>
                     67: #include <string.h>
1.13      espie      68: #include <stddef.h>
1.34      espie      69: #include <stdlib.h>
1.11      espie      70: #include <err.h>
1.1       deraadt    71: #include "mdef.h"
                     72: #include "stdd.h"
                     73: #include "extern.h"
                     74: #include "pathnames.h"
                     75:
                     76: ndptr hashtab[HASHSIZE];       /* hash table for macros etc.  */
1.34      espie      77: stae *mstack;                  /* stack of m4 machine         */
                     78: char *sstack;                  /* shadow stack, for string space extension */
                     79: static size_t STACKMAX;                /* current maximum size of stack */
1.1       deraadt    80: int sp;                        /* current m4  stack pointer   */
                     81: int fp;                        /* m4 call frame pointer       */
1.26      espie      82: struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
1.36      espie      83: FILE **outfile;                        /* diversion array(0=bitbucket)*/
                     84: int maxout;
1.1       deraadt    85: FILE *active;                  /* active output file pointer  */
                     86: int ilevel = 0;                /* input file stack pointer    */
                     87: int oindex = 0;                /* diversion index..           */
                     88: char *null = "";                /* as it says.. just a null..  */
                     89: char *m4wraps = "";             /* m4wrap string default..     */
1.2       deraadt    90: char lquote[MAXCCHARS+1] = {LQUOTE};   /* left quote character  (`)   */
                     91: char rquote[MAXCCHARS+1] = {RQUOTE};   /* right quote character (')   */
                     92: char scommt[MAXCCHARS+1] = {SCOMMT};   /* start character for comment */
                     93: char ecommt[MAXCCHARS+1] = {ECOMMT};   /* end character for comment   */
1.54    ! espie      94: int  synch_lines = 0;          /* line synchronisation for C preprocessor */
1.1       deraadt    95:
                     96: struct keyblk keywrds[] = {    /* m4 keywords to be installed */
1.8       millert    97:        { "include",      INCLTYPE },
                     98:        { "sinclude",     SINCTYPE },
                     99:        { "define",       DEFITYPE },
                    100:        { "defn",         DEFNTYPE },
1.24      espie     101:        { "divert",       DIVRTYPE | NOARGS },
1.8       millert   102:        { "expr",         EXPRTYPE },
                    103:        { "eval",         EXPRTYPE },
                    104:        { "substr",       SUBSTYPE },
                    105:        { "ifelse",       IFELTYPE },
                    106:        { "ifdef",        IFDFTYPE },
                    107:        { "len",          LENGTYPE },
                    108:        { "incr",         INCRTYPE },
                    109:        { "decr",         DECRTYPE },
1.24      espie     110:        { "dnl",          DNLNTYPE | NOARGS },
                    111:        { "changequote",  CHNQTYPE | NOARGS },
                    112:        { "changecom",    CHNCTYPE | NOARGS },
1.8       millert   113:        { "index",        INDXTYPE },
1.1       deraadt   114: #ifdef EXTENDED
1.8       millert   115:        { "paste",        PASTTYPE },
                    116:        { "spaste",       SPASTYPE },
1.31      espie     117:        /* Newer extensions, needed to handle gnu-m4 scripts */
                    118:        { "indir",        INDIRTYPE},
                    119:        { "builtin",      BUILTINTYPE},
                    120:        { "patsubst",     PATSTYPE},
                    121:        { "regexp",       REGEXPTYPE},
1.35      espie     122:        { "esyscmd",      ESYSCMDTYPE},
1.31      espie     123:        { "__file__",     FILENAMETYPE | NOARGS},
                    124:        { "__line__",     LINETYPE | NOARGS},
1.1       deraadt   125: #endif
1.8       millert   126:        { "popdef",       POPDTYPE },
                    127:        { "pushdef",      PUSDTYPE },
1.24      espie     128:        { "dumpdef",      DUMPTYPE | NOARGS },
                    129:        { "shift",        SHIFTYPE | NOARGS },
1.8       millert   130:        { "translit",     TRNLTYPE },
                    131:        { "undefine",     UNDFTYPE },
1.24      espie     132:        { "undivert",     UNDVTYPE | NOARGS },
                    133:        { "divnum",       DIVNTYPE | NOARGS },
1.8       millert   134:        { "maketemp",     MKTMTYPE },
1.24      espie     135:        { "errprint",     ERRPTYPE | NOARGS },
                    136:        { "m4wrap",       M4WRTYPE | NOARGS },
                    137:        { "m4exit",       EXITTYPE | NOARGS },
1.8       millert   138:        { "syscmd",       SYSCTYPE },
1.24      espie     139:        { "sysval",       SYSVTYPE | NOARGS },
1.49      espie     140:        { "traceon",      TRACEONTYPE | NOARGS },
                    141:        { "traceoff",     TRACEOFFTYPE | NOARGS },
1.1       deraadt   142:
1.24      espie     143: #if defined(unix) || defined(__unix__)
                    144:        { "unix",         SELFTYPE | NOARGS },
1.1       deraadt   145: #else
                    146: #ifdef vms
1.24      espie     147:        { "vms",          SELFTYPE | NOARGS },
1.1       deraadt   148: #endif
                    149: #endif
                    150: };
                    151:
                    152: #define MAXKEYS        (sizeof(keywrds)/sizeof(struct keyblk))
                    153:
                    154: extern int optind;
                    155: extern char *optarg;
                    156:
1.27      espie     157: #define MAXRECORD 50
                    158: static struct position {
                    159:        char *name;
                    160:        unsigned long line;
                    161: } quotes[MAXRECORD], paren[MAXRECORD];
                    162:
1.52      millert   163: static void record(struct position *, int);
                    164: static void dump_stack(struct position *, int);
1.27      espie     165:
1.52      millert   166: static void macro(void);
                    167: static void initkwds(void);
                    168: static ndptr inspect(int, char *);
                    169: static int do_look_ahead(int, const char *);
1.54    ! espie     170: static void reallyoutputstr(const char *);
        !           171: static void reallyputchar(int);
1.18      espie     172:
1.52      millert   173: static void enlarge_stack(void);
1.34      espie     174:
1.52      millert   175: int main(int, char *[]);
1.1       deraadt   176:
                    177: int
1.53      espie     178: main(int argc, char *argv[])
1.1       deraadt   179: {
1.17      espie     180:        int c;
                    181:        int n;
1.1       deraadt   182:        char *p;
                    183:
                    184:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    185:                signal(SIGINT, onintr);
                    186:
                    187:        initkwds();
1.14      espie     188:        initspaces();
1.34      espie     189:        STACKMAX = INITSTACKMAX;
                    190:
                    191:        mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
                    192:        sstack = (char *)xalloc(STACKMAX);
1.1       deraadt   193:
1.36      espie     194:        maxout = 0;
                    195:        outfile = NULL;
                    196:        resizedivs(MAXOUT);
                    197:
1.54    ! espie     198:        while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
1.1       deraadt   199:                switch(c) {
                    200:
                    201:                case 'D':               /* define something..*/
                    202:                        for (p = optarg; *p; p++)
                    203:                                if (*p == '=')
                    204:                                        break;
                    205:                        if (*p)
                    206:                                *p++ = EOS;
                    207:                        dodefine(optarg, p);
1.16      espie     208:                        break;
                    209:                case 'I':
                    210:                        addtoincludepath(optarg);
1.1       deraadt   211:                        break;
                    212:                case 'U':               /* undefine...       */
                    213:                        remhash(optarg, TOP);
1.32      espie     214:                        break;
                    215:                case 'g':
                    216:                        mimic_gnu = 1;
1.1       deraadt   217:                        break;
1.46      espie     218:                case 'd':
                    219:                        set_trace_flags(optarg);
1.47      espie     220:                        break;
1.54    ! espie     221:                case 's':
        !           222:                        synch_lines = 1;
        !           223:                        break;
1.47      espie     224:                case 't':
1.49      espie     225:                        mark_traced(optarg, 1);
1.46      espie     226:                        break;
1.38      aaron     227:                case 'o':
1.46      espie     228:                        trace_file(optarg);
1.38      aaron     229:                         break;
1.1       deraadt   230:                case '?':
                    231:                        usage();
                    232:                }
                    233:
                    234:         argc -= optind;
                    235:         argv += optind;
                    236:
                    237:        active = stdout;                /* default active output     */
                    238:        bbase[0] = bufbase;
                    239:         if (!argc) {
                    240:                sp = -1;                /* stack pointer initialized */
                    241:                fp = 0;                 /* frame pointer initialized */
1.26      espie     242:                set_input(infile+0, stdin, "stdin");
                    243:                                        /* default input (naturally) */
1.1       deraadt   244:                macro();
                    245:        } else
                    246:                for (; argc--; ++argv) {
                    247:                        p = *argv;
1.13      espie     248:                        if (p[0] == '-' && p[1] == EOS)
1.26      espie     249:                                set_input(infile, stdin, "stdin");
                    250:                        else if (fopen_trypath(infile, p) == NULL)
1.11      espie     251:                                err(1, "%s", p);
1.1       deraadt   252:                        sp = -1;
                    253:                        fp = 0;
                    254:                        macro();
1.26      espie     255:                        release_input(infile);
1.1       deraadt   256:                }
                    257:
                    258:        if (*m4wraps) {                 /* anything for rundown ??   */
                    259:                ilevel = 0;             /* in case m4wrap includes.. */
                    260:                bufbase = bp = buf;     /* use the entire buffer   */
                    261:                pbstr(m4wraps);         /* user-defined wrapup act   */
                    262:                macro();                /* last will and testament   */
                    263:        }
                    264:
                    265:        if (active != stdout)
                    266:                active = stdout;        /* reset output just in case */
1.36      espie     267:        for (n = 1; n < maxout; n++)    /* default wrap-up: undivert */
1.1       deraadt   268:                if (outfile[n] != NULL)
                    269:                        getdiv(n);
                    270:                                        /* remove bitbucket if used  */
                    271:        if (outfile[0] != NULL) {
                    272:                (void) fclose(outfile[0]);
                    273:        }
                    274:
                    275:        return 0;
                    276: }
                    277:
                    278: /*
1.21      espie     279:  * Look ahead for `token'.
1.2       deraadt   280:  * (on input `t == token[0]')
                    281:  * Used for comment and quoting delimiters.
                    282:  * Returns 1 if `token' present; copied to output.
                    283:  *         0 if `token' not found; all characters pushed back
                    284:  */
1.18      espie     285: static int
1.53      espie     286: do_look_ahead(int t, const char *token)
1.2       deraadt   287: {
                    288:        int i;
                    289:
1.43      espie     290:        assert((unsigned char)t == (unsigned char)token[0]);
1.2       deraadt   291:
                    292:        for (i = 1; *++token; i++) {
                    293:                t = gpbc();
1.43      espie     294:                if (t == EOF || (unsigned char)t != (unsigned char)*token) {
1.28      espie     295:                        putback(t);
1.2       deraadt   296:                        while (--i)
                    297:                                putback(*--token);
                    298:                        return 0;
                    299:                }
                    300:        }
                    301:        return 1;
                    302: }
                    303:
1.43      espie     304: #define LOOK_AHEAD(t, token) (t != EOF &&              \
                    305:     (unsigned char)(t)==(unsigned char)(token)[0] &&   \
                    306:     do_look_ahead(t,token))
1.2       deraadt   307:
                    308: /*
1.1       deraadt   309:  * macro - the work horse..
                    310:  */
1.18      espie     311: static void
1.17      espie     312: macro()
                    313: {
1.34      espie     314:        char token[MAXTOK+1];
1.17      espie     315:        int t, l;
                    316:        ndptr p;
                    317:        int  nlpar;
1.1       deraadt   318:
                    319:        cycle {
1.2       deraadt   320:                t = gpbc();
                    321:                if (t == '_' || isalpha(t)) {
1.29      espie     322:                        p = inspect(t, token);
1.24      espie     323:                        if (p != nil)
                    324:                                putback(l = gpbc());
                    325:                        if (p == nil || (l != LPAREN &&
                    326:                            (p->type & NEEDARGS) != 0))
1.29      espie     327:                                outputstr(token);
1.1       deraadt   328:                        else {
                    329:                /*
                    330:                 * real thing.. First build a call frame:
                    331:                 */
                    332:                                pushf(fp);      /* previous call frm */
                    333:                                pushf(p->type); /* type of the call  */
                    334:                                pushf(0);       /* parenthesis level */
                    335:                                fp = sp;        /* new frame pointer */
                    336:                /*
                    337:                 * now push the string arguments:
                    338:                 */
1.34      espie     339:                                pushs1(p->defn);        /* defn string */
                    340:                                pushs1(p->name);        /* macro name  */
                    341:                                pushs(ep);              /* start next..*/
1.1       deraadt   342:
1.41      espie     343:                                if (l != LPAREN && PARLEV == 0)  {
                    344:                                    /* no bracks  */
                    345:                                        chrsave(EOS);
                    346:
                    347:                                        if (sp == STACKMAX)
                    348:                                                errx(1, "internal stack overflow");
1.44      espie     349:                                        eval((const char **) mstack+fp+1, 2,
                    350:                                            CALTYP);
1.41      espie     351:
                    352:                                        ep = PREVEP;    /* flush strspace */
                    353:                                        sp = PREVSP;    /* previous sp..  */
                    354:                                        fp = PREVFP;    /* rewind stack...*/
1.1       deraadt   355:                                }
                    356:                        }
1.41      espie     357:                } else if (t == EOF) {
1.27      espie     358:                        if (sp > -1) {
                    359:                                warnx( "unexpected end of input, unclosed parenthesis:");
                    360:                                dump_stack(paren, PARLEV);
                    361:                                exit(1);
                    362:                        }
1.1       deraadt   363:                        if (ilevel <= 0)
                    364:                                break;                  /* all done thanks.. */
1.26      espie     365:                        release_input(infile+ilevel--);
1.54    ! espie     366:                        emit_synchline();
1.1       deraadt   367:                        bufbase = bbase[ilevel];
                    368:                        continue;
                    369:                }
                    370:        /*
1.7       deraadt   371:         * non-alpha token possibly seen..
1.1       deraadt   372:         * [the order of else if .. stmts is important.]
                    373:         */
1.2       deraadt   374:                else if (LOOK_AHEAD(t,lquote)) {        /* strip quotes */
1.27      espie     375:                        nlpar = 0;
                    376:                        record(quotes, nlpar++);
1.30      espie     377:                        /*
                    378:                         * Opening quote: scan forward until matching
                    379:                         * closing quote has been found.
                    380:                         */
1.1       deraadt   381:                        do {
1.7       deraadt   382:
1.2       deraadt   383:                                l = gpbc();
1.7       deraadt   384:                                if (LOOK_AHEAD(l,rquote)) {
1.29      espie     385:                                        if (--nlpar > 0)
                    386:                                                outputstr(rquote);
1.7       deraadt   387:                                } else if (LOOK_AHEAD(l,lquote)) {
1.27      espie     388:                                        record(quotes, nlpar++);
1.29      espie     389:                                        outputstr(lquote);
1.17      espie     390:                                } else if (l == EOF) {
                    391:                                        if (nlpar == 1)
1.27      espie     392:                                                warnx("unclosed quote:");
1.17      espie     393:                                        else
1.27      espie     394:                                                warnx("%d unclosed quotes:", nlpar);
                    395:                                        dump_stack(quotes, nlpar);
                    396:                                        exit(1);
1.17      espie     397:                                } else {
1.29      espie     398:                                        if (nlpar > 0) {
                    399:                                                if (sp < 0)
1.54    ! espie     400:                                                        reallyputchar(l);
1.29      espie     401:                                                else
1.48      espie     402:                                                        CHRSAVE(l);
1.29      espie     403:                                        }
1.7       deraadt   404:                                }
1.1       deraadt   405:                        }
                    406:                        while (nlpar != 0);
                    407:                }
                    408:
1.2       deraadt   409:                else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.54    ! espie     410:                        reallyoutputstr(scommt);
1.2       deraadt   411:
                    412:                        for(;;) {
                    413:                                t = gpbc();
                    414:                                if (LOOK_AHEAD(t, ecommt)) {
1.54    ! espie     415:                                        reallyoutputstr(ecommt);
1.2       deraadt   416:                                        break;
                    417:                                }
                    418:                                if (t == EOF)
                    419:                                        break;
1.54    ! espie     420:                                reallyputchar(t);
1.1       deraadt   421:                        }
1.2       deraadt   422:                }
                    423:
                    424:                else if (sp < 0) {              /* not in a macro at all */
1.54    ! espie     425:                        reallyputchar(t);       /* output directly..     */
1.1       deraadt   426:                }
                    427:
                    428:                else switch(t) {
                    429:
                    430:                case LPAREN:
                    431:                        if (PARLEV > 0)
                    432:                                chrsave(t);
                    433:                        while (isspace(l = gpbc()))
                    434:                                ;               /* skip blank, tab, nl.. */
                    435:                        putback(l);
1.27      espie     436:                        record(paren, PARLEV++);
1.1       deraadt   437:                        break;
                    438:
                    439:                case RPAREN:
                    440:                        if (--PARLEV > 0)
                    441:                                chrsave(t);
                    442:                        else {                  /* end of argument list */
                    443:                                chrsave(EOS);
                    444:
                    445:                                if (sp == STACKMAX)
1.11      espie     446:                                        errx(1, "internal stack overflow");
1.1       deraadt   447:
1.44      espie     448:                                eval((const char **) mstack+fp+1, sp-fp,
                    449:                                    CALTYP);
1.1       deraadt   450:
                    451:                                ep = PREVEP;    /* flush strspace */
                    452:                                sp = PREVSP;    /* previous sp..  */
                    453:                                fp = PREVFP;    /* rewind stack...*/
                    454:                        }
                    455:                        break;
                    456:
                    457:                case COMMA:
                    458:                        if (PARLEV == 1) {
                    459:                                chrsave(EOS);           /* new argument   */
                    460:                                while (isspace(l = gpbc()))
                    461:                                        ;
                    462:                                putback(l);
                    463:                                pushs(ep);
                    464:                        } else
                    465:                                chrsave(t);
                    466:                        break;
                    467:
                    468:                default:
1.22      espie     469:                        if (LOOK_AHEAD(t, scommt)) {
                    470:                                char *p;
                    471:                                for (p = scommt; *p; p++)
                    472:                                        chrsave(*p);
                    473:                                for(;;) {
                    474:                                        t = gpbc();
                    475:                                        if (LOOK_AHEAD(t, ecommt)) {
                    476:                                                for (p = ecommt; *p; p++)
                    477:                                                        chrsave(*p);
                    478:                                                break;
                    479:                                        }
                    480:                                        if (t == EOF)
                    481:                                            break;
1.48      espie     482:                                        CHRSAVE(t);
1.22      espie     483:                                }
                    484:                        } else
1.48      espie     485:                                CHRSAVE(t);             /* stack the char */
1.1       deraadt   486:                        break;
                    487:                }
                    488:        }
                    489: }
                    490:
1.24      espie     491: /*
                    492:  * output string directly, without pushing it for reparses.
                    493:  */
                    494: void
1.53      espie     495: outputstr(const char *s)
1.24      espie     496: {
                    497:        if (sp < 0)
1.54    ! espie     498:                reallyoutputstr(s);
1.24      espie     499:        else
                    500:                while (*s)
1.48      espie     501:                        CHRSAVE(*s++);
1.24      espie     502: }
                    503:
1.54    ! espie     504: void
        !           505: reallyoutputstr(const char *s)
        !           506: {
        !           507:        if (synch_lines) {
        !           508:                while (*s) {
        !           509:                        fputc(*s, active);
        !           510:                        if (*s++ == '\n') {
        !           511:                                infile[ilevel].synch_lineno++;
        !           512:                                if (infile[ilevel].synch_lineno !=
        !           513:                                    infile[ilevel].lineno)
        !           514:                                        do_emit_synchline();
        !           515:                        }
        !           516:                }
        !           517:        } else
        !           518:                fputs(s, active);
        !           519: }
        !           520:
        !           521: void
        !           522: reallyputchar(int c)
        !           523: {
        !           524:        putc(c, active);
        !           525:        if (synch_lines && c == '\n') {
        !           526:                infile[ilevel].synch_lineno++;
        !           527:                if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
        !           528:                        do_emit_synchline();
        !           529:        }
        !           530: }
        !           531:
1.1       deraadt   532: /*
                    533:  * build an input token..
                    534:  * consider only those starting with _ or A-Za-z. This is a
                    535:  * combo with lookup to speed things up.
                    536:  */
1.18      espie     537: static ndptr
1.53      espie     538: inspect(int c, char *tp)
1.1       deraadt   539: {
1.17      espie     540:        char *name = tp;
                    541:        char *etp = tp+MAXTOK;
                    542:        ndptr p;
1.25      espie     543:        unsigned int h;
                    544:
                    545:        h = *tp++ = c;
1.1       deraadt   546:
                    547:        while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
                    548:                h = (h << 5) + h + (*tp++ = c);
1.51      espie     549:        if (c != EOF)
                    550:                PUTBACK(c);
1.1       deraadt   551:        *tp = EOS;
1.33      espie     552:        /* token is too long, it won't match anything, but it can still
                    553:         * be output. */
                    554:        if (tp == ep) {
                    555:                outputstr(name);
                    556:                while (isalnum(c = gpbc()) || c == '_') {
                    557:                        if (sp < 0)
1.54    ! espie     558:                                reallyputchar(c);
1.33      espie     559:                        else
1.48      espie     560:                                CHRSAVE(c);
1.33      espie     561:                }
                    562:                *name = EOS;
                    563:                return nil;
                    564:        }
1.1       deraadt   565:
1.19      espie     566:        for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
                    567:                if (h == p->hv && STREQ(name, p->name))
1.1       deraadt   568:                        break;
                    569:        return p;
                    570: }
                    571:
                    572: /*
                    573:  * initkwds - initialise m4 keywords as fast as possible.
                    574:  * This very similar to install, but without certain overheads,
                    575:  * such as calling lookup. Malloc is not used for storing the
1.17      espie     576:  * keyword strings, since we simply use the static pointers
1.1       deraadt   577:  * within keywrds block.
                    578:  */
1.18      espie     579: static void
1.17      espie     580: initkwds()
                    581: {
                    582:        size_t i;
1.20      millert   583:        unsigned int h;
1.17      espie     584:        ndptr p;
1.1       deraadt   585:
                    586:        for (i = 0; i < MAXKEYS; i++) {
                    587:                h = hash(keywrds[i].knam);
                    588:                p = (ndptr) xalloc(sizeof(struct ndblock));
1.19      espie     589:                p->nxtptr = hashtab[h % HASHSIZE];
                    590:                hashtab[h % HASHSIZE] = p;
1.42      espie     591:                p->name = xstrdup(keywrds[i].knam);
1.1       deraadt   592:                p->defn = null;
1.19      espie     593:                p->hv = h;
1.42      espie     594:                p->type = keywrds[i].ktyp & TYPEMASK;
1.24      espie     595:                if ((keywrds[i].ktyp & NOARGS) == 0)
                    596:                        p->type |= NEEDARGS;
1.1       deraadt   597:        }
                    598: }
1.31      espie     599:
                    600: /* Look up a builtin type, even if overridden by the user */
                    601: int
1.53      espie     602: builtin_type(const char *key)
1.31      espie     603: {
                    604:        int i;
                    605:
                    606:        for (i = 0; i != MAXKEYS; i++)
                    607:                if (STREQ(keywrds[i].knam, key))
                    608:                        return keywrds[i].ktyp;
                    609:        return -1;
                    610: }
                    611:
1.45      espie     612: char *
1.53      espie     613: builtin_realname(int n)
1.45      espie     614: {
                    615:        int i;
                    616:
                    617:        for (i = 0; i != MAXKEYS; i++)
                    618:                if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
                    619:                        return keywrds[i].knam;
                    620:        return NULL;
                    621: }
1.17      espie     622:
1.27      espie     623: static void
1.53      espie     624: record(struct position *t, int lev)
1.27      espie     625: {
                    626:        if (lev < MAXRECORD) {
                    627:                t[lev].name = CURRENT_NAME;
                    628:                t[lev].line = CURRENT_LINE;
                    629:        }
                    630: }
                    631:
                    632: static void
1.53      espie     633: dump_stack(struct position *t, int lev)
1.27      espie     634: {
                    635:        int i;
                    636:
                    637:        for (i = 0; i < lev; i++) {
                    638:                if (i == MAXRECORD) {
                    639:                        fprintf(stderr, "   ...\n");
                    640:                        break;
                    641:                }
                    642:                fprintf(stderr, "   %s at line %lu\n",
                    643:                        t[i].name, t[i].line);
                    644:        }
1.34      espie     645: }
                    646:
                    647:
                    648: static void
                    649: enlarge_stack()
                    650: {
                    651:        STACKMAX *= 2;
                    652:        mstack = realloc(mstack, sizeof(stae) * STACKMAX);
                    653:        sstack = realloc(sstack, STACKMAX);
                    654:        if (mstack == NULL || sstack == NULL)
                    655:                errx(1, "Evaluation stack overflow (%lu)",
                    656:                    (unsigned long)STACKMAX);
1.27      espie     657: }