Fixed the use of double quotes in chat messages and removed single quotes

as an option to enclose tokens.
This commit is contained in:
Brian Koropoff 2002-04-29 01:04:29 +00:00
parent 2711cbbe7c
commit 601736719d
3 changed files with 40 additions and 68 deletions

View file

@ -31,6 +31,7 @@
#include "QF/qtypes.h" #include "QF/qtypes.h"
#include "QF/dstring.h"
typedef struct cmd_localvar_s { typedef struct cmd_localvar_s {
struct dstring_s *key, *value; struct dstring_s *key, *value;
@ -89,6 +90,8 @@ typedef struct cmd_buffer_s {
//=========================================================================== //===========================================================================
void escape (dstring_t * dstr, const char *clist);
/* /*
Any number of commands can be added in a frame, from several different sources. Any number of commands can be added in a frame, from several different sources.

View file

@ -308,9 +308,13 @@ C_ExecLine (const char *line)
static void static void
C_Say (const char *line) C_Say (const char *line)
{ {
Cbuf_AddTextTo (cmd_legacybuffer, "say \""); dstring_t *dstr = dstring_newstr ();
Cbuf_AddTextTo (cmd_legacybuffer, line); dstring_appendstr (dstr, line);
Cbuf_AddTextTo (cmd_legacybuffer, "\"\n"); escape (dstr, "\"$#~\\");
Cbuf_AddTextTo (cmd_keybindbuffer, "say \"");
Cbuf_AddTextTo (cmd_keybindbuffer, dstr->str);
Cbuf_AddTextTo (cmd_keybindbuffer, "\"\n");
dstring_delete (dstr);
key_dest = key_game; key_dest = key_game;
game_target = IMT_0; game_target = IMT_0;
} }
@ -318,9 +322,13 @@ C_Say (const char *line)
static void static void
C_SayTeam (const char *line) C_SayTeam (const char *line)
{ {
Cbuf_AddTextTo (cmd_legacybuffer, "say_team \""); dstring_t *dstr = dstring_newstr ();
Cbuf_AddTextTo (cmd_legacybuffer, line); dstring_appendstr (dstr, line);
Cbuf_AddTextTo (cmd_legacybuffer, "\"\n"); escape (dstr, "\"");
Cbuf_AddTextTo (cmd_keybindbuffer, "say_team \"");
Cbuf_AddTextTo (cmd_keybindbuffer, dstr->str);
Cbuf_AddTextTo (cmd_keybindbuffer, "\"\n");
dstring_delete (dstr);
key_dest = key_game; key_dest = key_game;
game_target = IMT_0; game_target = IMT_0;
} }

View file

@ -258,24 +258,15 @@ escaped (const char *str, int i)
/* Quick function to escape stuff in a dstring */ /* Quick function to escape stuff in a dstring */
void void
escape (dstring_t * dstr) escape (dstring_t * dstr, const char *clist)
{ {
int i; int i;
for (i = 0; i < strlen (dstr->str); i++) { for (i = 0; i < strlen (dstr->str); i++) {
switch (dstr->str[i]) { if (strchr(clist, dstr->str[i]) && !escaped(dstr->str,i)) {
case '\\':
case '$':
case '#':
case '{':
case '}':
case '\"':
case '\'':
case '<':
case '>':
dstring_insertstr (dstr, "\\", i); dstring_insertstr (dstr, "\\", i);
i++; i++;
break; continue;
} }
} }
} }
@ -394,27 +385,23 @@ Cbuf_InsertText (const char *text)
void void
Cbuf_ExtractLine (dstring_t * buffer, dstring_t * line, qboolean legacy) Cbuf_ExtractLine (dstring_t * buffer, dstring_t * line, qboolean legacy)
{ {
int i, squotes = 0, dquotes = 0, braces = 0, n; int i, dquotes = 0, braces = 0, n;
char *tmp; char *tmp;
for (i = 0; buffer->str[i]; i++) { for (i = 0; buffer->str[i]; i++) {
if (!legacy && buffer->str[i] == '\'' && !escaped (buffer->str, i) if (buffer->str[i] == '"' && !escaped (buffer->str, i))
&& !dquotes)
squotes ^= 1;
if (buffer->str[i] == '"' && !escaped (buffer->str, i)
&& !squotes)
dquotes ^= 1; dquotes ^= 1;
if (buffer->str[i] == ';' && !escaped (buffer->str, i) if (buffer->str[i] == ';' && !escaped (buffer->str, i)
&& !squotes && !dquotes && !braces) && !dquotes && !braces)
break; break;
if (!legacy && buffer->str[i] == '{' && !escaped (buffer->str, i) if (!legacy && buffer->str[i] == '{' && !escaped (buffer->str, i)
&& !squotes && !dquotes) && !dquotes)
braces++; braces++;
if (!legacy && buffer->str[i] == '}' && !escaped (buffer->str, i) if (!legacy && buffer->str[i] == '}' && !escaped (buffer->str, i)
&& !squotes && !dquotes) && !dquotes)
braces--; braces--;
if (buffer->str[i] == '/' && buffer->str[i + 1] == '/' if (buffer->str[i] == '/' && buffer->str[i + 1] == '/'
&& !squotes && !dquotes) { && !dquotes) {
// Filter out comments until newline // Filter out comments until newline
if ((tmp = strchr (buffer->str+i, '\n'))) if ((tmp = strchr (buffer->str+i, '\n')))
n = tmp - (buffer->str+i); n = tmp - (buffer->str+i);
@ -424,13 +411,8 @@ Cbuf_ExtractLine (dstring_t * buffer, dstring_t * line, qboolean legacy)
n = strlen(buffer->str+i); // snip till \0 n = strlen(buffer->str+i); // snip till \0
dstring_snip (buffer, i, n); dstring_snip (buffer, i, n);
} }
if (buffer->str[i] == '\n' || buffer->str[i] == '\r') { if ((buffer->str[i] == '\n' || buffer->str[i] == '\r') && !braces)
if (braces) { break;
dstring_snip (buffer, i, 1);
i--;
} else
break;
}
} }
if (i) if (i)
dstring_insert (line, buffer->str, i, 0); dstring_insert (line, buffer->str, i, 0);
@ -477,6 +459,9 @@ Cbuf_ExecuteBuffer (cmd_buffer_t *buffer)
if (buffer->position == cmd_ready) { if (buffer->position == cmd_ready) {
Cbuf_ExtractLine (buffer->buffer, buf, buffer->legacy); Cbuf_ExtractLine (buffer->buffer, buf, buffer->legacy);
if (!buf->str[0])
continue;
Sys_DPrintf("Cbuf_ExecuteBuffer: Executing line %s\n", buf->str);
Cmd_TokenizeString (buf->str, cmd_activebuffer->legacy); Cmd_TokenizeString (buf->str, cmd_activebuffer->legacy);
if (cmd_error) if (cmd_error)
break; break;
@ -875,17 +860,6 @@ Cmd_EndDoubleQuote (const char *str)
return -1; // Not found return -1; // Not found
} }
int
Cmd_EndSingleQuote (const char *str)
{
int i;
for (i = 1; i < strlen (str); i++) {
if (str[i] == '\'' && !escaped (str, i))
return i;
}
return -1; // Not found
}
int int
Cmd_EndBrace (const char *str) Cmd_EndBrace (const char *str)
@ -905,12 +879,6 @@ Cmd_EndBrace (const char *str)
return n; return n;
else else
i += n; i += n;
} else if (str[i] == '\'' && !escaped (str, i)) {
n = Cmd_EndSingleQuote (str + i);
if (n < 0)
return n;
else
i += n;
} else if (str[i] == '}' && !escaped (str, i)) } else if (str[i] == '}' && !escaped (str, i))
return i; return i;
} }
@ -923,8 +891,6 @@ Cmd_GetToken (const char *str, qboolean legacy)
int i, ret; int i, ret;
if (!legacy) { if (!legacy) {
if (*str == '\'')
return Cmd_EndSingleQuote (str);
if (*str == '{') if (*str == '{')
return Cmd_EndBrace (str); return Cmd_EndBrace (str);
if (*str == '}') if (*str == '}')
@ -936,13 +902,6 @@ Cmd_GetToken (const char *str, qboolean legacy)
if (isspace ((byte)str[i])) if (isspace ((byte)str[i]))
break; break;
if (!legacy) { if (!legacy) {
if (str[i] == '\'' && !escaped (str,i)) {
ret = Cmd_EndSingleQuote (str+i);
if (ret < 0)
return ret;
i += ret;
continue;
}
if (str[i] == '{' && !escaped (str,i)) { if (str[i] == '{' && !escaped (str,i)) {
ret = Cmd_EndBrace (str+i); ret = Cmd_EndBrace (str+i);
if (ret < 0) if (ret < 0)
@ -1403,8 +1362,10 @@ Cmd_TokenizeString (const char *text, qboolean legacy)
continue past token boundaries. */ continue past token boundaries. */
tag_shift = 0; tag_shift = 0;
tag_special = 0; tag_special = 0;
if (text[0] == '|') if (text[0] == '|') {
legacy = true;
str++; str++;
}
while (strlen (str + i)) { while (strlen (str + i)) {
if (!legacy && cmd_argc == 1) { // See if command wants unprocessed tokens if (!legacy && cmd_argc == 1) { // See if command wants unprocessed tokens
cmd = (cmd_function_t *) Hash_Find (cmd_hash, cmd_activebuffer->argv[0]->original->str); cmd = (cmd_function_t *) Hash_Find (cmd_hash, cmd_activebuffer->argv[0]->original->str);
@ -1463,7 +1424,7 @@ Cmd_TokenizeString (const char *text, qboolean legacy)
} }
dstring_insert (cmd_activebuffer->argv[cmd_argc-1]->original, str + i, len, 0); dstring_insert (cmd_activebuffer->argv[cmd_argc-1]->original, str + i, len, 0);
dstring_insert (cmd_activebuffer->argv[cmd_argc-1]->processed, str + i, len, 0); dstring_insert (cmd_activebuffer->argv[cmd_argc-1]->processed, str + i, len, 0);
if (!legacy && !braces && process && text[0] != '|') if (!legacy && !braces && process)
cmd_activebuffer->argv[cmd_argc-1]->state = cmd_process; cmd_activebuffer->argv[cmd_argc-1]->state = cmd_process;
else else
cmd_activebuffer->argv[cmd_argc-1]->state = cmd_original; cmd_activebuffer->argv[cmd_argc-1]->state = cmd_original;
@ -1905,7 +1866,7 @@ Cmd_If_f (void)
int ret; int ret;
if ((Cmd_Argc () !=3 && !(Cmd_Argc () >= 5)) || (Cmd_Argc () > 5 && strcmp(Cmd_Argv(3),"else"))) { if ((Cmd_Argc () !=3 && !(Cmd_Argc () >= 5)) || (Cmd_Argc () > 5 && strcmp(Cmd_Argv(3),"else"))) {
Sys_Printf ("Usage: if {condition} {commands} else {commands}\n"); Cmd_Error ("Malformed if statement.\n");
return; return;
} }
@ -1949,7 +1910,7 @@ Cmd_While_f (void) {
sub = Cmd_NewBuffer (false); sub = Cmd_NewBuffer (false);
sub->locals = cmd_activebuffer->locals; // Use current local variables sub->locals = cmd_activebuffer->locals; // Use current local variables
sub->loop = true; sub->loop = true;
dstring_appendstr (sub->looptext, va("ifnot '%s' break\n", Cmd_Argv(1))); dstring_appendstr (sub->looptext, va("ifnot %s break\n", Cmd_Argv(1)));
dstring_appendstr (sub->looptext, Cmd_Argv(2)); dstring_appendstr (sub->looptext, Cmd_Argv(2));
Cmd_ExecuteSubroutine (sub); Cmd_ExecuteSubroutine (sub);
return; return;
@ -1959,12 +1920,12 @@ void
Cmd_For_f (void) { Cmd_For_f (void) {
cmd_buffer_t *sub; cmd_buffer_t *sub;
dstring_t *arg1, *init, *cond, *inc; dstring_t *arg1, *init, *cond, *inc;
if (Cmd_Argc() < 2 || Cmd_Argc() > 3) { if (Cmd_Argc() < 2 || Cmd_Argc() > 3) {
Cmd_Error("Malformed for statement.\n"); Cmd_Error("Malformed for statement.\n");
return; return;
} }
arg1 = dstring_newstr (); arg1 = dstring_newstr ();
init = dstring_newstr (); init = dstring_newstr ();
cond = dstring_newstr (); cond = dstring_newstr ();
@ -1978,7 +1939,7 @@ Cmd_For_f (void) {
sub = Cmd_NewBuffer (false); sub = Cmd_NewBuffer (false);
sub->locals = cmd_activebuffer->locals; // Use current local variables sub->locals = cmd_activebuffer->locals; // Use current local variables
sub->loop = true; sub->loop = true;
dstring_appendstr (sub->looptext, va("ifnot '%s' break\n", cond->str)); dstring_appendstr (sub->looptext, va("ifnot %s break\n", cond->str));
dstring_appendstr (sub->looptext, va("%s\n", Cmd_Argv(2))); dstring_appendstr (sub->looptext, va("%s\n", Cmd_Argv(2)));
dstring_appendstr (sub->looptext, va("%s", inc->str)); dstring_appendstr (sub->looptext, va("%s", inc->str));
Cbuf_InsertTextTo (sub, init->str); Cbuf_InsertTextTo (sub, init->str);