History

scripts/config: use sed's POSIX interface

Script `config' relies on extensions of `GNU sed', and is thus not working on all Unixes: - in-place edition of files (-i), which can be replaced with a temporary file; - extended-regexps (-r), which can be split into basic regexps; - single-line calls to `a' command, while some implementations require a leading newline before the parameter. Rewrite calls to `sed' to comply with POSIX interface, and move them to helper functions.

Clement Chauplannaz committed 13 years ago

Commit 83e8b901 parent 129784a1 file · +41 −3Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/config+41−3
@@ -62,15 +62,52 @@ checkarg() { · 61 unmodified lines
6262 fi
6363 }
6464
65+txt_append() {
66+ local anchor="$1"
67+ local insert="$2"
68+ local infile="$3"
69+ local tmpfile="$infile.swp"
70+
71+ # sed append cmd: 'a\' + newline + text + newline
72+ cmd="$(printf "a\\%b$insert" "\n")"
73+
74+ sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
75+ # replace original file with the edited one
76+ mv "$tmpfile" "$infile"
77+}
78+
79+txt_subst() {
80+ local before="$1"
81+ local after="$2"
82+ local infile="$3"
83+ local tmpfile="$infile.swp"
84+
85+ sed -e "s/$before/$after/" "$infile" >"$tmpfile"
86+ # replace original file with the edited one
87+ mv "$tmpfile" "$infile"
88+}
89+
90+txt_delete() {
91+ local text="$1"
92+ local infile="$2"
93+ local tmpfile="$infile.swp"
94+
95+ sed -e "/$text/d" "$infile" >"$tmpfile"
96+ # replace original file with the edited one
97+ mv "$tmpfile" "$infile"
98+}
99+
65100 set_var() {
66101 local name=$1 new=$2 before=$3
67102
68103 name_re="^($name=|# $name is not set)"
69104 before_re="^($before=|# $before is not set)"
70105 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
71− sed -ri "/$before_re/a $new" "$FN"
106+ txt_append "^$before=" "$new" "$FN"
107+ txt_append "^# $before is not set" "$new" "$FN"
72108 elif grep -Eq "$name_re" "$FN"; then
73− sed -ri "s:$name_re.*:$new:" "$FN"
109+ txt_subst "^$name=.*" "$new" "$FN"
110+ txt_subst "^# $name is not set" "$new" "$FN"
74111 else
75112 echo "$new" >>"$FN"
76113 fi
@@ -79,7 +116,8 @@ set_var() { · 2 unmodified lines
79116 undef_var() {
80117 local name=$1
81118
82− sed -ri "/^($name=|# $name is not set)/d" "$FN"
119+ txt_delete "^$name=" "$FN"
120+ txt_delete "^# $name is not set" "$FN"
83121 }
84122
85123 if [ "$1" = "--file" ]; then