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

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