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

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