The first function allows to create parser_t objects for files
that are already open.

The second frees the parser_t instance without closing the file.

With these functions it is now possible to feed config files to
applets via stdin.

Signed-off-by: Bartosz Golaszewski <bartekg...@gmail.com>
---
 include/libbb.h      |  3 +++
 libbb/parse_config.c | 27 +++++++++++++++++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 8deddc8..4da308a 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1313,11 +1313,14 @@ typedef struct parser_t {
 } parser_t;
 parser_t* config_open(const char *filename) FAST_FUNC;
 parser_t* config_open2(const char *filename, FILE* FAST_FUNC 
(*fopen_func)(const char *path)) FAST_FUNC;
+parser_t* config_from_fp(FILE *fp) FAST_FUNC;
 /* delims[0] is a comment char (use '\0' to disable), the rest are token 
delimiters */
 int config_read(parser_t *parser, char **tokens, unsigned flags, const char 
*delims) FAST_FUNC;
 #define config_read(parser, tokens, max, min, str, flags) \
        config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 
0xFF)), str)
 void config_close(parser_t *parser) FAST_FUNC;
+/* Free all memory used by parser, but don't close the file. */
+void config_free(parser_t *parser) FAST_FUNC;
 
 /* Concatenate path and filename to new allocated buffer.
  * Add "/" only as needed (no duplicate "//" are produced).
diff --git a/libbb/parse_config.c b/libbb/parse_config.c
index 4084397..a3c3c5d 100644
--- a/libbb/parse_config.c
+++ b/libbb/parse_config.c
@@ -65,17 +65,25 @@ int parse_main(int argc UNUSED_PARAM, char **argv)
 }
 #endif
 
+parser_t* FAST_FUNC config_from_fp(FILE *fp)
+{
+       parser_t *parser;
+
+       parser = xzalloc(sizeof(*parser));
+       parser->fp = fp;
+
+       return parser;
+}
+
 parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC 
(*fopen_func)(const char *path))
 {
        FILE* fp;
-       parser_t *parser;
 
        fp = fopen_func(filename);
        if (!fp)
                return NULL;
-       parser = xzalloc(sizeof(*parser));
-       parser->fp = fp;
-       return parser;
+
+       return config_from_fp(fp);
 }
 
 parser_t* FAST_FUNC config_open(const char *filename)
@@ -83,18 +91,25 @@ parser_t* FAST_FUNC config_open(const char *filename)
        return config_open2(filename, fopen_or_warn_stdin);
 }
 
-void FAST_FUNC config_close(parser_t *parser)
+void FAST_FUNC config_free(parser_t *parser)
 {
        if (parser) {
                if (PARSE_KEEP_COPY) /* compile-time constant */
                        free(parser->data);
-               fclose(parser->fp);
                free(parser->line);
                free(parser->nline);
                free(parser);
        }
 }
 
+void FAST_FUNC config_close(parser_t *parser)
+{
+       if (parser) {
+               fclose(parser->fp);
+               config_free(parser);
+       }
+}
+
 /* This function reads an entire line from a text file,
  * up to a newline, exclusive.
  * Trailing '\' is recognized as line continuation.
-- 
2.7.4

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to