Skip to content

Commit

Permalink
Strip quotes when passing through raw command args.
Browse files Browse the repository at this point in the history
Strip surrounding quotes so that e.g. ‘stuffall "some long text"’
command does what users expect (sends ‘some long text’ string without
quotes).
  • Loading branch information
skullernet authored and Paril committed Dec 10, 2021
1 parent 11b2043 commit 4941a20
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions inc/shared/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ int SortStrcmp(const void *p1, const void *p2);
int SortStricmp(const void *p1, const void *p2);

size_t COM_strclr(char *s);
char *COM_StripQuotes(char *s);

// buffer safe operations
size_t Q_strlcpy(char *dst, const char *src, size_t size);
Expand Down
2 changes: 1 addition & 1 deletion src/client/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ static void start_message_mode(chatMode_t mode)
}

con.chat = mode;
IF_Replace(&con.chatPrompt.inputLine, Cmd_RawArgs());
IF_Replace(&con.chatPrompt.inputLine, COM_StripQuotes(Cmd_RawArgs()));
Key_SetDest(cls.key_dest | KEY_MESSAGE);
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ static void CL_Rcon_f(void)
address = cls.netchan->remote_address;
}

CL_SendRcon(&address, rcon_password->string, Cmd_RawArgs());
CL_SendRcon(&address, rcon_password->string, COM_StripQuotes(Cmd_RawArgs()));
}

static void CL_Rcon_c(genctx_t *ctx, int argnum)
Expand Down
2 changes: 1 addition & 1 deletion src/client/ui/servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ static void Draw(menuFrameWork_t *self)
static bool Push(menuFrameWork_t *self)
{
// save our arguments for refreshing
m_servers.args = UI_CopyString(Cmd_RawArgsFrom(2));
m_servers.args = UI_CopyString(COM_StripQuotes(Cmd_RawArgsFrom(2)));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ static void Cmd_EchoEx_f(void)
}
}

s = Cmd_RawArgsFrom(cmd_optind);
s = COM_StripQuotes(Cmd_RawArgsFrom(cmd_optind));
if (escapes) {
s = unescape_string(buffer, s);
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ static void SV_ConSay_f(void)
return;
}

s = Cmd_RawArgs();
s = COM_StripQuotes(Cmd_RawArgs());
FOR_EACH_CLIENT(client) {
if (client->state != cs_spawned)
continue;
Expand Down Expand Up @@ -860,7 +860,7 @@ static void SV_Stuff_f(void)
return;

MSG_WriteByte(svc_stufftext);
MSG_WriteString(Cmd_RawArgsFrom(2));
MSG_WriteString(COM_StripQuotes(Cmd_RawArgsFrom(2)));
SV_ClientAddMessage(sv_client, MSG_RELIABLE | MSG_CLEAR);

sv_client = NULL;
Expand Down Expand Up @@ -889,7 +889,7 @@ static void SV_StuffAll_f(void)
}

MSG_WriteByte(svc_stufftext);
MSG_WriteString(Cmd_RawArgsFrom(1));
MSG_WriteString(COM_StripQuotes(Cmd_RawArgs()));

FOR_EACH_CLIENT(client) {
SV_ClientAddMessage(client, MSG_RELIABLE);
Expand Down
2 changes: 1 addition & 1 deletion src/server/mvd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2309,7 +2309,7 @@ CVARS AND COMMANDS
static void SV_MvdStuff_f(void)
{
if (mvd.dummy) {
Cbuf_AddText(&dummy_buffer, Cmd_RawArgs());
Cbuf_AddText(&dummy_buffer, COM_StripQuotes(Cmd_RawArgs()));
Cbuf_AddText(&dummy_buffer, "\n");
} else {
Com_Printf("Can't '%s', dummy MVD client is not active\n", Cmd_Argv(0));
Expand Down
14 changes: 14 additions & 0 deletions src/shared/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@ size_t COM_strclr(char *s)
return len;
}

char *COM_StripQuotes(char *s)
{
if (*s == '"') {
size_t p = strlen(s) - 1;

if (s[p] == '"') {
s[p] = 0;
return s + 1;
}
}

return s;
}

/*
============
va
Expand Down

0 comments on commit 4941a20

Please sign in to comment.