Browse Source

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.
Dmitry V. Levin 3 years ago
parent
commit
469fd109cd
13 changed files with 44 additions and 44 deletions
  1. 8
    8
      Makefile.am
  2. 3
    3
      generate_mpers_am.sh
  3. 1
    1
      generate_sen.sh
  4. 1
    1
      linux/mips/genstub.sh
  5. 1
    1
      make-dsc
  6. 10
    10
      mpers.sh
  7. 1
    1
      tests/Makefile.am
  8. 5
    5
      tests/ksysent.sed
  9. 4
    4
      tests/pc.test
  10. 4
    4
      tests/strace-S.test
  11. 1
    1
      tests/strace-V.test
  12. 1
    1
      tests/strace-k.test
  13. 4
    4
      xlat/gen.sh

+ 8
- 8
Makefile.am View File

@@ -713,7 +713,7 @@ ioctl_redefs_h = $(filter-out ioctl_redefs0.h,$(subst ioctlent,ioctl_redefs,$(io
713 713
 ioctl_redefs%.h: ioctlent%.h ioctlent0.h
714 714
 	sort $< > $<-t
715 715
 	sort ioctlent0.h | comm -23 $<-t - | \
716
-		sed -n 's/^{ "\([^"]\+\)", \(0x[[:xdigit:]]\+\) },$$/#ifdef \1\n# undef \1\n# define \1 \2\n#endif/p' \
716
+		sed -r -n 's/^\{ "([^"]+)", (0x[[:xdigit:]]+) \},$$/#ifdef \1\n# undef \1\n# define \1 \2\n#endif/p' \
717 717
 		> $@-t
718 718
 	rm -f $<-t
719 719
 	mv $@-t $@
@@ -762,7 +762,7 @@ mpers-m%.stamp: $(srcdir_mpers_source_files) | printers.h
762 762
 
763 763
 m%_type_defs.h: $(srcdir_mpers_source_files)
764 764
 	for f in $^; do \
765
-		sed -n 's/^#include DEF_MPERS_TYPE(\([^)]\+\))/#ifdef MPERS_$(mpers_PREFIX)\1\n# define \1 MPERS_$(mpers_PREFIX)\1\n#endif/p' $$f || exit; \
765
+		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; \
766 766
 	done > $@-t
767 767
 	echo '#undef MPERS_PRINTER_NAME' >> $@-t
768 768
 	echo '#define MPERS_PRINTER_NAME(printer_name) printer_name' >> $@-t
@@ -772,7 +772,7 @@ m%_type_defs.h: $(srcdir_mpers_source_files)
772 772
 
773 773
 m%_funcs.h: $(srcdir_mpers_source_files)
774 774
 	for f in $^; do \
775
-		sed -n 's/^SYS_FUNC(\([^)]\+\))/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
775
+		sed -r -n 's/^SYS_FUNC\(([^)]+)\)/#undef sys_\1\n#define sys_\1 $(mpers_PREFIX)sys_\1/p' $$f || exit; \
776 776
 	done > $@-t && \
777 777
 	echo '#include "sys_func.h"' >> $@-t
778 778
 	mv $@-t $@
@@ -782,13 +782,13 @@ m%_funcs.h: $(srcdir_mpers_source_files)
782 782
 %.c.mpers.i: $(srcdir)/%.c
783 783
 	$(CPP) -P $(mpers_sh_opts) -DIN_MPERS_BOOTSTRAP $< -o $@
784 784
 
785
-mpers_printer_decl_pattern = ^MPERS_PRINTER_DECL(\([^,)]\+\),[[:space:]]*\([^,)]\+\),[[:space:]]*\([^)]\+\))$$
785
+mpers_printer_decl_pattern = ^MPERS_PRINTER_DECL\(([^,)]+),[[:space:]]*([^,)]+),[[:space:]]*([^)]+)\)$$
786 786
 
787 787
 printers.h: $(mpers_preproc_files)
788 788
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
789 789
 	echo 'typedef struct {' >> $@-t
790 790
 	for f in $^; do \
791
-		sed -n 's/$(mpers_printer_decl_pattern)/ \1 (*\2)(\3);\n#define \2 MPERS_PRINTER_NAME(\2)\n/p' $$f \
791
+		sed -r -n 's/$(mpers_printer_decl_pattern)/ \1 (*\2)(\3);\n#define \2 MPERS_PRINTER_NAME(\2)\n/p' $$f \
792 792
 		|| exit; \
793 793
 	done >> $@-t
794 794
 	echo '} struct_printers;' >> $@-t
@@ -799,7 +799,7 @@ printers.h: $(mpers_preproc_files)
799 799
 %_printer_decls.h: $(mpers_preproc_files)
800 800
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
801 801
 	for f in $^; do \
802
-		sed -n 's/$(mpers_printer_decl_pattern)/extern \1 $(mpers_PREFIX)\2(\3);/p' $$f \
802
+		sed -r -n 's/$(mpers_printer_decl_pattern)/extern \1 $(mpers_PREFIX)\2(\3);/p' $$f \
803 803
 		|| exit; \
804 804
 	done >> $@-t
805 805
 	mv $@-t $@
@@ -807,7 +807,7 @@ printers.h: $(mpers_preproc_files)
807 807
 %_printer_defs.h: $(mpers_preproc_files)
808 808
 	echo '/* Generated by Makefile from $^; do not edit. */' > $@-t
809 809
 	for f in $^; do \
810
-		sed -n 's/$(mpers_printer_decl_pattern)/\.\2 = $(mpers_PREFIX)\2,/p' $$f \
810
+		sed -r -n 's/$(mpers_printer_decl_pattern)/.\2 = $(mpers_PREFIX)\2,/p' $$f \
811 811
 		|| exit; \
812 812
 	done >> $@-t
813 813
 	mv $@-t $@
@@ -869,7 +869,7 @@ $(srcdir)/CREDITS: $(srcdir)/CREDITS.in $(srcdir)/.mailmap \
869 869
 	(								\
870 870
 	  cd $(srcdir);							\
871 871
 	  sed '/^##/,$$d' CREDITS.in;					\
872
-	  { sed -n '1,/^##>/d; s/  \+/\t/; s/^./&/p' CREDITS.in;	\
872
+	  { sed -n '1,/^##>/d; s/   */\t/; s/^./&/p' CREDITS.in;	\
873 873
 	    git log --pretty=format:'%aN	%aE';			\
874 874
 	  } | LC_ALL=C sort -u						\
875 875
 	    | awk -F'\t' '{printf("\t%s <%s>\n",$$1,$$2)}';		\

+ 3
- 3
generate_mpers_am.sh View File

@@ -1,7 +1,7 @@
1 1
 #!/bin/sh -e
2 2
 
3
-list="$(sed -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*\([[:alnum:]][^.]*\.c\)[[:space:]]*\\$/\1/p' Makefile.am |
4
-	xargs -r grep -lx '#[[:space:]]*include[[:space:]]\+MPERS_DEFS' |
3
+list="$(sed -r -n '/^strace_SOURCES[[:space:]]*=/,/^[[:space:]]*# end of strace_SOURCES/ s/^[[:space:]]*([[:alnum:]][^.]*\.c)[[:space:]]*\\$/\1/p' Makefile.am |
4
+	xargs -r grep -Elx '#[[:space:]]*include[[:space:]]+MPERS_DEFS' |
5 5
 	tr '\n' ' ')"
6 6
 
7 7
 cat > mpers.am <<EOF
@@ -9,5 +9,5 @@ cat > mpers.am <<EOF
9 9
 mpers_source_files = $list
10 10
 EOF
11 11
 
12
-sed -n 's/^#[[:space:]]*include[[:space:]]*"xlat\/\([^."]\+\)\.h".*/extern const struct xlat \1[];/p' \
12
+sed -r -n 's/^#[[:space:]]*include[[:space:]]*"xlat\/([a-z][a-z_0-9]*)\.h".*/extern const struct xlat \1[];/p' \
13 13
 	$list > mpers_xlat.h

+ 1
- 1
generate_sen.sh View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 echo 'enum {'
4 4
 echo 'SEN_printargs = 0,'
5
-    sed -n '/printargs/! s/.*SEN(\([^)]*\)).*/\1/p' |
5
+    sed -r -n '/printargs/! s/.*SEN\(([^)]+)\).*/\1/p' |
6 6
     sort -u |
7 7
     sed 's/.*/SEN_&,/'
8 8
 echo '};'

+ 1
- 1
linux/mips/genstub.sh View File

@@ -6,5 +6,5 @@ dstdir="$1"; shift
6 6
 for n in n32 n64 o32; do
7 7
 	in="$srcdir/syscallent-$n.h"
8 8
 	out="$dstdir/syscallent-$n-stub.h"
9
-	sed -n '/^#if/,/^#else/ {s/^\([^{]*{[^,]*,[^,]*,[[:space:]]*\)[^,[:space:]]\+,[[:space:]]*"\([^"]\+".*\)/\1SEN(printargs), "'$n'_\2/; s/^\[.*/&/p}' < "$in" > "$out"
9
+	sed -r -n '/^#if/,/^#else/ {s/^([^{]*\{[^,]*,[^,]*,[[:space:]]*)[^,[:space:]]+,[[:space:]]*"([^"]+".*)/\1SEN(printargs), "'$n':\2/; s/^\[.*/&/p}' < "$in" > "$out"
10 10
 done

+ 1
- 1
make-dsc View File

@@ -8,7 +8,7 @@ $(sed '/^Source:[[:space:]]*/!d;q' debian/control)
8 8
 Binary: $(sed '/^Package:[[:space:]]*/!d;s///' debian/control |
9 9
 	tr '\n' ' ' | sed 's/ ./,&/g')
10 10
 $(sed '/^Architecture:[[:space:]]*/!d;q' debian/control)
11
-Version: $(sed -n '1s/^[^(]*(\([^)]\+\)).*/\1/p' debian/changelog)
11
+Version: $(sed -r -n '1s/^[^(]*\(([^)]+)\).*/\1/p' debian/changelog)
12 12
 $(sed '/^Maintainer:[[:space:]]*/!d;q' debian/control)
13 13
 $(sed '/^Homepage:[[:space:]]*/!d;q' debian/control)
14 14
 $(sed '/^Standards-Version:[[:space:]]*/!d;q' debian/control)

+ 10
- 10
mpers.sh View File

@@ -40,8 +40,8 @@ VAR_NAME='mpers_target_var'
40 40
 BITS_DIR="mpers${ARCH_FLAG}"
41 41
 
42 42
 mkdir -p ${BITS_DIR}
43
-set -- $(sed -n \
44
-	's/^#[[:space:]]*include[[:space:]]\+DEF_MPERS_TYPE(\([^)[:space:]]*\))$/\1/p' \
43
+set -- $(sed -r -n \
44
+	's/^#[[:space:]]*include[[:space:]]+DEF_MPERS_TYPE\(([^)[:space:]]*)\)$/\1/p' \
45 45
 		"${PARSER_FILE}")
46 46
 for m_type; do
47 47
 	f_h="${BITS_DIR}/${m_type}.h"
@@ -53,8 +53,8 @@ for m_type; do
53 53
 	sed -e '
54 54
 		/DEF_MPERS_TYPE('"${m_type}"')$/n
55 55
 		/DEF_MPERS_TYPE/d
56
-		/^[[:space:]]*#[[:space:]]*include[[:space:]]\+\"xlat\//d
57
-		/^#[[:space:]]*include[[:space:]]\+MPERS_DEFS$/ {s//'"${m_type} ${VAR_NAME}"';/;q}
56
+		/^[[:space:]]*#[[:space:]]*include[[:space:]]*"xlat\//d
57
+		/^#[[:space:]]*include[[:space:]][[:space:]]*MPERS_DEFS$/ {s//'"${m_type} ${VAR_NAME}"';/;q}
58 58
 		' "${PARSER_FILE}" > "${f_c}"
59 59
 	$CPP $CPPFLAGS "${f_c}" > "${f_i}"
60 60
 	grep -F -q "${m_type}.h" "${f_i}" ||
@@ -62,12 +62,12 @@ for m_type; do
62 62
 	sed -i -e '/DEF_MPERS_TYPE/d' "${f_c}"
63 63
 	$CC $CFLAGS $ARCH_FLAG "${f_c}" -o "${f_o}"
64 64
 	readelf --debug-dump=info "${f_o}" > "${f_d1}"
65
-	sed -n '
66
-		/^[[:space:]]*<1>/,/^[[:space:]]*<1><[^>]\+>: Abbrev Number: 0/!d
67
-		/^[[:space:]]*<[^>]\+><[^>]\+>: Abbrev Number: 0/d
68
-		s/^[[:space:]]*<[[:xdigit:]]\+>[[:space:]]\+//
69
-		s/^[[:space:]]*\(\(<[[:xdigit:]]\+>\)\{2\}\):[[:space:]]\+/\1\n/
70
-		s/[[:space:]]\+$//
65
+	sed -r -n '
66
+		/^[[:space:]]*<1>/,/^[[:space:]]*<1><[^>]+>: Abbrev Number: 0/!d
67
+		/^[[:space:]]*<[^>]*><[^>]*>: Abbrev Number: 0/d
68
+		s/^[[:space:]]*<[[:xdigit:]]+>[[:space:]]+//
69
+		s/^[[:space:]]*((<[[:xdigit:]]+>){2}):[[:space:]]+/\1\n/
70
+		s/[[:space:]]+$//
71 71
 		p' "${f_d1}" > "${f_d2}"
72 72
 	gawk -v VAR_NAME="$VAR_NAME" -v ARCH_FLAG="${ARCH_FLAG#-}" \
73 73
 		-f "$MPERS_AWK" "${f_d2}" > "${f_h}"

+ 1
- 1
tests/Makefile.am View File

@@ -778,7 +778,7 @@ EXTRA_DIST = init.sh run.sh match.awk \
778 778
 ksysent.h: $(srcdir)/ksysent.sed
779 779
 	echo '#include <asm/unistd.h>' | \
780 780
 		$(CPP) $(AM_CPPFLAGS) $(CPPFLAGS) -dM - > $@.t1
781
-	LC_COLLATE=C sed -n -f $(srcdir)/ksysent.sed < $@.t1 > $@.t2
781
+	LC_COLLATE=C sed -r -n -f $(srcdir)/ksysent.sed < $@.t1 > $@.t2
782 782
 	mv -f $@.t2 $@
783 783
 	rm -f $@.t1
784 784
 

+ 5
- 5
tests/ksysent.sed View File

@@ -1,10 +1,10 @@
1
-#!/bin/sed -nf
1
+#!/bin/sed -rnf
2 2
 
3 3
 # should not have been exported at all
4
-/#define[[:space:]]\+__NR_\(sys_epoll_\|arch_specific_syscall\|syscalls\|syscall_count\|syscall_max\|available\|reserved\|unused\)/d
4
+/#define[[:space:]]+__NR_(sys_epoll_|arch_specific_syscall|syscalls|syscall_count|syscall_max|available|reserved|unused)/d
5 5
 
6
-# should not be named this way
7
-s/__NR_\(arm\|xtensa\)_fadvise64_64/__NR_fadvise64_64/
6
+# should not  have been named this way
7
+s/__NR_(arm|xtensa)_fadvise64_64/__NR_fadvise64_64/
8 8
 
9 9
 # legacy names
10 10
 s/__NR_get_cpu/__NR_getcpu/
@@ -12,4 +12,4 @@ s/__NR_madvise1/__NR_madvise/
12 12
 s/__NR_paccept/__NR_accept4/
13 13
 
14 14
 # generate
15
-s/#define[[:space:]]\+__NR_\([a-z_][^[:space:]]\+\)\([[:space:]].*\)\?$/#ifdef __NR_\1\n[__NR_\1 \& 0xffff] = "\1",\n#endif/p
15
+s/#define[[:space:]]+__NR_([a-z_][^[:space:]]+)([[:space:]].*)?$/#ifdef __NR_\1\n[__NR_\1 \& 0xffff] = "\1",\n#endif/p

+ 4
- 4
tests/pc.test View File

@@ -37,13 +37,13 @@ args="-if $args"
37 37
 $STRACE $args 2> "$LOG" ||
38 38
 	dump_log_and_fail_with "$STRACE $args failed"
39 39
 
40
-len="$(sed -n 's/^\[[[:xdigit:]]\+\] write(-1, NULL, \([[:digit:]]\{1,2\}\))[[:space:]]\+= -1 .*/\1/p' "$LOG")" &&
40
+len="$(sed -r -n 's/^\[[[:xdigit:]]+\] write\(-1, NULL, ([[:digit:]]{1,2})\)[[:space:]]+= -1 .*/\1/p' "$LOG")" &&
41 41
 [ -n "$len" ] &&
42
-pid="$(sed -n 's/^\[[[:xdigit:]]\{'"$len"'\}\] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_\(KILLED\|DUMPED\), si_pid=\([[:digit:]]\+\), .*/\2/p' "$LOG")" &&
42
+pid="$(sed -r -n 's/^\[[[:xdigit:]]{'"$len"'}\] --- SIGCHLD \{si_signo=SIGCHLD, si_code=CLD_(KILLED|DUMPED), si_pid=([[:digit:]]+), .*/\2/p' "$LOG")" &&
43 43
 [ -n "$pid" ] &&
44
-ip="$(sed -n 's/^\[pid \+'"$pid"'\] \[\([[:xdigit:]]\{'"$len"'\}\)] --- SIGSEGV {.*} ---$/\1/p' "$LOG")" &&
44
+ip="$(sed -r -n 's/^\[pid +'"$pid"'\] \[([[:xdigit:]]{'"$len"'})] --- SIGSEGV \{.*\} ---$/\1/p' "$LOG")" &&
45 45
 [ -n "$ip" ] &&
46
-addr="$(echo "$ip" |sed 's/^0\+//')" &&
46
+addr="$(echo "$ip" |sed -r 's/^0+//')" &&
47 47
 [ -n "$addr" ] ||
48 48
 	dump_log_and_fail_with
49 49
 

+ 4
- 4
tests/strace-S.test View File

@@ -14,7 +14,7 @@ test_c()
14 14
 	sedexpr="$1"; shift
15 15
 
16 16
 	run_strace -c -w -S "$sortby" ./readv > /dev/null
17
-	sed -ne "$sedexpr" < "$LOG" > "$OUT"
17
+	sed -r -n -e "$sedexpr" < "$LOG" > "$OUT"
18 18
 
19 19
 	[ -s "$OUT" ] ||
20 20
 		fail_ "$STRACE $args output mismatch"
@@ -25,8 +25,8 @@ test_c()
25 25
 	}
26 26
 }
27 27
 
28
-c='[[:space:]]\+\([^[:space:]]\+\)'
29
-test_c calls '-n -r' '/^[[:space:]]\+[0-9]/ s/^'"$c$c$c$c"'[[:space:]].*/\4/p'
30
-test_c name '' '/^[[:space:]]\+[0-9]/ s/^'"$c$c$c$c"'\([[:space:]]\+[0-9]\+\)\?'"$c"'$/\6/p'
28
+c='[[:space:]]+([^[:space:]]+)'
29
+test_c calls '-n -r' '/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'[[:space:]].*/\4/p'
30
+test_c name '' '/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'([[:space:]]+[0-9]+)?'"$c"'$/\6/p'
31 31
 
32 32
 rm -f "$OUT"

+ 1
- 1
tests/strace-V.test View File

@@ -8,7 +8,7 @@ run_strace -V > "$LOG"
8 8
 
9 9
 getval()
10 10
 {
11
-	sed -n 's/#define[[:space:]]*'"$1"'[[:space:]]*"\([^"]*\)".*/\1/p' ../config.h
11
+	sed -r -n 's/#define[[:space:]]*'"$1"'[[:space:]]*"([^"]*)".*/\1/p' ../config.h
12 12
 }
13 13
 
14 14
 printf "%s -- version %s\n" "$(getval PACKAGE_NAME)" "$(getval VERSION)" > "$EXP"

+ 1
- 1
tests/strace-k.test View File

@@ -41,7 +41,7 @@ run_prog ./stack-fcall
41 41
 run_strace -e getpid -k $args
42 42
 
43 43
 expected='getpid f3 f2 f1 f0 main '
44
-result=$(sed -n '1,/(main+0x[a-f0-9]\+) .*/ s/^.*(\([^+]\+\)+0x[a-f0-9]\+) .*/\1/p' "$LOG" |
44
+result=$(sed -r -n '1,/\(main\+0x[a-f0-9]+\) .*/ s/^.*\(([^+]+)\+0x[a-f0-9]+\) .*/\1/p' "$LOG" |
45 45
 	tr '\n' ' ')
46 46
 
47 47
 test "$result" = "$expected" || {

+ 4
- 4
xlat/gen.sh View File

@@ -44,11 +44,11 @@ cond_def()
44 44
 
45 45
 	local val
46 46
 	val="$(printf %s "$line" |
47
-		sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
47
+		sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
48 48
 
49 49
 	local def
50 50
 	def="$(printf %s "${line}" |
51
-		sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
51
+		sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
52 52
 
53 53
 	if [ -n "$def" ]; then
54 54
 		cat <<-EOF
@@ -89,10 +89,10 @@ cond_xlat()
89 89
 	local line val m def xlat
90 90
 	line="$1"; shift
91 91
 
92
-	val="$(printf %s "${line}" | sed -n 's/^\([^[:space:]]\+\).*$/\1/p')"
92
+	val="$(printf %s "${line}" | sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
93 93
 	m="${val%%|*}"
94 94
 	def="$(printf %s "${line}" |
95
-	       sed -n 's/^[^[:space:]]\+[[:space:]]\+\([^[:space:]].*\)$/\1/p')"
95
+	       sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
96 96
 
97 97
 	if [ "${m}" = "${m#1<<}" ]; then
98 98
 		xlat="$(print_xlat "${val}")"

Loading…
Cancel
Save