command: add read command
PATCH 1/1 command: add read command
this will allow us to read file content into a var
--- commands/Kconfig | 4 ++ commands/Makefile | 1 + commands/read.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 commands/read.c
commands/Kconfig +4 -0
@@ -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
commands/Makefile +1 -0
@@ -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
commands/read.c new file +126 -0
126 lines not on this page · raw message
Signature · 7 lines
1.8.4.rc3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 7 lines · Jean-Christophe PLAGNIOL-VILLARD
(quoted diff) +7
-0+static int do_read(int argc, char *argv[]) +{ + int opt; + int cont = 0; + int ret = -EINVAL; + char *var; + char *buf = xzalloc(2049);
Review
Quoted 10 lines
(quoted diff) +10
-0+ 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 <[email protected]>
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.
--- include/libbb.h | 2 ++ lib/libbb.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
include/libbb.h +2 -0
@@ -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 */
lib/libbb.c +42 -0
45 lines not on this page · raw message
Signature · 12 lines
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 [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 24 lines · Sascha Hauer
Quoted 7 lines · Jean-Christophe PLAGNIOL-VILLARD
(quoted diff) +7
-0+static int do_read(int argc, char *argv[]) +{ + int opt; + int cont = 0; + int ret = -EINVAL; + char *var; + char *buf = xzalloc(2049);Review
Quoted 10 lines
(quoted diff) +10
-0+ 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.
Quoted 2 lines
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.
Quoted 87 lines
Sascha
From
5d513270e46c5705262d79c66cc630e20d1a66d9Mon Sep 17 00:00:00 2001
From: Sascha Hauer <[email protected]>
Date: Sun, 22 Sep 2013 23:40:04 +0200
Subject: [PATCH] add function to read single line filesOften 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 <[email protected]>--- include/libbb.h | 2 ++ lib/libbb.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)include/libbb.h +2
-0@@ -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 */ lib/libbb.c +42
-0@@ -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);Signature · 7 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox