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

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