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

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