[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.57

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