[BACK]Return to serv.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / m4

Annotation of src/usr.bin/m4/serv.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: serv.c,v 1.3 1996/06/26 05:36:16 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: serv.c,v 1.7 1995/09/28 05:37:47 tls Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  * (c) UNIX System Laboratories, Inc.
                      8:  * All or some portions of this file are derived from material licensed
                      9:  * to the University of California by American Telephone and Telegraph
                     10:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     11:  * the permission of UNIX System Laboratories, Inc.
                     12:  *
                     13:  * This code is derived from software contributed to Berkeley by
                     14:  * Ozan Yigit.
                     15:  *
                     16:  * Redistribution and use in source and binary forms, with or without
                     17:  * modification, are permitted provided that the following conditions
                     18:  * are met:
                     19:  * 1. Redistributions of source code must retain the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer.
                     21:  * 2. Redistributions in binary form must reproduce the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer in the
                     23:  *    documentation and/or other materials provided with the distribution.
                     24:  * 3. All advertising materials mentioning features or use of this software
                     25:  *    must display the following acknowledgement:
                     26:  *     This product includes software developed by the University of
                     27:  *     California, Berkeley and its contributors.
                     28:  * 4. Neither the name of the University nor the names of its contributors
                     29:  *    may be used to endorse or promote products derived from this software
                     30:  *    without specific prior written permission.
                     31:  *
                     32:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     33:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     34:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     35:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     36:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     37:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     38:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     39:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     40:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     41:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     42:  * SUCH DAMAGE.
                     43:  */
                     44:
                     45: #ifndef lint
                     46: #if 0
                     47: static char sccsid[] = "@(#)serv.c     5.4 (Berkeley) 1/21/94";
                     48: #else
1.4     ! deraadt    49: static char rcsid[] = "$OpenBSD: serv.c,v 1.3 1996/06/26 05:36:16 deraadt Exp $";
1.1       deraadt    50: #endif
                     51: #endif /* not lint */
                     52:
                     53: /*
                     54:  * serv.c
                     55:  * Facility: m4 macro processor
                     56:  * by: oz
                     57:  */
                     58:
                     59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
                     62: #include "mdef.h"
                     63: #include "extr.h"
                     64: #include "pathnames.h"
                     65:
                     66: extern ndptr lookup();
                     67: extern ndptr addent();
                     68:
                     69: char *dumpfmt = "`%s'\t`%s'\n"; /* format string for dumpdef   */
                     70:
                     71: /*
                     72:  * expand - user-defined macro expansion
                     73:  *
                     74:  */
                     75: expand(argv, argc)
                     76: register char *argv[];
                     77: register int argc;
                     78: {
                     79:         register char *t;
                     80:         register char *p;
                     81:         register int  n;
                     82:         register int  argno;
                     83:
                     84:         t = argv[0];    /* defn string as a whole */
                     85:         p = t;
                     86:         while (*p)
                     87:                 p++;
                     88:         p--;            /* last character of defn */
                     89:         while (p > t) {
                     90:                 if (*(p-1) != ARGFLAG)
                     91:                         putback(*p);
                     92:                 else {
                     93:                         switch (*p) {
                     94:
                     95:                         case '#':
                     96:                                 pbnum(argc-2);
                     97:                                 break;
                     98:                         case '0':
                     99:                         case '1':
                    100:                         case '2':
                    101:                         case '3':
                    102:                         case '4':
                    103:                         case '5':
                    104:                         case '6':
                    105:                         case '7':
                    106:                         case '8':
                    107:                         case '9':
                    108:                                 if ((argno = *p - '0') < argc-1)
                    109:                                         pbstr(argv[argno+1]);
                    110:                                 break;
                    111:                         case '*':
                    112:                                 for (n = argc - 1; n > 2; n--) {
                    113:                                         pbstr(argv[n]);
                    114:                                         putback(',');
                    115:                                 }
                    116:                                 pbstr(argv[2]);
                    117:                                 break;
                    118:                         default :
                    119:                                 putback(*p);
                    120:                                 break;
                    121:                         }
                    122:                         p--;
                    123:                 }
                    124:                 p--;
                    125:         }
                    126:         if (p == t)         /* do last character */
                    127:                 putback(*p);
                    128: }
                    129:
                    130: /*
                    131:  * dodefine - install definition in the table
                    132:  *
                    133:  */
                    134: dodefine(name, defn)
                    135: register char *name;
                    136: register char *defn;
                    137: {
                    138:         register ndptr p;
                    139:
                    140:         if (!*name)
                    141:                 error("m4: null definition.");
                    142:         if (strcmp(name, defn) == 0)
                    143:                 error("m4: recursive definition.");
                    144:         if ((p = lookup(name)) == nil)
                    145:                 p = addent(name);
                    146:         else if (p->defn != null)
                    147:                 free(p->defn);
                    148:         if (!*defn)
                    149:                 p->defn = null;
                    150:         else
                    151:                 p->defn = strdup(defn);
                    152:         p->type = MACRTYPE;
                    153: }
                    154:
                    155: /*
                    156:  * dodefn - push back a quoted definition of
                    157:  *      the given name.
                    158:  */
                    159:
                    160: dodefn(name)
                    161: char *name;
                    162: {
                    163:         register ndptr p;
                    164:
                    165:         if ((p = lookup(name)) != nil && p->defn != null) {
                    166:                 putback(rquote);
                    167:                 pbstr(p->defn);
                    168:                 putback(lquote);
                    169:         }
                    170: }
                    171:
                    172: /*
                    173:  * dopushdef - install a definition in the hash table
                    174:  *      without removing a previous definition. Since
                    175:  *      each new entry is entered in *front* of the
                    176:  *      hash bucket, it hides a previous definition from
                    177:  *      lookup.
                    178:  */
                    179: dopushdef(name, defn)
                    180: register char *name;
                    181: register char *defn;
                    182: {
                    183:         register ndptr p;
                    184:
                    185:         if (!*name)
                    186:                 error("m4: null definition");
                    187:         if (strcmp(name, defn) == 0)
                    188:                 error("m4: recursive definition.");
                    189:         p = addent(name);
                    190:         if (!*defn)
                    191:                 p->defn = null;
                    192:         else
                    193:                 p->defn = strdup(defn);
                    194:         p->type = MACRTYPE;
                    195: }
                    196:
                    197: /*
                    198:  * dodumpdef - dump the specified definitions in the hash
                    199:  *      table to stderr. If nothing is specified, the entire
                    200:  *      hash table is dumped.
                    201:  *
                    202:  */
                    203: dodump(argv, argc)
                    204: register char *argv[];
                    205: register int argc;
                    206: {
                    207:         register int n;
                    208:         ndptr p;
                    209:
                    210:         if (argc > 2) {
                    211:                 for (n = 2; n < argc; n++)
                    212:                         if ((p = lookup(argv[n])) != nil)
                    213:                                 fprintf(stderr, dumpfmt, p->name,
                    214:                                 p->defn);
                    215:         }
                    216:         else {
                    217:                 for (n = 0; n < HASHSIZE; n++)
                    218:                         for (p = hashtab[n]; p != nil; p = p->nxtptr)
                    219:                                 fprintf(stderr, dumpfmt, p->name,
                    220:                                 p->defn);
                    221:         }
                    222: }
                    223:
                    224: /*
                    225:  * doifelse - select one of two alternatives - loop.
                    226:  *
                    227:  */
                    228: doifelse(argv,argc)
                    229: register char *argv[];
                    230: register int argc;
                    231: {
                    232:         cycle {
                    233:                 if (strcmp(argv[2], argv[3]) == 0)
                    234:                         pbstr(argv[4]);
                    235:                 else if (argc == 6)
                    236:                         pbstr(argv[5]);
                    237:                 else if (argc > 6) {
                    238:                         argv += 3;
                    239:                         argc -= 3;
                    240:                         continue;
                    241:                 }
                    242:                 break;
                    243:         }
                    244: }
                    245:
                    246: /*
                    247:  * doinclude - include a given file.
                    248:  *
                    249:  */
                    250: doincl(ifile)
                    251: char *ifile;
                    252: {
                    253:         if (ilevel+1 == MAXINP)
                    254:                 error("m4: too many include files.");
                    255:         if ((infile[ilevel+1] = fopen(ifile, "r")) != NULL) {
                    256:                 ilevel++;
                    257:                 return (1);
                    258:         }
                    259:         else
                    260:                 return (0);
                    261: }
                    262:
                    263: #ifdef EXTENDED
                    264: /*
                    265:  * dopaste - include a given file without any
                    266:  *           macro processing.
                    267:  */
                    268: dopaste(pfile)
                    269: char *pfile;
                    270: {
                    271:         FILE *pf;
                    272:         register int c;
                    273:
                    274:         if ((pf = fopen(pfile, "r")) != NULL) {
                    275:                 while((c = getc(pf)) != EOF)
                    276:                         putc(c, active);
                    277:                 (void) fclose(pf);
                    278:                 return(1);
                    279:         }
                    280:         else
                    281:                 return(0);
                    282: }
                    283: #endif
                    284:
                    285: /*
                    286:  * dochq - change quote characters
                    287:  *
                    288:  */
                    289: dochq(argv, argc)
                    290: register char *argv[];
                    291: register int argc;
                    292: {
                    293:         if (argc > 2) {
                    294:                 if (*argv[2])
                    295:                         lquote = *argv[2];
                    296:                 if (argc > 3) {
                    297:                         if (*argv[3])
                    298:                                 rquote = *argv[3];
                    299:                 }
                    300:                 else
                    301:                         rquote = lquote;
                    302:         }
                    303:         else {
                    304:                 lquote = LQUOTE;
                    305:                 rquote = RQUOTE;
                    306:         }
                    307: }
                    308:
                    309: /*
                    310:  * dochc - change comment characters
                    311:  *
                    312:  */
                    313: dochc(argv, argc)
                    314: register char *argv[];
                    315: register int argc;
                    316: {
                    317:         if (argc > 2) {
                    318:                 if (*argv[2])
                    319:                         scommt = *argv[2];
                    320:                 if (argc > 3) {
                    321:                         if (*argv[3])
                    322:                                 ecommt = *argv[3];
                    323:                 }
                    324:                 else
                    325:                         ecommt = ECOMMT;
                    326:         }
                    327:         else {
                    328:                 scommt = SCOMMT;
                    329:                 ecommt = ECOMMT;
                    330:         }
                    331: }
                    332:
                    333: /*
                    334:  * dodivert - divert the output to a temporary file
                    335:  *
                    336:  */
                    337: dodiv(n)
                    338: register int n;
                    339: {
                    340:         if (n < 0 || n >= MAXOUT)
                    341:                 n = 0;                  /* bitbucket */
                    342:         if (outfile[n] == NULL) {
                    343:                 m4temp[UNIQUE] = n + '0';
1.4     ! deraadt   344:                 if ((outfile[n] = fopen(m4temp, "w")) == NULL)
1.1       deraadt   345:                         error("m4: cannot divert.");
                    346:         }
                    347:         oindex = n;
                    348:         active = outfile[n];
                    349: }
                    350:
                    351: /*
                    352:  * doundivert - undivert a specified output, or all
                    353:  *              other outputs, in numerical order.
                    354:  */
                    355: doundiv(argv, argc)
                    356: register char *argv[];
                    357: register int argc;
                    358: {
                    359:         register int ind;
                    360:         register int n;
                    361:
                    362:         if (argc > 2) {
                    363:                 for (ind = 2; ind < argc; ind++) {
                    364:                         n = atoi(argv[ind]);
                    365:                         if (n > 0 && n < MAXOUT && outfile[n] != NULL)
                    366:                                 getdiv(n);
                    367:
                    368:                 }
                    369:         }
                    370:         else
                    371:                 for (n = 1; n < MAXOUT; n++)
                    372:                         if (outfile[n] != NULL)
                    373:                                 getdiv(n);
                    374: }
                    375:
                    376: /*
                    377:  * dosub - select substring
                    378:  *
                    379:  */
                    380: dosub (argv, argc)
                    381: register char *argv[];
                    382: register int  argc;
                    383: {
                    384:         register char *ap, *fc, *k;
                    385:         register int nc;
                    386:
                    387:         if (argc < 5)
                    388:                 nc = MAXTOK;
                    389:         else
                    390: #ifdef EXPR
                    391:                 nc = expr(argv[4]);
                    392: #else
                    393:                nc = atoi(argv[4]);
                    394: #endif
                    395:         ap = argv[2];                   /* target string */
                    396: #ifdef EXPR
                    397:         fc = ap + expr(argv[3]);        /* first char */
                    398: #else
                    399:         fc = ap + atoi(argv[3]);        /* first char */
                    400: #endif
                    401:         if (fc >= ap && fc < ap+strlen(ap))
                    402:                 for (k = fc+min(nc,strlen(fc))-1; k >= fc; k--)
                    403:                         putback(*k);
                    404: }
                    405:
                    406: /*
                    407:  * map:
                    408:  * map every character of s1 that is specified in from
                    409:  * into s3 and replace in s. (source s1 remains untouched)
                    410:  *
                    411:  * This is a standard implementation of map(s,from,to) function of ICON
                    412:  * language. Within mapvec, we replace every character of "from" with
                    413:  * the corresponding character in "to". If "to" is shorter than "from",
                    414:  * than the corresponding entries are null, which means that those
                    415:  * characters dissapear altogether. Furthermore, imagine
                    416:  * map(dest, "sourcestring", "srtin", "rn..*") type call. In this case,
                    417:  * `s' maps to `r', `r' maps to `n' and `n' maps to `*'. Thus, `s'
                    418:  * ultimately maps to `*'. In order to achieve this effect in an efficient
                    419:  * manner (i.e. without multiple passes over the destination string), we
                    420:  * loop over mapvec, starting with the initial source character. if the
                    421:  * character value (dch) in this location is different than the source
                    422:  * character (sch), sch becomes dch, once again to index into mapvec, until
                    423:  * the character value stabilizes (i.e. sch = dch, in other words
                    424:  * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
                    425:  * character, it will stabilize, since mapvec[0] == 0 at all times. At the
                    426:  * end, we restore mapvec* back to normal where mapvec[n] == n for
                    427:  * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
                    428:  * about 5 times faster than any algorithm that makes multiple passes over
                    429:  * destination string.
                    430:  *
                    431:  */
                    432:
                    433: map(dest,src,from,to)
                    434: register char *dest;
                    435: register char *src;
                    436: register char *from;
                    437: register char *to;
                    438: {
                    439:         register char *tmp;
                    440:         register char sch, dch;
                    441:         static char mapvec[128] = {
                    442:                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
                    443:                 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
                    444:                 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
                    445:                 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
                    446:                 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
                    447:                 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
                    448:                 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
                    449:                 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
                    450:                 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
                    451:                 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
                    452:                 120, 121, 122, 123, 124, 125, 126, 127
                    453:         };
                    454:
                    455:         if (*src) {
                    456:                 tmp = from;
                    457:        /*
                    458:         * create a mapping between "from" and "to"
                    459:         */
                    460:                 while (*from)
                    461:                         mapvec[*from++] = (*to) ? *to++ : (char) 0;
                    462:
                    463:                 while (*src) {
                    464:                         sch = *src++;
                    465:                         dch = mapvec[sch];
                    466:                         while (dch != sch) {
                    467:                                 sch = dch;
                    468:                                 dch = mapvec[sch];
                    469:                         }
                    470:                         if (*dest = dch)
                    471:                                 dest++;
                    472:                 }
                    473:        /*
                    474:         * restore all the changed characters
                    475:         */
                    476:                 while (*tmp) {
                    477:                         mapvec[*tmp] = *tmp;
                    478:                         tmp++;
                    479:                 }
                    480:         }
                    481:         *dest = (char) 0;
                    482: }