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
62 fi62 fi
63}63}
64 64
65txt_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
79txt_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
90txt_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
65set_var() {100set_var() {
66 local name=$1 new=$2 before=$3101 local name=$1 new=$2 before=$3
67 102
68 name_re="^($name=|# $name is not set)"103 name_re="^($name=|# $name is not set)"
69 before_re="^($before=|# $before is not set)"104 before_re="^($before=|# $before is not set)"
70 if test -n "$before" && grep -Eq "$before_re" "$FN"; then105 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"
72 elif grep -Eq "$name_re" "$FN"; then108 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"
74 else111 else
75 echo "$new" >>"$FN"112 echo "$new" >>"$FN"
76 fi113 fi
@@ -79,7 +116,8 @@ set_var() { · 2 unmodified lines
79undef_var() {116undef_var() {
80 local name=$1117 local name=$1
81 118
82 sed -ri "/^($name=|# $name is not set)/d" "$FN"119 txt_delete "^$name=" "$FN"
120 txt_delete "^# $name is not set" "$FN"
83}121}
84 122
85if [ "$1" = "--file" ]; then123if [ "$1" = "--file" ]; then