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