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