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

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