aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/config
diff options
context:
space:
mode:
authorClement Chauplannaz <chauplac@gmail.com>2013-07-13 10:36:56 -0400
committerYann E. MORIN <yann.morin.1998@free.fr>2013-08-15 16:48:07 -0400
commit83e8b90e1d2cc5ff5d2443f2486c2d786a4997ce (patch)
tree9d57d3bee9b3de3a623ab52bd2e4a14a9289b5ba /scripts/config
parent129784abc982ccac43322c2f175f3ca735c2ca73 (diff)
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. Signed-off-by: Clement Chauplannaz <chauplac@gmail.com> Tested-by: "Yann E. MORIN" <yann.morin.1998@free.fr> Reviewed-by: "Yann E. MORIN" <yann.morin.1998@free.fr> Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
Diffstat (limited to 'scripts/config')
-rwxr-xr-xscripts/config44
1 files changed, 41 insertions, 3 deletions
diff --git a/scripts/config b/scripts/config
index 567120a87c39..2283be2bb62c 100755
--- a/scripts/config
+++ b/scripts/config
@@ -62,15 +62,52 @@ checkarg() {
62 fi 62 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=$3 101 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"; then 105 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"; then 108 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 else 111 else
75 echo "$new" >>"$FN" 112 echo "$new" >>"$FN"
76 fi 113 fi
@@ -79,7 +116,8 @@ set_var() {
79undef_var() { 116undef_var() {
80 local name=$1 117 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" ]; then 123if [ "$1" = "--file" ]; then