diff --git a/src/execute.c b/src/execute.c index b09cb96..1d1fe83 100644 --- a/src/execute.c +++ b/src/execute.c @@ -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) { diff --git a/src/global_commands.c b/src/global_commands.c index b056f0d..68c4cf6 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -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]) diff --git a/src/group_commands.c b/src/group_commands.c index 7c58dd2..9ceffea 100644 --- a/src/group_commands.c +++ b/src/group_commands.c @@ -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);