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