From patchwork Fri Jul 12 22:26:17 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Clement Chauplannaz X-Patchwork-Id: 2827083 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 2A93DC0AB2 for ; Fri, 12 Jul 2013 22:26:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 438A620126 for ; Fri, 12 Jul 2013 22:26:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2D3552011D for ; Fri, 12 Jul 2013 22:26:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757840Ab3GLW0o (ORCPT ); Fri, 12 Jul 2013 18:26:44 -0400 Received: from mail-we0-f175.google.com ([74.125.82.175]:47185 "EHLO mail-we0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757776Ab3GLW0o (ORCPT ); Fri, 12 Jul 2013 18:26:44 -0400 Received: by mail-we0-f175.google.com with SMTP id t59so8305919wes.6 for ; Fri, 12 Jul 2013 15:26:43 -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=3fsF5MRkaX3K9rEoh7j9Wn9COT9AadKEpCloitP+kdc=; b=k66ItG12u5h6S4Qp9AhLBDY0qmB9dULytGVSywypvTNIGS5EQZI0w2U39G11kKcdpi ZVFJWHxd9Q0aTQR9YyfcLb1+g7ty/DARh+pWKmUlSax7x0MwDeSAkm8HioxDle2LCM9i ickgzKZnP9IIjiNalVvLdHW5IEOnNVPiwWDDcOsU8qwQE/st8OfiHvZ+E7bDPB7dRBVw BnfgZxwNM0ZSfZcqrjt/4HZZPJEKcXl8n/H3NCxiLXxUrQ2VTH+DPZPrF9/IJcW1JD9i kKshL0BHqOPanrFkD5jsj/tdr3TOG/AOHuAS0OkmvaSf75np5BjrMgnKgfPrpa3R4KQ3 yA9g== X-Received: by 10.194.63.229 with SMTP id j5mr24704575wjs.79.1373668002979; Fri, 12 Jul 2013 15:26:42 -0700 (PDT) Received: from localhost.localdomain (129.130.72.86.rev.sfr.net. [86.72.130.129]) by mx.google.com with ESMTPSA id fb9sm5764287wid.2.2013.07.12.15.26.41 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 12 Jul 2013 15:26:42 -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 v2] scripts/config: use sed's POSIX interface Date: Sat, 13 Jul 2013 00:26:17 +0200 Message-Id: <1373667977-8034-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=-7.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 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