gnum4.c revision 1.6 1 1.6 christos /* $NetBSD: gnum4.c,v 1.6 2009/10/26 21:11:28 christos Exp $ */
2 1.6 christos /* $OpenBSD: gnum4.c,v 1.39 2008/08/21 21:01:04 espie Exp $ */
3 1.1 tv
4 1.1 tv /*
5 1.1 tv * Copyright (c) 1999 Marc Espie
6 1.1 tv *
7 1.1 tv * Redistribution and use in source and binary forms, with or without
8 1.1 tv * modification, are permitted provided that the following conditions
9 1.1 tv * are met:
10 1.1 tv * 1. Redistributions of source code must retain the above copyright
11 1.1 tv * notice, this list of conditions and the following disclaimer.
12 1.1 tv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tv * notice, this list of conditions and the following disclaimer in the
14 1.1 tv * documentation and/or other materials provided with the distribution.
15 1.1 tv *
16 1.1 tv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 tv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 tv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 tv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 tv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 tv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 tv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 tv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 tv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 tv * SUCH DAMAGE.
27 1.1 tv */
28 1.1 tv
29 1.6 christos /*
30 1.6 christos * functions needed to support gnu-m4 extensions, including a fake freezing
31 1.6 christos */
32 1.4 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.4 jmc #include "nbtool_config.h"
34 1.4 jmc #endif
35 1.6 christos #include <sys/cdefs.h>
36 1.6 christos __RCSID("$NetBSD: gnum4.c,v 1.6 2009/10/26 21:11:28 christos Exp $");
37 1.1 tv
38 1.1 tv #include <sys/param.h>
39 1.1 tv #include <sys/types.h>
40 1.1 tv #include <sys/wait.h>
41 1.1 tv #include <ctype.h>
42 1.6 christos #include <err.h>
43 1.1 tv #include <paths.h>
44 1.1 tv #include <regex.h>
45 1.1 tv #include <stddef.h>
46 1.1 tv #include <stdlib.h>
47 1.1 tv #include <stdio.h>
48 1.1 tv #include <string.h>
49 1.6 christos #include <errno.h>
50 1.6 christos #include <unistd.h>
51 1.1 tv #include "mdef.h"
52 1.1 tv #include "stdd.h"
53 1.1 tv #include "extern.h"
54 1.1 tv
55 1.1 tv
56 1.1 tv int mimic_gnu = 0;
57 1.1 tv
58 1.1 tv /*
59 1.1 tv * Support for include path search
60 1.3 simonb * First search in the current directory.
61 1.1 tv * If not found, and the path is not absolute, include path kicks in.
62 1.1 tv * First, -I options, in the order found on the command line.
63 1.1 tv * Then M4PATH env variable
64 1.1 tv */
65 1.1 tv
66 1.1 tv struct path_entry {
67 1.1 tv char *name;
68 1.1 tv struct path_entry *next;
69 1.1 tv } *first, *last;
70 1.1 tv
71 1.6 christos static struct path_entry *new_path_entry(const char *);
72 1.6 christos static void ensure_m4path(void);
73 1.6 christos static struct input_file *dopath(struct input_file *, const char *);
74 1.1 tv
75 1.1 tv static struct path_entry *
76 1.6 christos new_path_entry(const char *dirname)
77 1.1 tv {
78 1.1 tv struct path_entry *n;
79 1.1 tv
80 1.1 tv n = malloc(sizeof(struct path_entry));
81 1.1 tv if (!n)
82 1.1 tv errx(1, "out of memory");
83 1.1 tv n->name = strdup(dirname);
84 1.1 tv if (!n->name)
85 1.1 tv errx(1, "out of memory");
86 1.1 tv n->next = 0;
87 1.1 tv return n;
88 1.1 tv }
89 1.1 tv
90 1.1 tv void
91 1.6 christos addtoincludepath(const char *dirname)
92 1.1 tv {
93 1.1 tv struct path_entry *n;
94 1.1 tv
95 1.1 tv n = new_path_entry(dirname);
96 1.1 tv
97 1.1 tv if (last) {
98 1.1 tv last->next = n;
99 1.1 tv last = n;
100 1.1 tv }
101 1.1 tv else
102 1.1 tv last = first = n;
103 1.1 tv }
104 1.1 tv
105 1.1 tv static void
106 1.1 tv ensure_m4path()
107 1.1 tv {
108 1.1 tv static int envpathdone = 0;
109 1.1 tv char *envpath;
110 1.1 tv char *sweep;
111 1.1 tv char *path;
112 1.1 tv
113 1.1 tv if (envpathdone)
114 1.1 tv return;
115 1.1 tv envpathdone = TRUE;
116 1.1 tv envpath = getenv("M4PATH");
117 1.1 tv if (!envpath)
118 1.1 tv return;
119 1.1 tv /* for portability: getenv result is read-only */
120 1.1 tv envpath = strdup(envpath);
121 1.1 tv if (!envpath)
122 1.1 tv errx(1, "out of memory");
123 1.1 tv for (sweep = envpath;
124 1.1 tv (path = strsep(&sweep, ":")) != NULL;)
125 1.1 tv addtoincludepath(path);
126 1.1 tv free(envpath);
127 1.1 tv }
128 1.1 tv
129 1.1 tv static
130 1.1 tv struct input_file *
131 1.6 christos dopath(struct input_file *i, const char *filename)
132 1.1 tv {
133 1.1 tv char path[MAXPATHLEN];
134 1.1 tv struct path_entry *pe;
135 1.1 tv FILE *f;
136 1.1 tv
137 1.1 tv for (pe = first; pe; pe = pe->next) {
138 1.1 tv snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
139 1.1 tv if ((f = fopen(path, "r")) != 0) {
140 1.1 tv set_input(i, f, path);
141 1.1 tv return i;
142 1.1 tv }
143 1.1 tv }
144 1.1 tv return NULL;
145 1.1 tv }
146 1.1 tv
147 1.1 tv struct input_file *
148 1.6 christos fopen_trypath(struct input_file *i, const char *filename)
149 1.1 tv {
150 1.1 tv FILE *f;
151 1.1 tv
152 1.1 tv f = fopen(filename, "r");
153 1.1 tv if (f != NULL) {
154 1.1 tv set_input(i, f, filename);
155 1.1 tv return i;
156 1.1 tv }
157 1.1 tv if (filename[0] == '/')
158 1.1 tv return NULL;
159 1.1 tv
160 1.1 tv ensure_m4path();
161 1.1 tv
162 1.1 tv return dopath(i, filename);
163 1.1 tv }
164 1.1 tv
165 1.1 tv void
166 1.6 christos doindir(const char *argv[], int argc)
167 1.1 tv {
168 1.6 christos ndptr n;
169 1.6 christos struct macro_definition *p;
170 1.1 tv
171 1.6 christos n = lookup(argv[2]);
172 1.6 christos if (n == NULL || (p = macro_getdef(n)) == NULL)
173 1.6 christos m4errx(1, "indir: undefined macro %s.", argv[2]);
174 1.1 tv argv[1] = p->defn;
175 1.6 christos
176 1.6 christos eval(argv+1, argc-1, p->type, is_traced(n));
177 1.1 tv }
178 1.1 tv
179 1.1 tv void
180 1.6 christos dobuiltin(const char *argv[], int argc)
181 1.1 tv {
182 1.6 christos ndptr p;
183 1.6 christos
184 1.1 tv argv[1] = NULL;
185 1.6 christos p = macro_getbuiltin(argv[2]);
186 1.6 christos if (p != NULL)
187 1.6 christos eval(argv+1, argc-1, macro_builtin_type(p), is_traced(p));
188 1.1 tv else
189 1.6 christos m4errx(1, "unknown builtin %s.", argv[2]);
190 1.1 tv }
191 1.1 tv
192 1.1 tv
193 1.1 tv /* We need some temporary buffer space, as pb pushes BACK and substitution
194 1.1 tv * proceeds forward... */
195 1.1 tv static char *buffer;
196 1.1 tv static size_t bufsize = 0;
197 1.1 tv static size_t current = 0;
198 1.1 tv
199 1.6 christos static void addchars(const char *, size_t);
200 1.6 christos static void addchar(int);
201 1.6 christos static char *twiddle(const char *);
202 1.6 christos static char *getstring(void);
203 1.6 christos static void exit_regerror(int, regex_t *);
204 1.6 christos static void do_subst(const char *, regex_t *, const char *, regmatch_t *);
205 1.6 christos static void do_regexpindex(const char *, regex_t *, regmatch_t *);
206 1.6 christos static void do_regexp(const char *, regex_t *, const char *, regmatch_t *);
207 1.6 christos static void add_sub(size_t, const char *, regex_t *, regmatch_t *);
208 1.6 christos static void add_replace(const char *, regex_t *, const char *, regmatch_t *);
209 1.1 tv #define addconstantstring(s) addchars((s), sizeof(s)-1)
210 1.1 tv
211 1.1 tv static void
212 1.6 christos addchars(const char *c, size_t n)
213 1.1 tv {
214 1.1 tv if (n == 0)
215 1.1 tv return;
216 1.1 tv while (current + n > bufsize) {
217 1.1 tv if (bufsize == 0)
218 1.1 tv bufsize = 1024;
219 1.1 tv else
220 1.1 tv bufsize *= 2;
221 1.6 christos buffer = xrealloc(buffer, bufsize, NULL);
222 1.1 tv }
223 1.1 tv memcpy(buffer+current, c, n);
224 1.1 tv current += n;
225 1.1 tv }
226 1.1 tv
227 1.1 tv static void
228 1.6 christos addchar(int c)
229 1.1 tv {
230 1.1 tv if (current +1 > bufsize) {
231 1.1 tv if (bufsize == 0)
232 1.1 tv bufsize = 1024;
233 1.1 tv else
234 1.1 tv bufsize *= 2;
235 1.6 christos buffer = xrealloc(buffer, bufsize, NULL);
236 1.1 tv }
237 1.1 tv buffer[current++] = c;
238 1.1 tv }
239 1.1 tv
240 1.1 tv static char *
241 1.1 tv getstring()
242 1.1 tv {
243 1.1 tv addchar('\0');
244 1.1 tv current = 0;
245 1.1 tv return buffer;
246 1.1 tv }
247 1.1 tv
248 1.1 tv
249 1.1 tv static void
250 1.6 christos exit_regerror(int er, regex_t *re)
251 1.1 tv {
252 1.1 tv size_t errlen;
253 1.1 tv char *errbuf;
254 1.1 tv
255 1.1 tv errlen = regerror(er, re, NULL, 0);
256 1.6 christos errbuf = xalloc(errlen,
257 1.6 christos "malloc in regerror: %lu", (unsigned long)errlen);
258 1.1 tv regerror(er, re, errbuf, errlen);
259 1.6 christos m4errx(1, "regular expression error: %s.", errbuf);
260 1.1 tv }
261 1.1 tv
262 1.1 tv static void
263 1.6 christos add_sub(size_t n, const char *string, regex_t *re, regmatch_t *pm)
264 1.1 tv {
265 1.1 tv if (n > re->re_nsub)
266 1.6 christos warnx("No subexpression %zu", n);
267 1.1 tv /* Subexpressions that did not match are
268 1.1 tv * not an error. */
269 1.1 tv else if (pm[n].rm_so != -1 &&
270 1.1 tv pm[n].rm_eo != -1) {
271 1.1 tv addchars(string + pm[n].rm_so,
272 1.1 tv pm[n].rm_eo - pm[n].rm_so);
273 1.1 tv }
274 1.1 tv }
275 1.1 tv
276 1.1 tv /* Add replacement string to the output buffer, recognizing special
277 1.1 tv * constructs and replacing them with substrings of the original string.
278 1.1 tv */
279 1.1 tv static void
280 1.6 christos add_replace(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
281 1.1 tv {
282 1.1 tv const char *p;
283 1.1 tv
284 1.1 tv for (p = replace; *p != '\0'; p++) {
285 1.1 tv if (*p == '&' && !mimic_gnu) {
286 1.1 tv add_sub(0, string, re, pm);
287 1.1 tv continue;
288 1.1 tv }
289 1.1 tv if (*p == '\\') {
290 1.1 tv if (p[1] == '\\') {
291 1.1 tv addchar(p[1]);
292 1.1 tv p++;
293 1.1 tv continue;
294 1.1 tv }
295 1.1 tv if (p[1] == '&') {
296 1.1 tv if (mimic_gnu)
297 1.1 tv add_sub(0, string, re, pm);
298 1.1 tv else
299 1.1 tv addchar(p[1]);
300 1.1 tv p++;
301 1.1 tv continue;
302 1.1 tv }
303 1.5 dsl if (isdigit((unsigned char)p[1])) {
304 1.1 tv add_sub(*(++p) - '0', string, re, pm);
305 1.1 tv continue;
306 1.1 tv }
307 1.1 tv }
308 1.1 tv addchar(*p);
309 1.1 tv }
310 1.1 tv }
311 1.1 tv
312 1.1 tv static void
313 1.6 christos do_subst(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
314 1.1 tv {
315 1.1 tv int error;
316 1.1 tv int flags = 0;
317 1.1 tv const char *last_match = NULL;
318 1.1 tv
319 1.1 tv while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
320 1.1 tv if (pm[0].rm_eo != 0) {
321 1.1 tv if (string[pm[0].rm_eo-1] == '\n')
322 1.1 tv flags = 0;
323 1.1 tv else
324 1.1 tv flags = REG_NOTBOL;
325 1.1 tv }
326 1.1 tv
327 1.1 tv /* NULL length matches are special... We use the `vi-mode'
328 1.1 tv * rule: don't allow a NULL-match at the last match
329 1.1 tv * position.
330 1.1 tv */
331 1.1 tv if (pm[0].rm_so == pm[0].rm_eo &&
332 1.1 tv string + pm[0].rm_so == last_match) {
333 1.1 tv if (*string == '\0')
334 1.1 tv return;
335 1.1 tv addchar(*string);
336 1.1 tv if (*string++ == '\n')
337 1.1 tv flags = 0;
338 1.1 tv else
339 1.1 tv flags = REG_NOTBOL;
340 1.1 tv continue;
341 1.1 tv }
342 1.1 tv last_match = string + pm[0].rm_so;
343 1.1 tv addchars(string, pm[0].rm_so);
344 1.1 tv add_replace(string, re, replace, pm);
345 1.1 tv string += pm[0].rm_eo;
346 1.1 tv }
347 1.1 tv if (error != REG_NOMATCH)
348 1.1 tv exit_regerror(error, re);
349 1.1 tv pbstr(string);
350 1.1 tv }
351 1.1 tv
352 1.1 tv static void
353 1.6 christos do_regexp(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
354 1.1 tv {
355 1.1 tv int error;
356 1.1 tv
357 1.1 tv switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
358 1.1 tv case 0:
359 1.1 tv add_replace(string, re, replace, pm);
360 1.1 tv pbstr(getstring());
361 1.1 tv break;
362 1.1 tv case REG_NOMATCH:
363 1.1 tv break;
364 1.1 tv default:
365 1.1 tv exit_regerror(error, re);
366 1.1 tv }
367 1.1 tv }
368 1.1 tv
369 1.1 tv static void
370 1.6 christos do_regexpindex(const char *string, regex_t *re, regmatch_t *pm)
371 1.1 tv {
372 1.1 tv int error;
373 1.1 tv
374 1.1 tv switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
375 1.1 tv case 0:
376 1.1 tv pbunsigned(pm[0].rm_so);
377 1.1 tv break;
378 1.1 tv case REG_NOMATCH:
379 1.1 tv pbnum(-1);
380 1.1 tv break;
381 1.1 tv default:
382 1.1 tv exit_regerror(error, re);
383 1.1 tv }
384 1.1 tv }
385 1.1 tv
386 1.1 tv /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
387 1.1 tv * says. So we twiddle with the regexp before passing it to regcomp.
388 1.1 tv */
389 1.1 tv static char *
390 1.6 christos twiddle(const char *p)
391 1.1 tv {
392 1.6 christos /* + at start of regexp is a normal character for Gnu m4 */
393 1.6 christos if (*p == '^') {
394 1.6 christos addchar(*p);
395 1.6 christos p++;
396 1.6 christos }
397 1.6 christos if (*p == '+') {
398 1.6 christos addchar('\\');
399 1.6 christos }
400 1.1 tv /* This could use strcspn for speed... */
401 1.1 tv while (*p != '\0') {
402 1.1 tv if (*p == '\\') {
403 1.1 tv switch(p[1]) {
404 1.1 tv case '(':
405 1.1 tv case ')':
406 1.1 tv case '|':
407 1.1 tv addchar(p[1]);
408 1.1 tv break;
409 1.1 tv case 'w':
410 1.1 tv addconstantstring("[_a-zA-Z0-9]");
411 1.1 tv break;
412 1.1 tv case 'W':
413 1.1 tv addconstantstring("[^_a-zA-Z0-9]");
414 1.1 tv break;
415 1.1 tv case '<':
416 1.1 tv addconstantstring("[[:<:]]");
417 1.1 tv break;
418 1.1 tv case '>':
419 1.1 tv addconstantstring("[[:>:]]");
420 1.1 tv break;
421 1.1 tv default:
422 1.1 tv addchars(p, 2);
423 1.1 tv break;
424 1.1 tv }
425 1.1 tv p+=2;
426 1.1 tv continue;
427 1.1 tv }
428 1.1 tv if (*p == '(' || *p == ')' || *p == '|')
429 1.1 tv addchar('\\');
430 1.1 tv
431 1.1 tv addchar(*p);
432 1.1 tv p++;
433 1.1 tv }
434 1.1 tv return getstring();
435 1.1 tv }
436 1.1 tv
437 1.1 tv /* patsubst(string, regexp, opt replacement) */
438 1.1 tv /* argv[2]: string
439 1.1 tv * argv[3]: regexp
440 1.1 tv * argv[4]: opt rep
441 1.1 tv */
442 1.1 tv void
443 1.6 christos dopatsubst(const char *argv[], int argc)
444 1.1 tv {
445 1.1 tv if (argc <= 3) {
446 1.1 tv warnx("Too few arguments to patsubst");
447 1.1 tv return;
448 1.1 tv }
449 1.6 christos /* special case: empty regexp */
450 1.6 christos if (argv[3][0] == '\0') {
451 1.6 christos const char *s;
452 1.6 christos size_t len;
453 1.6 christos if (argv[4] && argc > 4)
454 1.6 christos len = strlen(argv[4]);
455 1.6 christos else
456 1.6 christos len = 0;
457 1.6 christos for (s = argv[2]; *s != '\0'; s++) {
458 1.6 christos addchars(argv[4], len);
459 1.6 christos addchar(*s);
460 1.6 christos }
461 1.6 christos } else {
462 1.6 christos int error;
463 1.6 christos regex_t re;
464 1.6 christos regmatch_t *pmatch;
465 1.6 christos int mode = REG_EXTENDED;
466 1.6 christos size_t l = strlen(argv[3]);
467 1.6 christos
468 1.6 christos if (!mimic_gnu ||
469 1.6 christos (argv[3][0] == '^') ||
470 1.6 christos (l > 0 && argv[3][l-1] == '$'))
471 1.6 christos mode |= REG_NEWLINE;
472 1.6 christos
473 1.6 christos error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
474 1.6 christos mode);
475 1.6 christos if (error != 0)
476 1.6 christos exit_regerror(error, &re);
477 1.6 christos
478 1.6 christos pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1), NULL);
479 1.6 christos do_subst(argv[2], &re,
480 1.6 christos argc > 4 && argv[4] != NULL ? argv[4] : "", pmatch);
481 1.6 christos free(pmatch);
482 1.6 christos regfree(&re);
483 1.6 christos }
484 1.1 tv pbstr(getstring());
485 1.1 tv }
486 1.1 tv
487 1.1 tv void
488 1.6 christos doregexp(const char *argv[], int argc)
489 1.1 tv {
490 1.1 tv int error;
491 1.1 tv regex_t re;
492 1.1 tv regmatch_t *pmatch;
493 1.1 tv
494 1.1 tv if (argc <= 3) {
495 1.1 tv warnx("Too few arguments to regexp");
496 1.1 tv return;
497 1.1 tv }
498 1.1 tv error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
499 1.1 tv REG_EXTENDED);
500 1.1 tv if (error != 0)
501 1.1 tv exit_regerror(error, &re);
502 1.1 tv
503 1.6 christos pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1), NULL);
504 1.1 tv if (argv[4] == NULL || argc == 4)
505 1.1 tv do_regexpindex(argv[2], &re, pmatch);
506 1.1 tv else
507 1.1 tv do_regexp(argv[2], &re, argv[4], pmatch);
508 1.1 tv free(pmatch);
509 1.1 tv regfree(&re);
510 1.1 tv }
511 1.1 tv
512 1.1 tv void
513 1.6 christos doformat(const char *argv[], int argc)
514 1.6 christos {
515 1.6 christos const char *format = argv[2];
516 1.6 christos int pos = 3;
517 1.6 christos int left_padded;
518 1.6 christos long width;
519 1.6 christos size_t l;
520 1.6 christos const char *thisarg;
521 1.6 christos char temp[2];
522 1.6 christos size_t extra;
523 1.6 christos
524 1.6 christos while (*format != 0) {
525 1.6 christos if (*format != '%') {
526 1.6 christos addchar(*format++);
527 1.6 christos continue;
528 1.6 christos }
529 1.6 christos
530 1.6 christos format++;
531 1.6 christos if (*format == '%') {
532 1.6 christos addchar(*format++);
533 1.6 christos continue;
534 1.6 christos }
535 1.6 christos if (*format == 0) {
536 1.6 christos addchar('%');
537 1.6 christos break;
538 1.6 christos }
539 1.6 christos
540 1.6 christos if (*format == '*') {
541 1.6 christos format++;
542 1.6 christos if (pos >= argc)
543 1.6 christos m4errx(1,
544 1.6 christos "Format with too many format specifiers.");
545 1.6 christos width = strtol(argv[pos++], NULL, 10);
546 1.6 christos } else {
547 1.6 christos char *eformat;
548 1.6 christos width = strtol(format, &eformat, 10);
549 1.6 christos format = eformat;
550 1.6 christos }
551 1.6 christos if (width < 0) {
552 1.6 christos left_padded = 1;
553 1.6 christos width = -width;
554 1.6 christos } else {
555 1.6 christos left_padded = 0;
556 1.6 christos }
557 1.6 christos if (*format == '.') {
558 1.6 christos format++;
559 1.6 christos if (*format == '*') {
560 1.6 christos format++;
561 1.6 christos if (pos >= argc)
562 1.6 christos m4errx(1,
563 1.6 christos "Format with too many format specifiers.");
564 1.6 christos extra = strtol(argv[pos++], NULL, 10);
565 1.6 christos } else {
566 1.6 christos char *eformat;
567 1.6 christos extra = strtol(format, &eformat, 10);
568 1.6 christos format = eformat;
569 1.6 christos }
570 1.6 christos } else {
571 1.6 christos extra = SIZE_T_MAX;
572 1.6 christos }
573 1.6 christos if (pos >= argc)
574 1.6 christos m4errx(1, "Format with too many format specifiers.");
575 1.6 christos switch(*format) {
576 1.6 christos case 's':
577 1.6 christos thisarg = argv[pos++];
578 1.6 christos break;
579 1.6 christos case 'c':
580 1.6 christos temp[0] = strtoul(argv[pos++], NULL, 10);
581 1.6 christos temp[1] = 0;
582 1.6 christos thisarg = temp;
583 1.6 christos break;
584 1.6 christos default:
585 1.6 christos m4errx(1, "Unsupported format specification: %s.",
586 1.6 christos argv[2]);
587 1.6 christos }
588 1.6 christos format++;
589 1.6 christos l = strlen(thisarg);
590 1.6 christos if (l > extra)
591 1.6 christos l = extra;
592 1.6 christos if (!left_padded) {
593 1.6 christos while (l < (size_t)width--)
594 1.6 christos addchar(' ');
595 1.6 christos }
596 1.6 christos addchars(thisarg, l);
597 1.6 christos if (left_padded) {
598 1.6 christos while (l < (size_t)width--)
599 1.6 christos addchar(' ');
600 1.6 christos }
601 1.6 christos }
602 1.6 christos pbstr(getstring());
603 1.6 christos }
604 1.6 christos
605 1.6 christos void
606 1.6 christos doesyscmd(const char *cmd)
607 1.1 tv {
608 1.1 tv int p[2];
609 1.1 tv pid_t pid, cpid;
610 1.6 christos const char *argv[4];
611 1.1 tv int cc;
612 1.1 tv int status;
613 1.1 tv
614 1.1 tv /* Follow gnu m4 documentation: first flush buffers. */
615 1.1 tv fflush(NULL);
616 1.1 tv
617 1.1 tv argv[0] = "sh";
618 1.1 tv argv[1] = "-c";
619 1.6 christos argv[2] = cmd;
620 1.1 tv argv[3] = NULL;
621 1.1 tv
622 1.1 tv /* Just set up standard output, share stderr and stdin with m4 */
623 1.1 tv if (pipe(p) == -1)
624 1.1 tv err(1, "bad pipe");
625 1.1 tv switch(cpid = fork()) {
626 1.1 tv case -1:
627 1.1 tv err(1, "bad fork");
628 1.1 tv /* NOTREACHED */
629 1.1 tv case 0:
630 1.1 tv (void) close(p[0]);
631 1.1 tv (void) dup2(p[1], 1);
632 1.1 tv (void) close(p[1]);
633 1.6 christos execv(_PATH_BSHELL, __UNCONST(argv));
634 1.1 tv exit(1);
635 1.1 tv default:
636 1.1 tv /* Read result in two stages, since m4's buffer is
637 1.1 tv * pushback-only. */
638 1.1 tv (void) close(p[1]);
639 1.1 tv do {
640 1.1 tv char result[BUFSIZE];
641 1.1 tv cc = read(p[0], result, sizeof result);
642 1.1 tv if (cc > 0)
643 1.1 tv addchars(result, cc);
644 1.1 tv } while (cc > 0 || (cc == -1 && errno == EINTR));
645 1.1 tv
646 1.1 tv (void) close(p[0]);
647 1.1 tv while ((pid = wait(&status)) != cpid && pid >= 0)
648 1.1 tv continue;
649 1.1 tv pbstr(getstring());
650 1.1 tv }
651 1.1 tv }
652 1.6 christos
653 1.6 christos void
654 1.6 christos getdivfile(const char *name)
655 1.6 christos {
656 1.6 christos FILE *f;
657 1.6 christos int c;
658 1.6 christos
659 1.6 christos f = fopen(name, "r");
660 1.6 christos if (!f)
661 1.6 christos return;
662 1.6 christos
663 1.6 christos while ((c = getc(f))!= EOF)
664 1.6 christos putc(c, active);
665 1.6 christos (void) fclose(f);
666 1.6 christos }
667