2014-07-27 22:54:13 +00:00
|
|
|
/* $OpenBSD: gnum4.c,v 1.46 2014/07/10 14:12:31 espie Exp $ */
|
2002-02-16 21:27:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Marc Espie
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2002-04-19 17:33:12 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
/*
|
2002-02-16 21:27:48 +00:00
|
|
|
* functions needed to support gnu-m4 extensions, including a fake freezing
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <err.h>
|
2002-02-16 21:27:48 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "mdef.h"
|
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
|
|
|
|
int mimic_gnu = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for include path search
|
2011-02-21 09:01:34 +00:00
|
|
|
* First search in the current directory.
|
2002-02-16 21:27:48 +00:00
|
|
|
* If not found, and the path is not absolute, include path kicks in.
|
|
|
|
* First, -I options, in the order found on the command line.
|
|
|
|
* Then M4PATH env variable
|
|
|
|
*/
|
|
|
|
|
2012-10-20 10:33:15 +00:00
|
|
|
static struct path_entry {
|
2002-02-16 21:27:48 +00:00
|
|
|
char *name;
|
|
|
|
struct path_entry *next;
|
|
|
|
} *first, *last;
|
|
|
|
|
|
|
|
static struct path_entry *new_path_entry(const char *);
|
|
|
|
static void ensure_m4path(void);
|
|
|
|
static struct input_file *dopath(struct input_file *, const char *);
|
|
|
|
|
|
|
|
static struct path_entry *
|
2002-05-01 21:37:29 +00:00
|
|
|
new_path_entry(const char *dirname)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
struct path_entry *n;
|
|
|
|
|
|
|
|
n = malloc(sizeof(struct path_entry));
|
|
|
|
if (!n)
|
|
|
|
errx(1, "out of memory");
|
|
|
|
n->name = strdup(dirname);
|
|
|
|
if (!n->name)
|
|
|
|
errx(1, "out of memory");
|
|
|
|
n->next = 0;
|
|
|
|
return n;
|
|
|
|
}
|
2002-07-15 02:15:12 +00:00
|
|
|
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
addtoincludepath(const char *dirname)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
struct path_entry *n;
|
|
|
|
|
|
|
|
n = new_path_entry(dirname);
|
|
|
|
|
|
|
|
if (last) {
|
|
|
|
last->next = n;
|
|
|
|
last = n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
last = first = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-07-14 02:03:23 +00:00
|
|
|
ensure_m4path(void)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
static int envpathdone = 0;
|
|
|
|
char *envpath;
|
|
|
|
char *sweep;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (envpathdone)
|
|
|
|
return;
|
|
|
|
envpathdone = TRUE;
|
|
|
|
envpath = getenv("M4PATH");
|
2002-07-15 02:15:12 +00:00
|
|
|
if (!envpath)
|
2002-02-16 21:27:48 +00:00
|
|
|
return;
|
|
|
|
/* for portability: getenv result is read-only */
|
|
|
|
envpath = strdup(envpath);
|
|
|
|
if (!envpath)
|
|
|
|
errx(1, "out of memory");
|
2002-07-15 02:15:12 +00:00
|
|
|
for (sweep = envpath;
|
2002-02-16 21:27:48 +00:00
|
|
|
(path = strsep(&sweep, ":")) != NULL;)
|
|
|
|
addtoincludepath(path);
|
|
|
|
free(envpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
struct input_file *
|
2002-05-01 21:37:29 +00:00
|
|
|
dopath(struct input_file *i, const char *filename)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
struct path_entry *pe;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
for (pe = first; pe; pe = pe->next) {
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
|
2011-11-28 13:32:39 +00:00
|
|
|
if ((f = fopen(path, "r")) != 0) {
|
2002-02-16 21:27:48 +00:00
|
|
|
set_input(i, f, path);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct input_file *
|
2002-05-01 21:37:29 +00:00
|
|
|
fopen_trypath(struct input_file *i, const char *filename)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen(filename, "r");
|
|
|
|
if (f != NULL) {
|
|
|
|
set_input(i, f, filename);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (filename[0] == '/')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ensure_m4path();
|
|
|
|
|
|
|
|
return dopath(i, filename);
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
doindir(const char *argv[], int argc)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
ndptr n;
|
|
|
|
struct macro_definition *p = NULL;
|
2002-02-16 21:27:48 +00:00
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
n = lookup(argv[2]);
|
|
|
|
if (n == NULL || (p = macro_getdef(n)) == NULL)
|
|
|
|
m4errx(1, "indir: undefined macro %s.", argv[2]);
|
2002-02-16 21:27:48 +00:00
|
|
|
argv[1] = p->defn;
|
2011-11-28 13:32:39 +00:00
|
|
|
|
|
|
|
eval(argv+1, argc-1, p->type, is_traced(n));
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
dobuiltin(const char *argv[], int argc)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
ndptr p;
|
|
|
|
|
2002-02-16 21:27:48 +00:00
|
|
|
argv[1] = NULL;
|
2011-11-28 13:32:39 +00:00
|
|
|
p = macro_getbuiltin(argv[2]);
|
|
|
|
if (p != NULL)
|
|
|
|
eval(argv+1, argc-1, macro_builtin_type(p), is_traced(p));
|
2002-02-16 21:27:48 +00:00
|
|
|
else
|
2011-11-28 13:32:39 +00:00
|
|
|
m4errx(1, "unknown builtin %s.", argv[2]);
|
2002-07-15 02:15:12 +00:00
|
|
|
}
|
2002-02-16 21:27:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* We need some temporary buffer space, as pb pushes BACK and substitution
|
|
|
|
* proceeds forward... */
|
|
|
|
static char *buffer;
|
|
|
|
static size_t bufsize = 0;
|
|
|
|
static size_t current = 0;
|
|
|
|
|
|
|
|
static void addchars(const char *, size_t);
|
2002-05-01 21:37:29 +00:00
|
|
|
static void addchar(int);
|
2002-02-16 21:27:48 +00:00
|
|
|
static char *twiddle(const char *);
|
|
|
|
static char *getstring(void);
|
2014-07-27 22:54:13 +00:00
|
|
|
static void exit_regerror(int, regex_t *, const char *);
|
|
|
|
static void do_subst(const char *, regex_t *, const char *, const char *,
|
|
|
|
regmatch_t *);
|
|
|
|
static void do_regexpindex(const char *, regex_t *, const char *, regmatch_t *);
|
|
|
|
static void do_regexp(const char *, regex_t *, const char *, const char *,
|
|
|
|
regmatch_t *);
|
2011-11-28 13:32:39 +00:00
|
|
|
static void add_sub(int, const char *, regex_t *, regmatch_t *);
|
2002-02-16 21:27:48 +00:00
|
|
|
static void add_replace(const char *, regex_t *, const char *, regmatch_t *);
|
|
|
|
#define addconstantstring(s) addchars((s), sizeof(s)-1)
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
addchars(const char *c, size_t n)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
while (current + n > bufsize) {
|
|
|
|
if (bufsize == 0)
|
|
|
|
bufsize = 1024;
|
|
|
|
else
|
|
|
|
bufsize *= 2;
|
2011-11-28 13:32:39 +00:00
|
|
|
buffer = xrealloc(buffer, bufsize, NULL);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
memcpy(buffer+current, c, n);
|
|
|
|
current += n;
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
addchar(int c)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
if (current +1 > bufsize) {
|
|
|
|
if (bufsize == 0)
|
|
|
|
bufsize = 1024;
|
|
|
|
else
|
|
|
|
bufsize *= 2;
|
2011-11-28 13:32:39 +00:00
|
|
|
buffer = xrealloc(buffer, bufsize, NULL);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
buffer[current++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2002-07-14 02:03:23 +00:00
|
|
|
getstring(void)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
addchar('\0');
|
|
|
|
current = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(int er, regex_t *re, const char *source)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
size_t errlen;
|
|
|
|
char *errbuf;
|
2002-02-16 21:27:48 +00:00
|
|
|
|
|
|
|
errlen = regerror(er, re, NULL, 0);
|
2011-11-28 13:32:39 +00:00
|
|
|
errbuf = xalloc(errlen,
|
|
|
|
"malloc in regerror: %lu", (unsigned long)errlen);
|
2002-02-16 21:27:48 +00:00
|
|
|
regerror(er, re, errbuf, errlen);
|
2014-07-27 22:54:13 +00:00
|
|
|
m4errx(1, "regular expression error in %s: %s.", source, errbuf);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-28 13:32:39 +00:00
|
|
|
add_sub(int n, const char *string, regex_t *re, regmatch_t *pm)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (n > (int)re->re_nsub)
|
|
|
|
warnx("No subexpression %d", n);
|
2002-02-16 21:27:48 +00:00
|
|
|
/* Subexpressions that did not match are
|
|
|
|
* not an error. */
|
|
|
|
else if (pm[n].rm_so != -1 &&
|
|
|
|
pm[n].rm_eo != -1) {
|
|
|
|
addchars(string + pm[n].rm_so,
|
|
|
|
pm[n].rm_eo - pm[n].rm_so);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add replacement string to the output buffer, recognizing special
|
|
|
|
* constructs and replacing them with substrings of the original string.
|
|
|
|
*/
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
add_replace(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (p = replace; *p != '\0'; p++) {
|
|
|
|
if (*p == '&' && !mimic_gnu) {
|
|
|
|
add_sub(0, string, re, pm);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*p == '\\') {
|
|
|
|
if (p[1] == '\\') {
|
|
|
|
addchar(p[1]);
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[1] == '&') {
|
|
|
|
if (mimic_gnu)
|
|
|
|
add_sub(0, string, re, pm);
|
|
|
|
else
|
|
|
|
addchar(p[1]);
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-27 22:54:13 +00:00
|
|
|
if (isdigit((unsigned char)p[1])) {
|
2002-02-16 21:27:48 +00:00
|
|
|
add_sub(*(++p) - '0', string, re, pm);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
addchar(*p);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2014-07-27 22:54:13 +00:00
|
|
|
do_subst(const char *string, regex_t *re, const char *source,
|
|
|
|
const char *replace, regmatch_t *pm)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int flags = 0;
|
|
|
|
const char *last_match = NULL;
|
|
|
|
|
|
|
|
while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
|
|
|
|
if (pm[0].rm_eo != 0) {
|
|
|
|
if (string[pm[0].rm_eo-1] == '\n')
|
|
|
|
flags = 0;
|
|
|
|
else
|
|
|
|
flags = REG_NOTBOL;
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
/* NULL length matches are special... We use the `vi-mode'
|
2002-02-16 21:27:48 +00:00
|
|
|
* rule: don't allow a NULL-match at the last match
|
2002-07-15 02:15:12 +00:00
|
|
|
* position.
|
2002-02-16 21:27:48 +00:00
|
|
|
*/
|
2002-07-15 02:15:12 +00:00
|
|
|
if (pm[0].rm_so == pm[0].rm_eo &&
|
2002-02-16 21:27:48 +00:00
|
|
|
string + pm[0].rm_so == last_match) {
|
|
|
|
if (*string == '\0')
|
|
|
|
return;
|
|
|
|
addchar(*string);
|
|
|
|
if (*string++ == '\n')
|
|
|
|
flags = 0;
|
|
|
|
else
|
|
|
|
flags = REG_NOTBOL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
last_match = string + pm[0].rm_so;
|
|
|
|
addchars(string, pm[0].rm_so);
|
|
|
|
add_replace(string, re, replace, pm);
|
|
|
|
string += pm[0].rm_eo;
|
|
|
|
}
|
|
|
|
if (error != REG_NOMATCH)
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(error, re, source);
|
2002-02-16 21:27:48 +00:00
|
|
|
pbstr(string);
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2014-07-27 22:54:13 +00:00
|
|
|
do_regexp(const char *string, regex_t *re, const char *source,
|
|
|
|
const char *replace, regmatch_t *pm)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
|
2002-07-15 02:15:12 +00:00
|
|
|
case 0:
|
2002-02-16 21:27:48 +00:00
|
|
|
add_replace(string, re, replace, pm);
|
|
|
|
pbstr(getstring());
|
|
|
|
break;
|
|
|
|
case REG_NOMATCH:
|
|
|
|
break;
|
|
|
|
default:
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(error, re, source);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
static void
|
2014-07-27 22:54:13 +00:00
|
|
|
do_regexpindex(const char *string, regex_t *re, const char *source,
|
|
|
|
regmatch_t *pm)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
|
|
|
|
case 0:
|
|
|
|
pbunsigned(pm[0].rm_so);
|
|
|
|
break;
|
|
|
|
case REG_NOMATCH:
|
|
|
|
pbnum(-1);
|
|
|
|
break;
|
|
|
|
default:
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(error, re, source);
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
|
|
|
|
* says. So we twiddle with the regexp before passing it to regcomp.
|
|
|
|
*/
|
|
|
|
static char *
|
2002-05-01 21:37:29 +00:00
|
|
|
twiddle(const char *p)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
/* + at start of regexp is a normal character for Gnu m4 */
|
|
|
|
if (*p == '^') {
|
|
|
|
addchar(*p);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == '+') {
|
|
|
|
addchar('\\');
|
|
|
|
}
|
2002-02-16 21:27:48 +00:00
|
|
|
/* This could use strcspn for speed... */
|
|
|
|
while (*p != '\0') {
|
|
|
|
if (*p == '\\') {
|
|
|
|
switch(p[1]) {
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '|':
|
|
|
|
addchar(p[1]);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
addconstantstring("[_a-zA-Z0-9]");
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
addconstantstring("[^_a-zA-Z0-9]");
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
addconstantstring("[[:<:]]");
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
addconstantstring("[[:>:]]");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
addchars(p, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p+=2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*p == '(' || *p == ')' || *p == '|')
|
|
|
|
addchar('\\');
|
|
|
|
|
|
|
|
addchar(*p);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return getstring();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* patsubst(string, regexp, opt replacement) */
|
|
|
|
/* argv[2]: string
|
|
|
|
* argv[3]: regexp
|
|
|
|
* argv[4]: opt rep
|
|
|
|
*/
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
dopatsubst(const char *argv[], int argc)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
if (argc <= 3) {
|
|
|
|
warnx("Too few arguments to patsubst");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
/* special case: empty regexp */
|
|
|
|
if (argv[3][0] == '\0') {
|
|
|
|
const char *s;
|
|
|
|
size_t len;
|
|
|
|
if (argc > 4 && argv[4])
|
|
|
|
len = strlen(argv[4]);
|
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
for (s = argv[2]; *s != '\0'; s++) {
|
|
|
|
addchars(argv[4], len);
|
|
|
|
addchar(*s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int error;
|
|
|
|
regex_t re;
|
|
|
|
regmatch_t *pmatch;
|
|
|
|
int mode = REG_EXTENDED;
|
2014-07-27 22:54:13 +00:00
|
|
|
const char *source;
|
2011-11-28 13:32:39 +00:00
|
|
|
size_t l = strlen(argv[3]);
|
|
|
|
|
|
|
|
if (!mimic_gnu ||
|
|
|
|
(argv[3][0] == '^') ||
|
|
|
|
(l > 0 && argv[3][l-1] == '$'))
|
|
|
|
mode |= REG_NEWLINE;
|
|
|
|
|
2014-07-27 22:54:13 +00:00
|
|
|
source = mimic_gnu ? twiddle(argv[3]) : argv[3];
|
|
|
|
error = regcomp(&re, source, mode);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (error != 0)
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(error, &re, source);
|
2011-11-28 13:32:39 +00:00
|
|
|
|
2014-07-27 22:54:13 +00:00
|
|
|
pmatch = xreallocarray(NULL, re.re_nsub+1, sizeof(regmatch_t),
|
|
|
|
NULL);
|
|
|
|
do_subst(argv[2], &re, source,
|
2011-11-28 13:32:39 +00:00
|
|
|
argc > 4 && argv[4] != NULL ? argv[4] : "", pmatch);
|
|
|
|
free(pmatch);
|
|
|
|
regfree(&re);
|
|
|
|
}
|
2002-02-16 21:27:48 +00:00
|
|
|
pbstr(getstring());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
doregexp(const char *argv[], int argc)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
regex_t re;
|
|
|
|
regmatch_t *pmatch;
|
2014-07-27 22:54:13 +00:00
|
|
|
const char *source;
|
2002-02-16 21:27:48 +00:00
|
|
|
|
|
|
|
if (argc <= 3) {
|
|
|
|
warnx("Too few arguments to regexp");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
/* special gnu case */
|
|
|
|
if (argv[3][0] == '\0' && mimic_gnu) {
|
|
|
|
if (argc == 4 || argv[4] == NULL)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
pbstr(argv[4]);
|
|
|
|
}
|
2014-07-27 22:54:13 +00:00
|
|
|
source = mimic_gnu ? twiddle(argv[3]) : argv[3];
|
|
|
|
error = regcomp(&re, source, REG_EXTENDED|REG_NEWLINE);
|
2002-02-16 21:27:48 +00:00
|
|
|
if (error != 0)
|
2014-07-27 22:54:13 +00:00
|
|
|
exit_regerror(error, &re, source);
|
2002-07-15 02:15:12 +00:00
|
|
|
|
2014-07-27 22:54:13 +00:00
|
|
|
pmatch = xreallocarray(NULL, re.re_nsub+1, sizeof(regmatch_t), NULL);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (argc == 4 || argv[4] == NULL)
|
2014-07-27 22:54:13 +00:00
|
|
|
do_regexpindex(argv[2], &re, source, pmatch);
|
2002-02-16 21:27:48 +00:00
|
|
|
else
|
2014-07-27 22:54:13 +00:00
|
|
|
do_regexp(argv[2], &re, source, argv[4], pmatch);
|
2002-02-16 21:27:48 +00:00
|
|
|
free(pmatch);
|
|
|
|
regfree(&re);
|
|
|
|
}
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
void
|
|
|
|
doformat(const char *argv[], int argc)
|
|
|
|
{
|
|
|
|
const char *format = argv[2];
|
|
|
|
int pos = 3;
|
|
|
|
int left_padded;
|
|
|
|
long width;
|
|
|
|
size_t l;
|
|
|
|
const char *thisarg = NULL;
|
|
|
|
char temp[2];
|
|
|
|
long extra;
|
|
|
|
|
|
|
|
while (*format != 0) {
|
|
|
|
if (*format != '%') {
|
|
|
|
addchar(*format++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
format++;
|
|
|
|
if (*format == '%') {
|
|
|
|
addchar(*format++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 0) {
|
|
|
|
addchar('%');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*format == '*') {
|
|
|
|
format++;
|
|
|
|
if (pos >= argc)
|
|
|
|
m4errx(1,
|
|
|
|
"Format with too many format specifiers.");
|
|
|
|
width = strtol(argv[pos++], NULL, 10);
|
|
|
|
} else {
|
|
|
|
width = strtol(format, __DECONST(char **,&format), 10);
|
|
|
|
}
|
|
|
|
if (width < 0) {
|
|
|
|
left_padded = 1;
|
|
|
|
width = -width;
|
|
|
|
} else {
|
|
|
|
left_padded = 0;
|
|
|
|
}
|
|
|
|
if (*format == '.') {
|
|
|
|
format++;
|
|
|
|
if (*format == '*') {
|
|
|
|
format++;
|
|
|
|
if (pos >= argc)
|
|
|
|
m4errx(1,
|
|
|
|
"Format with too many format specifiers.");
|
|
|
|
extra = strtol(argv[pos++], NULL, 10);
|
|
|
|
} else {
|
|
|
|
extra = strtol(format, __DECONST(char **, &format), 10);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
extra = LONG_MAX;
|
|
|
|
}
|
|
|
|
if (pos >= argc)
|
|
|
|
m4errx(1, "Format with too many format specifiers.");
|
|
|
|
switch(*format) {
|
|
|
|
case 's':
|
|
|
|
thisarg = argv[pos++];
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
temp[0] = strtoul(argv[pos++], NULL, 10);
|
|
|
|
temp[1] = 0;
|
|
|
|
thisarg = temp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m4errx(1, "Unsupported format specification: %s.",
|
|
|
|
argv[2]);
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
l = strlen(thisarg);
|
|
|
|
if ((long)l > extra)
|
|
|
|
l = extra;
|
|
|
|
if (!left_padded) {
|
|
|
|
while ((long)l < width--)
|
|
|
|
addchar(' ');
|
|
|
|
}
|
|
|
|
addchars(thisarg, l);
|
|
|
|
if (left_padded) {
|
|
|
|
while ((long)l < width--)
|
|
|
|
addchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pbstr(getstring());
|
|
|
|
}
|
|
|
|
|
2002-02-16 21:27:48 +00:00
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
doesyscmd(const char *cmd)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int p[2];
|
|
|
|
pid_t pid, cpid;
|
2011-11-28 13:32:39 +00:00
|
|
|
char *argv[4];
|
2002-02-16 21:27:48 +00:00
|
|
|
int cc;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Follow gnu m4 documentation: first flush buffers. */
|
|
|
|
fflush(NULL);
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
argv[0] = __DECONST(char *, "sh");
|
|
|
|
argv[1] = __DECONST(char *, "-c");
|
|
|
|
argv[2] = __DECONST(char *, cmd);
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
2002-02-16 21:27:48 +00:00
|
|
|
/* Just set up standard output, share stderr and stdin with m4 */
|
|
|
|
if (pipe(p) == -1)
|
|
|
|
err(1, "bad pipe");
|
|
|
|
switch(cpid = fork()) {
|
|
|
|
case -1:
|
|
|
|
err(1, "bad fork");
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 0:
|
|
|
|
(void) close(p[0]);
|
|
|
|
(void) dup2(p[1], 1);
|
|
|
|
(void) close(p[1]);
|
2011-11-28 13:32:39 +00:00
|
|
|
execv(_PATH_BSHELL, argv);
|
2002-02-16 21:27:48 +00:00
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
/* Read result in two stages, since m4's buffer is
|
|
|
|
* pushback-only. */
|
|
|
|
(void) close(p[1]);
|
|
|
|
do {
|
|
|
|
char result[BUFSIZE];
|
|
|
|
cc = read(p[0], result, sizeof result);
|
|
|
|
if (cc > 0)
|
|
|
|
addchars(result, cc);
|
|
|
|
} while (cc > 0 || (cc == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
(void) close(p[0]);
|
|
|
|
while ((pid = wait(&status)) != cpid && pid >= 0)
|
|
|
|
continue;
|
|
|
|
pbstr(getstring());
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
getdivfile(const char *name)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
f = fopen(name, "r");
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((c = getc(f))!= EOF)
|
|
|
|
putc(c, active);
|
|
|
|
(void) fclose(f);
|
|
|
|
}
|