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