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