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