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