From 469fd109cdbd04215ce4efcd13b13485d1fb3ba5 Mon Sep 17 00:00:00 2001
From: "Dmitry V. Levin" <ldv@altlinux.org>
Date: Thu, 28 Jul 2016 16:51:58 +0000
Subject: [PATCH] Consistently use extended regular expressions where
 appropriate

When grep or sed is used with basic regular expressions containing
'(', ')', '{', '}', '|', '?', and '+' special characters, convert them
to extended regular expressions for better portability and readability.

* generate_mpers_am.sh: Convert grep and sed BREs to EREs.
* Makefile.am (ioctl_redefs%.h, m%_type_defs.h, m%_funcs.h,
mpers_printer_decl_pattern, printers.h, %_printer_decls.h,
%_printer_defs.h): Convert sed BREs to EREs.
* generate_sen.sh: Likewise.
* linux/mips/genstub.sh: Likewise.
* make-dsc: Likewise.
* mpers.sh: Likewise.
* xlat/gen.sh: Likewise.
* tests/Makefile.am (ksysent.h): Likewise.
* tests/ksysent.sed: Likewise.
* tests/pc.test: Likewise.
* tests/strace-S.test: Likewise.
* tests/strace-V.test: Likewise.
* tests/strace-k.test: Likewise.
---
 Makefile.am           | 16 ++++++++--------
 generate_mpers_am.sh  |  6 +++---
 generate_sen.sh       |  2 +-
 linux/mips/genstub.sh |  2 +-
 make-dsc              |  2 +-
 mpers.sh              | 20 ++++++++++----------
 tests/Makefile.am     |  2 +-
 tests/ksysent.sed     | 10 +++++-----
 tests/pc.test         |  8 ++++----
 tests/strace-S.test   |  8 ++++----
 tests/strace-V.test   |  2 +-
 tests/strace-k.test   |  2 +-
 xlat/gen.sh           |  8 ++++----
 13 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 1e7554e1..cb0bd92c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -713,7 +713,7 @@ ioctl_redefs_h = $(filter-out ioctl_redefs0.h,$(subst ioctlent,ioctl_redefs,$(io
 ioctl_redefs%.h: ioctlent%.h ioctlent0.h
 	sort $< > $<-t
 	sort ioctlent0.h | comm -23 $<-t - | \
-		sed -n 's/^{ "\([^"]\+\)", \(0x[[:xdigit:]]\+\) },$$/#ifdef \1\n# undef \1\n# define \1 \2\n#endif/p' \
+		sed -r -n 's/^\{ "([^"]+)", (0x[[:xdigit:]]+) \},$$/#ifdef \1\n# undef \1\n# define \1 \2\n#endif/p' \
 		> $@-t
 	rm -f $<-t
 	mv $@-t $@
@@ -762,7 +762,7 @@ mpers-m%.stamp: $(srcdir_mpers_source_files) | printers.h
 
 m%_type_defs.h: $(srcdir_mpers_source_files)
 	for f in $^; do \
-		sed -n 's/^#include DEF_MPERS_TYPE(\([^)]\+\))/#ifdef MPERS_$(mpers_PREFIX)\1\n# define \1 MPERS_$(mpers_PREFIX)\1\n#endif/p' $$f || exit; \
+		sed -r -n 's/^#include DEF_MPERS_TYPE\(([^)]+)\)/#ifdef MPERS_$(mpers_PREFIX)\1\n# define \1 MPERS_$(mpers_PREFIX)\1\n#endif/p' $$f || exit; \
 	done > $@-t
 	echo '#undef MPERS_PRINTER_NAME' >> $@-t
 	echo '#define MPERS_PRINTER_NAME(printer_name) printer_name' >> $@-t
@@ -772,7 +772,7 @@ m%_type_defs.h: $(srcdir_mpers_source_files)
 
 m%_funcs.h: $(srcdir_mpers_source_files)
 	for f in $^; do \
-		sed -n 's/^SYS_FUNC(\([^)]\+\))/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
+		sed -r -n 's/^SYS_FUNC\(([^)]+)\)/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
 	done > $@-t && \
 	echo '#include "sys_func.h"' >> $@-t
 	mv $@-t $@
@@ -782,13 +782,13 @@ m%_funcs.h: $(srcdir_mpers_source_files)
 %.c.mpers.i: $(srcdir)/%.c
 	$(CPP) -P $(mpers_sh_opts) -DIN_MPERS_BOOTSTRAP $< -o $@
 
-mpers_printer_decl_pattern = ^MPERS_PRINTER_DECL(\([^,)]\+\),[[:space:]]*\([^,)]\+\),[[:space:]]*\([^)]\+\))$$
+mpers_printer_decl_pattern = ^MPERS_PRINTER_DECL\(([^,)]+),[[:space:]]*([^,)]+),[[:space:]]*([^)]+)\)$$
 
 printers.h: $(mpers_preproc_files)
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
 	echo 'typedef struct {' >> $@-t
 	for f in $^; do \
-		sed -n 's/$(mpers_printer_decl_pattern)/ \1 (*\2)(\3);\n#define \2 MPERS_PRINTER_NAME(\2)\n/p' $$f \
+		sed -r -n 's/$(mpers_printer_decl_pattern)/ \1 (*\2)(\3);\n#define \2 MPERS_PRINTER_NAME(\2)\n/p' $$f \
 		|| exit; \
 	done >> $@-t
 	echo '} struct_printers;' >> $@-t
@@ -799,7 +799,7 @@ printers.h: $(mpers_preproc_files)
 %_printer_decls.h: $(mpers_preproc_files)
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
 	for f in $^; do \
-		sed -n 's/$(mpers_printer_decl_pattern)/extern \1 $(mpers_PREFIX)\2(\3);/p' $$f \
+		sed -r -n 's/$(mpers_printer_decl_pattern)/extern \1 $(mpers_PREFIX)\2(\3);/p' $$f \
 		|| exit; \
 	done >> $@-t
 	mv $@-t $@
@@ -807,7 +807,7 @@ printers.h: $(mpers_preproc_files)
 %_printer_defs.h: $(mpers_preproc_files)
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
 	for f in $^; do \
-		sed -n 's/$(mpers_printer_decl_pattern)/\.\2 = $(mpers_PREFIX)\2,/p' $$f \
+		sed -r -n 's/$(mpers_printer_decl_pattern)/.\2 = $(mpers_PREFIX)\2,/p' $$f \
 		|| exit; \
 	done >> $@-t
 	mv $@-t $@
@@ -869,7 +869,7 @@ $(srcdir)/CREDITS: $(srcdir)/CREDITS.in $(srcdir)/.mailmap \
 	(								\
 	  cd $(srcdir);							\
 	  sed '/^##/,$$d' CREDITS.in;					\
-	  { sed -n '1,/^##>/d; s/  \+/\t/; s/^./&/p' CREDITS.in;	\
+	  { sed -n '1,/^##>/d; s/   */\t/; s/^./&/p' CREDITS.in;	\
 	    git log --pretty=format:'%aN	%aE';			\
 	  } | LC_ALL=C sort -u						\
 	    | awk -F'\t' '{printf("\t%s <%s>\n",$$1,$$2)}';		\
diff --git a/generate_mpers_am.sh b/generate_mpers_am.sh
index 1ee4791b..833cd29b 100755
--- a/generate_mpers_am.sh
+++ b/generate_mpers_am.sh
@@ -1,7 +1,7 @@
 #!/bin/sh -e
 
-list="$(sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.am |
-	xargs -r grep -lx '#[[:space:]]*include[[:space:]]\+MPERS_DEFS' |
+list="$(sed -r -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*([[:alnum:]][^.]*\.c)[[:space:]]*\\$/\1/p' Makefile.am |
+	xargs -r grep -Elx '#[[:space:]]*include[[:space:]]+MPERS_DEFS' |
 	tr '\n' ' ')"
 
 cat > mpers.am <<EOF
@@ -9,5 +9,5 @@ cat > mpers.am <<EOF
 mpers_source_files = $list
 EOF
 
-sed -n 's/^#[[:space:]]*include[[:space:]]*"xlat\/\([^."]\+\)\.h".*/extern const struct xlat \1[];/p' \
+sed -r -n 's/^#[[:space:]]*include[[:space:]]*"xlat\/([a-z][a-z_0-9]*)\.h".*/extern const struct xlat \1[];/p' \
 	$list > mpers_xlat.h
diff --git a/generate_sen.sh b/generate_sen.sh
index 6c177abf..854c15b3 100755
--- a/generate_sen.sh
+++ b/generate_sen.sh
@@ -2,7 +2,7 @@
 
 echo 'enum {'
 echo 'SEN_printargs = 0,'
-    sed -n '/printargs/! s/.*SEN(\([^)]*\)).*/\1/p' |
+    sed -r -n '/printargs/! s/.*SEN\(([^)]+)\).*/\1/p' |
     sort -u |
     sed 's/.*/SEN_&,/'
 echo '};'
diff --git a/linux/mips/genstub.sh b/linux/mips/genstub.sh
index 5ebb1d44..11f141f5 100755
--- a/linux/mips/genstub.sh
+++ b/linux/mips/genstub.sh
@@ -6,5 +6,5 @@ dstdir="$1"; shift
 for n in n32 n64 o32; do
 	in="$srcdir/syscallent-$n.h"
 	out="$dstdir/syscallent-$n-stub.h"
-	sed -n '/^#if/,/^#else/ {s/^\([^{]*{[^,]*,[^,]*,[[:space:]]*\)[^,[:space:]]\+,[[:space:]]*"\([^"]\+".*\)/\1SEN(printargs), "'$n'_\2/; s/^\[.*/&/p}' < "$in" > "$out"
+	sed -r -n '/^#if/,/^#else/ {s/^([^{]*\{[^,]*,[^,]*,[[:space:]]*)[^,[:space:]]+,[[:space:]]*"([^"]+".*)/\1SEN(printargs), "'$n':\2/; s/^\[.*/&/p}' < "$in" > "$out"
 done
diff --git a/make-dsc b/make-dsc
index 951c5a9e..f0e958ad 100755
--- a/make-dsc
+++ b/make-dsc
@@ -8,7 +8,7 @@ $(sed '/^Source:[[:space:]]*/!d;q' debian/control)
 Binary: $(sed '/^Package:[[:space:]]*/!d;s///' debian/control |
 	tr '\n' ' ' | sed 's/ ./,&/g')
 $(sed '/^Architecture:[[:space:]]*/!d;q' debian/control)
-Version: $(sed -n '1s/^[^(]*(\([^)]\+\)).*/\1/p' debian/changelog)
+Version: $(sed -r -n '1s/^[^(]*\(([^)]+)\).*/\1/p' debian/changelog)
 $(sed '/^Maintainer:[[:space:]]*/!d;q' debian/control)
 $(sed '/^Homepage:[[:space:]]*/!d;q' debian/control)
 $(sed '/^Standards-Version:[[:space:]]*/!d;q' debian/control)
diff --git a/mpers.sh b/mpers.sh
index 559d1f4e..36913e7d 100755
--- a/mpers.sh
+++ b/mpers.sh
@@ -40,8 +40,8 @@ VAR_NAME='mpers_target_var'
 BITS_DIR="mpers${ARCH_FLAG}"
 
 mkdir -p ${BITS_DIR}
-set -- $(sed -n \
-	's/^#[[:space:]]*include[[:space:]]\+DEF_MPERS_TYPE(\([^)[:space:]]*\))$/\1/p' \
+set -- $(sed -r -n \
+	's/^#[[:space:]]*include[[:space:]]+DEF_MPERS_TYPE\(([^)[:space:]]*)\)$/\1/p' \
 		"${PARSER_FILE}")
 for m_type; do
 	f_h="${BITS_DIR}/${m_type}.h"
@@ -53,8 +53,8 @@ for m_type; do
 	sed -e '
 		/DEF_MPERS_TYPE('"${m_type}"')$/n
 		/DEF_MPERS_TYPE/d
-		/^[[:space:]]*#[[:space:]]*include[[:space:]]\+\"xlat\//d
-		/^#[[:space:]]*include[[:space:]]\+MPERS_DEFS$/ {s//'"${m_type} ${VAR_NAME}"';/;q}
+		/^[[:space:]]*#[[:space:]]*include[[:space:]]*"xlat\//d
+		/^#[[:space:]]*include[[:space:]][[:space:]]*MPERS_DEFS$/ {s//'"${m_type} ${VAR_NAME}"';/;q}
 		' "${PARSER_FILE}" > "${f_c}"
 	$CPP $CPPFLAGS "${f_c}" > "${f_i}"
 	grep -F -q "${m_type}.h" "${f_i}" ||
@@ -62,12 +62,12 @@ for m_type; do
 	sed -i -e '/DEF_MPERS_TYPE/d' "${f_c}"
 	$CC $CFLAGS $ARCH_FLAG "${f_c}" -o "${f_o}"
 	readelf --debug-dump=info "${f_o}" > "${f_d1}"
-	sed -n '
-		/^[[:space:]]*<1>/,/^[[:space:]]*<1><[^>]\+>: Abbrev Number: 0/!d
-		/^[[:space:]]*<[^>]\+><[^>]\+>: Abbrev Number: 0/d
-		s/^[[:space:]]*<[[:xdigit:]]\+>[[:space:]]\+//
-		s/^[[:space:]]*\(\(<[[:xdigit:]]\+>\)\{2\}\):[[:space:]]\+/\1\n/
-		s/[[:space:]]\+$//
+	sed -r -n '
+		/^[[:space:]]*<1>/,/^[[:space:]]*<1><[^>]+>: Abbrev Number: 0/!d
+		/^[[:space:]]*<[^>]*><[^>]*>: Abbrev Number: 0/d
+		s/^[[:space:]]*<[[:xdigit:]]+>[[:space:]]+//
+		s/^[[:space:]]*((<[[:xdigit:]]+>){2}):[[:space:]]+/\1\n/
+		s/[[:space:]]+$//
 		p' "${f_d1}" > "${f_d2}"
 	gawk -v VAR_NAME="$VAR_NAME" -v ARCH_FLAG="${ARCH_FLAG#-}" \
 		-f "$MPERS_AWK" "${f_d2}" > "${f_h}"
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 90230293..1e99c44a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -778,7 +778,7 @@ EXTRA_DIST = init.sh run.sh match.awk \
 ksysent.h: $(srcdir)/ksysent.sed
 	echo '#include <asm/unistd.h>' | \
 		$(CPP) $(AM_CPPFLAGS) $(CPPFLAGS) -dM - > $@.t1
-	LC_COLLATE=C sed -n -f $(srcdir)/ksysent.sed < $@.t1 > $@.t2
+	LC_COLLATE=C sed -r -n -f $(srcdir)/ksysent.sed < $@.t1 > $@.t2
 	mv -f $@.t2 $@
 	rm -f $@.t1
 
diff --git a/tests/ksysent.sed b/tests/ksysent.sed
index 56fe042e..63ded4a9 100644
--- a/tests/ksysent.sed
+++ b/tests/ksysent.sed
@@ -1,10 +1,10 @@
-#!/bin/sed -nf
+#!/bin/sed -rnf
 
 # should not have been exported at all
-/#define[[:space:]]\+__NR_\(sys_epoll_\|arch_specific_syscall\|syscalls\|syscall_count\|syscall_max\|available\|reserved\|unused\)/d
+/#define[[:space:]]+__NR_(sys_epoll_|arch_specific_syscall|syscalls|syscall_count|syscall_max|available|reserved|unused)/d
 
-# should not be named this way
-s/__NR_\(arm\|xtensa\)_fadvise64_64/__NR_fadvise64_64/
+# should not  have been named this way
+s/__NR_(arm|xtensa)_fadvise64_64/__NR_fadvise64_64/
 
 # legacy names
 s/__NR_get_cpu/__NR_getcpu/
@@ -12,4 +12,4 @@ s/__NR_madvise1/__NR_madvise/
 s/__NR_paccept/__NR_accept4/
 
 # generate
-s/#define[[:space:]]\+__NR_\([a-z_][^[:space:]]\+\)\([[:space:]].*\)\?$/#ifdef __NR_\1\n[__NR_\1 \& 0xffff] = "\1",\n#endif/p
+s/#define[[:space:]]+__NR_([a-z_][^[:space:]]+)([[:space:]].*)?$/#ifdef __NR_\1\n[__NR_\1 \& 0xffff] = "\1",\n#endif/p
diff --git a/tests/pc.test b/tests/pc.test
index cc558786..beadb7d7 100755
--- a/tests/pc.test
+++ b/tests/pc.test
@@ -37,13 +37,13 @@ args="-if $args"
 $STRACE $args 2> "$LOG" ||
 	dump_log_and_fail_with "$STRACE $args failed"
 
-len="$(sed -n 's/^\[[[:xdigit:]]\+\] write(-1, NULL, \([[:digit:]]\{1,2\}\))[[:space:]]\+= -1 .*/\1/p' "$LOG")" &&
+len="$(sed -r -n 's/^\[[[:xdigit:]]+\] write\(-1, NULL, ([[:digit:]]{1,2})\)[[:space:]]+= -1 .*/\1/p' "$LOG")" &&
 [ -n "$len" ] &&
-pid="$(sed -n 's/^\[[[:xdigit:]]\{'"$len"'\}\] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_\(KILLED\|DUMPED\), si_pid=\([[:digit:]]\+\), .*/\2/p' "$LOG")" &&
+pid="$(sed -r -n 's/^\[[[:xdigit:]]{'"$len"'}\] --- SIGCHLD \{si_signo=SIGCHLD, si_code=CLD_(KILLED|DUMPED), si_pid=([[:digit:]]+), .*/\2/p' "$LOG")" &&
 [ -n "$pid" ] &&
-ip="$(sed -n 's/^\[pid \+'"$pid"'\] \[\([[:xdigit:]]\{'"$len"'\}\)] --- SIGSEGV {.*} ---$/\1/p' "$LOG")" &&
+ip="$(sed -r -n 's/^\[pid +'"$pid"'\] \[([[:xdigit:]]{'"$len"'})] --- SIGSEGV \{.*\} ---$/\1/p' "$LOG")" &&
 [ -n "$ip" ] &&
-addr="$(echo "$ip" |sed 's/^0\+//')" &&
+addr="$(echo "$ip" |sed -r 's/^0+//')" &&
 [ -n "$addr" ] ||
 	dump_log_and_fail_with
 
diff --git a/tests/strace-S.test b/tests/strace-S.test
index 44c15ccd..3ea315ce 100755
--- a/tests/strace-S.test
+++ b/tests/strace-S.test
@@ -14,7 +14,7 @@ test_c()
 	sedexpr="$1"; shift
 
 	run_strace -c -w -S "$sortby" ./readv > /dev/null
-	sed -ne "$sedexpr" < "$LOG" > "$OUT"
+	sed -r -n -e "$sedexpr" < "$LOG" > "$OUT"
 
 	[ -s "$OUT" ] ||
 		fail_ "$STRACE $args output mismatch"
@@ -25,8 +25,8 @@ test_c()
 	}
 }
 
-c='[[:space:]]\+\([^[:space:]]\+\)'
-test_c calls '-n -r' '/^[[:space:]]\+[0-9]/ s/^'"$c$c$c$c"'[[:space:]].*/\4/p'
-test_c name '' '/^[[:space:]]\+[0-9]/ s/^'"$c$c$c$c"'\([[:space:]]\+[0-9]\+\)\?'"$c"'$/\6/p'
+c='[[:space:]]+([^[:space:]]+)'
+test_c calls '-n -r' '/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'[[:space:]].*/\4/p'
+test_c name '' '/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'([[:space:]]+[0-9]+)?'"$c"'$/\6/p'
 
 rm -f "$OUT"
diff --git a/tests/strace-V.test b/tests/strace-V.test
index 91093acb..d441371e 100755
--- a/tests/strace-V.test
+++ b/tests/strace-V.test
@@ -8,7 +8,7 @@ run_strace -V > "$LOG"
 
 getval()
 {
-	sed -n 's/#define[[:space:]]*'"$1"'[[:space:]]*"\([^"]*\)".*/\1/p' ../config.h
+	sed -r -n 's/#define[[:space:]]*'"$1"'[[:space:]]*"([^"]*)".*/\1/p' ../config.h
 }
 
 printf "%s -- version %s\n" "$(getval PACKAGE_NAME)" "$(getval VERSION)" > "$EXP"
diff --git a/tests/strace-k.test b/tests/strace-k.test
index 5919bacb..d5e8ed2b 100755
--- a/tests/strace-k.test
+++ b/tests/strace-k.test
@@ -41,7 +41,7 @@ run_prog ./stack-fcall
 run_strace -e getpid -k $args
 
 expected='getpid f3 f2 f1 f0 main '
-result=$(sed -n '1,/(main+0x[a-f0-9]\+) .*/ s/^.*(\([^+]\+\)+0x[a-f0-9]\+) .*/\1/p' "$LOG" |
+result=$(sed -r -n '1,/\(main\+0x[a-f0-9]+\) .*/ s/^.*\(([^+]+)\+0x[a-f0-9]+\) .*/\1/p' "$LOG" |
 	tr '\n' ' ')
 
 test "$result" = "$expected" || {
diff --git a/xlat/gen.sh b/xlat/gen.sh
index cf78c47f..ec632803 100755
--- a/xlat/gen.sh
+++ b/xlat/gen.sh
@@ -44,11 +44,11 @@ cond_def()
 
 	local val
 	val="$(printf %s "$line" |
-		sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
+		sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
 
 	local def
 	def="$(printf %s "${line}" |
-		sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
+		sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
 
 	if [ -n "$def" ]; then
 		cat <<-EOF
@@ -89,10 +89,10 @@ cond_xlat()
 	local line val m def xlat
 	line="$1"; shift
 
-	val="$(printf %s "${line}" | sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
+	val="$(printf %s "${line}" | sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
 	m="${val%%|*}"
 	def="$(printf %s "${line}" |
-	       sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
+	       sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
 
 	if [ "${m}" = "${m#1<<}" ]; then
 		xlat="$(print_xlat "${val}")"