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

1.27    ! espie       1: /*     $OpenBSD: main.c,v 1.26 2000/01/12 17:49:53 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.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: static char copyright[] =
                     42: "@(#) Copyright (c) 1989, 1993\n\
                     43:        The Regents of the University of California.  All rights reserved.\n";
                     44: #endif /* not lint */
                     45:
                     46: #ifndef lint
                     47: #if 0
                     48: static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 6/6/93";
                     49: #else
1.27    ! espie      50: static char rcsid[] = "$OpenBSD: main.c,v 1.26 2000/01/12 17:49:53 espie Exp $";
1.1       deraadt    51: #endif
                     52: #endif /* not lint */
                     53:
                     54: /*
                     55:  * main.c
                     56:  * Facility: m4 macro processor
                     57:  * by: oz
                     58:  */
                     59:
                     60: #include <sys/types.h>
1.21      espie      61: #include <assert.h>
1.1       deraadt    62: #include <signal.h>
                     63: #include <errno.h>
                     64: #include <unistd.h>
                     65: #include <stdio.h>
                     66: #include <ctype.h>
                     67: #include <string.h>
1.13      espie      68: #include <stddef.h>
1.11      espie      69: #include <err.h>
1.1       deraadt    70: #include "mdef.h"
                     71: #include "stdd.h"
                     72: #include "extern.h"
                     73: #include "pathnames.h"
                     74:
                     75: ndptr hashtab[HASHSIZE];       /* hash table for macros etc.  */
                     76: stae mstack[STACKMAX+1];       /* stack of m4 machine         */
                     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.1       deraadt    80: FILE *outfile[MAXOUT];         /* diversion array(0=bitbucket)*/
                     81: FILE *active;                  /* active output file pointer  */
                     82: int ilevel = 0;                /* input file stack pointer    */
                     83: int oindex = 0;                /* diversion index..           */
                     84: char *null = "";                /* as it says.. just a null..  */
                     85: char *m4wraps = "";             /* m4wrap string default..     */
1.2       deraadt    86: char lquote[MAXCCHARS+1] = {LQUOTE};   /* left quote character  (`)   */
                     87: char rquote[MAXCCHARS+1] = {RQUOTE};   /* right quote character (')   */
                     88: char scommt[MAXCCHARS+1] = {SCOMMT};   /* start character for comment */
                     89: char ecommt[MAXCCHARS+1] = {ECOMMT};   /* end character for comment   */
1.1       deraadt    90:
                     91: struct keyblk keywrds[] = {    /* m4 keywords to be installed */
1.8       millert    92:        { "include",      INCLTYPE },
                     93:        { "sinclude",     SINCTYPE },
                     94:        { "define",       DEFITYPE },
                     95:        { "defn",         DEFNTYPE },
1.24      espie      96:        { "divert",       DIVRTYPE | NOARGS },
1.8       millert    97:        { "expr",         EXPRTYPE },
                     98:        { "eval",         EXPRTYPE },
                     99:        { "substr",       SUBSTYPE },
                    100:        { "ifelse",       IFELTYPE },
                    101:        { "ifdef",        IFDFTYPE },
                    102:        { "len",          LENGTYPE },
                    103:        { "incr",         INCRTYPE },
                    104:        { "decr",         DECRTYPE },
1.24      espie     105:        { "dnl",          DNLNTYPE | NOARGS },
                    106:        { "changequote",  CHNQTYPE | NOARGS },
                    107:        { "changecom",    CHNCTYPE | NOARGS },
1.8       millert   108:        { "index",        INDXTYPE },
1.1       deraadt   109: #ifdef EXTENDED
1.8       millert   110:        { "paste",        PASTTYPE },
                    111:        { "spaste",       SPASTYPE },
1.1       deraadt   112: #endif
1.8       millert   113:        { "popdef",       POPDTYPE },
                    114:        { "pushdef",      PUSDTYPE },
1.24      espie     115:        { "dumpdef",      DUMPTYPE | NOARGS },
                    116:        { "shift",        SHIFTYPE | NOARGS },
1.8       millert   117:        { "translit",     TRNLTYPE },
                    118:        { "undefine",     UNDFTYPE },
1.24      espie     119:        { "undivert",     UNDVTYPE | NOARGS },
                    120:        { "divnum",       DIVNTYPE | NOARGS },
1.8       millert   121:        { "maketemp",     MKTMTYPE },
1.24      espie     122:        { "errprint",     ERRPTYPE | NOARGS },
                    123:        { "m4wrap",       M4WRTYPE | NOARGS },
                    124:        { "m4exit",       EXITTYPE | NOARGS },
1.8       millert   125:        { "syscmd",       SYSCTYPE },
1.24      espie     126:        { "sysval",       SYSVTYPE | NOARGS },
1.1       deraadt   127:
1.24      espie     128: #if defined(unix) || defined(__unix__)
                    129:        { "unix",         SELFTYPE | NOARGS },
1.1       deraadt   130: #else
                    131: #ifdef vms
1.24      espie     132:        { "vms",          SELFTYPE | NOARGS },
1.1       deraadt   133: #endif
                    134: #endif
                    135: };
                    136:
                    137: #define MAXKEYS        (sizeof(keywrds)/sizeof(struct keyblk))
                    138:
                    139: extern int optind;
                    140: extern char *optarg;
                    141:
1.27    ! espie     142: #define MAXRECORD 50
        !           143: static struct position {
        !           144:        char *name;
        !           145:        unsigned long line;
        !           146: } quotes[MAXRECORD], paren[MAXRECORD];
        !           147:
        !           148: static void record __P((struct position *, int));
        !           149: static void dump_stack __P((struct position *, int));
        !           150:
1.18      espie     151: static void macro __P((void));
                    152: static void initkwds __P((void));
1.25      espie     153: static ndptr inspect __P((char, char *));
1.18      espie     154: static int do_look_ahead __P((int, const char *));
                    155:
                    156: int main __P((int, char *[]));
1.1       deraadt   157:
                    158: int
                    159: main(argc,argv)
                    160:        int argc;
                    161:        char *argv[];
                    162: {
1.17      espie     163:        int c;
                    164:        int n;
1.1       deraadt   165:        char *p;
1.17      espie     166:        FILE *ifp;
1.1       deraadt   167:
                    168:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    169:                signal(SIGINT, onintr);
                    170:
                    171:        initkwds();
1.14      espie     172:        initspaces();
1.1       deraadt   173:
1.16      espie     174:        while ((c = getopt(argc, argv, "tD:U:o:I:")) != -1)
1.1       deraadt   175:                switch(c) {
                    176:
                    177:                case 'D':               /* define something..*/
                    178:                        for (p = optarg; *p; p++)
                    179:                                if (*p == '=')
                    180:                                        break;
                    181:                        if (*p)
                    182:                                *p++ = EOS;
                    183:                        dodefine(optarg, p);
1.16      espie     184:                        break;
                    185:                case 'I':
                    186:                        addtoincludepath(optarg);
1.1       deraadt   187:                        break;
                    188:                case 'U':               /* undefine...       */
                    189:                        remhash(optarg, TOP);
                    190:                        break;
                    191:                case 'o':               /* specific output   */
                    192:                case '?':
                    193:                        usage();
                    194:                }
                    195:
                    196:         argc -= optind;
                    197:         argv += optind;
                    198:
                    199:        active = stdout;                /* default active output     */
                    200:        bbase[0] = bufbase;
                    201:         if (!argc) {
                    202:                sp = -1;                /* stack pointer initialized */
                    203:                fp = 0;                 /* frame pointer initialized */
1.26      espie     204:                set_input(infile+0, stdin, "stdin");
                    205:                                        /* default input (naturally) */
1.1       deraadt   206:                macro();
                    207:        } else
                    208:                for (; argc--; ++argv) {
                    209:                        p = *argv;
1.13      espie     210:                        if (p[0] == '-' && p[1] == EOS)
1.26      espie     211:                                set_input(infile, stdin, "stdin");
                    212:                        else if (fopen_trypath(infile, p) == NULL)
1.11      espie     213:                                err(1, "%s", p);
1.1       deraadt   214:                        sp = -1;
                    215:                        fp = 0;
                    216:                        macro();
1.26      espie     217:                        release_input(infile);
1.1       deraadt   218:                }
                    219:
                    220:        if (*m4wraps) {                 /* anything for rundown ??   */
                    221:                ilevel = 0;             /* in case m4wrap includes.. */
                    222:                bufbase = bp = buf;     /* use the entire buffer   */
                    223:                putback(EOF);           /* eof is a must !!          */
                    224:                pbstr(m4wraps);         /* user-defined wrapup act   */
                    225:                macro();                /* last will and testament   */
                    226:        }
                    227:
                    228:        if (active != stdout)
                    229:                active = stdout;        /* reset output just in case */
                    230:        for (n = 1; n < MAXOUT; n++)    /* default wrap-up: undivert */
                    231:                if (outfile[n] != NULL)
                    232:                        getdiv(n);
                    233:                                        /* remove bitbucket if used  */
                    234:        if (outfile[0] != NULL) {
                    235:                (void) fclose(outfile[0]);
                    236:        }
                    237:
                    238:        return 0;
                    239: }
                    240:
                    241: /*
1.21      espie     242:  * Look ahead for `token'.
1.2       deraadt   243:  * (on input `t == token[0]')
                    244:  * Used for comment and quoting delimiters.
                    245:  * Returns 1 if `token' present; copied to output.
                    246:  *         0 if `token' not found; all characters pushed back
                    247:  */
1.18      espie     248: static int
1.2       deraadt   249: do_look_ahead(t, token)
                    250:        int     t;
1.18      espie     251:        const char      *token;
1.2       deraadt   252: {
                    253:        int i;
                    254:
1.21      espie     255:        assert(t == token[0]);
1.2       deraadt   256:
                    257:        for (i = 1; *++token; i++) {
                    258:                t = gpbc();
                    259:                if (t == EOF || t != *token) {
                    260:                        if (t != EOF)
                    261:                                putback(t);
                    262:                        while (--i)
                    263:                                putback(*--token);
                    264:                        return 0;
                    265:                }
                    266:        }
                    267:        return 1;
                    268: }
                    269:
                    270: #define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token))
                    271:
                    272: /*
1.1       deraadt   273:  * macro - the work horse..
                    274:  */
1.18      espie     275: static void
1.17      espie     276: macro()
                    277: {
1.7       deraadt   278:        char token[MAXTOK], chars[2];
1.17      espie     279:        char *s;
                    280:        int t, l;
                    281:        ndptr p;
                    282:        int  nlpar;
1.1       deraadt   283:
                    284:        cycle {
1.2       deraadt   285:                t = gpbc();
                    286:                if (t == '_' || isalpha(t)) {
1.10      deraadt   287:                        s = token;
1.25      espie     288:                        p = inspect(t, s);
1.24      espie     289:                        if (p != nil)
                    290:                                putback(l = gpbc());
                    291:                        if (p == nil || (l != LPAREN &&
                    292:                            (p->type & NEEDARGS) != 0))
                    293:                                outputstr(s);
1.1       deraadt   294:                        else {
                    295:                /*
                    296:                 * real thing.. First build a call frame:
                    297:                 */
                    298:                                pushf(fp);      /* previous call frm */
                    299:                                pushf(p->type); /* type of the call  */
                    300:                                pushf(0);       /* parenthesis level */
                    301:                                fp = sp;        /* new frame pointer */
                    302:                /*
                    303:                 * now push the string arguments:
                    304:                 */
                    305:                                pushs(p->defn);       /* defn string */
                    306:                                pushs(p->name);       /* macro name  */
                    307:                                pushs(ep);            /* start next..*/
                    308:
                    309:                                if (l != LPAREN)  {   /* add bracks  */
                    310:                                        putback(RPAREN);
                    311:                                        putback(LPAREN);
                    312:                                }
                    313:                        }
                    314:                }
                    315:                else if (t == EOF) {
1.27    ! espie     316:                        if (sp > -1) {
        !           317:                                warnx( "unexpected end of input, unclosed parenthesis:");
        !           318:                                dump_stack(paren, PARLEV);
        !           319:                                exit(1);
        !           320:                        }
1.1       deraadt   321:                        if (ilevel <= 0)
                    322:                                break;                  /* all done thanks.. */
1.26      espie     323:                        release_input(infile+ilevel--);
1.1       deraadt   324:                        bufbase = bbase[ilevel];
                    325:                        continue;
                    326:                }
                    327:        /*
1.7       deraadt   328:         * non-alpha token possibly seen..
1.1       deraadt   329:         * [the order of else if .. stmts is important.]
                    330:         */
1.2       deraadt   331:                else if (LOOK_AHEAD(t,lquote)) {        /* strip quotes */
1.27    ! espie     332:                        nlpar = 0;
        !           333:                        record(quotes, nlpar++);
1.1       deraadt   334:                        do {
1.7       deraadt   335:
1.2       deraadt   336:                                l = gpbc();
1.7       deraadt   337:                                if (LOOK_AHEAD(l,rquote)) {
1.1       deraadt   338:                                        nlpar--;
1.7       deraadt   339:                                        s = rquote;
                    340:                                } else if (LOOK_AHEAD(l,lquote)) {
1.27    ! espie     341:                                        record(quotes, nlpar++);
1.7       deraadt   342:                                        s = lquote;
1.17      espie     343:                                } else if (l == EOF) {
                    344:                                        if (nlpar == 1)
1.27    ! espie     345:                                                warnx("unclosed quote:");
1.17      espie     346:                                        else
1.27    ! espie     347:                                                warnx("%d unclosed quotes:", nlpar);
        !           348:                                        dump_stack(quotes, nlpar);
        !           349:                                        exit(1);
1.17      espie     350:                                } else {
1.7       deraadt   351:                                        chars[0] = l;
1.13      espie     352:                                        chars[1] = EOS;
1.7       deraadt   353:                                        s = chars;
                    354:                                }
1.24      espie     355:                                if (nlpar > 0)
                    356:                                        outputstr(s);
1.1       deraadt   357:                        }
                    358:                        while (nlpar != 0);
                    359:                }
                    360:
1.2       deraadt   361:                else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.21      espie     362:                        fputs(scommt, active);
1.2       deraadt   363:
                    364:                        for(;;) {
                    365:                                t = gpbc();
                    366:                                if (LOOK_AHEAD(t, ecommt)) {
1.21      espie     367:                                        fputs(ecommt, active);
1.2       deraadt   368:                                        break;
                    369:                                }
                    370:                                if (t == EOF)
                    371:                                        break;
1.1       deraadt   372:                                putc(t, active);
                    373:                        }
1.2       deraadt   374:                }
                    375:
                    376:                else if (sp < 0) {              /* not in a macro at all */
1.1       deraadt   377:                        putc(t, active);        /* output directly..     */
                    378:                }
                    379:
                    380:                else switch(t) {
                    381:
                    382:                case LPAREN:
                    383:                        if (PARLEV > 0)
                    384:                                chrsave(t);
                    385:                        while (isspace(l = gpbc()))
                    386:                                ;               /* skip blank, tab, nl.. */
                    387:                        putback(l);
1.27    ! espie     388:                        record(paren, PARLEV++);
1.1       deraadt   389:                        break;
                    390:
                    391:                case RPAREN:
                    392:                        if (--PARLEV > 0)
                    393:                                chrsave(t);
                    394:                        else {                  /* end of argument list */
                    395:                                chrsave(EOS);
                    396:
                    397:                                if (sp == STACKMAX)
1.11      espie     398:                                        errx(1, "internal stack overflow");
1.1       deraadt   399:
                    400:                                if (CALTYP == MACRTYPE)
1.18      espie     401:                                        expand((const char **) mstack+fp+1, sp-fp);
1.1       deraadt   402:                                else
1.18      espie     403:                                        eval((const char **) mstack+fp+1, sp-fp, CALTYP);
1.1       deraadt   404:
                    405:                                ep = PREVEP;    /* flush strspace */
                    406:                                sp = PREVSP;    /* previous sp..  */
                    407:                                fp = PREVFP;    /* rewind stack...*/
                    408:                        }
                    409:                        break;
                    410:
                    411:                case COMMA:
                    412:                        if (PARLEV == 1) {
                    413:                                chrsave(EOS);           /* new argument   */
                    414:                                while (isspace(l = gpbc()))
                    415:                                        ;
                    416:                                putback(l);
                    417:                                pushs(ep);
                    418:                        } else
                    419:                                chrsave(t);
                    420:                        break;
                    421:
                    422:                default:
1.22      espie     423:                        if (LOOK_AHEAD(t, scommt)) {
                    424:                                char *p;
                    425:                                for (p = scommt; *p; p++)
                    426:                                        chrsave(*p);
                    427:                                for(;;) {
                    428:                                        t = gpbc();
                    429:                                        if (LOOK_AHEAD(t, ecommt)) {
                    430:                                                for (p = ecommt; *p; p++)
                    431:                                                        chrsave(*p);
                    432:                                                break;
                    433:                                        }
                    434:                                        if (t == EOF)
                    435:                                            break;
                    436:                                        chrsave(t);
                    437:                                }
                    438:                        } else
                    439:                                chrsave(t);             /* stack the char */
1.1       deraadt   440:                        break;
                    441:                }
                    442:        }
                    443: }
                    444:
1.24      espie     445: /*
                    446:  * output string directly, without pushing it for reparses.
                    447:  */
                    448: void
                    449: outputstr(s)
                    450:        const char *s;
                    451: {
                    452:        if (sp < 0)
                    453:                while (*s)
                    454:                        putc(*s++, active);
                    455:        else
                    456:                while (*s)
                    457:                        chrsave(*s++);
                    458: }
                    459:
1.1       deraadt   460: /*
                    461:  * build an input token..
                    462:  * consider only those starting with _ or A-Za-z. This is a
                    463:  * combo with lookup to speed things up.
                    464:  */
1.18      espie     465: static ndptr
1.25      espie     466: inspect(c, tp)
                    467:        char c;
1.17      espie     468:        char *tp;
1.1       deraadt   469: {
1.17      espie     470:        char *name = tp;
                    471:        char *etp = tp+MAXTOK;
                    472:        ndptr p;
1.25      espie     473:        unsigned int h;
                    474:
                    475:        h = *tp++ = c;
1.1       deraadt   476:
                    477:        while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
                    478:                h = (h << 5) + h + (*tp++ = c);
                    479:        putback(c);
                    480:        if (tp == etp)
1.11      espie     481:                errx(1, "token too long");
1.1       deraadt   482:
                    483:        *tp = EOS;
                    484:
1.19      espie     485:        for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
                    486:                if (h == p->hv && STREQ(name, p->name))
1.1       deraadt   487:                        break;
                    488:        return p;
                    489: }
                    490:
                    491: /*
                    492:  * initkwds - initialise m4 keywords as fast as possible.
                    493:  * This very similar to install, but without certain overheads,
                    494:  * such as calling lookup. Malloc is not used for storing the
1.17      espie     495:  * keyword strings, since we simply use the static pointers
1.1       deraadt   496:  * within keywrds block.
                    497:  */
1.18      espie     498: static void
1.17      espie     499: initkwds()
                    500: {
                    501:        size_t i;
1.20      millert   502:        unsigned int h;
1.17      espie     503:        ndptr p;
1.1       deraadt   504:
                    505:        for (i = 0; i < MAXKEYS; i++) {
                    506:                h = hash(keywrds[i].knam);
                    507:                p = (ndptr) xalloc(sizeof(struct ndblock));
1.19      espie     508:                p->nxtptr = hashtab[h % HASHSIZE];
                    509:                hashtab[h % HASHSIZE] = p;
1.1       deraadt   510:                p->name = keywrds[i].knam;
                    511:                p->defn = null;
1.19      espie     512:                p->hv = h;
1.24      espie     513:                p->type = (keywrds[i].ktyp & TYPEMASK) | STATIC;
                    514:                if ((keywrds[i].ktyp & NOARGS) == 0)
                    515:                        p->type |= NEEDARGS;
1.1       deraadt   516:        }
                    517: }
1.17      espie     518:
1.27    ! espie     519: static void
        !           520: record(t, lev)
        !           521:        struct position *t;
        !           522:        int lev;
        !           523: {
        !           524:        if (lev < MAXRECORD) {
        !           525:                t[lev].name = CURRENT_NAME;
        !           526:                t[lev].line = CURRENT_LINE;
        !           527:        }
        !           528: }
        !           529:
        !           530: static void
        !           531: dump_stack(t, lev)
        !           532:        struct position *t;
        !           533:        int lev;
        !           534: {
        !           535:        int i;
        !           536:
        !           537:        for (i = 0; i < lev; i++) {
        !           538:                if (i == MAXRECORD) {
        !           539:                        fprintf(stderr, "   ...\n");
        !           540:                        break;
        !           541:                }
        !           542:                fprintf(stderr, "   %s at line %lu\n",
        !           543:                        t[i].name, t[i].line);
        !           544:        }
        !           545: }