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