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

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