[PATCH] sed: Add support for POSIX -E to select EREs (this is a synonym for -r)

David A. Wheeler dwheeler at dwheeler.com
Mon Oct 28 12:33:41 UTC 2013


From 09bf8a960eb4de382be17efa4085f85b57e7961e Mon Sep 17 00:00:00 2001
From: David A. Wheeler <dwheeler at dwheeler.com>
Date: Sun, 27 Oct 2013 19:42:46 -0400
Subject: [PATCH] sed: Add support for POSIX -E to select EREs (this is a synonym for -r)

POSIX has added a new sed option "-E" to sed to select
extended regular expressions (EREs), as described here:
  http://austingroupbugs.net/view.php?id=528

Busybox already supported this using "-r", so this patch just adds "-E"
as a synonym, documents "-E", and adds a trivial test to check it.

Signed-off-by: David A. Wheeler <dwheeler at dwheeler.com>
---
 editors/sed.c       |   14 ++++++++------
 testsuite/sed.tests |    6 ++++--
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/editors/sed.c b/editors/sed.c
index 3a0d917..2f38585 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -56,18 +56,20 @@
  * Reference
  * http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
  * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
+ * http://austingroupbugs.net/view.php?id=528
  */
 
 //usage:#define sed_trivial_usage
-//usage:       "[-inr] [-f FILE]... [-e CMD]... [FILE]...\n"
-//usage:       "or: sed [-inr] CMD [FILE]..."
+//usage:       "[-inEr] [-f FILE]... [-e CMD]... [FILE]...\n"
+//usage:       "or: sed [-inEr] CMD [FILE]..."
 //usage:#define sed_full_usage "\n\n"
 //usage:       "	-e CMD	Add CMD to sed commands to be executed"
 //usage:     "\n	-f FILE	Add FILE contents to sed commands to be executed"
 //usage:     "\n	-i[SFX]	Edit files in-place (otherwise sends to stdout)"
 //usage:     "\n		Optionally back files up, appending SFX"
 //usage:     "\n	-n	Suppress automatic printing of pattern space"
-//usage:     "\n	-r	Use extended regex syntax"
+//usage:     "\n	-E	Use extended regex (ERE) syntax (per POSIX)"
+//usage:     "\n	-r	Use ERE syntax (same as -E for backwards compatibility)"
 //usage:     "\n"
 //usage:     "\nIf no -e or -f, the first non-option argument is the sed command string."
 //usage:     "\nRemaining arguments are input files (stdin if none)."
@@ -1406,7 +1408,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 	static const char sed_longopts[] ALIGN1 =
 		/* name             has_arg             short */
 		"in-place\0"        Optional_argument   "i"
-		"regexp-extended\0" No_argument         "r"
+		"regexp-extended\0" No_argument         "E"
 		"quiet\0"           No_argument         "n"
 		"silent\0"          No_argument         "n"
 		"expression\0"      Required_argument   "e"
@@ -1435,14 +1437,14 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 	IF_LONG_OPTS(applet_long_options = sed_longopts);
 
 	/* -i must be first, to match OPT_in_place definition */
-	opt = getopt32(argv, "i::rne:f:", &opt_i, &opt_e, &opt_f,
+	opt = getopt32(argv, "i::rne:f:E", &opt_i, &opt_e, &opt_f,
 			    &G.be_quiet); /* counter for -n */
 	//argc -= optind;
 	argv += optind;
 	if (opt & OPT_in_place) { // -i
 		atexit(cleanup_outname);
 	}
-	if (opt & 0x2) G.regex_type |= REG_EXTENDED; // -r
+	if ((opt & 0x2) || (opt & 0x20)) G.regex_type |= REG_EXTENDED; // -r | -E
 	//if (opt & 0x4) G.be_quiet++; // -n
 	while (opt_e) { // -e
 		add_cmd_block(llist_pop(&opt_e));
diff --git a/testsuite/sed.tests b/testsuite/sed.tests
index 2af1e4c..d16b8f1 100755
--- a/testsuite/sed.tests
+++ b/testsuite/sed.tests
@@ -26,8 +26,7 @@ testing "sed stdin twice" 'sed "" - -' "hello" "" "hello"
 #	-e -f -e
 # -n corner cases
 #	no newline at EOF?
-# -r corner cases
-#	Just make sure it works.
+# -E corner cases
 # -i corner cases:
 #	sed -i -
 #	permissions
@@ -60,6 +59,9 @@ testing "sed s chains2" "sed -e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
 testing "sed s [delimiter]" "sed -e 's@[@]@@'" "onetwo" "" "one at two"
 testing "sed s with \\t (GNU ext)" "sed 's/\t/ /'" "one two" "" "one\ttwo"
 
+testing "sed s with -E" "sed -E -e 's/he.*o/goodbye/g'" "A goodbye test" "" "A hello test"
+testing "sed s with -r" "sed -r -e 's/he.*o/goodbye/g'" "A goodbye test2" "" "A hello test2"
+
 # branch
 testing "sed b (branch)" "sed -e 'b one;p;: one'" "foo\n" "" "foo\n"
 testing "sed b (branch with no label jumps to end)" "sed -e 'b;p'" \
-- 
1.6.2.5


-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-sed-Add-support-for-POSIX-E-to-select-EREs-this-i.patch
Type: text/x-diff
Size: 4189 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20131028/396ffd09/attachment.bin>


More information about the busybox mailing list