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

1.55    ! millert     1: /*     $OpenBSD: main.c,v 1.54 2002/04/28 14:37:12 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.
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.55    ! millert    46: static char rcsid[] = "$OpenBSD: main.c,v 1.54 2002/04/28 14:37:12 espie 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:
                    183:        initkwds();
1.14      espie     184:        initspaces();
1.34      espie     185:        STACKMAX = INITSTACKMAX;
                    186:
                    187:        mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
                    188:        sstack = (char *)xalloc(STACKMAX);
1.1       deraadt   189:
1.36      espie     190:        maxout = 0;
                    191:        outfile = NULL;
                    192:        resizedivs(MAXOUT);
                    193:
1.54      espie     194:        while ((c = getopt(argc, argv, "gst:d:D:U:o:I:")) != -1)
1.1       deraadt   195:                switch(c) {
                    196:
                    197:                case 'D':               /* define something..*/
                    198:                        for (p = optarg; *p; p++)
                    199:                                if (*p == '=')
                    200:                                        break;
                    201:                        if (*p)
                    202:                                *p++ = EOS;
                    203:                        dodefine(optarg, p);
1.16      espie     204:                        break;
                    205:                case 'I':
                    206:                        addtoincludepath(optarg);
1.1       deraadt   207:                        break;
                    208:                case 'U':               /* undefine...       */
                    209:                        remhash(optarg, TOP);
1.32      espie     210:                        break;
                    211:                case 'g':
                    212:                        mimic_gnu = 1;
1.1       deraadt   213:                        break;
1.46      espie     214:                case 'd':
                    215:                        set_trace_flags(optarg);
1.47      espie     216:                        break;
1.54      espie     217:                case 's':
                    218:                        synch_lines = 1;
                    219:                        break;
1.47      espie     220:                case 't':
1.49      espie     221:                        mark_traced(optarg, 1);
1.46      espie     222:                        break;
1.38      aaron     223:                case 'o':
1.46      espie     224:                        trace_file(optarg);
1.38      aaron     225:                         break;
1.1       deraadt   226:                case '?':
                    227:                        usage();
                    228:                }
                    229:
                    230:         argc -= optind;
                    231:         argv += optind;
                    232:
                    233:        active = stdout;                /* default active output     */
                    234:        bbase[0] = bufbase;
                    235:         if (!argc) {
                    236:                sp = -1;                /* stack pointer initialized */
                    237:                fp = 0;                 /* frame pointer initialized */
1.26      espie     238:                set_input(infile+0, stdin, "stdin");
                    239:                                        /* default input (naturally) */
1.1       deraadt   240:                macro();
                    241:        } else
                    242:                for (; argc--; ++argv) {
                    243:                        p = *argv;
1.13      espie     244:                        if (p[0] == '-' && p[1] == EOS)
1.26      espie     245:                                set_input(infile, stdin, "stdin");
                    246:                        else if (fopen_trypath(infile, p) == NULL)
1.11      espie     247:                                err(1, "%s", p);
1.1       deraadt   248:                        sp = -1;
                    249:                        fp = 0;
                    250:                        macro();
1.26      espie     251:                        release_input(infile);
1.1       deraadt   252:                }
                    253:
                    254:        if (*m4wraps) {                 /* anything for rundown ??   */
                    255:                ilevel = 0;             /* in case m4wrap includes.. */
                    256:                bufbase = bp = buf;     /* use the entire buffer   */
                    257:                pbstr(m4wraps);         /* user-defined wrapup act   */
                    258:                macro();                /* last will and testament   */
                    259:        }
                    260:
                    261:        if (active != stdout)
                    262:                active = stdout;        /* reset output just in case */
1.36      espie     263:        for (n = 1; n < maxout; n++)    /* default wrap-up: undivert */
1.1       deraadt   264:                if (outfile[n] != NULL)
                    265:                        getdiv(n);
                    266:                                        /* remove bitbucket if used  */
                    267:        if (outfile[0] != NULL) {
                    268:                (void) fclose(outfile[0]);
                    269:        }
                    270:
                    271:        return 0;
                    272: }
                    273:
                    274: /*
1.21      espie     275:  * Look ahead for `token'.
1.2       deraadt   276:  * (on input `t == token[0]')
                    277:  * Used for comment and quoting delimiters.
                    278:  * Returns 1 if `token' present; copied to output.
                    279:  *         0 if `token' not found; all characters pushed back
                    280:  */
1.18      espie     281: static int
1.53      espie     282: do_look_ahead(int t, const char *token)
1.2       deraadt   283: {
                    284:        int i;
                    285:
1.43      espie     286:        assert((unsigned char)t == (unsigned char)token[0]);
1.2       deraadt   287:
                    288:        for (i = 1; *++token; i++) {
                    289:                t = gpbc();
1.43      espie     290:                if (t == EOF || (unsigned char)t != (unsigned char)*token) {
1.28      espie     291:                        putback(t);
1.2       deraadt   292:                        while (--i)
                    293:                                putback(*--token);
                    294:                        return 0;
                    295:                }
                    296:        }
                    297:        return 1;
                    298: }
                    299:
1.43      espie     300: #define LOOK_AHEAD(t, token) (t != EOF &&              \
                    301:     (unsigned char)(t)==(unsigned char)(token)[0] &&   \
                    302:     do_look_ahead(t,token))
1.2       deraadt   303:
                    304: /*
1.1       deraadt   305:  * macro - the work horse..
                    306:  */
1.18      espie     307: static void
1.17      espie     308: macro()
                    309: {
1.34      espie     310:        char token[MAXTOK+1];
1.17      espie     311:        int t, l;
                    312:        ndptr p;
                    313:        int  nlpar;
1.1       deraadt   314:
                    315:        cycle {
1.2       deraadt   316:                t = gpbc();
                    317:                if (t == '_' || isalpha(t)) {
1.29      espie     318:                        p = inspect(t, token);
1.24      espie     319:                        if (p != nil)
                    320:                                putback(l = gpbc());
                    321:                        if (p == nil || (l != LPAREN &&
                    322:                            (p->type & NEEDARGS) != 0))
1.29      espie     323:                                outputstr(token);
1.1       deraadt   324:                        else {
                    325:                /*
                    326:                 * real thing.. First build a call frame:
                    327:                 */
                    328:                                pushf(fp);      /* previous call frm */
                    329:                                pushf(p->type); /* type of the call  */
                    330:                                pushf(0);       /* parenthesis level */
                    331:                                fp = sp;        /* new frame pointer */
                    332:                /*
                    333:                 * now push the string arguments:
                    334:                 */
1.34      espie     335:                                pushs1(p->defn);        /* defn string */
                    336:                                pushs1(p->name);        /* macro name  */
                    337:                                pushs(ep);              /* start next..*/
1.1       deraadt   338:
1.41      espie     339:                                if (l != LPAREN && PARLEV == 0)  {
                    340:                                    /* no bracks  */
                    341:                                        chrsave(EOS);
                    342:
                    343:                                        if (sp == STACKMAX)
                    344:                                                errx(1, "internal stack overflow");
1.44      espie     345:                                        eval((const char **) mstack+fp+1, 2,
                    346:                                            CALTYP);
1.41      espie     347:
                    348:                                        ep = PREVEP;    /* flush strspace */
                    349:                                        sp = PREVSP;    /* previous sp..  */
                    350:                                        fp = PREVFP;    /* rewind stack...*/
1.1       deraadt   351:                                }
                    352:                        }
1.41      espie     353:                } else if (t == EOF) {
1.27      espie     354:                        if (sp > -1) {
                    355:                                warnx( "unexpected end of input, unclosed parenthesis:");
                    356:                                dump_stack(paren, PARLEV);
                    357:                                exit(1);
                    358:                        }
1.1       deraadt   359:                        if (ilevel <= 0)
                    360:                                break;                  /* all done thanks.. */
1.26      espie     361:                        release_input(infile+ilevel--);
1.54      espie     362:                        emit_synchline();
1.1       deraadt   363:                        bufbase = bbase[ilevel];
                    364:                        continue;
                    365:                }
                    366:        /*
1.7       deraadt   367:         * non-alpha token possibly seen..
1.1       deraadt   368:         * [the order of else if .. stmts is important.]
                    369:         */
1.2       deraadt   370:                else if (LOOK_AHEAD(t,lquote)) {        /* strip quotes */
1.27      espie     371:                        nlpar = 0;
                    372:                        record(quotes, nlpar++);
1.30      espie     373:                        /*
                    374:                         * Opening quote: scan forward until matching
                    375:                         * closing quote has been found.
                    376:                         */
1.1       deraadt   377:                        do {
1.7       deraadt   378:
1.2       deraadt   379:                                l = gpbc();
1.7       deraadt   380:                                if (LOOK_AHEAD(l,rquote)) {
1.29      espie     381:                                        if (--nlpar > 0)
                    382:                                                outputstr(rquote);
1.7       deraadt   383:                                } else if (LOOK_AHEAD(l,lquote)) {
1.27      espie     384:                                        record(quotes, nlpar++);
1.29      espie     385:                                        outputstr(lquote);
1.17      espie     386:                                } else if (l == EOF) {
                    387:                                        if (nlpar == 1)
1.27      espie     388:                                                warnx("unclosed quote:");
1.17      espie     389:                                        else
1.27      espie     390:                                                warnx("%d unclosed quotes:", nlpar);
                    391:                                        dump_stack(quotes, nlpar);
                    392:                                        exit(1);
1.17      espie     393:                                } else {
1.29      espie     394:                                        if (nlpar > 0) {
                    395:                                                if (sp < 0)
1.54      espie     396:                                                        reallyputchar(l);
1.29      espie     397:                                                else
1.48      espie     398:                                                        CHRSAVE(l);
1.29      espie     399:                                        }
1.7       deraadt   400:                                }
1.1       deraadt   401:                        }
                    402:                        while (nlpar != 0);
                    403:                }
                    404:
1.2       deraadt   405:                else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.54      espie     406:                        reallyoutputstr(scommt);
1.2       deraadt   407:
                    408:                        for(;;) {
                    409:                                t = gpbc();
                    410:                                if (LOOK_AHEAD(t, ecommt)) {
1.54      espie     411:                                        reallyoutputstr(ecommt);
1.2       deraadt   412:                                        break;
                    413:                                }
                    414:                                if (t == EOF)
                    415:                                        break;
1.54      espie     416:                                reallyputchar(t);
1.1       deraadt   417:                        }
1.2       deraadt   418:                }
                    419:
                    420:                else if (sp < 0) {              /* not in a macro at all */
1.54      espie     421:                        reallyputchar(t);       /* output directly..     */
1.1       deraadt   422:                }
                    423:
                    424:                else switch(t) {
                    425:
                    426:                case LPAREN:
                    427:                        if (PARLEV > 0)
                    428:                                chrsave(t);
                    429:                        while (isspace(l = gpbc()))
                    430:                                ;               /* skip blank, tab, nl.. */
                    431:                        putback(l);
1.27      espie     432:                        record(paren, PARLEV++);
1.1       deraadt   433:                        break;
                    434:
                    435:                case RPAREN:
                    436:                        if (--PARLEV > 0)
                    437:                                chrsave(t);
                    438:                        else {                  /* end of argument list */
                    439:                                chrsave(EOS);
                    440:
                    441:                                if (sp == STACKMAX)
1.11      espie     442:                                        errx(1, "internal stack overflow");
1.1       deraadt   443:
1.44      espie     444:                                eval((const char **) mstack+fp+1, sp-fp,
                    445:                                    CALTYP);
1.1       deraadt   446:
                    447:                                ep = PREVEP;    /* flush strspace */
                    448:                                sp = PREVSP;    /* previous sp..  */
                    449:                                fp = PREVFP;    /* rewind stack...*/
                    450:                        }
                    451:                        break;
                    452:
                    453:                case COMMA:
                    454:                        if (PARLEV == 1) {
                    455:                                chrsave(EOS);           /* new argument   */
                    456:                                while (isspace(l = gpbc()))
                    457:                                        ;
                    458:                                putback(l);
                    459:                                pushs(ep);
                    460:                        } else
                    461:                                chrsave(t);
                    462:                        break;
                    463:
                    464:                default:
1.22      espie     465:                        if (LOOK_AHEAD(t, scommt)) {
                    466:                                char *p;
                    467:                                for (p = scommt; *p; p++)
                    468:                                        chrsave(*p);
                    469:                                for(;;) {
                    470:                                        t = gpbc();
                    471:                                        if (LOOK_AHEAD(t, ecommt)) {
                    472:                                                for (p = ecommt; *p; p++)
                    473:                                                        chrsave(*p);
                    474:                                                break;
                    475:                                        }
                    476:                                        if (t == EOF)
                    477:                                            break;
1.48      espie     478:                                        CHRSAVE(t);
1.22      espie     479:                                }
                    480:                        } else
1.48      espie     481:                                CHRSAVE(t);             /* stack the char */
1.1       deraadt   482:                        break;
                    483:                }
                    484:        }
                    485: }
                    486:
1.24      espie     487: /*
                    488:  * output string directly, without pushing it for reparses.
                    489:  */
                    490: void
1.53      espie     491: outputstr(const char *s)
1.24      espie     492: {
                    493:        if (sp < 0)
1.54      espie     494:                reallyoutputstr(s);
1.24      espie     495:        else
                    496:                while (*s)
1.48      espie     497:                        CHRSAVE(*s++);
1.24      espie     498: }
                    499:
1.54      espie     500: void
                    501: reallyoutputstr(const char *s)
                    502: {
                    503:        if (synch_lines) {
                    504:                while (*s) {
                    505:                        fputc(*s, active);
                    506:                        if (*s++ == '\n') {
                    507:                                infile[ilevel].synch_lineno++;
                    508:                                if (infile[ilevel].synch_lineno !=
                    509:                                    infile[ilevel].lineno)
                    510:                                        do_emit_synchline();
                    511:                        }
                    512:                }
                    513:        } else
                    514:                fputs(s, active);
                    515: }
                    516:
                    517: void
                    518: reallyputchar(int c)
                    519: {
                    520:        putc(c, active);
                    521:        if (synch_lines && c == '\n') {
                    522:                infile[ilevel].synch_lineno++;
                    523:                if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
                    524:                        do_emit_synchline();
                    525:        }
                    526: }
                    527:
1.1       deraadt   528: /*
                    529:  * build an input token..
                    530:  * consider only those starting with _ or A-Za-z. This is a
                    531:  * combo with lookup to speed things up.
                    532:  */
1.18      espie     533: static ndptr
1.53      espie     534: inspect(int c, char *tp)
1.1       deraadt   535: {
1.17      espie     536:        char *name = tp;
                    537:        char *etp = tp+MAXTOK;
                    538:        ndptr p;
1.25      espie     539:        unsigned int h;
                    540:
                    541:        h = *tp++ = c;
1.1       deraadt   542:
                    543:        while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
                    544:                h = (h << 5) + h + (*tp++ = c);
1.51      espie     545:        if (c != EOF)
                    546:                PUTBACK(c);
1.1       deraadt   547:        *tp = EOS;
1.33      espie     548:        /* token is too long, it won't match anything, but it can still
                    549:         * be output. */
                    550:        if (tp == ep) {
                    551:                outputstr(name);
                    552:                while (isalnum(c = gpbc()) || c == '_') {
                    553:                        if (sp < 0)
1.54      espie     554:                                reallyputchar(c);
1.33      espie     555:                        else
1.48      espie     556:                                CHRSAVE(c);
1.33      espie     557:                }
                    558:                *name = EOS;
                    559:                return nil;
                    560:        }
1.1       deraadt   561:
1.19      espie     562:        for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
                    563:                if (h == p->hv && STREQ(name, p->name))
1.1       deraadt   564:                        break;
                    565:        return p;
                    566: }
                    567:
                    568: /*
                    569:  * initkwds - initialise m4 keywords as fast as possible.
                    570:  * This very similar to install, but without certain overheads,
                    571:  * such as calling lookup. Malloc is not used for storing the
1.17      espie     572:  * keyword strings, since we simply use the static pointers
1.1       deraadt   573:  * within keywrds block.
                    574:  */
1.18      espie     575: static void
1.17      espie     576: initkwds()
                    577: {
                    578:        size_t i;
1.20      millert   579:        unsigned int h;
1.17      espie     580:        ndptr p;
1.1       deraadt   581:
                    582:        for (i = 0; i < MAXKEYS; i++) {
                    583:                h = hash(keywrds[i].knam);
                    584:                p = (ndptr) xalloc(sizeof(struct ndblock));
1.19      espie     585:                p->nxtptr = hashtab[h % HASHSIZE];
                    586:                hashtab[h % HASHSIZE] = p;
1.42      espie     587:                p->name = xstrdup(keywrds[i].knam);
1.1       deraadt   588:                p->defn = null;
1.19      espie     589:                p->hv = h;
1.42      espie     590:                p->type = keywrds[i].ktyp & TYPEMASK;
1.24      espie     591:                if ((keywrds[i].ktyp & NOARGS) == 0)
                    592:                        p->type |= NEEDARGS;
1.1       deraadt   593:        }
                    594: }
1.31      espie     595:
                    596: /* Look up a builtin type, even if overridden by the user */
                    597: int
1.53      espie     598: builtin_type(const char *key)
1.31      espie     599: {
                    600:        int i;
                    601:
                    602:        for (i = 0; i != MAXKEYS; i++)
                    603:                if (STREQ(keywrds[i].knam, key))
                    604:                        return keywrds[i].ktyp;
                    605:        return -1;
                    606: }
                    607:
1.45      espie     608: char *
1.53      espie     609: builtin_realname(int n)
1.45      espie     610: {
                    611:        int i;
                    612:
                    613:        for (i = 0; i != MAXKEYS; i++)
                    614:                if (((keywrds[i].ktyp ^ n) & TYPEMASK) == 0)
                    615:                        return keywrds[i].knam;
                    616:        return NULL;
                    617: }
1.17      espie     618:
1.27      espie     619: static void
1.53      espie     620: record(struct position *t, int lev)
1.27      espie     621: {
                    622:        if (lev < MAXRECORD) {
                    623:                t[lev].name = CURRENT_NAME;
                    624:                t[lev].line = CURRENT_LINE;
                    625:        }
                    626: }
                    627:
                    628: static void
1.53      espie     629: dump_stack(struct position *t, int lev)
1.27      espie     630: {
                    631:        int i;
                    632:
                    633:        for (i = 0; i < lev; i++) {
                    634:                if (i == MAXRECORD) {
                    635:                        fprintf(stderr, "   ...\n");
                    636:                        break;
                    637:                }
                    638:                fprintf(stderr, "   %s at line %lu\n",
                    639:                        t[i].name, t[i].line);
                    640:        }
1.34      espie     641: }
                    642:
                    643:
                    644: static void
                    645: enlarge_stack()
                    646: {
                    647:        STACKMAX *= 2;
                    648:        mstack = realloc(mstack, sizeof(stae) * STACKMAX);
                    649:        sstack = realloc(sstack, STACKMAX);
                    650:        if (mstack == NULL || sstack == NULL)
                    651:                errx(1, "Evaluation stack overflow (%lu)",
                    652:                    (unsigned long)STACKMAX);
1.27      espie     653: }