[BACK]Return to eval.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / m4

Annotation of src/usr.bin/m4/eval.c, Revision 1.31

1.31    ! espie       1: /*     $OpenBSD: eval.c,v 1.30 2001/09/18 13:52:58 espie Exp $ */
1.7       millert     2: /*     $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk 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: #if 0
                     42: static char sccsid[] = "@(#)eval.c     8.2 (Berkeley) 4/27/95";
                     43: #else
1.31    ! espie      44: static char rcsid[] = "$OpenBSD: eval.c,v 1.30 2001/09/18 13:52:58 espie Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: /*
                     49:  * eval.c
                     50:  * Facility: m4 macro processor
                     51:  * by: oz
                     52:  */
                     53:
                     54: #include <sys/types.h>
                     55: #include <errno.h>
                     56: #include <unistd.h>
                     57: #include <stdio.h>
                     58: #include <stdlib.h>
1.13      espie      59: #include <stddef.h>
1.1       deraadt    60: #include <string.h>
1.6       millert    61: #include <fcntl.h>
1.12      espie      62: #include <err.h>
1.1       deraadt    63: #include "mdef.h"
                     64: #include "stdd.h"
                     65: #include "extern.h"
                     66: #include "pathnames.h"
                     67:
1.31    ! espie      68: #define BUILTIN_MARKER "__builtin_"
        !            69:
1.20      espie      70: static void    dodefn __P((const char *));
                     71: static void    dopushdef __P((const char *, const char *));
                     72: static void    dodump __P((const char *[], int));
                     73: static void    doifelse __P((const char *[], int));
                     74: static int     doincl __P((const char *));
                     75: static int     dopaste __P((const char *));
                     76: static void    dochq __P((const char *[], int));
                     77: static void    dochc __P((const char *[], int));
                     78: static void    dodiv __P((int));
                     79: static void    doundiv __P((const char *[], int));
                     80: static void    dosub __P((const char *[], int));
                     81: static void    map __P((char *, const char *, const char *, const char *));
1.26      espie      82: static const char *handledash __P((char *, char *, const char *));
1.30      espie      83: static void    expand_builtin __P((const char *[], int, int));
                     84: static void    expand_macro __P((const char *[], int));
                     85:
                     86:
1.1       deraadt    87: /*
1.30      espie      88:  * eval - eval all macros and builtins calls
                     89:  */
                     90: void
                     91: eval(argv, argc, td)
                     92:        const char *argv[];
                     93:        int argc;
                     94:        int td;
                     95: {
                     96:        if (td & RECDEF)
                     97:                errx(1, "%s at line %lu: expanding recursive definition for %s",
                     98:                        CURRENT_NAME, CURRENT_LINE, argv[1]);
                     99:        if (td == MACRTYPE)
                    100:                expand_macro(argv, argc);
                    101:        else
                    102:                expand_builtin(argv, argc, td);
                    103: }
                    104:
                    105: /*
                    106:  * expand_builtin - evaluate built-in macros.
1.1       deraadt   107:  *       argc - number of elements in argv.
                    108:  *       argv - element vector :
                    109:  *                     argv[0] = definition of a user
                    110:  *                               macro or nil if built-in.
                    111:  *                     argv[1] = name of the macro or
                    112:  *                               built-in.
                    113:  *                     argv[2] = parameters to user-defined
                    114:  *                        .      macro or built-in.
                    115:  *                        .
                    116:  *
                    117:  * Note that the minimum value for argc is 3. A call in the form
                    118:  * of macro-or-builtin() will result in:
                    119:  *                     argv[0] = nullstr
                    120:  *                     argv[1] = macro-or-builtin
                    121:  *                     argv[2] = nullstr
                    122:  */
                    123:
                    124: void
1.30      espie     125: expand_builtin(argv, argc, td)
1.20      espie     126:        const char *argv[];
1.17      espie     127:        int argc;
                    128:        int td;
1.1       deraadt   129: {
1.17      espie     130:        int c, n;
1.1       deraadt   131:        static int sysval = 0;
                    132:
                    133: #ifdef DEBUG
                    134:        printf("argc = %d\n", argc);
                    135:        for (n = 0; n < argc; n++)
                    136:                printf("argv[%d] = %s\n", n, argv[n]);
                    137: #endif
1.22      espie     138:
1.1       deraadt   139:  /*
                    140:   * if argc == 3 and argv[2] is null, then we
                    141:   * have macro-or-builtin() type call. We adjust
                    142:   * argc to avoid further checking..
                    143:   */
                    144:        if (argc == 3 && !*(argv[2]))
                    145:                argc--;
                    146:
1.22      espie     147:        switch (td & TYPEMASK) {
1.1       deraadt   148:
                    149:        case DEFITYPE:
                    150:                if (argc > 2)
                    151:                        dodefine(argv[2], (argc > 3) ? argv[3] : null);
                    152:                break;
                    153:
                    154:        case PUSDTYPE:
                    155:                if (argc > 2)
                    156:                        dopushdef(argv[2], (argc > 3) ? argv[3] : null);
                    157:                break;
                    158:
                    159:        case DUMPTYPE:
                    160:                dodump(argv, argc);
                    161:                break;
                    162:
                    163:        case EXPRTYPE:
                    164:        /*
                    165:         * doexpr - evaluate arithmetic
                    166:         * expression
                    167:         */
                    168:                if (argc > 2)
                    169:                        pbnum(expr(argv[2]));
                    170:                break;
                    171:
                    172:        case IFELTYPE:
                    173:                if (argc > 4)
                    174:                        doifelse(argv, argc);
                    175:                break;
                    176:
                    177:        case IFDFTYPE:
                    178:        /*
                    179:         * doifdef - select one of two
                    180:         * alternatives based on the existence of
                    181:         * another definition
                    182:         */
                    183:                if (argc > 3) {
                    184:                        if (lookup(argv[2]) != nil)
                    185:                                pbstr(argv[3]);
                    186:                        else if (argc > 4)
                    187:                                pbstr(argv[4]);
                    188:                }
                    189:                break;
                    190:
                    191:        case LENGTYPE:
                    192:        /*
                    193:         * dolen - find the length of the
                    194:         * argument
                    195:         */
1.18      espie     196:                pbnum((argc > 2) ? strlen(argv[2]) : 0);
1.1       deraadt   197:                break;
                    198:
                    199:        case INCRTYPE:
                    200:        /*
                    201:         * doincr - increment the value of the
                    202:         * argument
                    203:         */
                    204:                if (argc > 2)
                    205:                        pbnum(atoi(argv[2]) + 1);
                    206:                break;
                    207:
                    208:        case DECRTYPE:
                    209:        /*
                    210:         * dodecr - decrement the value of the
                    211:         * argument
                    212:         */
                    213:                if (argc > 2)
                    214:                        pbnum(atoi(argv[2]) - 1);
                    215:                break;
                    216:
                    217:        case SYSCTYPE:
                    218:        /*
                    219:         * dosys - execute system command
                    220:         */
                    221:                if (argc > 2)
                    222:                        sysval = system(argv[2]);
                    223:                break;
                    224:
                    225:        case SYSVTYPE:
                    226:        /*
                    227:         * dosysval - return value of the last
                    228:         * system call.
                    229:         *
                    230:         */
                    231:                pbnum(sysval);
                    232:                break;
                    233:
1.27      espie     234:        case ESYSCMDTYPE:
                    235:                if (argc > 2)
                    236:                        doesyscmd(argv[2]);
                    237:                break;
1.1       deraadt   238:        case INCLTYPE:
                    239:                if (argc > 2)
                    240:                        if (!doincl(argv[2]))
1.24      espie     241:                                err(1, "%s at line %lu: include(%s)",
                    242:                                    CURRENT_NAME, CURRENT_LINE, argv[2]);
1.1       deraadt   243:                break;
                    244:
                    245:        case SINCTYPE:
                    246:                if (argc > 2)
                    247:                        (void) doincl(argv[2]);
                    248:                break;
                    249: #ifdef EXTENDED
                    250:        case PASTTYPE:
                    251:                if (argc > 2)
                    252:                        if (!dopaste(argv[2]))
1.24      espie     253:                                err(1, "%s at line %lu: paste(%s)",
                    254:                                    CURRENT_NAME, CURRENT_LINE, argv[2]);
1.1       deraadt   255:                break;
                    256:
                    257:        case SPASTYPE:
                    258:                if (argc > 2)
                    259:                        (void) dopaste(argv[2]);
                    260:                break;
                    261: #endif
                    262:        case CHNQTYPE:
                    263:                dochq(argv, argc);
                    264:                break;
                    265:
                    266:        case CHNCTYPE:
                    267:                dochc(argv, argc);
                    268:                break;
                    269:
                    270:        case SUBSTYPE:
                    271:        /*
                    272:         * dosub - select substring
                    273:         *
                    274:         */
                    275:                if (argc > 3)
                    276:                        dosub(argv, argc);
                    277:                break;
                    278:
                    279:        case SHIFTYPE:
                    280:        /*
                    281:         * doshift - push back all arguments
                    282:         * except the first one (i.e. skip
                    283:         * argv[2])
                    284:         */
                    285:                if (argc > 3) {
                    286:                        for (n = argc - 1; n > 3; n--) {
1.10      deraadt   287:                                pbstr(rquote);
1.1       deraadt   288:                                pbstr(argv[n]);
1.10      deraadt   289:                                pbstr(lquote);
1.7       millert   290:                                putback(COMMA);
1.1       deraadt   291:                        }
1.10      deraadt   292:                        pbstr(rquote);
1.1       deraadt   293:                        pbstr(argv[3]);
1.10      deraadt   294:                        pbstr(lquote);
1.1       deraadt   295:                }
                    296:                break;
                    297:
                    298:        case DIVRTYPE:
                    299:                if (argc > 2 && (n = atoi(argv[2])) != 0)
                    300:                        dodiv(n);
                    301:                else {
                    302:                        active = stdout;
                    303:                        oindex = 0;
                    304:                }
                    305:                break;
                    306:
                    307:        case UNDVTYPE:
                    308:                doundiv(argv, argc);
                    309:                break;
                    310:
                    311:        case DIVNTYPE:
                    312:        /*
                    313:         * dodivnum - return the number of
                    314:         * current output diversion
                    315:         */
                    316:                pbnum(oindex);
                    317:                break;
                    318:
                    319:        case UNDFTYPE:
                    320:        /*
                    321:         * doundefine - undefine a previously
                    322:         * defined macro(s) or m4 keyword(s).
                    323:         */
                    324:                if (argc > 2)
                    325:                        for (n = 2; n < argc; n++)
                    326:                                remhash(argv[n], ALL);
                    327:                break;
                    328:
                    329:        case POPDTYPE:
                    330:        /*
                    331:         * dopopdef - remove the topmost
                    332:         * definitions of macro(s) or m4
                    333:         * keyword(s).
                    334:         */
                    335:                if (argc > 2)
                    336:                        for (n = 2; n < argc; n++)
                    337:                                remhash(argv[n], TOP);
                    338:                break;
                    339:
                    340:        case MKTMTYPE:
                    341:        /*
                    342:         * dotemp - create a temporary file
                    343:         */
1.16      espie     344:                if (argc > 2) {
                    345:                        int fd;
1.20      espie     346:                        char *temp;
                    347:
                    348:                        temp = xstrdup(argv[2]);
1.16      espie     349:
1.20      espie     350:                        fd = mkstemp(temp);
1.16      espie     351:                        if (fd == -1)
1.24      espie     352:                                err(1,
                    353:            "%s at line %lu: couldn't make temp file %s",
                    354:            CURRENT_NAME, CURRENT_LINE, argv[2]);
1.16      espie     355:                        close(fd);
1.20      espie     356:                        pbstr(temp);
                    357:                        free(temp);
1.16      espie     358:                }
1.1       deraadt   359:                break;
                    360:
                    361:        case TRNLTYPE:
                    362:        /*
                    363:         * dotranslit - replace all characters in
                    364:         * the source string that appears in the
                    365:         * "from" string with the corresponding
                    366:         * characters in the "to" string.
                    367:         */
                    368:                if (argc > 3) {
1.8       deraadt   369:                        char temp[STRSPMAX+1];
1.1       deraadt   370:                        if (argc > 4)
                    371:                                map(temp, argv[2], argv[3], argv[4]);
                    372:                        else
                    373:                                map(temp, argv[2], argv[3], null);
                    374:                        pbstr(temp);
1.8       deraadt   375:                } else if (argc > 2)
1.1       deraadt   376:                        pbstr(argv[2]);
                    377:                break;
                    378:
                    379:        case INDXTYPE:
                    380:        /*
                    381:         * doindex - find the index of the second
                    382:         * argument string in the first argument
                    383:         * string. -1 if not present.
                    384:         */
                    385:                pbnum((argc > 3) ? indx(argv[2], argv[3]) : -1);
                    386:                break;
                    387:
                    388:        case ERRPTYPE:
                    389:        /*
                    390:         * doerrp - print the arguments to stderr
                    391:         * file
                    392:         */
                    393:                if (argc > 2) {
                    394:                        for (n = 2; n < argc; n++)
                    395:                                fprintf(stderr, "%s ", argv[n]);
                    396:                        fprintf(stderr, "\n");
                    397:                }
                    398:                break;
                    399:
                    400:        case DNLNTYPE:
                    401:        /*
                    402:         * dodnl - eat-up-to and including
                    403:         * newline
                    404:         */
                    405:                while ((c = gpbc()) != '\n' && c != EOF)
                    406:                        ;
                    407:                break;
                    408:
                    409:        case M4WRTYPE:
                    410:        /*
                    411:         * dom4wrap - set up for
                    412:         * wrap-up/wind-down activity
                    413:         */
                    414:                m4wraps = (argc > 2) ? xstrdup(argv[2]) : null;
                    415:                break;
                    416:
                    417:        case EXITTYPE:
                    418:        /*
                    419:         * doexit - immediate exit from m4.
                    420:         */
                    421:                killdiv();
                    422:                exit((argc > 2) ? atoi(argv[2]) : 0);
                    423:                break;
                    424:
                    425:        case DEFNTYPE:
                    426:                if (argc > 2)
                    427:                        for (n = 2; n < argc; n++)
                    428:                                dodefn(argv[n]);
                    429:                break;
                    430:
1.25      espie     431:        case INDIRTYPE: /* Indirect call */
                    432:                if (argc > 2)
                    433:                        doindir(argv, argc);
                    434:                break;
                    435:
                    436:        case BUILTINTYPE: /* Builtins only */
                    437:                if (argc > 2)
                    438:                        dobuiltin(argv, argc);
                    439:                break;
                    440:
                    441:        case PATSTYPE:
                    442:                if (argc > 2)
                    443:                        dopatsubst(argv, argc);
                    444:                break;
                    445:        case REGEXPTYPE:
                    446:                if (argc > 2)
                    447:                        doregexp(argv, argc);
                    448:                break;
                    449:        case LINETYPE:
                    450:                doprintlineno(infile+ilevel);
                    451:                break;
                    452:        case FILENAMETYPE:
                    453:                doprintfilename(infile+ilevel);
                    454:                break;
1.23      espie     455:        case SELFTYPE:
                    456:                pbstr(rquote);
                    457:                pbstr(argv[1]);
                    458:                pbstr(lquote);
                    459:                break;
1.1       deraadt   460:        default:
1.24      espie     461:                errx(1, "%s at line %lu: eval: major botch.",
                    462:                        CURRENT_NAME, CURRENT_LINE);
1.1       deraadt   463:                break;
                    464:        }
                    465: }
                    466:
                    467: char *dumpfmt = "`%s'\t`%s'\n";               /* format string for dumpdef   */
                    468:
                    469: /*
1.30      espie     470:  * expand_macro - user-defined macro expansion
1.1       deraadt   471:  */
                    472: void
1.30      espie     473: expand_macro(argv, argc)
1.20      espie     474:        const char *argv[];
1.17      espie     475:        int argc;
1.1       deraadt   476: {
1.20      espie     477:        const char *t;
                    478:        const char *p;
1.17      espie     479:        int n;
                    480:        int argno;
1.1       deraadt   481:
                    482:        t = argv[0];                   /* defn string as a whole */
                    483:        p = t;
                    484:        while (*p)
                    485:                p++;
                    486:        p--;                           /* last character of defn */
                    487:        while (p > t) {
                    488:                if (*(p - 1) != ARGFLAG)
                    489:                        putback(*p);
                    490:                else {
                    491:                        switch (*p) {
                    492:
                    493:                        case '#':
                    494:                                pbnum(argc - 2);
                    495:                                break;
                    496:                        case '0':
                    497:                        case '1':
                    498:                        case '2':
                    499:                        case '3':
                    500:                        case '4':
                    501:                        case '5':
                    502:                        case '6':
                    503:                        case '7':
                    504:                        case '8':
                    505:                        case '9':
                    506:                                if ((argno = *p - '0') < argc - 1)
                    507:                                        pbstr(argv[argno + 1]);
                    508:                                break;
                    509:                        case '*':
                    510:                                for (n = argc - 1; n > 2; n--) {
                    511:                                        pbstr(argv[n]);
1.7       millert   512:                                        putback(COMMA);
1.1       deraadt   513:                                }
                    514:                                pbstr(argv[2]);
                    515:                                break;
1.7       millert   516:                         case '@':
                    517:                                 for (n = argc - 1; n > 2; n--) {
                    518:                                         pbstr(rquote);
                    519:                                         pbstr(argv[n]);
                    520:                                         pbstr(lquote);
                    521:                                        putback(COMMA);
                    522:                                 }
                    523:                                pbstr(rquote);
                    524:                                 pbstr(argv[2]);
                    525:                                pbstr(lquote);
                    526:                                 break;
1.1       deraadt   527:                        default:
                    528:                                putback(*p);
                    529:                                putback('$');
                    530:                                break;
                    531:                        }
                    532:                        p--;
                    533:                }
                    534:                p--;
                    535:        }
                    536:        if (p == t)                    /* do last character */
                    537:                putback(*p);
                    538: }
                    539:
                    540: /*
                    541:  * dodefine - install definition in the table
                    542:  */
                    543: void
                    544: dodefine(name, defn)
1.20      espie     545:        const char *name;
                    546:        const char *defn;
1.1       deraadt   547: {
1.17      espie     548:        ndptr p;
1.31    ! espie     549:        int n;
1.1       deraadt   550:
                    551:        if (!*name)
1.24      espie     552:                errx(1, "%s at line %lu: null definition.", CURRENT_NAME,
                    553:                    CURRENT_LINE);
1.1       deraadt   554:        if ((p = lookup(name)) == nil)
                    555:                p = addent(name);
                    556:        else if (p->defn != null)
                    557:                free((char *) p->defn);
1.31    ! espie     558:        if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0) {
        !           559:                n = builtin_type(defn+sizeof(BUILTIN_MARKER)-1);
        !           560:                if (n != -1) {
        !           561:                        p->type = n;
        !           562:                        p->defn = null;
        !           563:                        return;
        !           564:                }
        !           565:        }
1.1       deraadt   566:        if (!*defn)
                    567:                p->defn = null;
                    568:        else
                    569:                p->defn = xstrdup(defn);
                    570:        p->type = MACRTYPE;
1.22      espie     571:        if (STREQ(name, defn))
                    572:                p->type |= RECDEF;
1.1       deraadt   573: }
                    574:
                    575: /*
                    576:  * dodefn - push back a quoted definition of
                    577:  *      the given name.
                    578:  */
1.20      espie     579: static void
1.1       deraadt   580: dodefn(name)
1.20      espie     581:        const char *name;
1.1       deraadt   582: {
1.17      espie     583:        ndptr p;
1.31    ! espie     584:        char *real;
1.1       deraadt   585:
1.31    ! espie     586:        if ((p = lookup(name)) != nil) {
        !           587:                if (p->defn != null) {
1.10      deraadt   588:                pbstr(rquote);
1.1       deraadt   589:                pbstr(p->defn);
1.10      deraadt   590:                pbstr(lquote);
1.31    ! espie     591:                } else if ((real = builtin_realname(p->type)) != NULL) {
        !           592:                        pbstr(real);
        !           593:                        pbstr(BUILTIN_MARKER);
        !           594:                }
1.1       deraadt   595:        }
                    596: }
                    597:
                    598: /*
                    599:  * dopushdef - install a definition in the hash table
                    600:  *      without removing a previous definition. Since
                    601:  *      each new entry is entered in *front* of the
                    602:  *      hash bucket, it hides a previous definition from
                    603:  *      lookup.
                    604:  */
1.20      espie     605: static void
1.1       deraadt   606: dopushdef(name, defn)
1.20      espie     607:        const char *name;
                    608:        const char *defn;
1.1       deraadt   609: {
1.17      espie     610:        ndptr p;
1.1       deraadt   611:
                    612:        if (!*name)
1.24      espie     613:                errx(1, "%s at line %lu: null definition", CURRENT_NAME,
                    614:                    CURRENT_LINE);
1.1       deraadt   615:        p = addent(name);
                    616:        if (!*defn)
                    617:                p->defn = null;
                    618:        else
                    619:                p->defn = xstrdup(defn);
                    620:        p->type = MACRTYPE;
1.22      espie     621:        if (STREQ(name, defn))
                    622:                p->type |= RECDEF;
1.1       deraadt   623: }
                    624:
                    625: /*
                    626:  * dodumpdef - dump the specified definitions in the hash
                    627:  *      table to stderr. If nothing is specified, the entire
                    628:  *      hash table is dumped.
                    629:  */
1.20      espie     630: static void
1.1       deraadt   631: dodump(argv, argc)
1.20      espie     632:        const char *argv[];
1.17      espie     633:        int argc;
1.1       deraadt   634: {
1.17      espie     635:        int n;
1.1       deraadt   636:        ndptr p;
                    637:
                    638:        if (argc > 2) {
                    639:                for (n = 2; n < argc; n++)
                    640:                        if ((p = lookup(argv[n])) != nil)
                    641:                                fprintf(stderr, dumpfmt, p->name,
                    642:                                        p->defn);
1.8       deraadt   643:        } else {
1.1       deraadt   644:                for (n = 0; n < HASHSIZE; n++)
                    645:                        for (p = hashtab[n]; p != nil; p = p->nxtptr)
                    646:                                fprintf(stderr, dumpfmt, p->name,
                    647:                                        p->defn);
                    648:        }
                    649: }
                    650:
                    651: /*
                    652:  * doifelse - select one of two alternatives - loop.
                    653:  */
1.20      espie     654: static void
1.1       deraadt   655: doifelse(argv, argc)
1.20      espie     656:        const char *argv[];
1.17      espie     657:        int argc;
1.1       deraadt   658: {
                    659:        cycle {
                    660:                if (STREQ(argv[2], argv[3]))
                    661:                        pbstr(argv[4]);
                    662:                else if (argc == 6)
                    663:                        pbstr(argv[5]);
                    664:                else if (argc > 6) {
                    665:                        argv += 3;
                    666:                        argc -= 3;
                    667:                        continue;
                    668:                }
                    669:                break;
                    670:        }
                    671: }
                    672:
                    673: /*
                    674:  * doinclude - include a given file.
                    675:  */
1.20      espie     676: static int
1.1       deraadt   677: doincl(ifile)
1.20      espie     678:        const char *ifile;
1.1       deraadt   679: {
                    680:        if (ilevel + 1 == MAXINP)
1.24      espie     681:                errx(1, "%s at line %lu: too many include files.",
                    682:                    CURRENT_NAME, CURRENT_LINE);
                    683:        if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
1.1       deraadt   684:                ilevel++;
                    685:                bbase[ilevel] = bufbase = bp;
                    686:                return (1);
1.8       deraadt   687:        } else
1.1       deraadt   688:                return (0);
                    689: }
                    690:
                    691: #ifdef EXTENDED
                    692: /*
                    693:  * dopaste - include a given file without any
                    694:  *           macro processing.
                    695:  */
1.20      espie     696: static int
1.1       deraadt   697: dopaste(pfile)
1.20      espie     698:        const char *pfile;
1.1       deraadt   699: {
                    700:        FILE *pf;
1.17      espie     701:        int c;
1.1       deraadt   702:
                    703:        if ((pf = fopen(pfile, "r")) != NULL) {
                    704:                while ((c = getc(pf)) != EOF)
                    705:                        putc(c, active);
                    706:                (void) fclose(pf);
                    707:                return (1);
1.8       deraadt   708:        } else
1.1       deraadt   709:                return (0);
                    710: }
                    711: #endif
                    712:
                    713: /*
                    714:  * dochq - change quote characters
                    715:  */
1.20      espie     716: static void
1.1       deraadt   717: dochq(argv, argc)
1.20      espie     718:        const char *argv[];
1.17      espie     719:        int argc;
1.1       deraadt   720: {
1.26      espie     721:        /* In gnu-m4 mode, having two empty arguments means no quotes at
                    722:         * all.  */
                    723:        if (mimic_gnu) {
                    724:                if (argc > 3 && !*argv[2] && !*argv[3]) {
                    725:                        lquote[0] = EOS;
                    726:                        rquote[0] = EOS;
                    727:                        return;
                    728:                }
                    729:        }
1.1       deraadt   730:        if (argc > 2) {
1.9       deraadt   731:                if (*argv[2])
1.21      espie     732:                        strlcpy(lquote, argv[2], sizeof(lquote));
1.9       deraadt   733:                else {
                    734:                        lquote[0] = LQUOTE;
1.14      espie     735:                        lquote[1] = EOS;
1.9       deraadt   736:                }
1.1       deraadt   737:                if (argc > 3) {
                    738:                        if (*argv[3])
1.21      espie     739:                                strlcpy(rquote, argv[3], sizeof(rquote));
1.8       deraadt   740:                } else
1.2       deraadt   741:                        strcpy(rquote, lquote);
1.8       deraadt   742:        } else {
1.14      espie     743:                lquote[0] = LQUOTE, lquote[1] = EOS;
                    744:                rquote[0] = RQUOTE, rquote[1] = EOS;
1.1       deraadt   745:        }
                    746: }
                    747:
                    748: /*
                    749:  * dochc - change comment characters
                    750:  */
1.20      espie     751: static void
1.1       deraadt   752: dochc(argv, argc)
1.20      espie     753:        const char *argv[];
1.17      espie     754:        int argc;
1.1       deraadt   755: {
                    756:        if (argc > 2) {
                    757:                if (*argv[2])
1.21      espie     758:                        strlcpy(scommt, argv[2], sizeof(scommt));
1.1       deraadt   759:                if (argc > 3) {
                    760:                        if (*argv[3])
1.21      espie     761:                                strlcpy(ecommt, argv[3], sizeof(ecommt));
1.1       deraadt   762:                }
                    763:                else
1.14      espie     764:                        ecommt[0] = ECOMMT, ecommt[1] = EOS;
1.1       deraadt   765:        }
                    766:        else {
1.14      espie     767:                scommt[0] = SCOMMT, scommt[1] = EOS;
                    768:                ecommt[0] = ECOMMT, ecommt[1] = EOS;
1.1       deraadt   769:        }
                    770: }
                    771:
                    772: /*
                    773:  * dodivert - divert the output to a temporary file
                    774:  */
1.20      espie     775: static void
1.1       deraadt   776: dodiv(n)
1.17      espie     777:        int n;
1.1       deraadt   778: {
1.6       millert   779:        int fd;
                    780:
1.8       deraadt   781:        oindex = n;
1.28      espie     782:        if (n >= maxout) {
                    783:                if (mimic_gnu)
                    784:                        resizedivs(n + 10);
                    785:                else
                    786:                        n = 0;          /* bitbucket */
                    787:        }
                    788:
                    789:        if (n < 0)
1.1       deraadt   790:                n = 0;                 /* bitbucket */
                    791:        if (outfile[n] == NULL) {
1.13      espie     792:                char fname[] = _PATH_DIVNAME;
                    793:
                    794:                if ((fd = mkstemp(fname)) < 0 ||
                    795:                        (outfile[n] = fdopen(fd, "w+")) == NULL)
                    796:                                err(1, "%s: cannot divert", fname);
                    797:                if (unlink(fname) == -1)
                    798:                        err(1, "%s: cannot unlink", fname);
1.1       deraadt   799:        }
                    800:        active = outfile[n];
                    801: }
                    802:
                    803: /*
                    804:  * doundivert - undivert a specified output, or all
                    805:  *              other outputs, in numerical order.
                    806:  */
1.20      espie     807: static void
1.1       deraadt   808: doundiv(argv, argc)
1.20      espie     809:        const char *argv[];
1.17      espie     810:        int argc;
1.1       deraadt   811: {
1.17      espie     812:        int ind;
                    813:        int n;
1.1       deraadt   814:
                    815:        if (argc > 2) {
                    816:                for (ind = 2; ind < argc; ind++) {
                    817:                        n = atoi(argv[ind]);
1.28      espie     818:                        if (n > 0 && n < maxout && outfile[n] != NULL)
1.1       deraadt   819:                                getdiv(n);
                    820:
                    821:                }
                    822:        }
                    823:        else
1.28      espie     824:                for (n = 1; n < maxout; n++)
1.1       deraadt   825:                        if (outfile[n] != NULL)
                    826:                                getdiv(n);
                    827: }
                    828:
                    829: /*
                    830:  * dosub - select substring
                    831:  */
1.20      espie     832: static void
1.1       deraadt   833: dosub(argv, argc)
1.20      espie     834:        const char *argv[];
1.17      espie     835:        int argc;
1.1       deraadt   836: {
1.20      espie     837:        const char *ap, *fc, *k;
1.17      espie     838:        int nc;
1.1       deraadt   839:
1.29      espie     840:        ap = argv[2];                  /* target string */
1.1       deraadt   841: #ifdef EXPR
1.29      espie     842:        fc = ap + expr(argv[3]);       /* first char */
1.1       deraadt   843: #else
1.29      espie     844:        fc = ap + atoi(argv[3]);       /* first char */
1.1       deraadt   845: #endif
1.29      espie     846:        nc = strlen(fc);
                    847:        if (argc >= 5)
1.1       deraadt   848: #ifdef EXPR
1.29      espie     849:                nc = min(nc, expr(argv[4]));
1.1       deraadt   850: #else
1.29      espie     851:                nc = min(nc, atoi(argv[4]));
1.1       deraadt   852: #endif
                    853:        if (fc >= ap && fc < ap + strlen(ap))
1.29      espie     854:                for (k = fc + nc - 1; k >= fc; k--)
1.1       deraadt   855:                        putback(*k);
                    856: }
                    857:
                    858: /*
                    859:  * map:
                    860:  * map every character of s1 that is specified in from
                    861:  * into s3 and replace in s. (source s1 remains untouched)
                    862:  *
                    863:  * This is a standard implementation of map(s,from,to) function of ICON
                    864:  * language. Within mapvec, we replace every character of "from" with
                    865:  * the corresponding character in "to". If "to" is shorter than "from",
                    866:  * than the corresponding entries are null, which means that those
                    867:  * characters dissapear altogether. Furthermore, imagine
                    868:  * map(dest, "sourcestring", "srtin", "rn..*") type call. In this case,
                    869:  * `s' maps to `r', `r' maps to `n' and `n' maps to `*'. Thus, `s'
                    870:  * ultimately maps to `*'. In order to achieve this effect in an efficient
                    871:  * manner (i.e. without multiple passes over the destination string), we
                    872:  * loop over mapvec, starting with the initial source character. if the
                    873:  * character value (dch) in this location is different than the source
                    874:  * character (sch), sch becomes dch, once again to index into mapvec, until
                    875:  * the character value stabilizes (i.e. sch = dch, in other words
                    876:  * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
                    877:  * character, it will stabilize, since mapvec[0] == 0 at all times. At the
                    878:  * end, we restore mapvec* back to normal where mapvec[n] == n for
                    879:  * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
                    880:  * about 5 times faster than any algorithm that makes multiple passes over
                    881:  * destination string.
                    882:  */
1.20      espie     883: static void
1.1       deraadt   884: map(dest, src, from, to)
1.17      espie     885:        char *dest;
1.20      espie     886:        const char *src;
                    887:        const char *from;
                    888:        const char *to;
1.1       deraadt   889: {
1.20      espie     890:        const char *tmp;
1.19      espie     891:        unsigned char sch, dch;
1.26      espie     892:        static char frombis[257];
                    893:        static char tobis[257];
1.19      espie     894:        static unsigned char mapvec[256] = {
                    895:            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
                    896:            19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
                    897:            36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
                    898:            53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
                    899:            70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
                    900:            87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
                    901:            103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
                    902:            116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
                    903:            129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
                    904:            142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
                    905:            155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
                    906:            168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
                    907:            181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
                    908:            194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
                    909:            207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
                    910:            220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
                    911:            233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
                    912:            246, 247, 248, 249, 250, 251, 252, 253, 254, 255
1.1       deraadt   913:        };
                    914:
                    915:        if (*src) {
1.26      espie     916:                if (mimic_gnu) {
                    917:                        /*
                    918:                         * expand character ranges on the fly
                    919:                         */
                    920:                        from = handledash(frombis, frombis + 256, from);
                    921:                        to = handledash(tobis, tobis + 256, to);
                    922:                }
1.1       deraadt   923:                tmp = from;
                    924:        /*
                    925:         * create a mapping between "from" and
                    926:         * "to"
                    927:         */
                    928:                while (*from)
1.19      espie     929:                        mapvec[(unsigned char)(*from++)] = (*to) ?
                    930:                                (unsigned char)(*to++) : 0;
1.1       deraadt   931:
                    932:                while (*src) {
1.19      espie     933:                        sch = (unsigned char)(*src++);
1.1       deraadt   934:                        dch = mapvec[sch];
                    935:                        while (dch != sch) {
                    936:                                sch = dch;
                    937:                                dch = mapvec[sch];
                    938:                        }
1.19      espie     939:                        if ((*dest = (char)dch))
1.1       deraadt   940:                                dest++;
                    941:                }
                    942:        /*
                    943:         * restore all the changed characters
                    944:         */
                    945:                while (*tmp) {
1.19      espie     946:                        mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
1.1       deraadt   947:                        tmp++;
                    948:                }
                    949:        }
1.19      espie     950:        *dest = '\0';
1.1       deraadt   951: }
1.26      espie     952:
                    953:
                    954: /*
                    955:  * handledash:
                    956:  *  use buffer to copy the src string, expanding character ranges
                    957:  * on the way.
                    958:  */
                    959: static const char *
                    960: handledash(buffer, end, src)
                    961:        char *buffer;
                    962:        char *end;
                    963:        const char *src;
                    964: {
                    965:        char *p;
                    966:
                    967:        p = buffer;
                    968:        while(*src) {
                    969:                if (src[1] == '-' && src[2]) {
                    970:                        unsigned char i;
                    971:                        for (i = (unsigned char)src[0];
                    972:                            i <= (unsigned char)src[2]; i++) {
                    973:                                *p++ = i;
                    974:                                if (p == end) {
                    975:                                        *p = '\0';
                    976:                                        return buffer;
                    977:                                }
                    978:                        }
                    979:                        src += 3;
                    980:                } else
                    981:                        *p++ = *src++;
                    982:                if (p == end)
                    983:                        break;
                    984:        }
                    985:        *p = '\0';
                    986:        return buffer;
                    987: }
                    988: