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

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