libarchive: open_zipped() does not need to check extensions for e.g. gzip
[oweals/busybox.git] / libbb / parse_config.c
index ace6f3ad3e807020c5d8e3a01017708066d49a24..1590d9a4c0b346663b346432a67d21e90b685886 100644 (file)
@@ -4,62 +4,67 @@
  *
  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
  */
 
+/* Uncomment to enable test applet */
+////config:config PARSE
+////config:    bool "Uniform config file parser debugging applet: parse"
+////config:    default n
+////config:    help
+////config:      Typical usage of parse API:
+////config:            char *t[3];
+////config:            parser_t *p = config_open(filename);
+////config:            while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
+////config:                    bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]);
+////config:            }
+////config:            config_close(p);
+
+////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-y += parse_config.o
+
+//usage:#define parse_trivial_usage
+//usage:       "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..."
+//usage:#define parse_full_usage "\n\n"
+//usage:       "       -x      Suppress output (for benchmarking)"
+
 #include "libbb.h"
 
-#if ENABLE_PARSE
+#if defined ENABLE_PARSE && ENABLE_PARSE
 int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int parse_main(int argc UNUSED_PARAM, char **argv)
 {
        const char *delims = "# \t";
+       char **t;
        unsigned flags = PARSE_NORMAL;
        int mintokens = 0, ntokens = 128;
+       unsigned noout;
 
        opt_complementary = "-1:n+:m+:f+";
-       getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
+       noout = 1 & getopt32(argv, "xn:m:d:f:", &ntokens, &mintokens, &delims, &flags);
        //argc -= optind;
        argv += optind;
+
+       t = xmalloc(sizeof(t[0]) * ntokens);
        while (*argv) {
+               int n;
                parser_t *p = config_open(*argv);
-               if (p) {
-                       int n;
-                       char **t = xmalloc(sizeof(char *) * ntokens);
-                       while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
+               while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
+                       if (!noout) {
                                for (int i = 0; i < n; ++i)
                                        printf("[%s]", t[i]);
                                puts("");
                        }
-                       config_close(p);
                }
+               config_close(p);
                argv++;
        }
        return EXIT_SUCCESS;
 }
 #endif
 
-/*
-
-Typical usage:
-
------ CUT -----
-       char *t[3];     // tokens placeholder
-       parser_t *p = config_open(filename);
-       if (p) {
-               // parse line-by-line
-               while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
-                       // use tokens
-                       bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
-               }
-               ...
-               // free parser
-               config_close(p);
-       }
------ CUT -----
-
-*/
-
 parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
 {
        FILE* fp;
@@ -78,30 +83,63 @@ parser_t* FAST_FUNC config_open(const char *filename)
        return config_open2(filename, fopen_or_warn_stdin);
 }
 
-static void config_free_data(parser_t *const parser)
-{
-       free(parser->line);
-       parser->line = NULL;
-       if (PARSE_KEEP_COPY) { /* compile-time constant */
-               free(parser->data);
-               parser->data = NULL;
-       }
-}
-
 void FAST_FUNC config_close(parser_t *parser)
 {
        if (parser) {
-               config_free_data(parser);
+               if (PARSE_KEEP_COPY) /* compile-time constant */
+                       free(parser->data);
                fclose(parser->fp);
+               free(parser->line);
+               free(parser->nline);
                free(parser);
        }
 }
 
+/* This function reads an entire line from a text file,
+ * up to a newline, exclusive.
+ * Trailing '\' is recognized as line continuation.
+ * Returns -1 if EOF/error.
+ */
+static int get_line_with_continuation(parser_t *parser)
+{
+       ssize_t len, nlen;
+       char *line;
+
+       len = getline(&parser->line, &parser->line_alloc, parser->fp);
+       if (len <= 0)
+               return len;
+
+       line = parser->line;
+       for (;;) {
+               parser->lineno++;
+               if (line[len - 1] == '\n')
+                       len--;
+               if (len == 0 || line[len - 1] != '\\')
+                       break;
+               len--;
+
+               nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp);
+               if (nlen <= 0)
+                       break;
+
+               if (parser->line_alloc < len + nlen + 1) {
+                       parser->line_alloc = len + nlen + 1;
+                       line = parser->line = xrealloc(line, parser->line_alloc);
+               }
+               memcpy(&line[len], parser->nline, nlen);
+               len += nlen;
+       }
+
+       line[len] = '\0';
+       return len;
+}
+
+
 /*
 0. If parser is NULL return 0.
 1. Read a line from config file. If nothing to read then return 0.
    Handle continuation character. Advance lineno for each physical line.
-   Discard everything past comment characher.
+   Discard everything past comment character.
 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
 3. If resulting line is empty goto 1.
 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
@@ -123,137 +161,83 @@ mintokens > 0 make config_read() print error message if less than mintokens
 #undef config_read
 int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
 {
-       char *line, *q;
-       char comment;
-       int ii;
-       int ntokens;
-       int mintokens;
+       char *line;
+       int ntokens, mintokens;
+       int t;
+
+       if (!parser)
+               return 0;
 
-       comment = *delims++;
-       ntokens = flags & 0xFF;
-       mintokens = (flags & 0xFF00) >> 8;
+       ntokens = (uint8_t)flags;
+       mintokens = (uint8_t)(flags >> 8);
 
  again:
        memset(tokens, 0, sizeof(tokens[0]) * ntokens);
-       if (!parser)
+
+       /* Read one line (handling continuations with backslash) */
+       if (get_line_with_continuation(parser) < 0)
                return 0;
-       config_free_data(parser);
 
-       while (1) {
-//TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc
-               line = xmalloc_fgetline(parser->fp);
-               if (!line)
-                       return 0;
+       line = parser->line;
 
-               parser->lineno++;
-               // handle continuations. Tito's code stolen :)
-               while (1) {
-                       ii = strlen(line);
-                       if (!ii)
-                               goto next_line;
-                       if (line[ii - 1] != '\\')
-                               break;
-                       // multi-line object
-                       line[--ii] = '\0';
-//TODO: add xmalloc_fgetline-like iface but with appending to existing str
-                       q = xmalloc_fgetline(parser->fp);
-                       if (!q)
-                               break;
-                       parser->lineno++;
-                       line = xasprintf("%s%s", line, q);
-                       free(q);
-               }
-               // discard comments
-               if (comment) {
-                       q = strchrnul(line, comment);
-                       *q = '\0';
-                       ii = q - line;
-               }
-               // skip leading and trailing delimiters
-               if (flags & PARSE_TRIM) {
-                       // skip leading
-                       int n = strspn(line, delims);
-                       if (n) {
-                               ii -= n;
-                               overlapping_strcpy(line, line + n);
-                       }
-                       // cut trailing
-                       if (ii) {
-                               while (strchr(delims, line[--ii]))
-                                       continue;
-                               line[++ii] = '\0';
-                       }
-               }
-               // if something still remains -> return it
-               if (ii)
-                       break;
+       /* Skip token in the start of line? */
+       if (flags & PARSE_TRIM)
+               line += strspn(line, delims + 1);
 
- next_line:
-               // skip empty line
-               free(line);
-       }
-       // non-empty line found, parse and return the number of tokens
+       if (line[0] == '\0' || line[0] == delims[0])
+               goto again;
 
-       // store line
-       parser->line = line = xrealloc(line, ii + 1);
        if (flags & PARSE_KEEP_COPY) {
+               free(parser->data);
                parser->data = xstrdup(line);
        }
 
-       // split line to tokens
-       ntokens--; // now it's max allowed token no
-       // N.B. non-empty remainder is also a token,
-       // so if ntokens <= 1, we just return the whole line
-       // N.B. if PARSE_GREEDY is set the remainder of the line is stuck to the last token
-       ii = 0;
-       while (*line && ii <= ntokens) {
-               //bb_info_msg("L[%s]", line);
-               // get next token
-               // at last token and need greedy token ->
-               if ((flags & PARSE_GREEDY) && (ii == ntokens)) {
-                       // skip possible delimiters
-                       if (flags & PARSE_COLLAPSE)
-                               line += strspn(line, delims);
-                       // don't cut the line
-                       q = line + strlen(line);
+       /* Tokenize the line */
+       t = 0;
+       do {
+               /* Pin token */
+               tokens[t] = line;
+
+               /* Combine remaining arguments? */
+               if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
+                       /* Vanilla token, find next delimiter */
+                       line += strcspn(line, delims[0] ? delims : delims + 1);
                } else {
-                       // vanilla token. cut the line at the first delim
-                       q = line + strcspn(line, delims);
-                       if (*q) // watch out: do not step past the line end!
-                               *q++ = '\0';
-               }
-               // pin token
-               if (!(flags & (PARSE_COLLAPSE | PARSE_TRIM)) || *line) {
-                       //bb_info_msg("N[%d] T[%s]", ii, line);
-                       tokens[ii++] = line;
-                       // process escapes in token
-#if 0 // unused so far
-                       if (flags & PARSE_ESCAPE) {
-                               char *s = line;
-                               while (*s) {
-                                       if (*s == '\\') {
-                                               s++;
-                                               *line++ = bb_process_escape_sequence((const char **)&s);
-                                       } else {
-                                               *line++ = *s++;
-                                       }
-                               }
-                               *line = '\0';
+                       /* Combining, find comment char if any */
+                       line = strchrnul(line, PARSE_EOL_COMMENTS ? delims[0] : '\0');
+
+                       /* Trim any extra delimiters from the end */
+                       if (flags & PARSE_TRIM) {
+                               while (strchr(delims + 1, line[-1]) != NULL)
+                                       line--;
                        }
-#endif
                }
-               line = q;
-               //bb_info_msg("A[%s]", line);
-       }
 
-       if (ii < mintokens) {
+               /* Token not terminated? */
+               if (*line == delims[0])
+                       *line = '\0';
+               else if (*line != '\0')
+                       *line++ = '\0';
+
+#if 0 /* unused so far */
+               if (flags & PARSE_ESCAPE) {
+                       strcpy_and_process_escape_sequences(tokens[t], tokens[t]);
+               }
+#endif
+               /* Skip possible delimiters */
+               if (flags & PARSE_COLLAPSE)
+                       line += strspn(line, delims + 1);
+
+               t++;
+       } while (*line && *line != delims[0] && t < ntokens);
+
+       if (t < mintokens) {
                bb_error_msg("bad line %u: %d tokens found, %d needed",
-                               parser->lineno, ii, mintokens);
+                               parser->lineno, t, mintokens);
                if (flags & PARSE_MIN_DIE)
                        xfunc_die();
-               ntokens++;
                goto again;
        }
 
-       return ii;
+       return t;
 }