From patchwork Sat Jul 13 14:36:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Clement Chauplannaz X-Patchwork-Id: 2827168 Return-Path: X-Original-To: patchwork-linux-kbuild@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 891E3C0AB2 for ; Sat, 13 Jul 2013 14:37:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6382420117 for ; Sat, 13 Jul 2013 14:37:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5AA9E20116 for ; Sat, 13 Jul 2013 14:37:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933059Ab3GMOhL (ORCPT ); Sat, 13 Jul 2013 10:37:11 -0400 Received: from mail-wi0-f179.google.com ([209.85.212.179]:46238 "EHLO mail-wi0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932571Ab3GMOhL (ORCPT ); Sat, 13 Jul 2013 10:37:11 -0400 Received: by mail-wi0-f179.google.com with SMTP id hj3so1563939wib.12 for ; Sat, 13 Jul 2013 07:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=4T8k7llNA/PDI8KCPDppZiypkpUxqheza+U8Dh8m28A=; b=nWku4n2SiSZDisYdMb5s8iD2EsFPxhlObvh4izYjPwyajNXPIZwb2HXZy9uNXc3OqR 8LB6RJSflWapR80vzfES1vVix4hSnk9c4Gjemp7KmPfvact+YL15d2OlbtHInqAZPLBC tOtcEMOhcWz8Jzg85LGeU8pLkY1TtJ0sfTKFT9jAk8s1qB3cmA17NraNiID9heZPI0ou cjJV977/T2Sq5BC/iZFFAGYsVRdFeclKbCi2u/vQu/CLzIMKrWh6xHyCdD9I2LJu/QzI 9e7pDLNZ2fdEkRESDGH+h3sv3VtJQ26vBA286eYzjpCC3oOjhHOb6FXkogfIreB6OD++ gGVA== X-Received: by 10.194.216.99 with SMTP id op3mr27646545wjc.52.1373726229448; Sat, 13 Jul 2013 07:37:09 -0700 (PDT) Received: from localhost.localdomain (129.130.72.86.rev.sfr.net. [86.72.130.129]) by mx.google.com with ESMTPSA id s19sm9152885wik.11.2013.07.13.07.37.07 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Sat, 13 Jul 2013 07:37:08 -0700 (PDT) From: Clement Chauplannaz To: linux-kbuild@vger.kernel.org Cc: mmarek@suse.cz, ak@linux.intel.com, yann.morin.1998@free.fr Subject: [PATCH v3] scripts/config: use sed's POSIX interface Date: Sat, 13 Jul 2013 16:36:56 +0200 Message-Id: <1373726216-13415-1-git-send-email-chauplac@gmail.com> X-Mailer: git-send-email 1.8.3.rc1.44.gb387c77.dirty Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- Changes v2 -> v3: - Moved temporary files handling to txt_* helper functions. - Removed useless trailing slash in sed's append command parameter. - Edited commit message. Changes v1 -> 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 | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/scripts/config b/scripts/config index a65ecbb..58383e2 100755 --- a/scripts/config +++ b/scripts/config @@ -60,15 +60,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 @@ -77,7 +114,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