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

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