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

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