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

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