From d251bac24fdb081f45abb9a00fa83ad5c1b81642 Mon Sep 17 00:00:00 2001 From: divverent Date: Sat, 19 Feb 2011 16:31:16 +0000 Subject: [PATCH] Cmd_QuoteString: make it also able to put the enclosing quotation marks git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10849 d7cf8633-e32d-0410-b094-e92efae38249 ::stable-branch::merge=1a73db7156d00f2a1a549718d47e9ec39684325f --- cmd.c | 44 +++++++++++++++++++++++++------------------- cmd.h | 5 +++-- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/cmd.c b/cmd.c index 3fb741e0..7b156277 100644 --- a/cmd.c +++ b/cmd.c @@ -801,57 +801,63 @@ static const char *Cmd_GetDirectCvarValue(const char *varname, cmdalias_t *alias return NULL; } -qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset) +qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes) { qboolean quote_quot = !!strchr(quoteset, '"'); qboolean quote_backslash = !!strchr(quoteset, '\\'); qboolean quote_dollar = !!strchr(quoteset, '$'); + if(putquotes) + { + if(outlen <= 2) + { + *out++ = 0; + return false; + } + *out++ = '"'; --outlen; + --outlen; + } + while(*in) { if(*in == '"' && quote_quot) { if(outlen <= 2) - { - *out++ = 0; - return false; - } + goto fail; *out++ = '\\'; --outlen; *out++ = '"'; --outlen; } else if(*in == '\\' && quote_backslash) { if(outlen <= 2) - { - *out++ = 0; - return false; - } + goto fail; *out++ = '\\'; --outlen; *out++ = '\\'; --outlen; } else if(*in == '$' && quote_dollar) { if(outlen <= 2) - { - *out++ = 0; - return false; - } + goto fail; *out++ = '$'; --outlen; *out++ = '$'; --outlen; } else { if(outlen <= 1) - { - *out++ = 0; - return false; - } + goto fail; *out++ = *in; --outlen; } ++in; } + if(putquotes) + *out++ = '"'; *out++ = 0; return true; +fail: + if(putquotes) + *out++ = '"'; + *out++ = 0; + return false; } static const char *Cmd_GetCvarValue(const char *var, size_t varlen, cmdalias_t *alias) @@ -907,7 +913,7 @@ static char asis[] = "asis"; // just to suppress const char warnings { // quote it so it can be used inside double quotes // we just need to replace " by \", and of course, double backslashes - Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\"); + Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false); return varval; } else if(!strcmp(varfunc, "asis")) @@ -1055,7 +1061,7 @@ static void Cmd_ExecuteAlias (cmdalias_t *alias) // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we // have to make sure that no second variable expansion takes place, otherwise // alias parameters containing dollar signs can have bad effects. - Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$"); + Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false); Cbuf_InsertText( buffer2 ); } diff --git a/cmd.h b/cmd.h index 38d43463..a4881c65 100644 --- a/cmd.h +++ b/cmd.h @@ -156,8 +156,9 @@ void Cmd_Print(const char *text); /// quoteset is a string that contains one or more of ", \, $ and specifies /// the characters to be quoted (you usually want to either pass "\"\\" or /// "\"\\$"). Returns true on success, and false on overrun (in which case out -/// will contain a part of the quoted string). -qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset); +/// will contain a part of the quoted string). If putquotes is set, the +/// enclosing quote marks are also put. +qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes); #endif -- 2.39.2