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

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