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