Commit 20c20826efabf9ed64f5555bc8739bdbb89c1edd

Authored by Stefan Roese
Committed by Tom Rini
1 parent 4d80051b63

Kconfig: Enable usage of escape char '\' in string values

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>

Showing 2 changed files with 9 additions and 54 deletions Side-by-side Diff

scripts/kconfig/confdata.c
... ... @@ -155,18 +155,14 @@
155 155 case S_STRING:
156 156 if (*p++ != '"')
157 157 break;
158   - for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
159   - if (*p2 == '"') {
160   - *p2 = 0;
161   - break;
162   - }
163   - memmove(p2, p2 + 1, strlen(p2));
164   - }
165   - if (!p2) {
  158 + /* Last char has to be a '"' */
  159 + if (p[strlen(p) - 1] != '"') {
166 160 if (def != S_DEF_AUTO)
167 161 conf_warning("invalid string found");
168 162 return 1;
169 163 }
  164 + /* Overwrite '"' with \0 for string termination */
  165 + p[strlen(p) - 1] = 0;
170 166 /* fall through */
171 167 case S_INT:
172 168 case S_HEX:
... ... @@ -624,6 +620,7 @@
624 620 struct conf_printer *printer, void *printer_arg)
625 621 {
626 622 const char *str;
  623 + char *str2;
627 624  
628 625 switch (sym->type) {
629 626 case S_OTHER:
... ... @@ -631,9 +628,10 @@
631 628 break;
632 629 case S_STRING:
633 630 str = sym_get_string_value(sym);
634   - str = sym_escape_string_value(str);
635   - printer->print_symbol(fp, sym, str, printer_arg);
636   - free((void *)str);
  631 + str2 = xmalloc(strlen(str) + 3);
  632 + sprintf(str2, "\"%s\"", str);
  633 + printer->print_symbol(fp, sym, str2, printer_arg);
  634 + free((void *)str2);
637 635 break;
638 636 default:
639 637 str = sym_get_string_value(sym);
scripts/kconfig/symbol.c
... ... @@ -912,49 +912,6 @@
912 912 return res;
913 913 }
914 914  
915   -const char *sym_escape_string_value(const char *in)
916   -{
917   - const char *p;
918   - size_t reslen;
919   - char *res;
920   - size_t l;
921   -
922   - reslen = strlen(in) + strlen("\"\"") + 1;
923   -
924   - p = in;
925   - for (;;) {
926   - l = strcspn(p, "\"\\");
927   - p += l;
928   -
929   - if (p[0] == '\0')
930   - break;
931   -
932   - reslen++;
933   - p++;
934   - }
935   -
936   - res = xmalloc(reslen);
937   - res[0] = '\0';
938   -
939   - strcat(res, "\"");
940   -
941   - p = in;
942   - for (;;) {
943   - l = strcspn(p, "\"\\");
944   - strncat(res, p, l);
945   - p += l;
946   -
947   - if (p[0] == '\0')
948   - break;
949   -
950   - strcat(res, "\\");
951   - strncat(res, p++, 1);
952   - }
953   -
954   - strcat(res, "\"");
955   - return res;
956   -}
957   -
958 915 struct sym_match {
959 916 struct symbol *sym;
960 917 off_t so, eo;