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