Re: [U-Boot] [PATCH V4 09/11] tools: add parse_helper file
>-Original Message- >From: Troy Kisky [mailto:troy.ki...@boundarydevices.com] >Sent: Wednesday, November 28, 2012 9:32 AM >To: sba...@denx.de >Cc: dirk.be...@googlemail.com; u-boot@lists.denx.de; Liu Hui-R64343; >feste...@gmail.com; Troy Kisky >Subject: [PATCH V4 09/11] tools: add parse_helper file > >This file can help you parse >configuration files. One line is enough. :) > >Signed-off-by: Troy Kisky Acked-by: Jason Liu >--- > tools/Makefile |2 + > tools/parse_helper.c | 173 >++ > tools/parse_helper.h | 28 > 3 files changed, 203 insertions(+) > create mode 100644 tools/parse_helper.c create mode 100644 >tools/parse_helper.h > >diff --git a/tools/Makefile b/tools/Makefile index 686840a..db3b247 100644 >--- a/tools/Makefile >+++ b/tools/Makefile >@@ -94,6 +94,7 @@ NOPED_OBJ_FILES-y += aisimage.o NOPED_OBJ_FILES-y >+= kwbimage.o NOPED_OBJ_FILES-y += pblimage.o NOPED_OBJ_FILES-y += >imximage.o >+NOPED_OBJ_FILES-y += parse_helper.o > NOPED_OBJ_FILES-y += omapimage.o > NOPED_OBJ_FILES-y += mkenvimage.o > NOPED_OBJ_FILES-y += mkimage.o >@@ -208,6 +209,7 @@ $(obj)mkimage$(SFX): $(obj)aisimage.o \ > $(obj)fit_image.o \ > $(obj)image.o \ > $(obj)imximage.o \ >+ $(obj)parse_helper.o \ > $(obj)kwbimage.o \ > $(obj)pblimage.o \ > $(obj)md5.o \ >diff --git a/tools/parse_helper.c b/tools/parse_helper.c new file mode 100644 >index 000..0a5c5f6 >--- /dev/null >+++ b/tools/parse_helper.c >@@ -0,0 +1,173 @@ >+/* >+ * (C) Copyright 20012 Boundary Devices Inc, >+troy.ki...@boundarydevices.com >+ * >+ * Licensed under the GPL-2 or later. >+ */ >+ >+/* Required to obtain the getline prototype from stdio.h */ #define >+_GNU_SOURCE >+ >+#include "mkimage.h" >+#include >+#include "parse_helper.h" >+ >+int ph_open(struct parse_helper *ph, char *filename) { >+ ph->line = NULL; >+ ph->len = 0; >+ ph->fd = fopen(filename, "r"); >+ ph->lineno = 0; >+ ph->cmd_started = 0; >+ ph->filename = filename; >+ ph->p = NULL; >+ return (!ph->fd) ? -1 : 0; >+} >+ >+void ph_close(struct parse_helper *ph) >+{ >+ fclose(ph->fd); >+ ph->fd = NULL; >+} >+ >+int ph_skip_separators(struct parse_helper *ph) { >+ int line_no = ph->lineno; >+ char *p = ph->p; >+ >+ for (;;) { >+ char c; >+ if (!p) { >+ if (getline(&ph->line, &ph->len, ph->fd) <= 0) >+ return -1; >+ ph->lineno++; >+ p = ph->line; >+ if (ph->cmd_started) { >+ fprintf(stderr, "warning: continuing command >on" >+ " next line, line %s[%d](%s)\n", >+ ph->filename, ph->lineno, p); >+ } >+ } >+ c = *p; >+ if ((c == ' ') || (c == '\t')) { >+ p++; >+ continue; >+ } >+ /* Drop all text starting with '#' as comments */ >+ if ((c == '#') || (c == '\r') || (c == '\n') >+ || !c) { >+ p = NULL; >+ continue; >+ } >+ if (c == ';') { >+ if (ph->cmd_started) { >+ fprintf(stderr, "Error: command not " >+ "finished:%s[%d](%s)\n", >+ ph->filename, ph->lineno, p); >+ exit(EXIT_FAILURE); >+ } >+ p++; >+ continue; >+ } >+ if (!ph->cmd_started && line_no == ph->lineno) { >+ fprintf(stderr, "Error: extra data at end " >+ "of line %s[%d](%s)\n", >+ ph->filename, ph->lineno, p); >+ exit(EXIT_FAILURE); >+ } >+ ph->p = p; >+ return 0; >+ } >+} >+ >+int ph_skip_comma(struct parse_helper *ph) { >+ char *p = ph->p; >+ >+ for (;;) { >+ char c = *p++; >+ if ((c == '#') || (c == '\r') || (c == '\n') || !c) >+ return 0; >+ if (c == ',') { >+ ph->p = p; >+ ph_skip_separators(ph); >+ return 1; >+ } >+ if ((c != ' ') && (c == '\t')) >+ return 0; >+ } >+} >+ >+int ph_get_value(struct parse_helper *ph, uint32_t *pval) { >+ char *endptr; >+ uint32_t value; >+ >+ if (ph_skip_separators(ph)) >+ return -1; >+ errno
Re: [U-Boot] [PATCH V4 09/11] tools: add parse_helper file
Dear Troy Kisky, In message <1354066303-29762-10-git-send-email-troy.ki...@boundarydevices.com> you wrote: > This file can help you parse > configuration files. If this is intended to be useful, you need to add documentation, for example what your definition of "configuration files" is, and which data format[s] is/are supported, etc. As is, this cannot be reused. Best regards, Wolfgang Denk -- DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de Wenn das dann in die Hose geht, nehme ich es auf meine Kappe. -- Rudi Völler, 15. Nov 2003 ___ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
[U-Boot] [PATCH V4 09/11] tools: add parse_helper file
This file can help you parse configuration files. Signed-off-by: Troy Kisky --- tools/Makefile |2 + tools/parse_helper.c | 173 ++ tools/parse_helper.h | 28 3 files changed, 203 insertions(+) create mode 100644 tools/parse_helper.c create mode 100644 tools/parse_helper.h diff --git a/tools/Makefile b/tools/Makefile index 686840a..db3b247 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -94,6 +94,7 @@ NOPED_OBJ_FILES-y += aisimage.o NOPED_OBJ_FILES-y += kwbimage.o NOPED_OBJ_FILES-y += pblimage.o NOPED_OBJ_FILES-y += imximage.o +NOPED_OBJ_FILES-y += parse_helper.o NOPED_OBJ_FILES-y += omapimage.o NOPED_OBJ_FILES-y += mkenvimage.o NOPED_OBJ_FILES-y += mkimage.o @@ -208,6 +209,7 @@ $(obj)mkimage$(SFX):$(obj)aisimage.o \ $(obj)fit_image.o \ $(obj)image.o \ $(obj)imximage.o \ + $(obj)parse_helper.o \ $(obj)kwbimage.o \ $(obj)pblimage.o \ $(obj)md5.o \ diff --git a/tools/parse_helper.c b/tools/parse_helper.c new file mode 100644 index 000..0a5c5f6 --- /dev/null +++ b/tools/parse_helper.c @@ -0,0 +1,173 @@ +/* + * (C) Copyright 20012 Boundary Devices Inc, troy.ki...@boundarydevices.com + * + * Licensed under the GPL-2 or later. + */ + +/* Required to obtain the getline prototype from stdio.h */ +#define _GNU_SOURCE + +#include "mkimage.h" +#include +#include "parse_helper.h" + +int ph_open(struct parse_helper *ph, char *filename) +{ + ph->line = NULL; + ph->len = 0; + ph->fd = fopen(filename, "r"); + ph->lineno = 0; + ph->cmd_started = 0; + ph->filename = filename; + ph->p = NULL; + return (!ph->fd) ? -1 : 0; +} + +void ph_close(struct parse_helper *ph) +{ + fclose(ph->fd); + ph->fd = NULL; +} + +int ph_skip_separators(struct parse_helper *ph) +{ + int line_no = ph->lineno; + char *p = ph->p; + + for (;;) { + char c; + if (!p) { + if (getline(&ph->line, &ph->len, ph->fd) <= 0) + return -1; + ph->lineno++; + p = ph->line; + if (ph->cmd_started) { + fprintf(stderr, "warning: continuing command on" + " next line, line %s[%d](%s)\n", + ph->filename, ph->lineno, p); + } + } + c = *p; + if ((c == ' ') || (c == '\t')) { + p++; + continue; + } + /* Drop all text starting with '#' as comments */ + if ((c == '#') || (c == '\r') || (c == '\n') + || !c) { + p = NULL; + continue; + } + if (c == ';') { + if (ph->cmd_started) { + fprintf(stderr, "Error: command not " + "finished:%s[%d](%s)\n", + ph->filename, ph->lineno, p); + exit(EXIT_FAILURE); + } + p++; + continue; + } + if (!ph->cmd_started && line_no == ph->lineno) { + fprintf(stderr, "Error: extra data at end " + "of line %s[%d](%s)\n", + ph->filename, ph->lineno, p); + exit(EXIT_FAILURE); + } + ph->p = p; + return 0; + } +} + +int ph_skip_comma(struct parse_helper *ph) +{ + char *p = ph->p; + + for (;;) { + char c = *p++; + if ((c == '#') || (c == '\r') || (c == '\n') || !c) + return 0; + if (c == ',') { + ph->p = p; + ph_skip_separators(ph); + return 1; + } + if ((c != ' ') && (c == '\t')) + return 0; + } +} + +int ph_get_value(struct parse_helper *ph, uint32_t *pval) +{ + char *endptr; + uint32_t value; + + if (ph_skip_separators(ph)) + return -1; + errno = 0; + value = strtoul(ph->p, &endptr, 16); + if (errno || (ph->p == endptr)) + return -1; + *pval = value; + ph->p = endptr; + return 0; +} + +/* + * Comma separator optional + * Input: + * ph - input source + * data - array to fill in + * cnt - exact number of elements to parse + * Return: number of elements parsed, or error + */ +int ph_get_array(struct parse_helpe