summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorespie <espie@openbsd.org>2003-06-08 20:11:45 +0000
committerespie <espie@openbsd.org>2003-06-08 20:11:45 +0000
commit86e0babb026f1579b5017dd830042b34ebf9b21d (patch)
tree06f4fbd5796092eebe8ea40433257012e62d8cea
parentuse err/warn (diff)
downloadwireguard-openbsd-86e0babb026f1579b5017dd830042b34ebf9b21d.tar.xz
wireguard-openbsd-86e0babb026f1579b5017dd830042b34ebf9b21d.zip
Let patsubst handle empty patterns: those match anywhere, and don't
replace any character. Like gnu-m4 does, and other regexp languages do. okay fries@
-rw-r--r--regress/usr.bin/m4/patterns.m44
-rw-r--r--regress/usr.bin/m4/patterns.out1
-rw-r--r--usr.bin/m4/gnum4.c40
3 files changed, 29 insertions, 16 deletions
diff --git a/regress/usr.bin/m4/patterns.m4 b/regress/usr.bin/m4/patterns.m4
index 6aedcd25bdc..af37c2ba177 100644
--- a/regress/usr.bin/m4/patterns.m4
+++ b/regress/usr.bin/m4/patterns.m4
@@ -1,4 +1,4 @@
-dnl $OpenBSD: patterns.m4,v 1.3 2001/09/27 12:34:52 espie Exp $
+dnl $OpenBSD: patterns.m4,v 1.4 2003/06/08 20:11:45 espie Exp $
patsubst(`quote s in string', `(s)', `\\\1')
patsubst(`check whether subst
over several lines
@@ -7,3 +7,5 @@ patsubst(`# This is a line to zap
# and a second line
keep this one', `^ *#.*
')
+dnl Special case: empty regexp
+patsubst(`empty regexp',`',`a ')
diff --git a/regress/usr.bin/m4/patterns.out b/regress/usr.bin/m4/patterns.out
index 9e33be7c2ea..c273b589d77 100644
--- a/regress/usr.bin/m4/patterns.out
+++ b/regress/usr.bin/m4/patterns.out
@@ -3,3 +3,4 @@ quote \s in \string
>>>over several lines
>>>works as expected
keep this one
+a ea ma pa ta ya a ra ea ga ea xa p
diff --git a/usr.bin/m4/gnum4.c b/usr.bin/m4/gnum4.c
index 6ca4d9a45cd..553ef38da1a 100644
--- a/usr.bin/m4/gnum4.c
+++ b/usr.bin/m4/gnum4.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gnum4.c,v 1.19 2003/06/07 12:02:35 espie Exp $ */
+/* $OpenBSD: gnum4.c,v 1.20 2003/06/08 20:11:45 espie Exp $ */
/*
* Copyright (c) 1999 Marc Espie
@@ -429,25 +429,35 @@ twiddle(const char *p)
void
dopatsubst(const char *argv[], int argc)
{
- int error;
- regex_t re;
- regmatch_t *pmatch;
-
if (argc <= 3) {
warnx("Too few arguments to patsubst");
return;
}
- error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
- REG_NEWLINE | REG_EXTENDED);
- if (error != 0)
- exit_regerror(error, &re);
-
- pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
- do_subst(argv[2], &re,
- argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
+ /* special case: empty regexp */
+ if (argv[3][0] == '\0') {
+ const char *s;
+ size_t len = strlen(argv[4]);
+ for (s = argv[2]; *s != '\0'; s++) {
+ addchars(argv[4], len);
+ addchar(*s);
+ }
+ } else {
+ int error;
+ regex_t re;
+ regmatch_t *pmatch;
+
+ error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
+ REG_NEWLINE | REG_EXTENDED);
+ if (error != 0)
+ exit_regerror(error, &re);
+
+ pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
+ do_subst(argv[2], &re,
+ argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
+ free(pmatch);
+ regfree(&re);
+ }
pbstr(getstring());
- free(pmatch);
- regfree(&re);
}
void