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)
{
// 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"))
// 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 );
}
/// 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