1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-29 14:47:46 +02:00

No longer require quotes for commands that take strings as arguments

This commit is contained in:
jfreegman 2018-10-07 17:30:43 -04:00
parent 03673cbced
commit b6c746b5f5
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
3 changed files with 53 additions and 22 deletions

View File

@ -107,10 +107,60 @@ static struct cmd_func group_commands[] = {
{ NULL, NULL },
};
/* Special commands are commands that only take one argument even if it contains spaces */
#define SPECIAL_COMMANDS 3
static const char special_commands[SPECIAL_COMMANDS][MAX_CMDNAME_SIZE] = {
"/nick",
"/note",
"/title",
};
/* Returns true if input command is in the special_commands array. */
static bool is_special_command(const char *input)
{
int start = char_find(0, input, ' ');
int i;
for (i = 0; i < SPECIAL_COMMANDS; ++i) {
if (strncmp(input, special_commands[i], start) == 0) {
return true;
}
}
return false;
}
/* Parses commands in the special_commands array. Unlike parse_command, this function
* does not split the input string at spaces.
*
* Returns number of arguments on success, returns -1 on failure
*/
static int parse_special_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
{
int len = strlen(input);
int s = char_find(0, input, ' ');
if (s + 1 >= len) {
return -1;
}
memcpy(args[0], input, s);
args[0][s++] = '\0'; /* increment to remove space after /command */
memcpy(args[1], input + s, len - s);
args[1][len - s] = '\0';
return 2;
}
/* Parses input command and puts args into arg array.
Returns number of arguments on success, -1 on failure. */
static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
{
if (is_special_command(input)) {
return parse_special_command(w, self, input, args);
}
char *cmd = strdup(input);
if (cmd == NULL) {

View File

@ -534,19 +534,7 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
return;
}
if (argv[1][0] != '\"') {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Note must be enclosed in quotes.");
return;
}
/* remove opening and closing quotes and replace linebreaks with spaces */
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%s", &argv[1][1]);
int len = strlen(msg) - 1;
msg[len] = '\0';
strsubst(msg, '\n', ' ');
prompt_update_statusmessage(prompt, m, msg);
prompt_update_statusmessage(prompt, m, argv[1]);
}
void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])

View File

@ -52,15 +52,8 @@ void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
return;
}
if (argv[1][0] != '\"') {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title must be enclosed in quotes.");
return;
}
/* remove opening and closing quotes */
snprintf(title, sizeof(title), "%s", &argv[1][1]);
int len = strlen(title) - 1;
title[len] = '\0';
snprintf(title, sizeof(title), "%s", argv[1]);
int len = strlen(title);
if (!tox_conference_set_title(m, self->num, (uint8_t *) title, len, &err)) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title (error %d)", err);