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

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