Skip to contentSkip to conversation

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

PATCH Superseded Stefan Roese 1 participant 1 message May 7, 2015

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Yann E. MORIN <[email protected]>
---
 scripts/kconfig/confdata.c | 20 +++++++++-----------
 scripts/kconfig/symbol.c   | 43 -------------------------------------------
 2 files changed, 9 insertions(+), 54 deletions(-)
scripts/kconfig/confdata.c +9 -11
index c814f57..0c4200a 100644
@@ -161,18 +161,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:
@@ -630,6 +626,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:
@@ -637,9 +634,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) + 2);
+		sprintf(str2, "\"%s\"", str);
+		printer->print_symbol(fp, sym, str2, printer_arg);
+		free((void *)str2);
 		break;
 	default:
 		str = sym_get_string_value(sym);
scripts/kconfig/symbol.c +0 -43

49 lines not on this page · raw message

Signature · 1 line
2.4.0