]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
Kconfig: Enable usage of escape char '\' in string values
authorStefan Roese <sr@denx.de>
Fri, 29 May 2015 09:47:32 +0000 (11:47 +0200)
committerLothar Waßmann <LW@KARO-electronics.de>
Tue, 8 Sep 2015 22:42:24 +0000 (00:42 +0200)
I might have missed something, but I failed to use the escape char '\'
in strings. To pass a printf format string like "foo %d bar\n" via
Kconfig to the code.

Right now its not possible to use the escape character '\' in Kconfig
string values correctly to e.g. set this string value "test output\n".
The '\n' will be converted to 'n'.

The current implementation removes some of the '\' chars from the input
string in conf_set_sym_val(). Examples:

'\' -> ''
'\\' -> '\'
'\\\' -> '\'
'\\\\' -> '\\'
...

And then doubles the backslash chars in the output string in
sym_escape_string_value(). Example:

'\' -> '' -> ''
'\\' -> '\' -> '\\'
'\\\' -> '\' -> '\\'
'\\\\' -> '\\' -> '\\\\'
...

As you see in these examples, its impossible to generate a single '\'
charater in the output string as its needed for something like '\n'.

This patch now changes this behavior to not drop some backslashes in
conf_set_sym_val() and to not add new backslashes in the resulting
output string. Removing the function sym_escape_string_value()
completely as its not needed anymore.

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
scripts/kconfig/confdata.c
scripts/kconfig/symbol.c

index f88d90f20228e8783b5ca39accc436140af7b3ff..2f778df206f5b4e19b959c9044ab78afd5de40a6 100644 (file)
@@ -155,18 +155,14 @@ static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
        case S_STRING:
                if (*p++ != '"')
                        break;
-               for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
-                       if (*p2 == '"') {
-                               *p2 = 0;
-                               break;
-                       }
-                       memmove(p2, p2 + 1, strlen(p2));
-               }
-               if (!p2) {
+               /* Last char has to be a '"' */
+               if (p[strlen(p) - 1] != '"') {
                        if (def != S_DEF_AUTO)
                                conf_warning("invalid string found");
                        return 1;
                }
+               /* Overwrite '"' with \0 for string termination */
+               p[strlen(p) - 1] = 0;
                /* fall through */
        case S_INT:
        case S_HEX:
@@ -624,6 +620,7 @@ static void conf_write_symbol(FILE *fp, struct symbol *sym,
                              struct conf_printer *printer, void *printer_arg)
 {
        const char *str;
+       char *str2;
 
        switch (sym->type) {
        case S_OTHER:
@@ -631,9 +628,10 @@ static void conf_write_symbol(FILE *fp, struct symbol *sym,
                break;
        case S_STRING:
                str = sym_get_string_value(sym);
-               str = sym_escape_string_value(str);
-               printer->print_symbol(fp, sym, str, printer_arg);
-               free((void *)str);
+               str2 = xmalloc(strlen(str) + 3);
+               sprintf(str2, "\"%s\"", str);
+               printer->print_symbol(fp, sym, str2, printer_arg);
+               free((void *)str2);
                break;
        default:
                str = sym_get_string_value(sym);
index 7caabdb51c647e12e35b500bdebd262ba1545eb0..ab339ebbe30b72ff02d6603598501d8f3cad1c8d 100644 (file)
@@ -912,49 +912,6 @@ const char *sym_expand_string_value(const char *in)
        return res;
 }
 
-const char *sym_escape_string_value(const char *in)
-{
-       const char *p;
-       size_t reslen;
-       char *res;
-       size_t l;
-
-       reslen = strlen(in) + strlen("\"\"") + 1;
-
-       p = in;
-       for (;;) {
-               l = strcspn(p, "\"\\");
-               p += l;
-
-               if (p[0] == '\0')
-                       break;
-
-               reslen++;
-               p++;
-       }
-
-       res = xmalloc(reslen);
-       res[0] = '\0';
-
-       strcat(res, "\"");
-
-       p = in;
-       for (;;) {
-               l = strcspn(p, "\"\\");
-               strncat(res, p, l);
-               p += l;
-
-               if (p[0] == '\0')
-                       break;
-
-               strcat(res, "\\");
-               strncat(res, p++, 1);
-       }
-
-       strcat(res, "\"");
-       return res;
-}
-
 struct sym_match {
        struct symbol   *sym;
        off_t           so, eo;