From 700cd732ca6f6f20e30aa3e28101515e4cbed60e Mon Sep 17 00:00:00 2001 From: "p.kosyh" Date: Thu, 14 Jan 2010 12:45:14 +0000 Subject: [PATCH] encode esc in instead_cmd --- src/sdl-instead/instead.c | 19 +++++++++---------- src/sdl-instead/util.c | 33 +++++++++++++++++++++++++++++++++ src/sdl-instead/util.h | 1 + 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/sdl-instead/instead.c b/src/sdl-instead/instead.c index 4e09796..f14f456 100644 --- a/src/sdl-instead/instead.c +++ b/src/sdl-instead/instead.c @@ -149,16 +149,15 @@ int instead_iretval(int n) char *instead_cmd(char *s) { - char buf[1024]; - char *p = s; - while (*p) { - if (*p == '\\' || *p == '\'' || *p == '\"' || *p == '[' || *p == ']') - return NULL; - p ++; - } - s = togame(s); - snprintf(buf, sizeof(buf), "return iface:cmd('%s')", s); - free(s); + char buf[4096]; + char *p; + p = encode_esc_string(s); + if (!p) + return NULL; + s = togame(p); free(p); + if (!s) + return NULL; + snprintf(buf, sizeof(buf), "return iface:cmd('%s')", s); free(s); p = getstring(buf); return p; } diff --git a/src/sdl-instead/util.c b/src/sdl-instead/util.c index 57b163b..f7171e3 100644 --- a/src/sdl-instead/util.c +++ b/src/sdl-instead/util.c @@ -154,6 +154,39 @@ int parse_string(const char *v, void *data) return 0; } +char *encode_esc_string(const char *v) +{ + char *r, *p; + if (!v) + return NULL; + p = r = malloc((strlen(v)*2) + 1); + if (!r) + return NULL; + while (*v) { + switch (*v) { + case ' ': + *p ++ = '\\'; + *p ++ = ' '; + break; + case '"': + *p ++ = '\\'; + *p ++ = '"'; + break; + case '\'': + *p ++ = '\\'; + *p ++ = '\''; + break; + case '\\': + *p ++ = '\\'; + *p ++ = '\\'; + default: + *p ++ = *v; + } + v ++; + } + *p ++ = 0; + return r; +} int parse_esc_string(const char *v, void *data) { diff --git a/src/sdl-instead/util.h b/src/sdl-instead/util.h index 59588af..ea7ee67 100644 --- a/src/sdl-instead/util.h +++ b/src/sdl-instead/util.h @@ -21,6 +21,7 @@ extern int parse_esc_string(const char *v, void *data); extern int parse_string(const char *v, void *data); extern int parse_int(const char *v, void *data); extern int parse_full_path(const char *v, void *data); +extern char *encode_esc_string(const char *v); #ifdef _HAVE_ICONV extern char *decode(iconv_t hiconv, const char *s);