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