From patchwork Thu Aug 15 21:17:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yann E. MORIN" X-Patchwork-Id: 2845293 Return-Path: X-Original-To: patchwork-linux-kbuild@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 216149F271 for ; Thu, 15 Aug 2013 21:19:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2F01420317 for ; Thu, 15 Aug 2013 21:19:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3EC5420320 for ; Thu, 15 Aug 2013 21:19:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752706Ab3HOVSn (ORCPT ); Thu, 15 Aug 2013 17:18:43 -0400 Received: from mail-wg0-f42.google.com ([74.125.82.42]:40275 "EHLO mail-wg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751232Ab3HOVRm (ORCPT ); Thu, 15 Aug 2013 17:17:42 -0400 Received: by mail-wg0-f42.google.com with SMTP id j13so823319wgh.3 for ; Thu, 15 Aug 2013 14:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=xh5O9Rj8SC2fsvwF9LDAqd//i7v2hHhwvNQhDRU33Kc=; b=rpDB3q2XZJlMI0A40vGYBlMS03TBFRptvNww+z6wtxYOkJ/B1LMFu6TVZak3jYTgUm ALiDXN2VSSXqf5mYalEcxZzwPy+ZY1fbnAHDGq3FaJDh6Pw9pJ6zUdN3n6EMIlwDhmSm MPe+P7/PS77Q8bMWVa9ZgDXCR24yg66jXSLWS/0ROnq+MIEu/B+xUBBZXr6U0oy2xzIa qxDk8KNeB76W4689nSFEAVSf0roKs5Yh5xSGSU8VvqKl83ROzeX70fSG367Sl2BVOWBK VBcB7B0dXZEIyM1p/UmSGa2eCFidDSxl+vWlwcOoqU/evrm30ci2DYiA7v5bCsIfjDrz 7JFg== X-Received: by 10.194.173.163 with SMTP id bl3mr11641267wjc.10.1376601460744; Thu, 15 Aug 2013 14:17:40 -0700 (PDT) Received: from gourin.bzh.lan (ks3095497.kimsufi.com. [94.23.60.27]) by mx.google.com with ESMTPSA id fz8sm4914606wic.0.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 15 Aug 2013 14:17:40 -0700 (PDT) From: "Yann E. MORIN" To: linux-kbuild@vger.kernel.org, Michal Marek Cc: linux-kernel@vger.kernel.org, Clement Chauplannaz , "Yann E. MORIN" Subject: [PATCH 2/4] scripts/config: use sed's POSIX interface Date: Thu, 15 Aug 2013 23:17:31 +0200 Message-Id: <83e8b90e1d2cc5ff5d2443f2486c2d786a4997ce.1376600922.git.yann.morin.1998@free.fr> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: References: In-Reply-To: References: Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM,RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable 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 From: Clement Chauplannaz 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 Tested-by: "Yann E. MORIN" Reviewed-by: "Yann E. MORIN" Signed-off-by: Yann E. MORIN --- scripts/config | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/scripts/config b/scripts/config index 567120a..2283be2 100755 --- a/scripts/config +++ b/scripts/config @@ -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 @@ -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