mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] commands: add lodvar - load variable from file content
@ 2014-09-06 12:25 Sebastian Block
  2014-09-06 12:37 ` Alexander Aring
  0 siblings, 1 reply; 4+ messages in thread
From: Sebastian Block @ 2014-09-06 12:25 UTC (permalink / raw)
  To: barebox

This adds a command to load a variable from file content.
It is a work-a-round for "var=$(cat file)".

Signed-off-by: Sebastian Block <basti@linux-source.de>
---
 commands/Kconfig   |  7 +++++
 commands/Makefile  |  1 +
 commands/loadvar.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 commands/loadvar.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 3a49baf..c6b4e03 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -752,6 +752,13 @@ config CMD_SAVEENV
 	  /dev/env0. Note that envfs can only handle files, directories are being
 	  skipped silently.
 
+config CMD_LOADVAR
+	tristate
+	prompt "loadvar"
+	help
+	  Load variables from file contents.
+	  Usage: loadvar VAR FILE
+
 # end Environment commands
 endmenu
 
diff --git a/commands/Makefile b/commands/Makefile
index 52b6137..6553154 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -103,3 +103,4 @@ obj-$(CONFIG_CMD_LSPCI)		+= lspci.o
 obj-$(CONFIG_CMD_IMD)		+= imd.o
 obj-$(CONFIG_CMD_HWCLOCK)	+= hwclock.o
 obj-$(CONFIG_CMD_USBGADGET)	+= usbgadget.o
+obj-$(CONFIG_CMD_LOADVAR)	+= loadvar.o
diff --git a/commands/loadvar.c b/commands/loadvar.c
new file mode 100644
index 0000000..f0b38d2
--- /dev/null
+++ b/commands/loadvar.c
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014 Sebastian Block <basti@linux-source.de>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/ctype.h>
+#include <linux/stat.h>
+#include <errno.h>
+#include <xfuncs.h>
+#include <malloc.h>
+#include <environment.h>
+
+static int do_loadvar(int argc, char *argv[])
+{
+	int ret;
+	int fd;
+	char *buf;
+	int err = 0;
+	struct stat s;
+
+	if (argc < 3) {
+		perror("loadvar");
+		return 1;
+	}
+
+	ret = stat(argv[2], &s);
+	if (ret) {
+		perror("loadvar - could not stat file");
+		return 1;
+	}
+
+	buf = xmalloc(s.st_size + 1);
+
+	fd = open(argv[2], O_RDONLY);
+	if (fd < 0) {
+		err = 1;
+		printf("could not open %s: %s\n", argv[2], errno_str());
+		goto out;
+	}
+
+	ret = read(fd, buf, s.st_size);
+	if (ret < 0) {
+		err = 1;
+		printf("could not read %s: %s\n", argv[2], errno_str());
+		goto out;
+	}
+	close(fd);
+
+	buf[s.st_size] = '\0'; /* add string termination */
+	
+	ret = setenv(argv[1], buf);
+	if (ret) {
+		printf("could not set var %s with value %s\n", argv[1], buf);
+		err = 1;
+		goto out;
+	}
+out:
+	free(buf);
+
+	return err;
+}
+
+BAREBOX_CMD_HELP_START(loadvar)
+BAREBOX_CMD_HELP_TEXT("Load content of FILE into VAR.")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(loadvar)
+	.cmd		= do_loadvar,
+	BAREBOX_CMD_DESC("load file content to var")
+	BAREBOX_CMD_OPTS("VAR FILE")
+	BAREBOX_CMD_GROUP(CMD_GRP_ENV)
+	BAREBOX_CMD_HELP(cmd_loadvar_help)
+BAREBOX_CMD_END
+
-- 
1.9.1

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] commands: add lodvar - load variable from file content
  2014-09-06 12:25 [PATCH] commands: add lodvar - load variable from file content Sebastian Block
@ 2014-09-06 12:37 ` Alexander Aring
  2014-09-06 13:03   ` Sebastian Block
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Aring @ 2014-09-06 12:37 UTC (permalink / raw)
  To: Sebastian Block; +Cc: barebox

Hi Sebastian,

On Sat, Sep 06, 2014 at 02:25:08PM +0200, Sebastian Block wrote:
> This adds a command to load a variable from file content.
> It is a work-a-round for "var=$(cat file)".
> 

doesn't commands/readf.c do similar things?

- Alex

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] commands: add lodvar - load variable from file content
  2014-09-06 12:37 ` Alexander Aring
@ 2014-09-06 13:03   ` Sebastian Block
  2014-09-06 13:39     ` Alexander Aring
  0 siblings, 1 reply; 4+ messages in thread
From: Sebastian Block @ 2014-09-06 13:03 UTC (permalink / raw)
  To: Alexander Aring; +Cc: barebox

Hi Alex,

> On Sat, Sep 06, 2014 at 02:25:08PM +0200, Sebastian Block wrote:
>> This adds a command to load a variable from file content.
>> It is a work-a-round for "var=$(cat file)".
>>
> 
> doesn't commands/readf.c do similar things?
> 

you are right. Please forget this patch.

Sebastian

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] commands: add lodvar - load variable from file content
  2014-09-06 13:03   ` Sebastian Block
@ 2014-09-06 13:39     ` Alexander Aring
  0 siblings, 0 replies; 4+ messages in thread
From: Alexander Aring @ 2014-09-06 13:39 UTC (permalink / raw)
  To: Sebastian Block; +Cc: barebox

On Sat, Sep 06, 2014 at 03:03:07PM +0200, Sebastian Block wrote:
> Hi Alex,
> 
> > On Sat, Sep 06, 2014 at 02:25:08PM +0200, Sebastian Block wrote:
> >> This adds a command to load a variable from file content.
> >> It is a work-a-round for "var=$(cat file)".
> >>
> > 
> > doesn't commands/readf.c do similar things?
> > 
> 
> you are right. Please forget this patch.
> 

Please don't interpret this as negative comment, you are always welcome
to introduce new commands. :-)

- Alex

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-09-06 13:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-06 12:25 [PATCH] commands: add lodvar - load variable from file content Sebastian Block
2014-09-06 12:37 ` Alexander Aring
2014-09-06 13:03   ` Sebastian Block
2014-09-06 13:39     ` Alexander Aring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox