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

1.20    ! millert     1: /*     $OpenBSD: main.c,v 1.19 1999/11/20 17:48:59 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.20    ! millert    50: static char rcsid[] = "$OpenBSD: main.c,v 1.19 1999/11/20 17:48:59 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>
                     61: #include <signal.h>
                     62: #include <errno.h>
                     63: #include <unistd.h>
                     64: #include <stdio.h>
                     65: #include <ctype.h>
                     66: #include <string.h>
1.13      espie      67: #include <stddef.h>
1.11      espie      68: #include <err.h>
1.1       deraadt    69: #include "mdef.h"
                     70: #include "stdd.h"
                     71: #include "extern.h"
                     72: #include "pathnames.h"
                     73:
                     74: ndptr hashtab[HASHSIZE];       /* hash table for macros etc.  */
                     75: stae mstack[STACKMAX+1];       /* stack of m4 machine         */
                     76: int sp;                        /* current m4  stack pointer   */
                     77: int fp;                        /* m4 call frame pointer       */
                     78: FILE *infile[MAXINP];          /* input file stack (0=stdin)  */
                     79: FILE *outfile[MAXOUT];         /* diversion array(0=bitbucket)*/
                     80: FILE *active;                  /* active output file pointer  */
                     81: int ilevel = 0;                /* input file stack pointer    */
                     82: int oindex = 0;                /* diversion index..           */
                     83: char *null = "";                /* as it says.. just a null..  */
                     84: char *m4wraps = "";             /* m4wrap string default..     */
1.2       deraadt    85: char lquote[MAXCCHARS+1] = {LQUOTE};   /* left quote character  (`)   */
                     86: char rquote[MAXCCHARS+1] = {RQUOTE};   /* right quote character (')   */
                     87: char scommt[MAXCCHARS+1] = {SCOMMT};   /* start character for comment */
                     88: char ecommt[MAXCCHARS+1] = {ECOMMT};   /* end character for comment   */
1.1       deraadt    89:
                     90: struct keyblk keywrds[] = {    /* m4 keywords to be installed */
1.8       millert    91:        { "include",      INCLTYPE },
                     92:        { "sinclude",     SINCTYPE },
                     93:        { "define",       DEFITYPE },
                     94:        { "defn",         DEFNTYPE },
                     95:        { "divert",       DIVRTYPE },
                     96:        { "expr",         EXPRTYPE },
                     97:        { "eval",         EXPRTYPE },
                     98:        { "substr",       SUBSTYPE },
                     99:        { "ifelse",       IFELTYPE },
                    100:        { "ifdef",        IFDFTYPE },
                    101:        { "len",          LENGTYPE },
                    102:        { "incr",         INCRTYPE },
                    103:        { "decr",         DECRTYPE },
                    104:        { "dnl",          DNLNTYPE },
                    105:        { "changequote",  CHNQTYPE },
                    106:        { "changecom",    CHNCTYPE },
                    107:        { "index",        INDXTYPE },
1.1       deraadt   108: #ifdef EXTENDED
1.8       millert   109:        { "paste",        PASTTYPE },
                    110:        { "spaste",       SPASTYPE },
1.1       deraadt   111: #endif
1.8       millert   112:        { "popdef",       POPDTYPE },
                    113:        { "pushdef",      PUSDTYPE },
                    114:        { "dumpdef",      DUMPTYPE },
                    115:        { "shift",        SHIFTYPE },
                    116:        { "translit",     TRNLTYPE },
                    117:        { "undefine",     UNDFTYPE },
                    118:        { "undivert",     UNDVTYPE },
                    119:        { "divnum",       DIVNTYPE },
                    120:        { "maketemp",     MKTMTYPE },
                    121:        { "errprint",     ERRPTYPE },
                    122:        { "m4wrap",       M4WRTYPE },
                    123:        { "m4exit",       EXITTYPE },
                    124:        { "syscmd",       SYSCTYPE },
                    125:        { "sysval",       SYSVTYPE },
1.1       deraadt   126:
1.4       deraadt   127: #if defined(unix) || defined(__NetBSD__) || defined(__OpenBSD__)
1.8       millert   128:        { "unix",         MACRTYPE },
1.1       deraadt   129: #else
                    130: #ifdef vms
1.8       millert   131:        { "vms",          MACRTYPE },
1.1       deraadt   132: #endif
                    133: #endif
                    134: };
                    135:
                    136: #define MAXKEYS        (sizeof(keywrds)/sizeof(struct keyblk))
                    137:
                    138: extern int optind;
                    139: extern char *optarg;
                    140:
1.18      espie     141: static void macro __P((void));
                    142: static void initkwds __P((void));
                    143: static ndptr inspect __P((char *));
                    144: static int do_look_ahead __P((int, const char *));
                    145:
                    146: int main __P((int, char *[]));
1.1       deraadt   147:
                    148: int
                    149: main(argc,argv)
                    150:        int argc;
                    151:        char *argv[];
                    152: {
1.17      espie     153:        int c;
                    154:        int n;
1.1       deraadt   155:        char *p;
1.17      espie     156:        FILE *ifp;
1.1       deraadt   157:
                    158:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    159:                signal(SIGINT, onintr);
                    160:
                    161:        initkwds();
1.14      espie     162:        initspaces();
1.1       deraadt   163:
1.16      espie     164:        while ((c = getopt(argc, argv, "tD:U:o:I:")) != -1)
1.1       deraadt   165:                switch(c) {
                    166:
                    167:                case 'D':               /* define something..*/
                    168:                        for (p = optarg; *p; p++)
                    169:                                if (*p == '=')
                    170:                                        break;
                    171:                        if (*p)
                    172:                                *p++ = EOS;
                    173:                        dodefine(optarg, p);
1.16      espie     174:                        break;
                    175:                case 'I':
                    176:                        addtoincludepath(optarg);
1.1       deraadt   177:                        break;
                    178:                case 'U':               /* undefine...       */
                    179:                        remhash(optarg, TOP);
                    180:                        break;
                    181:                case 'o':               /* specific output   */
                    182:                case '?':
                    183:                        usage();
                    184:                }
                    185:
                    186:         argc -= optind;
                    187:         argv += optind;
                    188:
                    189:        active = stdout;                /* default active output     */
                    190:        bbase[0] = bufbase;
                    191:         if (!argc) {
                    192:                sp = -1;                /* stack pointer initialized */
                    193:                fp = 0;                 /* frame pointer initialized */
                    194:                infile[0] = stdin;      /* default input (naturally) */
                    195:                macro();
                    196:        } else
                    197:                for (; argc--; ++argv) {
                    198:                        p = *argv;
1.13      espie     199:                        if (p[0] == '-' && p[1] == EOS)
1.1       deraadt   200:                                ifp = stdin;
1.15      espie     201:                        else if ((ifp = fopen_trypath(p)) == NULL)
1.11      espie     202:                                err(1, "%s", p);
1.1       deraadt   203:                        sp = -1;
                    204:                        fp = 0;
                    205:                        infile[0] = ifp;
                    206:                        macro();
                    207:                        if (ifp != stdin)
                    208:                                (void)fclose(ifp);
                    209:                }
                    210:
                    211:        if (*m4wraps) {                 /* anything for rundown ??   */
                    212:                ilevel = 0;             /* in case m4wrap includes.. */
                    213:                bufbase = bp = buf;     /* use the entire buffer   */
                    214:                putback(EOF);           /* eof is a must !!          */
                    215:                pbstr(m4wraps);         /* user-defined wrapup act   */
                    216:                macro();                /* last will and testament   */
                    217:        }
                    218:
                    219:        if (active != stdout)
                    220:                active = stdout;        /* reset output just in case */
                    221:        for (n = 1; n < MAXOUT; n++)    /* default wrap-up: undivert */
                    222:                if (outfile[n] != NULL)
                    223:                        getdiv(n);
                    224:                                        /* remove bitbucket if used  */
                    225:        if (outfile[0] != NULL) {
                    226:                (void) fclose(outfile[0]);
                    227:        }
                    228:
                    229:        return 0;
                    230: }
                    231:
                    232: /*
1.2       deraadt   233:  * Look ahead (at most MAXCCHARS characters) for `token'.
                    234:  * (on input `t == token[0]')
                    235:  * Used for comment and quoting delimiters.
                    236:  * Returns 1 if `token' present; copied to output.
                    237:  *         0 if `token' not found; all characters pushed back
                    238:  */
1.18      espie     239: static int
1.2       deraadt   240: do_look_ahead(t, token)
                    241:        int     t;
1.18      espie     242:        const char      *token;
1.2       deraadt   243: {
                    244:        int i;
                    245:
                    246:        if (t != token[0])
1.11      espie     247:                errx(1, "internal error");
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.9       mickey    281:                        if ((p = inspect(s)) == nil) {
1.1       deraadt   282:                                if (sp < 0)
                    283:                                        while (*s)
                    284:                                                putc(*s++, active);
                    285:                                else
                    286:                                        while (*s)
                    287:                                                chrsave(*s++);
                    288:                        }
                    289:                        else {
                    290:                /*
                    291:                 * real thing.. First build a call frame:
                    292:                 */
                    293:                                pushf(fp);      /* previous call frm */
                    294:                                pushf(p->type); /* type of the call  */
                    295:                                pushf(0);       /* parenthesis level */
                    296:                                fp = sp;        /* new frame pointer */
                    297:                /*
                    298:                 * now push the string arguments:
                    299:                 */
                    300:                                pushs(p->defn);       /* defn string */
                    301:                                pushs(p->name);       /* macro name  */
                    302:                                pushs(ep);            /* start next..*/
                    303:
                    304:                                putback(l = gpbc());
                    305:                                if (l != LPAREN)  {   /* add bracks  */
                    306:                                        putback(RPAREN);
                    307:                                        putback(LPAREN);
                    308:                                }
                    309:                        }
                    310:                }
                    311:                else if (t == EOF) {
                    312:                        if (sp > -1)
1.11      espie     313:                                errx(1, "unexpected end of input");
1.1       deraadt   314:                        if (ilevel <= 0)
                    315:                                break;                  /* all done thanks.. */
                    316:                        --ilevel;
                    317:                        (void) fclose(infile[ilevel+1]);
                    318:                        bufbase = bbase[ilevel];
                    319:                        continue;
                    320:                }
                    321:        /*
1.7       deraadt   322:         * non-alpha token possibly seen..
1.1       deraadt   323:         * [the order of else if .. stmts is important.]
                    324:         */
1.2       deraadt   325:                else if (LOOK_AHEAD(t,lquote)) {        /* strip quotes */
1.1       deraadt   326:                        nlpar = 1;
                    327:                        do {
1.7       deraadt   328:
1.2       deraadt   329:                                l = gpbc();
1.7       deraadt   330:                                if (LOOK_AHEAD(l,rquote)) {
1.1       deraadt   331:                                        nlpar--;
1.7       deraadt   332:                                        s = rquote;
                    333:                                } else if (LOOK_AHEAD(l,lquote)) {
1.1       deraadt   334:                                        nlpar++;
1.7       deraadt   335:                                        s = lquote;
1.17      espie     336:                                } else if (l == EOF) {
                    337:                                        if (nlpar == 1)
                    338:                                                errx(1, "missing right quote.");
                    339:                                        else
                    340:                                                errx(1, "missing %d right quotes.", nlpar);
                    341:                                } else {
1.7       deraadt   342:                                        chars[0] = l;
1.13      espie     343:                                        chars[1] = EOS;
1.7       deraadt   344:                                        s = chars;
                    345:                                }
1.1       deraadt   346:                                if (nlpar > 0) {
                    347:                                        if (sp < 0)
1.7       deraadt   348:                                                while (*s)
                    349:                                                        putc(*s++, active);
1.1       deraadt   350:                                        else
1.7       deraadt   351:                                                while (*s)
                    352:                                                        chrsave(*s++);
1.1       deraadt   353:                                }
                    354:                        }
                    355:                        while (nlpar != 0);
                    356:                }
                    357:
1.2       deraadt   358:                else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
                    359:                        int i;
                    360:                        for (i = 0; i < MAXCCHARS && scommt[i]; i++)
                    361:                                putc(scommt[i], active);
                    362:
                    363:                        for(;;) {
                    364:                                t = gpbc();
                    365:                                if (LOOK_AHEAD(t, ecommt)) {
                    366:                                        for (i = 0; i < MAXCCHARS && ecommt[i];
                    367:                                             i++)
                    368:                                                putc(ecommt[i], active);
                    369:                                        break;
                    370:                                }
                    371:                                if (t == EOF)
                    372:                                        break;
1.1       deraadt   373:                                putc(t, active);
                    374:                        }
1.2       deraadt   375:                }
                    376:
                    377:                else if (sp < 0) {              /* not in a macro at all */
1.1       deraadt   378:                        putc(t, active);        /* output directly..     */
                    379:                }
                    380:
                    381:                else switch(t) {
                    382:
                    383:                case LPAREN:
                    384:                        if (PARLEV > 0)
                    385:                                chrsave(t);
                    386:                        while (isspace(l = gpbc()))
                    387:                                ;               /* skip blank, tab, nl.. */
                    388:                        putback(l);
                    389:                        PARLEV++;
                    390:                        break;
                    391:
                    392:                case RPAREN:
                    393:                        if (--PARLEV > 0)
                    394:                                chrsave(t);
                    395:                        else {                  /* end of argument list */
                    396:                                chrsave(EOS);
                    397:
                    398:                                if (sp == STACKMAX)
1.11      espie     399:                                        errx(1, "internal stack overflow");
1.1       deraadt   400:
                    401:                                if (CALTYP == MACRTYPE)
1.18      espie     402:                                        expand((const char **) mstack+fp+1, sp-fp);
1.1       deraadt   403:                                else
1.18      espie     404:                                        eval((const char **) mstack+fp+1, sp-fp, CALTYP);
1.1       deraadt   405:
                    406:                                ep = PREVEP;    /* flush strspace */
                    407:                                sp = PREVSP;    /* previous sp..  */
                    408:                                fp = PREVFP;    /* rewind stack...*/
                    409:                        }
                    410:                        break;
                    411:
                    412:                case COMMA:
                    413:                        if (PARLEV == 1) {
                    414:                                chrsave(EOS);           /* new argument   */
                    415:                                while (isspace(l = gpbc()))
                    416:                                        ;
                    417:                                putback(l);
                    418:                                pushs(ep);
                    419:                        } else
                    420:                                chrsave(t);
                    421:                        break;
                    422:
                    423:                default:
                    424:                        chrsave(t);                     /* stack the char */
                    425:                        break;
                    426:                }
                    427:        }
                    428: }
                    429:
                    430: /*
                    431:  * build an input token..
                    432:  * consider only those starting with _ or A-Za-z. This is a
                    433:  * combo with lookup to speed things up.
                    434:  */
1.18      espie     435: static ndptr
1.1       deraadt   436: inspect(tp)
1.17      espie     437:        char *tp;
1.1       deraadt   438: {
1.17      espie     439:        char c;
                    440:        char *name = tp;
                    441:        char *etp = tp+MAXTOK;
                    442:        ndptr p;
1.20    ! millert   443:        unsigned int h = 0;
1.1       deraadt   444:
                    445:        while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
                    446:                h = (h << 5) + h + (*tp++ = c);
                    447:        putback(c);
                    448:        if (tp == etp)
1.11      espie     449:                errx(1, "token too long");
1.1       deraadt   450:
                    451:        *tp = EOS;
                    452:
1.19      espie     453:        for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
                    454:                if (h == p->hv && STREQ(name, p->name))
1.1       deraadt   455:                        break;
                    456:        return p;
                    457: }
                    458:
                    459: /*
                    460:  * initkwds - initialise m4 keywords as fast as possible.
                    461:  * This very similar to install, but without certain overheads,
                    462:  * such as calling lookup. Malloc is not used for storing the
1.17      espie     463:  * keyword strings, since we simply use the static pointers
1.1       deraadt   464:  * within keywrds block.
                    465:  */
1.18      espie     466: static void
1.17      espie     467: initkwds()
                    468: {
                    469:        size_t i;
1.20    ! millert   470:        unsigned int h;
1.17      espie     471:        ndptr p;
1.1       deraadt   472:
                    473:        for (i = 0; i < MAXKEYS; i++) {
                    474:                h = hash(keywrds[i].knam);
                    475:                p = (ndptr) xalloc(sizeof(struct ndblock));
1.19      espie     476:                p->nxtptr = hashtab[h % HASHSIZE];
                    477:                hashtab[h % HASHSIZE] = p;
1.1       deraadt   478:                p->name = keywrds[i].knam;
                    479:                p->defn = null;
1.19      espie     480:                p->hv = h;
1.1       deraadt   481:                p->type = keywrds[i].ktyp | STATIC;
                    482:        }
                    483: }
1.17      espie     484: