From: Clement Chauplannaz <chauplac@gmail.com>
To: linux-kbuild@vger.kernel.org
Cc: mmarek@suse.cz, ak@linux.intel.com, yann.morin.1998@free.fr
Subject: [PATCH v2] scripts/config: use sed's POSIX interface
Date: Sat, 13 Jul 2013 00:26:17 +0200 [thread overview]
Message-ID: <1373667977-8034-1-git-send-email-chauplac@gmail.com> (raw)
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>
---
Changes in v2:
- ANSI C style quoting to produce newlines ($'\n') is replaced with
printf's, not to introduce further dependency on bash.
- helper functions are introduced to wrap calls to `sed'.
scripts/config | 41 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 38 insertions(+), 3 deletions(-)
diff --git a/scripts/config b/scripts/config
index a65ecbb..a01bc11 100755
--- a/scripts/config
+++ b/scripts/config
@@ -60,15 +60,48 @@ checkarg() {
fi
}
+txt_append() {
+ local anchor="$1"
+ local insert="$2"
+ local infile="$3"
+ local outfile="$4"
+
+ # sed append cmd: 'a\' + newline + text + newline
+ cmd="$(printf "a\\%b$insert%b" "\n" "\n")"
+
+ sed -e "/$anchor/$cmd" "$infile" >"$outfile"
+}
+
+txt_subst() {
+ local before="$1"
+ local after="$2"
+ local infile="$3"
+ local outfile="$4"
+
+ sed -e "s/$before/$after/" "$infile" >"$outfile"
+}
+
+txt_delete() {
+ local text="$1"
+ local infile="$2"
+ local outfile="$3"
+
+ sed -e "/$text/d" "$infile" >"$outfile"
+}
+
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" "$FN.swp"
+ txt_append "^# $before is not set" "$new" "$FN.swp" "$FN"
+ rm "$FN.swp"
elif grep -Eq "$name_re" "$FN"; then
- sed -ri "s:$name_re.*:$new:" "$FN"
+ txt_subst "^$name=.*" "$new" "$FN" "$FN.swp"
+ txt_subst "^# $name is not set" "$new" "$FN.swp" "$FN"
+ rm "$FN.swp"
else
echo "$new" >>"$FN"
fi
@@ -77,7 +110,9 @@ set_var() {
undef_var() {
local name=$1
- sed -ri "/^($name=|# $name is not set)/d" "$FN"
+ txt_delete "^$name=" "$FN" "$FN.swp"
+ txt_delete "^# $name is not set" "$FN.swp" "$FN"
+ rm "$FN.swp"
}
if [ "$1" = "--file" ]; then
--
1.8.3.rc1.44.gb387c77.dirty
next reply other threads:[~2013-07-12 22:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-12 22:26 Clement Chauplannaz [this message]
2013-07-13 12:54 ` [PATCH v2] scripts/config: use sed's POSIX interface Yann E. MORIN
2013-07-13 14:16 ` Clément Chauplannaz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1373667977-8034-1-git-send-email-chauplac@gmail.com \
--to=chauplac@gmail.com \
--cc=ak@linux.intel.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=mmarek@suse.cz \
--cc=yann.morin.1998@free.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox