Skip to content

Commit

Permalink
scripts/config: use sed's POSIX interface
Browse files Browse the repository at this point in the history
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 <[email protected]>
Tested-by: "Yann E. MORIN" <[email protected]>
Reviewed-by: "Yann E. MORIN" <[email protected]>
Signed-off-by: Yann E. MORIN <[email protected]>
  • Loading branch information
chauplac authored and yann-morin-1998 committed Aug 15, 2013
1 parent 129784a commit 83e8b90
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions scripts/config
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,52 @@ checkarg() {
fi
}

txt_append() {
local anchor="$1"
local insert="$2"
local infile="$3"
local tmpfile="$infile.swp"

# sed append cmd: 'a\' + newline + text + newline
cmd="$(printf "a\\%b$insert" "\n")"

sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}

txt_subst() {
local before="$1"
local after="$2"
local infile="$3"
local tmpfile="$infile.swp"

sed -e "s/$before/$after/" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}

txt_delete() {
local text="$1"
local infile="$2"
local tmpfile="$infile.swp"

sed -e "/$text/d" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}

set_var() {
local name=$1 new=$2 before=$3

name_re="^($name=|# $name is not set)"
before_re="^($before=|# $before is not set)"
if test -n "$before" && grep -Eq "$before_re" "$FN"; then
sed -ri "/$before_re/a $new" "$FN"
txt_append "^$before=" "$new" "$FN"
txt_append "^# $before is not set" "$new" "$FN"
elif grep -Eq "$name_re" "$FN"; then
sed -ri "s:$name_re.*:$new:" "$FN"
txt_subst "^$name=.*" "$new" "$FN"
txt_subst "^# $name is not set" "$new" "$FN"
else
echo "$new" >>"$FN"
fi
Expand All @@ -79,7 +116,8 @@ set_var() {
undef_var() {
local name=$1

sed -ri "/^($name=|# $name is not set)/d" "$FN"
txt_delete "^$name=" "$FN"
txt_delete "^# $name is not set" "$FN"
}

if [ "$1" = "--file" ]; then
Expand Down

0 comments on commit 83e8b90

Please sign in to comment.