Annotation of src/usr.bin/m4/eval.c, Revision 1.53
1.53 ! espie 1: /* $OpenBSD: eval.c,v 1.52 2005/01/20 23:47:04 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.53 ! espie 40: static char rcsid[] = "$OpenBSD: eval.c,v 1.52 2005/01/20 23:47:04 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.53 ! espie 617: if (!traceout)
! 618: traceout = stderr;
1.33 espie 619: if (mimic_gnu) {
620: if ((p->type & TYPEMASK) == MACRTYPE)
1.49 espie 621: fprintf(traceout, "%s:\t%s\n", name, p->defn);
1.33 espie 622: else {
1.49 espie 623: fprintf(traceout, "%s:\t<%s>\n", name, p->defn);
1.33 espie 624: }
625: } else
1.49 espie 626: fprintf(traceout, "`%s'\t`%s'\n", name, p->defn);
1.32 espie 627: }
628:
629: /*
1.1 deraadt 630: * dodumpdef - dump the specified definitions in the hash
631: * table to stderr. If nothing is specified, the entire
632: * hash table is dumped.
633: */
1.20 espie 634: static void
1.44 espie 635: dodump(const char *argv[], int argc)
1.1 deraadt 636: {
1.17 espie 637: int n;
1.49 espie 638: struct macro_definition *p;
1.1 deraadt 639:
640: if (argc > 2) {
641: for (n = 2; n < argc; n++)
1.49 espie 642: if ((p = lookup_macro_definition(argv[n])) != NULL)
643: dump_one_def(argv[n], p);
644: } else
645: macro_for_all(dump_one_def);
1.37 espie 646: }
647:
648: /*
649: * dotrace - mark some macros as traced/untraced depending upon on.
650: */
651: static void
1.44 espie 652: dotrace(const char *argv[], int argc, int on)
1.37 espie 653: {
654: int n;
655:
656: if (argc > 2) {
657: for (n = 2; n < argc; n++)
658: mark_traced(argv[n], on);
659: } else
660: mark_traced(NULL, on);
1.1 deraadt 661: }
662:
663: /*
664: * doifelse - select one of two alternatives - loop.
665: */
1.20 espie 666: static void
1.44 espie 667: doifelse(const char *argv[], int argc)
1.1 deraadt 668: {
669: cycle {
670: if (STREQ(argv[2], argv[3]))
671: pbstr(argv[4]);
672: else if (argc == 6)
673: pbstr(argv[5]);
674: else if (argc > 6) {
675: argv += 3;
676: argc -= 3;
677: continue;
678: }
679: break;
680: }
681: }
682:
683: /*
684: * doinclude - include a given file.
685: */
1.20 espie 686: static int
1.44 espie 687: doincl(const char *ifile)
1.1 deraadt 688: {
689: if (ilevel + 1 == MAXINP)
1.24 espie 690: errx(1, "%s at line %lu: too many include files.",
691: CURRENT_NAME, CURRENT_LINE);
692: if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
1.1 deraadt 693: ilevel++;
694: bbase[ilevel] = bufbase = bp;
695: return (1);
1.8 deraadt 696: } else
1.1 deraadt 697: return (0);
698: }
699:
700: #ifdef EXTENDED
701: /*
702: * dopaste - include a given file without any
703: * macro processing.
704: */
1.20 espie 705: static int
1.44 espie 706: dopaste(const char *pfile)
1.1 deraadt 707: {
708: FILE *pf;
1.17 espie 709: int c;
1.1 deraadt 710:
711: if ((pf = fopen(pfile, "r")) != NULL) {
1.45 espie 712: if (synch_lines)
713: fprintf(active, "#line 1 \"%s\"\n", pfile);
1.1 deraadt 714: while ((c = getc(pf)) != EOF)
715: putc(c, active);
716: (void) fclose(pf);
1.45 espie 717: emit_synchline();
1.1 deraadt 718: return (1);
1.8 deraadt 719: } else
1.1 deraadt 720: return (0);
721: }
722: #endif
723:
1.39 espie 724: static void
1.44 espie 725: gnu_dochq(const char *argv[], int ac)
1.39 espie 726: {
727: /* In gnu-m4 mode, the only way to restore quotes is to have no
728: * arguments at all. */
729: if (ac == 2) {
730: lquote[0] = LQUOTE, lquote[1] = EOS;
731: rquote[0] = RQUOTE, rquote[1] = EOS;
732: } else {
733: strlcpy(lquote, argv[2], sizeof(lquote));
734: if(ac > 3)
735: strlcpy(rquote, argv[3], sizeof(rquote));
736: else
737: rquote[0] = EOS;
738: }
739: }
740:
1.1 deraadt 741: /*
742: * dochq - change quote characters
743: */
1.20 espie 744: static void
1.44 espie 745: dochq(const char *argv[], int argc)
1.1 deraadt 746: {
747: if (argc > 2) {
1.9 deraadt 748: if (*argv[2])
1.21 espie 749: strlcpy(lquote, argv[2], sizeof(lquote));
1.9 deraadt 750: else {
751: lquote[0] = LQUOTE;
1.14 espie 752: lquote[1] = EOS;
1.9 deraadt 753: }
1.1 deraadt 754: if (argc > 3) {
755: if (*argv[3])
1.21 espie 756: strlcpy(rquote, argv[3], sizeof(rquote));
1.8 deraadt 757: } else
1.46 espie 758: strlcpy(rquote, lquote, sizeof(rquote));
1.8 deraadt 759: } else {
1.14 espie 760: lquote[0] = LQUOTE, lquote[1] = EOS;
761: rquote[0] = RQUOTE, rquote[1] = EOS;
1.1 deraadt 762: }
763: }
764:
1.39 espie 765: static void
1.44 espie 766: gnu_dochc(const char *argv[], int ac)
1.39 espie 767: {
768: /* In gnu-m4 mode, no arguments mean no comment
769: * arguments at all. */
770: if (ac == 2) {
771: scommt[0] = EOS;
772: ecommt[0] = EOS;
773: } else {
774: if (*argv[2])
775: strlcpy(scommt, argv[2], sizeof(scommt));
776: else
777: scommt[0] = SCOMMT, scommt[1] = EOS;
778: if(ac > 3 && *argv[3])
779: strlcpy(ecommt, argv[3], sizeof(ecommt));
780: else
781: ecommt[0] = ECOMMT, ecommt[1] = EOS;
782: }
783: }
1.1 deraadt 784: /*
785: * dochc - change comment characters
786: */
1.20 espie 787: static void
1.44 espie 788: dochc(const char *argv[], int argc)
1.1 deraadt 789: {
790: if (argc > 2) {
791: if (*argv[2])
1.21 espie 792: strlcpy(scommt, argv[2], sizeof(scommt));
1.1 deraadt 793: if (argc > 3) {
794: if (*argv[3])
1.21 espie 795: strlcpy(ecommt, argv[3], sizeof(ecommt));
1.1 deraadt 796: }
797: else
1.14 espie 798: ecommt[0] = ECOMMT, ecommt[1] = EOS;
1.1 deraadt 799: }
800: else {
1.14 espie 801: scommt[0] = SCOMMT, scommt[1] = EOS;
802: ecommt[0] = ECOMMT, ecommt[1] = EOS;
1.1 deraadt 803: }
804: }
805:
806: /*
807: * dodivert - divert the output to a temporary file
808: */
1.20 espie 809: static void
1.44 espie 810: dodiv(int n)
1.1 deraadt 811: {
1.6 millert 812: int fd;
813:
1.8 deraadt 814: oindex = n;
1.28 espie 815: if (n >= maxout) {
816: if (mimic_gnu)
817: resizedivs(n + 10);
818: else
819: n = 0; /* bitbucket */
820: }
821:
822: if (n < 0)
1.1 deraadt 823: n = 0; /* bitbucket */
824: if (outfile[n] == NULL) {
1.13 espie 825: char fname[] = _PATH_DIVNAME;
826:
827: if ((fd = mkstemp(fname)) < 0 ||
828: (outfile[n] = fdopen(fd, "w+")) == NULL)
829: err(1, "%s: cannot divert", fname);
830: if (unlink(fname) == -1)
831: err(1, "%s: cannot unlink", fname);
1.1 deraadt 832: }
833: active = outfile[n];
834: }
835:
836: /*
837: * doundivert - undivert a specified output, or all
838: * other outputs, in numerical order.
839: */
1.20 espie 840: static void
1.44 espie 841: doundiv(const char *argv[], int argc)
1.1 deraadt 842: {
1.17 espie 843: int ind;
844: int n;
1.1 deraadt 845:
846: if (argc > 2) {
847: for (ind = 2; ind < argc; ind++) {
848: n = atoi(argv[ind]);
1.28 espie 849: if (n > 0 && n < maxout && outfile[n] != NULL)
1.1 deraadt 850: getdiv(n);
851:
852: }
853: }
854: else
1.28 espie 855: for (n = 1; n < maxout; n++)
1.1 deraadt 856: if (outfile[n] != NULL)
857: getdiv(n);
858: }
859:
860: /*
861: * dosub - select substring
862: */
1.20 espie 863: static void
1.44 espie 864: dosub(const char *argv[], int argc)
1.1 deraadt 865: {
1.20 espie 866: const char *ap, *fc, *k;
1.17 espie 867: int nc;
1.1 deraadt 868:
1.29 espie 869: ap = argv[2]; /* target string */
1.1 deraadt 870: #ifdef EXPR
1.29 espie 871: fc = ap + expr(argv[3]); /* first char */
1.1 deraadt 872: #else
1.29 espie 873: fc = ap + atoi(argv[3]); /* first char */
1.1 deraadt 874: #endif
1.29 espie 875: nc = strlen(fc);
876: if (argc >= 5)
1.1 deraadt 877: #ifdef EXPR
1.29 espie 878: nc = min(nc, expr(argv[4]));
1.1 deraadt 879: #else
1.29 espie 880: nc = min(nc, atoi(argv[4]));
1.1 deraadt 881: #endif
882: if (fc >= ap && fc < ap + strlen(ap))
1.29 espie 883: for (k = fc + nc - 1; k >= fc; k--)
1.1 deraadt 884: putback(*k);
885: }
886:
887: /*
888: * map:
889: * map every character of s1 that is specified in from
890: * into s3 and replace in s. (source s1 remains untouched)
891: *
892: * This is a standard implementation of map(s,from,to) function of ICON
893: * language. Within mapvec, we replace every character of "from" with
894: * the corresponding character in "to". If "to" is shorter than "from",
895: * than the corresponding entries are null, which means that those
896: * characters dissapear altogether. Furthermore, imagine
897: * map(dest, "sourcestring", "srtin", "rn..*") type call. In this case,
898: * `s' maps to `r', `r' maps to `n' and `n' maps to `*'. Thus, `s'
899: * ultimately maps to `*'. In order to achieve this effect in an efficient
900: * manner (i.e. without multiple passes over the destination string), we
901: * loop over mapvec, starting with the initial source character. if the
902: * character value (dch) in this location is different than the source
903: * character (sch), sch becomes dch, once again to index into mapvec, until
904: * the character value stabilizes (i.e. sch = dch, in other words
905: * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
906: * character, it will stabilize, since mapvec[0] == 0 at all times. At the
907: * end, we restore mapvec* back to normal where mapvec[n] == n for
908: * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
909: * about 5 times faster than any algorithm that makes multiple passes over
910: * destination string.
911: */
1.20 espie 912: static void
1.44 espie 913: map(char *dest, const char *src, const char *from, const char *to)
1.1 deraadt 914: {
1.20 espie 915: const char *tmp;
1.19 espie 916: unsigned char sch, dch;
1.26 espie 917: static char frombis[257];
918: static char tobis[257];
1.19 espie 919: static unsigned char mapvec[256] = {
920: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
921: 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
922: 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
923: 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
924: 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
925: 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
926: 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
927: 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
928: 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
929: 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
930: 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
931: 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
932: 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
933: 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
934: 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
935: 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
936: 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
937: 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
1.1 deraadt 938: };
939:
940: if (*src) {
1.26 espie 941: if (mimic_gnu) {
942: /*
943: * expand character ranges on the fly
944: */
945: from = handledash(frombis, frombis + 256, from);
946: to = handledash(tobis, tobis + 256, to);
947: }
1.1 deraadt 948: tmp = from;
949: /*
950: * create a mapping between "from" and
951: * "to"
952: */
953: while (*from)
1.19 espie 954: mapvec[(unsigned char)(*from++)] = (*to) ?
955: (unsigned char)(*to++) : 0;
1.1 deraadt 956:
957: while (*src) {
1.19 espie 958: sch = (unsigned char)(*src++);
1.1 deraadt 959: dch = mapvec[sch];
960: while (dch != sch) {
961: sch = dch;
962: dch = mapvec[sch];
963: }
1.19 espie 964: if ((*dest = (char)dch))
1.1 deraadt 965: dest++;
966: }
967: /*
968: * restore all the changed characters
969: */
970: while (*tmp) {
1.19 espie 971: mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
1.1 deraadt 972: tmp++;
973: }
974: }
1.19 espie 975: *dest = '\0';
1.1 deraadt 976: }
1.26 espie 977:
978:
979: /*
980: * handledash:
981: * use buffer to copy the src string, expanding character ranges
982: * on the way.
983: */
984: static const char *
1.44 espie 985: handledash(char *buffer, char *end, const char *src)
1.26 espie 986: {
987: char *p;
988:
989: p = buffer;
990: while(*src) {
991: if (src[1] == '-' && src[2]) {
992: unsigned char i;
993: for (i = (unsigned char)src[0];
994: i <= (unsigned char)src[2]; i++) {
995: *p++ = i;
996: if (p == end) {
997: *p = '\0';
998: return buffer;
999: }
1000: }
1001: src += 3;
1002: } else
1003: *p++ = *src++;
1004: if (p == end)
1005: break;
1006: }
1007: *p = '\0';
1008: return buffer;
1009: }