mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/1] command: add read command
@ 2013-09-26  5:45 Jean-Christophe PLAGNIOL-VILLARD
  2013-09-27  8:15 ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2013-09-26  5:45 UTC (permalink / raw)
  To: barebox

this will allow us to read file content into a var

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig  |   4 ++
 commands/Makefile |   1 +
 commands/read.c   | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 commands/read.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 55e46a0..cf17d2c 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -64,6 +64,10 @@ config CMD_LET
 	  the 'let' command is used for arithmetics. It works like the corresponding
 	  Unix shell command.
 
+config CMD_READ
+	tristate
+	prompt "read"
+
 config CMD_TRUE
 	tristate
 	default y
diff --git a/commands/Makefile b/commands/Makefile
index 6acffc8..64dbe99 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -91,3 +91,4 @@ obj-$(CONFIG_CMD_FILETYPE)	+= filetype.o
 obj-$(CONFIG_CMD_BAREBOX_UPDATE)+= barebox-update.o
 obj-$(CONFIG_CMD_MIITOOL)	+= miitool.o
 obj-$(CONFIG_CMD_DETECT)	+= detect.o
+obj-$(CONFIG_CMD_READ)		+= read.o
diff --git a/commands/read.c b/commands/read.c
new file mode 100644
index 0000000..8ba0dbc
--- /dev/null
+++ b/commands/read.c
@@ -0,0 +1,126 @@
+/*
+ * (C) Copyright 2013 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * Under GPLv2 only
+ */
+
+#include <common.h>
+#include <command.h>
+#include <getopt.h>
+#include <errno.h>
+#include <linux/err.h>
+#include <fs.h>
+#include <environment.h>
+#include <malloc.h>
+#include <fcntl.h>
+
+static char *file;
+static int fd = -1;
+static size_t offset = 0;
+
+static void read_close(void)
+{
+	close(fd);
+	fd = -1;
+	free(file);
+	file = NULL;
+}
+
+static int do_read(int argc, char *argv[])
+{
+	int opt;
+	int cont = 0;
+	int ret = -EINVAL;
+	char *var;
+	char *buf = xzalloc(2049);
+	char *filename = NULL;
+
+	if (!argc)
+		return COMMAND_ERROR_USAGE;
+
+	while((opt = getopt(argc, argv, "cf:")) > 0) {
+		switch(opt) {
+		case 'c':
+			cont = 1;
+			break;
+		case 'f':
+			filename = optarg;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	if (optind == argc)
+		return COMMAND_ERROR_USAGE;
+
+	if (!file && !filename)
+		return COMMAND_ERROR_USAGE;
+
+	if (file && filename) {
+		if (strcmp(file, filename)) {
+			read_close();
+			file = xstrdup(filename);
+		}
+	} else if (!file && filename) {
+		file = xstrdup(filename);
+	}
+
+	var = argv[optind];
+
+	if (!cont || fd < 0) {
+		if (!file) {
+			fd = -EIO;
+		} else {
+			fd = open(file, O_RDONLY);
+			offset = 0;
+		}
+	}
+
+	if (fd < 0)
+		return 1;
+
+	ret = read(fd, buf, 2048);
+	if (ret < 0)
+		goto err;
+
+	if (ret) {
+		char *tmp = strchr(buf, '\n');
+		if (tmp) {
+			*tmp = '\0';
+			lseek(fd, offset, SEEK_SET);
+		}
+		setenv(var, buf);
+		offset += strlen(buf);
+		export(var);
+	}
+
+	if (!cont)
+		read_close();
+
+	free(buf);
+
+	return 0;
+
+err:
+	free(buf);
+	read_close();
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(read)
+BAREBOX_CMD_HELP_USAGE("read [-c] -f file va\n")
+BAREBOX_CMD_HELP_SHORT("read a ligne from a file into a var\n")
+BAREBOX_CMD_HELP_OPT  ("-c", "continue to read the same file.\n")
+BAREBOX_CMD_HELP_OPT  ("-f file", "file to work on.\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(read)
+	.cmd		= do_read,
+	.usage		= "read",
+	BAREBOX_CMD_HELP(cmd_read_help)
+BAREBOX_CMD_END
-- 
1.8.4.rc3


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

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

* Re: [PATCH 1/1] command: add read command
  2013-09-26  5:45 [PATCH 1/1] command: add read command Jean-Christophe PLAGNIOL-VILLARD
@ 2013-09-27  8:15 ` Sascha Hauer
  2013-09-27  8:21   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2013-09-27  8:15 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Thu, Sep 26, 2013 at 07:45:56AM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> +static int do_read(int argc, char *argv[])
> +{
> +	int opt;
> +	int cont = 0;
> +	int ret = -EINVAL;
> +	char *var;
> +	char *buf = xzalloc(2049);
> +	char *filename = NULL;
> +
> +	if (!argc)
> +		return COMMAND_ERROR_USAGE;
> +
> +	while((opt = getopt(argc, argv, "cf:")) > 0) {
> +		switch(opt) {
> +		case 'c':
> +			cont = 1;
> +			break;

Do you really need this continue mode? It makes this stuff look rather
flawy. For example one must know that this command is not reentrant. Not
that it's likely that someone parses a file while parsing another file,
but doing so would lead to strange results and is hard to fix.

BTW I had to do very similar stuff recently, see attached what I came up
with.

Sascha


From 5d513270e46c5705262d79c66cc630e20d1a66d9 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer@pengutronix.de>
Date: Sun, 22 Sep 2013 23:40:04 +0200
Subject: [PATCH] add function to read single line files

Often small files are used to store the value of a variable. This
adds a function to easily read such a variable.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/libbb.h |  2 ++
 lib/libbb.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/libbb.h b/include/libbb.h
index 47b2e08..2fe710c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -35,4 +35,6 @@ char *simple_itoa(unsigned int i);
 int write_full(int fd, void *buf, size_t size);
 int read_full(int fd, void *buf, size_t size);
 
+char *read_file_line(const char *fmt, ...);
+
 #endif /* __LIBBB_H */
diff --git a/lib/libbb.c b/lib/libbb.c
index e0d7481..200629e 100644
--- a/lib/libbb.c
+++ b/lib/libbb.c
@@ -176,3 +176,45 @@ int read_full(int fd, void *buf, size_t size)
 	return insize;
 }
 EXPORT_SYMBOL(read_full);
+
+/*
+ * read_file_line - read a line from a file
+ *
+ * Used to compose a filename from a printf format and to read a line from this
+ * file. All leading and trailing whitespaces (including line endings) are
+ * removed. The returned buffer must be freed with free(). This function is
+ * supposed for reading variable like content into a buffer, so files > 1024
+ * bytes are ignored.
+ */
+char *read_file_line(const char *fmt, ...)
+{
+	va_list args;
+	char *filename;
+	char *buf, *line = NULL;
+	int size, ret;
+	struct stat s;
+
+	va_start(args, fmt);
+	filename = asprintf(fmt, args);
+	va_end(args);
+
+	ret = stat(filename, &s);
+	if (ret)
+		goto out;
+
+	if (s.st_size > 1024)
+		goto out;
+
+	buf = read_file(filename, &size);
+	if (!buf)
+		goto out;
+
+	line = strim(buf);
+
+	line = xstrdup(line);
+	free(buf);
+out:
+	free(filename);
+	return line;
+}
+EXPORT_SYMBOL_GPL(read_file_line);
-- 
1.8.4.rc3

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

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

* Re: [PATCH 1/1] command: add read command
  2013-09-27  8:15 ` Sascha Hauer
@ 2013-09-27  8:21   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2013-09-27  8:21 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 10:15 Fri 27 Sep     , Sascha Hauer wrote:
> On Thu, Sep 26, 2013 at 07:45:56AM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > +static int do_read(int argc, char *argv[])
> > +{
> > +	int opt;
> > +	int cont = 0;
> > +	int ret = -EINVAL;
> > +	char *var;
> > +	char *buf = xzalloc(2049);
> > +	char *filename = NULL;
> > +
> > +	if (!argc)
> > +		return COMMAND_ERROR_USAGE;
> > +
> > +	while((opt = getopt(argc, argv, "cf:")) > 0) {
> > +		switch(opt) {
> > +		case 'c':
> > +			cont = 1;
> > +			break;
> 
> Do you really need this continue mode? It makes this stuff look rather
> flawy. For example one must know that this command is not reentrant. Not
> that it's likely that someone parses a file while parsing another file,
> but doing so would lead to strange results and is hard to fix.
> 
> BTW I had to do very similar stuff recently, see attached what I came up
> with.

I agree on the issue but as we do not have the | on the shell and I do need to
read multiple  line on a project. SO the only way would be to use a list of
open files

Best Regards,
J.
> 
> Sascha
> 
> 
> From 5d513270e46c5705262d79c66cc630e20d1a66d9 Mon Sep 17 00:00:00 2001
> From: Sascha Hauer <s.hauer@pengutronix.de>
> Date: Sun, 22 Sep 2013 23:40:04 +0200
> Subject: [PATCH] add function to read single line files
> 
> Often small files are used to store the value of a variable. This
> adds a function to easily read such a variable.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  include/libbb.h |  2 ++
>  lib/libbb.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/include/libbb.h b/include/libbb.h
> index 47b2e08..2fe710c 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -35,4 +35,6 @@ char *simple_itoa(unsigned int i);
>  int write_full(int fd, void *buf, size_t size);
>  int read_full(int fd, void *buf, size_t size);
>  
> +char *read_file_line(const char *fmt, ...);
> +
>  #endif /* __LIBBB_H */
> diff --git a/lib/libbb.c b/lib/libbb.c
> index e0d7481..200629e 100644
> --- a/lib/libbb.c
> +++ b/lib/libbb.c
> @@ -176,3 +176,45 @@ int read_full(int fd, void *buf, size_t size)
>  	return insize;
>  }
>  EXPORT_SYMBOL(read_full);
> +
> +/*
> + * read_file_line - read a line from a file
> + *
> + * Used to compose a filename from a printf format and to read a line from this
> + * file. All leading and trailing whitespaces (including line endings) are
> + * removed. The returned buffer must be freed with free(). This function is
> + * supposed for reading variable like content into a buffer, so files > 1024
> + * bytes are ignored.
> + */
> +char *read_file_line(const char *fmt, ...)
> +{
> +	va_list args;
> +	char *filename;
> +	char *buf, *line = NULL;
> +	int size, ret;
> +	struct stat s;
> +
> +	va_start(args, fmt);
> +	filename = asprintf(fmt, args);
> +	va_end(args);
> +
> +	ret = stat(filename, &s);
> +	if (ret)
> +		goto out;
> +
> +	if (s.st_size > 1024)
> +		goto out;
> +
> +	buf = read_file(filename, &size);
> +	if (!buf)
> +		goto out;
> +
> +	line = strim(buf);
> +
> +	line = xstrdup(line);
> +	free(buf);
> +out:
> +	free(filename);
> +	return line;
> +}
> +EXPORT_SYMBOL_GPL(read_file_line);
> -- 
> 1.8.4.rc3
> 
> -- 
> Pengutronix e.K.                           |                             |
> Industrial Linux Solutions                 | http://www.pengutronix.de/  |
> Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

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

end of thread, other threads:[~2013-09-27  8:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-26  5:45 [PATCH 1/1] command: add read command Jean-Christophe PLAGNIOL-VILLARD
2013-09-27  8:15 ` Sascha Hauer
2013-09-27  8:21   ` Jean-Christophe PLAGNIOL-VILLARD

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