mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH] Add readf command
Date: Fri, 14 Feb 2014 16:40:17 +0100	[thread overview]
Message-ID: <1392392417-27338-1-git-send-email-s.hauer@pengutronix.de> (raw)

The readf command is useful to read the content of a file into
a shell variable. It should be used for ascii content and thus
stops reading at all nonprintable characters including newline.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/Kconfig  |  6 ++++++
 commands/Makefile |  1 +
 commands/readf.c  | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 commands/readf.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 1e07b5b..352e8bf 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -57,6 +57,12 @@ config CMD_READLINE
 	tristate
 	prompt "readline"
 
+config CMD_READF
+	tristate
+	prompt "readf"
+	help
+	  The readf command is used to read a files content into a shell variable.
+
 config CMD_LET
 	tristate
 	prompt "let"
diff --git a/commands/Makefile b/commands/Makefile
index 58d27fa..91ec0e9 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -93,3 +93,4 @@ obj-$(CONFIG_CMD_MIITOOL)	+= miitool.o
 obj-$(CONFIG_CMD_DETECT)	+= detect.o
 obj-$(CONFIG_CMD_BOOT)		+= boot.o
 obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
+obj-$(CONFIG_CMD_READF)		+= readf.o
diff --git a/commands/readf.c b/commands/readf.c
new file mode 100644
index 0000000..6314c7e
--- /dev/null
+++ b/commands/readf.c
@@ -0,0 +1,63 @@
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <linux/stat.h>
+#include <linux/ctype.h>
+#include <environment.h>
+
+static int do_readf(int argc, char *argv[])
+{
+	unsigned char *buf = NULL, *val;
+	char *variable, *filename;
+	struct stat s;
+	size_t size;
+	int ret, i;
+
+	if (argc != 3)
+		return COMMAND_ERROR_USAGE;
+
+	filename = argv[1];
+	variable = argv[2];
+
+	ret = stat(filename, &s);
+	if (ret)
+		goto out;
+
+	if (s.st_size > 1024) {
+		ret = -EFBIG;
+		goto out;
+	}
+
+	buf = read_file(filename, &size);
+	if (!buf)
+		goto out;
+
+	for (i = 0; i < size; i++) {
+		if (!isprint(buf[i])) {
+			buf[i] = '\0';
+			break;
+		}
+	}
+
+	val = strim(buf);
+
+	ret = setenv(variable, val);
+out:
+	free(buf);
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(readf)
+BAREBOX_CMD_HELP_USAGE("readf <file> <variable>\n")
+BAREBOX_CMD_HELP_SHORT("Read a single line of a file into a shell variable. Leading and trailing whitespaces\n")
+BAREBOX_CMD_HELP_SHORT("are removed, nonvisible characters are stripped. Input is limited to 1024\n")
+BAREBOX_CMD_HELP_SHORT("characters.\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(readf)
+	.cmd		= do_readf,
+	.usage		= "read file into variable",
+	BAREBOX_CMD_HELP(cmd_readf_help)
+BAREBOX_CMD_END
-- 
1.8.5.3


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

                 reply	other threads:[~2014-02-14 15:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392392417-27338-1-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox