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