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