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

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