From patchwork Thu May 17 06:16:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 136115 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp1884922lji; Wed, 16 May 2018 23:28:26 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoDP6NVLPC+0lqO4BEicY7HRXhS4wVZVOnOd52hxYRG9p30C4JUvmaw/5ajEqwCijiQSzUX X-Received: by 2002:a62:4544:: with SMTP id s65-v6mr4034374pfa.150.1526538506105; Wed, 16 May 2018 23:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526538506; cv=none; d=google.com; s=arc-20160816; b=RKtjuBe5EAw7Nm0u9pUZ6s3C13E/qRRgfdJl2c+2BXzu/V4MLd+1VcdJe/gSGAVzCR HFyp/yfWMw4KdMRKkHM2+VAwPc96un0Mq0WuF8MjS1cm0Nx62/2kaOPB+QGXennGb4eq qEhd4FHcNFB6gDd/Z/CVCA17P13LxqwvJ748W2YOSHL0TbqHhqU4W/49ozG3IRGY+NXU Es7m4RxCIa3gjKRvDmx2MF5sjiz4GjmMZfMze0F6pV7gw3ovfp9hRZNxr9dPU2uUFWMO vhkTikh5pAKHI1K7Jwm2150LEJgRgo3HAj4n7bd0rpGaxgdf71S0fNiKYgeHuafBAoRf tBfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=QV2ke3u7Gh2sqXLcu7CAM0IRxu2ux/+jy1skkNhYDBo=; b=owrhz7E3q8LZbwFOkVUof6mMovhwSnJ4kRYjXwdpc+81YHgOOB2sKTIlUyhZ+zHcY5 alNg5oQM8NXmyDUFe2kFpKhgfuHQSX9YnY6sNY/1h8zgInhMfqm/B5w2L/jhXpWHzj/X NyHVSCEpa5wCX4FQPOBX1N7DkHSkQg6dTA4jgUOnEPiT3SHIDL8Dz64jCRvGRqFVkxEC XVjN5ry65YTYoAW2r8QJ9J25JYhv7ym0NUoZqpqFBk+zzC4/HnMdqZI+stALijD0aBvx DS3ifOoMLYL5MsdaikSjQJ9IjriSKh+FJeAFLxc+c7wgx1LxquaU6BZNYCA6P/spcKXG mGiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=hOTe2hgG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5-v6si3369458pgn.134.2018.05.16.23.28.25; Wed, 16 May 2018 23:28:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=hOTe2hgG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752467AbeEQG2V (ORCPT + 29 others); Thu, 17 May 2018 02:28:21 -0400 Received: from conuserg-10.nifty.com ([210.131.2.77]:24196 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751061AbeEQGSv (ORCPT ); Thu, 17 May 2018 02:18:51 -0400 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id w4H6HbU6002841; Thu, 17 May 2018 15:17:46 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com w4H6HbU6002841 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1526537866; bh=QV2ke3u7Gh2sqXLcu7CAM0IRxu2ux/+jy1skkNhYDBo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hOTe2hgGUbWrwLw00MVSAbBUU0ZrD2HfHUDCU1vC7EUWc29b3RtWFleUNj4LySnuT mNDVKXcTObZ5qt8ngmKMG309raaaP8sI/78x1M7+8pTLIFnC71BbTKVhtum7rWGclU OJIOwZbyc6K2h02zUwGQQSDjgFSccuD+Wuaii1jljQLdG0/OoRJXk7abnZZTwO9U0W t74pHuors+I+QDxRC/JhmLH5TTzO4xpcY6poJ9Yj2kyrRESXxulil67se4mjbt0rZO wJB1DN9xSu/6ccv4IEFhZkKI+yHkyYHkQS0mq4YbSJ0ZX/lnaUHcrfY2rMht2hyYIg vVx2dd2j5Fj6Q== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Linus Torvalds , Sam Ravnborg , Ulf Magnusson , "Luis R . Rodriguez" , linux-kernel@vger.kernel.org, Nicholas Piggin , Kees Cook , Emese Revfy , x86@kernel.org, Masahiro Yamada Subject: [PATCH v4 08/31] kconfig: add 'shell' built-in function Date: Thu, 17 May 2018 15:16:47 +0900 Message-Id: <1526537830-22606-9-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1526537830-22606-1-git-send-email-yamada.masahiro@socionext.com> References: <1526537830-22606-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This accepts a single command to execute. It returns the standard output from it. [Example code] config HELLO string default "$(shell,echo hello world)" config Y def_bool $(shell,echo y) [Result] $ make -s alldefconfig && tail -n 2 .config CONFIG_HELLO="hello world" CONFIG_Y=y Caveat: Like environments, functions are expanded in the lexer. You cannot pass symbols to function arguments. This is a limitation to simplify the implementation. I want to avoid the dynamic function evaluation, which would introduce much more complexity. Signed-off-by: Masahiro Yamada --- Changes in v4: - Accept only one argument to simplify the implementation Changes in v3: None Changes in v2: None scripts/kconfig/preprocess.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) -- 2.7.4 diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index 5be28ec..d8c5f60 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c @@ -104,8 +104,49 @@ struct function { char *(*func)(int argc, char *argv[], int old_argc, char *old_argv[]); }; +static char *do_shell(int argc, char *argv[], int old_argc, char *old_argv[]) +{ + FILE *p; + char buf[256]; + char *cmd; + size_t nread; + int i; + + cmd = argv[0]; + + p = popen(cmd, "r"); + if (!p) { + perror(cmd); + exit(1); + } + + nread = fread(buf, 1, sizeof(buf), p); + if (nread == sizeof(buf)) + nread--; + + /* remove trailing new lines */ + while (buf[nread - 1] == '\n') + nread--; + + buf[nread] = 0; + + /* replace a new line with a space */ + for (i = 0; i < nread; i++) { + if (buf[i] == '\n') + buf[i] = ' '; + } + + if (pclose(p) == -1) { + perror(cmd); + exit(1); + } + + return xstrdup(buf); +} + static const struct function function_table[] = { /* Name MIN MAX EXP? Function */ + { "shell", 1, 1, true, do_shell }, }; #define FUNCTION_MAX_ARGS 16