From patchwork Fri Apr 13 05:06:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 133341 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp348593ljb; Thu, 12 Apr 2018 22:15:12 -0700 (PDT) X-Google-Smtp-Source: AIpwx48AGDsqBrJJNuvXSOrSmvevRj1D0MAbYOz0DTSa4BynuQTYXOzEEzMwzOZX1J1H/dK1CS10 X-Received: by 10.99.113.86 with SMTP id b22mr298874pgn.321.1523596512175; Thu, 12 Apr 2018 22:15:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523596512; cv=none; d=google.com; s=arc-20160816; b=WmMrwh1qh1KhiPU7tvxieCKWgo1shgVXb7mF3cJgsCJyR2zGZvAgcuZdfYSh6Trwzt A0s+iK1fyZv0909ztwor+MUjlsCb3xNJ2glpE2a5R51VKT7NbwmaX+xhqA3rrszs/rB1 HQfWU026HQsaH+rpJYopFuzMWLoyojmZrwIU6oqkkc2uyhhMjnvoCRva3d1iGYafQ024 1HVAqlMn0XJMGkOZ4zocSxxiziXeUrs2KEZuAiBVROSsZGFt8nNYPKfDlO+7TuBQq1uG u0zYsREvTUobfcPhJSBqxexfX/VB2S4oUBjWxTzXlEEfd+uEb1HgOi6vbQ7PTCNEycs2 z1uQ== 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=hCQXgSfnKEzUmJomq6wiVg7pXMa6INgMpB99c5otC70=; b=rxbG3ZqVdQYsD3v1+C5s0hs0a07ZOlCDgdRgUCE7F2LW11u6qW4obXHuTG27i2DGUz 2auV5QQ00jDimnRf1dEPp+uqJtjtnoiDo4HKeg6xbLO2Ztgp8o+bjd9VX56tzAav4QG2 7OL2m0aMDnZ8gCK4mF0WG0uXoLjCM7prNvhTI+39P+8YXJfp9IOr7UqOWsEJxNF+OQKW vgajgeGwjsW9JqNcJkEmGrZYMmRqOrBuKFCy9NrrQpoEqcsOdqBbqIn0EVWCCCYKzi9p EcZ4nCHHw+CwLt4NGhyYfmNUmwZgQdOmYvkhG0U8+DW0w+978NHjke/CoY+SF4sY4i+M DYVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=2TUHcwwY; 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 g3-v6si4915708pld.513.2018.04.12.22.15.11; Thu, 12 Apr 2018 22:15:12 -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=2TUHcwwY; 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 S1753909AbeDMFOo (ORCPT + 29 others); Fri, 13 Apr 2018 01:14:44 -0400 Received: from conuserg-07.nifty.com ([210.131.2.74]:54464 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751010AbeDMFIP (ORCPT ); Fri, 13 Apr 2018 01:08:15 -0400 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-07.nifty.com with ESMTP id w3D56lg3029209; Fri, 13 Apr 2018 14:06:55 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com w3D56lg3029209 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1523596016; bh=hCQXgSfnKEzUmJomq6wiVg7pXMa6INgMpB99c5otC70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2TUHcwwYx/96q06HzbVXc3gsDfmcbm6MlAzQJxL+ENtPmkuuX3uwJJrn9COxtdqxj GPMt3KhzRHvW0gi++8MAhOPY9wH37cgTCrYmIPtDCu8+Qv8OUhlc39BctZkNK23y6Q V7hbY678plEgpzgvzaYrn3nc3YEaU6EQqwfEneveMBI1oG2/SqfJWKsVU5sIc0dHSW 6inpaVlxod/ov2TEb/gb1msmZCdz4Xg5F7mL2x25N2s6n4QPHMJVJtuFLLusTo+KzR /+zvmo8K15ZAU/yYkBU4VmNzU1UXYfiqE+Dec53atQirhgDDh4jKt0Z/+7GcWdml8i DYuC+/Th3/9dw== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Linus Torvalds , Sam Ravnborg , Ulf Magnusson , Nicholas Piggin , Kees Cook , Emese Revfy , x86@kernel.org, Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH 09/30] kconfig: add 'shell' built-in function Date: Fri, 13 Apr 2018 14:06:18 +0900 Message-Id: <1523595999-27433-10-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523595999-27433-1-git-send-email-yamada.masahiro@socionext.com> References: <1523595999-27433-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 v3: None Changes in v2: None scripts/kconfig/preprocess.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) -- 2.7.4 diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index e77cf7c..f4c606f 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c @@ -86,7 +86,73 @@ struct function { char *(*func)(int argc, char *argv[]); }; +/* + * Some commands treats commas verbatim. Concatenate arguments to get + * back the original input. The returned string must be freed when done. + */ +static char *join_args(int argc, char *argv[]) +{ + size_t len = 0; + char *out; + int i; + + for (i = 0; i < argc; i++) + len += strlen(argv[i]) + 1; + + out = xmalloc(len); + out[0] = 0; + for (i = 0; i < argc; i++) { + strcat(out, argv[i]); + if (i != argc - 1) + strcat(out, ","); + } + + return out; +} + +static char *do_shell(int argc, char *argv[]) +{ + FILE *p; + char buf[256]; + char *cmd; + size_t nread; + int i; + + cmd = join_args(argc, argv); + + p = popen(cmd, "r"); + if (!p) { + perror(cmd); + goto free; + } + + 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); + +free: + free(cmd); + + return xstrdup(buf); +} + static const struct function function_table[] = { + { .name = "shell", .func = do_shell }, }; static char *function_call(const char *name, int argc, char *argv[])