encode esc in instead_cmd

This commit is contained in:
p.kosyh 2010-01-14 12:45:14 +00:00
parent 52568c31b5
commit 700cd732ca
3 changed files with 43 additions and 10 deletions

View file

@ -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;
}

View file

@ -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)
{

View file

@ -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);