volume_id: recognize compressed hibernate swap signature
[oweals/busybox.git] / libbb / parse_config.c
index a0599d4b4505b4cfdbc0e37073c19012409624fd..d471edbeecda819ad3353cb8b31b229c71f1fd5b 100644 (file)
@@ -4,12 +4,13 @@
  *
  * 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.
  */
 
 #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)
 {
@@ -44,7 +45,7 @@ int parse_main(int argc UNUSED_PARAM, char **argv)
 Typical usage:
 
 ----- CUT -----
-       char *t[3];     // tokens placeholder
+       char *t[3];  // tokens placeholder
        parser_t *p = config_open(filename);
        if (p) {
                // parse line-by-line
@@ -78,7 +79,7 @@ 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)
+static void config_free_data(parser_t *parser)
 {
        free(parser->line);
        parser->line = NULL;
@@ -101,7 +102,7 @@ void FAST_FUNC config_close(parser_t *parser)
 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
@@ -127,8 +128,8 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
        int ntokens, mintokens;
        int t, len;
 
-       ntokens = flags & 0xFF;
-       mintokens = (flags & 0xFF00) >> 8;
+       ntokens = (uint8_t)flags;
+       mintokens = (uint8_t)(flags >> 8);
 
        if (parser == NULL)
                return 0;
@@ -158,7 +159,8 @@ again:
                parser->data = xstrdup(line);
 
        /* Tokenize the line */
-       for (t = 0; *line && *line != delims[0] && t < ntokens; t++) {
+       t = 0;
+       do {
                /* Pin token */
                tokens[t] = line;
 
@@ -178,39 +180,30 @@ again:
                }
 
                /* Token not terminated? */
-               if (line[0] == delims[0])
+               if (*line == delims[0])
                        *line = '\0';
-               else if (line[0] != '\0')
-                       *(line++) = '\0';
+               else if (*line != '\0')
+                       *line++ = '\0';
 
 #if 0 /* unused so far */
                if (flags & PARSE_ESCAPE) {
-                       const char *from;
-                       char *to;
-
-                       from = to = tokens[t];
-                       while (*from) {
-                               if (*from == '\\') {
-                                       from++;
-                                       *to++ = bb_process_escape_sequence(&from);
-                               } else {
-                                       *to++ = *from++;
-                               }
-                       }
-                       *to = '\0';
+                       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, t, mintokens);
                if (flags & PARSE_MIN_DIE)
                        xfunc_die();
+               if (flags & PARSE_KEEP_COPY)
+                       free(parser->data);
                goto again;
        }