[PATCH 11/23] kconfig: add 'shell-stdout' function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is the second built-in function, which retrieves the first line
of stdout from the given shell command.

Example code:

  config CC_IS_GCC
          bool
          default $(shell $CC --version | grep -q gcc)

  config GCC_VERSION
          int
          default $(shell-stdout $srctree/scripts/gcc-version.sh $CC | sed 's/^0*//') if CC_IS_GCC
          default 0

Result:

  $ make -s alldefconfig && tail -n 2 .config
  CONFIG_CC_IS_GCC=y
  CONFIG_GCC_VERSION=504

  $ make CC=clang -s alldefconfig && tail -n 2 .config
  # CONFIG_CC_IS_GCC is not set
  CONFIG_GCC_VERSION=0

By the way, function calls can be nested, so the following works.

Example code:

  config FOO
          bool
          default $(shell $(shell-stdout echo $COMMAND_IN_CAPITAL | tr [A-Z] [a-z]))

Result:
  $ make -s COMMAND=TRUE alldefconfig && tail -n 1 .config
  CONFIG_FOO=y
  $ make -s COMMAND=FALSE alldefconfig && tail -n 1 .config
  # CONFIG_FOO is not set

Signed-off-by: Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx>
---

 scripts/kconfig/function.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/scripts/kconfig/function.c b/scripts/kconfig/function.c
index f7f154d..266f4ec 100644
--- a/scripts/kconfig/function.c
+++ b/scripts/kconfig/function.c
@@ -189,10 +189,56 @@ static char *do_shell(struct function *f, int argc, char *argv[])
 	return xstrdup(ret == 0 ? "y" : "n");
 }
 
+static char *do_shell_stdout(struct function *f, int argc, char *argv[])
+{
+	static const char *pre = "(";
+	static const char *post = ") 2>/dev/null";
+	FILE *p;
+	char buf[256];
+	char *cmd;
+	int ret;
+
+	if (argc != 2)
+		return NULL;
+
+	/*
+	 * Surround the command with ( ) in case it is piped commands.
+	 * Also, redirect stderr to /dev/null.
+	 */
+	cmd = xmalloc(strlen(pre) + strlen(argv[1]) + strlen(post) + 1);
+	strcpy(cmd, pre);
+	strcat(cmd, argv[1]);
+	strcat(cmd, post);
+
+	p = popen(cmd, "r");
+	if (!p) {
+		perror(cmd);
+		goto free;
+	}
+	if (fgets(buf, sizeof(buf), p)) {
+		size_t len = strlen(buf);
+
+		if (buf[len - 1] == '\n')
+			buf[len - 1] = '\0';
+	} else {
+		buf[0] = '\0';
+	}
+
+	ret = pclose(p);
+	if (ret == -1)
+		perror(cmd);
+
+free:
+	free(cmd);
+
+	return xstrdup(buf);
+}
+
 void func_init(void)
 {
 	/* register built-in functions */
 	func_add("shell", do_shell, NULL);
+	func_add("shell-stdout", do_shell_stdout, NULL);
 }
 
 void func_exit(void)
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux&nblp;USB Development]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite Secrets]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux