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

1.24    ! espie       1: /*     $OpenBSD: main.c,v 1.23 2000/01/05 16:06:14 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.24    ! espie      50: static char rcsid[] = "$OpenBSD: main.c,v 1.23 2000/01/05 16:06:14 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       */
                     79: FILE *infile[MAXINP];          /* input file stack (0=stdin)  */
                     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.18      espie     142: static void macro __P((void));
                    143: static void initkwds __P((void));
                    144: static ndptr inspect __P((char *));
                    145: static int do_look_ahead __P((int, const char *));
                    146:
                    147: int main __P((int, char *[]));
1.1       deraadt   148:
                    149: int
                    150: main(argc,argv)
                    151:        int argc;
                    152:        char *argv[];
                    153: {
1.17      espie     154:        int c;
                    155:        int n;
1.1       deraadt   156:        char *p;
1.17      espie     157:        FILE *ifp;
1.1       deraadt   158:
                    159:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    160:                signal(SIGINT, onintr);
                    161:
                    162:        initkwds();
1.14      espie     163:        initspaces();
1.1       deraadt   164:
1.16      espie     165:        while ((c = getopt(argc, argv, "tD:U:o:I:")) != -1)
1.1       deraadt   166:                switch(c) {
                    167:
                    168:                case 'D':               /* define something..*/
                    169:                        for (p = optarg; *p; p++)
                    170:                                if (*p == '=')
                    171:                                        break;
                    172:                        if (*p)
                    173:                                *p++ = EOS;
                    174:                        dodefine(optarg, p);
1.16      espie     175:                        break;
                    176:                case 'I':
                    177:                        addtoincludepath(optarg);
1.1       deraadt   178:                        break;
                    179:                case 'U':               /* undefine...       */
                    180:                        remhash(optarg, TOP);
                    181:                        break;
                    182:                case 'o':               /* specific output   */
                    183:                case '?':
                    184:                        usage();
                    185:                }
                    186:
                    187:         argc -= optind;
                    188:         argv += optind;
                    189:
                    190:        active = stdout;                /* default active output     */
                    191:        bbase[0] = bufbase;
                    192:         if (!argc) {
                    193:                sp = -1;                /* stack pointer initialized */
                    194:                fp = 0;                 /* frame pointer initialized */
                    195:                infile[0] = stdin;      /* default input (naturally) */
                    196:                macro();
                    197:        } else
                    198:                for (; argc--; ++argv) {
                    199:                        p = *argv;
1.13      espie     200:                        if (p[0] == '-' && p[1] == EOS)
1.1       deraadt   201:                                ifp = stdin;
1.15      espie     202:                        else if ((ifp = fopen_trypath(p)) == NULL)
1.11      espie     203:                                err(1, "%s", p);
1.1       deraadt   204:                        sp = -1;
                    205:                        fp = 0;
                    206:                        infile[0] = ifp;
                    207:                        macro();
                    208:                        if (ifp != stdin)
                    209:                                (void)fclose(ifp);
                    210:                }
                    211:
                    212:        if (*m4wraps) {                 /* anything for rundown ??   */
                    213:                ilevel = 0;             /* in case m4wrap includes.. */
                    214:                bufbase = bp = buf;     /* use the entire buffer   */
                    215:                putback(EOF);           /* eof is a must !!          */
                    216:                pbstr(m4wraps);         /* user-defined wrapup act   */
                    217:                macro();                /* last will and testament   */
                    218:        }
                    219:
                    220:        if (active != stdout)
                    221:                active = stdout;        /* reset output just in case */
                    222:        for (n = 1; n < MAXOUT; n++)    /* default wrap-up: undivert */
                    223:                if (outfile[n] != NULL)
                    224:                        getdiv(n);
                    225:                                        /* remove bitbucket if used  */
                    226:        if (outfile[0] != NULL) {
                    227:                (void) fclose(outfile[0]);
                    228:        }
                    229:
                    230:        return 0;
                    231: }
                    232:
                    233: /*
1.21      espie     234:  * Look ahead for `token'.
1.2       deraadt   235:  * (on input `t == token[0]')
                    236:  * Used for comment and quoting delimiters.
                    237:  * Returns 1 if `token' present; copied to output.
                    238:  *         0 if `token' not found; all characters pushed back
                    239:  */
1.18      espie     240: static int
1.2       deraadt   241: do_look_ahead(t, token)
                    242:        int     t;
1.18      espie     243:        const char      *token;
1.2       deraadt   244: {
                    245:        int i;
                    246:
1.21      espie     247:        assert(t == token[0]);
1.2       deraadt   248:
                    249:        for (i = 1; *++token; i++) {
                    250:                t = gpbc();
                    251:                if (t == EOF || t != *token) {
                    252:                        if (t != EOF)
                    253:                                putback(t);
                    254:                        while (--i)
                    255:                                putback(*--token);
                    256:                        return 0;
                    257:                }
                    258:        }
                    259:        return 1;
                    260: }
                    261:
                    262: #define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token))
                    263:
                    264: /*
1.1       deraadt   265:  * macro - the work horse..
                    266:  */
1.18      espie     267: static void
1.17      espie     268: macro()
                    269: {
1.7       deraadt   270:        char token[MAXTOK], chars[2];
1.17      espie     271:        char *s;
                    272:        int t, l;
                    273:        ndptr p;
                    274:        int  nlpar;
1.1       deraadt   275:
                    276:        cycle {
1.2       deraadt   277:                t = gpbc();
                    278:                if (t == '_' || isalpha(t)) {
1.1       deraadt   279:                        putback(t);
1.10      deraadt   280:                        s = token;
1.24    ! espie     281:                        p = inspect(s);
        !           282:                        if (p != nil)
        !           283:                                putback(l = gpbc());
        !           284:                        if (p == nil || (l != LPAREN &&
        !           285:                            (p->type & NEEDARGS) != 0))
        !           286:                                outputstr(s);
1.1       deraadt   287:                        else {
                    288:                /*
                    289:                 * real thing.. First build a call frame:
                    290:                 */
                    291:                                pushf(fp);      /* previous call frm */
                    292:                                pushf(p->type); /* type of the call  */
                    293:                                pushf(0);       /* parenthesis level */
                    294:                                fp = sp;        /* new frame pointer */
                    295:                /*
                    296:                 * now push the string arguments:
                    297:                 */
                    298:                                pushs(p->defn);       /* defn string */
                    299:                                pushs(p->name);       /* macro name  */
                    300:                                pushs(ep);            /* start next..*/
                    301:
                    302:                                if (l != LPAREN)  {   /* add bracks  */
                    303:                                        putback(RPAREN);
                    304:                                        putback(LPAREN);
                    305:                                }
                    306:                        }
                    307:                }
                    308:                else if (t == EOF) {
                    309:                        if (sp > -1)
1.11      espie     310:                                errx(1, "unexpected end of input");
1.1       deraadt   311:                        if (ilevel <= 0)
                    312:                                break;                  /* all done thanks.. */
                    313:                        --ilevel;
                    314:                        (void) fclose(infile[ilevel+1]);
                    315:                        bufbase = bbase[ilevel];
                    316:                        continue;
                    317:                }
                    318:        /*
1.7       deraadt   319:         * non-alpha token possibly seen..
1.1       deraadt   320:         * [the order of else if .. stmts is important.]
                    321:         */
1.2       deraadt   322:                else if (LOOK_AHEAD(t,lquote)) {        /* strip quotes */
1.1       deraadt   323:                        nlpar = 1;
                    324:                        do {
1.7       deraadt   325:
1.2       deraadt   326:                                l = gpbc();
1.7       deraadt   327:                                if (LOOK_AHEAD(l,rquote)) {
1.1       deraadt   328:                                        nlpar--;
1.7       deraadt   329:                                        s = rquote;
                    330:                                } else if (LOOK_AHEAD(l,lquote)) {
1.1       deraadt   331:                                        nlpar++;
1.7       deraadt   332:                                        s = lquote;
1.17      espie     333:                                } else if (l == EOF) {
                    334:                                        if (nlpar == 1)
                    335:                                                errx(1, "missing right quote.");
                    336:                                        else
                    337:                                                errx(1, "missing %d right quotes.", nlpar);
                    338:                                } else {
1.7       deraadt   339:                                        chars[0] = l;
1.13      espie     340:                                        chars[1] = EOS;
1.7       deraadt   341:                                        s = chars;
                    342:                                }
1.24    ! espie     343:                                if (nlpar > 0)
        !           344:                                        outputstr(s);
1.1       deraadt   345:                        }
                    346:                        while (nlpar != 0);
                    347:                }
                    348:
1.2       deraadt   349:                else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
1.21      espie     350:                        fputs(scommt, active);
1.2       deraadt   351:
                    352:                        for(;;) {
                    353:                                t = gpbc();
                    354:                                if (LOOK_AHEAD(t, ecommt)) {
1.21      espie     355:                                        fputs(ecommt, active);
1.2       deraadt   356:                                        break;
                    357:                                }
                    358:                                if (t == EOF)
                    359:                                        break;
1.1       deraadt   360:                                putc(t, active);
                    361:                        }
1.2       deraadt   362:                }
                    363:
                    364:                else if (sp < 0) {              /* not in a macro at all */
1.1       deraadt   365:                        putc(t, active);        /* output directly..     */
                    366:                }
                    367:
                    368:                else switch(t) {
                    369:
                    370:                case LPAREN:
                    371:                        if (PARLEV > 0)
                    372:                                chrsave(t);
                    373:                        while (isspace(l = gpbc()))
                    374:                                ;               /* skip blank, tab, nl.. */
                    375:                        putback(l);
                    376:                        PARLEV++;
                    377:                        break;
                    378:
                    379:                case RPAREN:
                    380:                        if (--PARLEV > 0)
                    381:                                chrsave(t);
                    382:                        else {                  /* end of argument list */
                    383:                                chrsave(EOS);
                    384:
                    385:                                if (sp == STACKMAX)
1.11      espie     386:                                        errx(1, "internal stack overflow");
1.1       deraadt   387:
                    388:                                if (CALTYP == MACRTYPE)
1.18      espie     389:                                        expand((const char **) mstack+fp+1, sp-fp);
1.1       deraadt   390:                                else
1.18      espie     391:                                        eval((const char **) mstack+fp+1, sp-fp, CALTYP);
1.1       deraadt   392:
                    393:                                ep = PREVEP;    /* flush strspace */
                    394:                                sp = PREVSP;    /* previous sp..  */
                    395:                                fp = PREVFP;    /* rewind stack...*/
                    396:                        }
                    397:                        break;
                    398:
                    399:                case COMMA:
                    400:                        if (PARLEV == 1) {
                    401:                                chrsave(EOS);           /* new argument   */
                    402:                                while (isspace(l = gpbc()))
                    403:                                        ;
                    404:                                putback(l);
                    405:                                pushs(ep);
                    406:                        } else
                    407:                                chrsave(t);
                    408:                        break;
                    409:
                    410:                default:
1.22      espie     411:                        if (LOOK_AHEAD(t, scommt)) {
                    412:                                char *p;
                    413:                                for (p = scommt; *p; p++)
                    414:                                        chrsave(*p);
                    415:                                for(;;) {
                    416:                                        t = gpbc();
                    417:                                        if (LOOK_AHEAD(t, ecommt)) {
                    418:                                                for (p = ecommt; *p; p++)
                    419:                                                        chrsave(*p);
                    420:                                                break;
                    421:                                        }
                    422:                                        if (t == EOF)
                    423:                                            break;
                    424:                                        chrsave(t);
                    425:                                }
                    426:                        } else
                    427:                                chrsave(t);             /* stack the char */
1.1       deraadt   428:                        break;
                    429:                }
                    430:        }
                    431: }
                    432:
1.24    ! espie     433: /*
        !           434:  * output string directly, without pushing it for reparses.
        !           435:  */
        !           436: void
        !           437: outputstr(s)
        !           438:        const char *s;
        !           439: {
        !           440:        if (sp < 0)
        !           441:                while (*s)
        !           442:                        putc(*s++, active);
        !           443:        else
        !           444:                while (*s)
        !           445:                        chrsave(*s++);
        !           446: }
        !           447:
1.1       deraadt   448: /*
                    449:  * build an input token..
                    450:  * consider only those starting with _ or A-Za-z. This is a
                    451:  * combo with lookup to speed things up.
                    452:  */
1.18      espie     453: static ndptr
1.1       deraadt   454: inspect(tp)
1.17      espie     455:        char *tp;
1.1       deraadt   456: {
1.17      espie     457:        char c;
                    458:        char *name = tp;
                    459:        char *etp = tp+MAXTOK;
                    460:        ndptr p;
1.20      millert   461:        unsigned int h = 0;
1.1       deraadt   462:
                    463:        while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
                    464:                h = (h << 5) + h + (*tp++ = c);
                    465:        putback(c);
                    466:        if (tp == etp)
1.11      espie     467:                errx(1, "token too long");
1.1       deraadt   468:
                    469:        *tp = EOS;
                    470:
1.19      espie     471:        for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
                    472:                if (h == p->hv && STREQ(name, p->name))
1.1       deraadt   473:                        break;
                    474:        return p;
                    475: }
                    476:
                    477: /*
                    478:  * initkwds - initialise m4 keywords as fast as possible.
                    479:  * This very similar to install, but without certain overheads,
                    480:  * such as calling lookup. Malloc is not used for storing the
1.17      espie     481:  * keyword strings, since we simply use the static pointers
1.1       deraadt   482:  * within keywrds block.
                    483:  */
1.18      espie     484: static void
1.17      espie     485: initkwds()
                    486: {
                    487:        size_t i;
1.20      millert   488:        unsigned int h;
1.17      espie     489:        ndptr p;
1.1       deraadt   490:
                    491:        for (i = 0; i < MAXKEYS; i++) {
                    492:                h = hash(keywrds[i].knam);
                    493:                p = (ndptr) xalloc(sizeof(struct ndblock));
1.19      espie     494:                p->nxtptr = hashtab[h % HASHSIZE];
                    495:                hashtab[h % HASHSIZE] = p;
1.1       deraadt   496:                p->name = keywrds[i].knam;
                    497:                p->defn = null;
1.19      espie     498:                p->hv = h;
1.24    ! espie     499:                p->type = (keywrds[i].ktyp & TYPEMASK) | STATIC;
        !           500:                if ((keywrds[i].ktyp & NOARGS) == 0)
        !           501:                        p->type |= NEEDARGS;
1.1       deraadt   502:        }
                    503: }
1.17      espie     504: