1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:43:01 +01:00

simplify execute function some more

This commit is contained in:
Jfreegman 2013-10-23 01:03:18 -04:00
parent 6595e2bce5
commit 72565cdaec

View File

@ -360,43 +360,39 @@ void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv
#define MAX_NUM_ARGS 4 /* Includes command */ #define MAX_NUM_ARGS 4 /* Includes command */
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd) void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd)
{ {
char args[MAX_NUM_ARGS][MAX_STR_SIZE]; if (string_is_empty(cmd))
return;
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
int num_args = 0; int num_args = 0;
int i = 0; int i = 0;
bool f_end = false; bool cmd_end = false; // flags when we get to the end of cmd
char *start; char *end; // points to the end of the current arg
/* Put arguments into args array (characters wrapped in double quotes count as one arg) */ /* Put arguments into args array (characters wrapped in double quotes count as one arg) */
while (u_cmd[i] != '\0' && num_args < MAX_NUM_ARGS) { while (!cmd_end && num_args < MAX_NUM_ARGS) {
start = &u_cmd[i]; if (*cmd == '\"') {
end = strchr(cmd+1, '\"');
if (*start == '\"') { if (end++ == NULL) { /* Increment past the end quote */
while (u_cmd[++i] != '\"') { wprintw(window, "Invalid command. Did you forget a closing \"?\n");
if (u_cmd[i] == '\0') { return;
wprintw(window, "Invalid argument. Did you forget a closing \"?\n");
return;
}
} }
if (u_cmd[++i] == '\0') cmd_end = *end == '\0';
f_end = true;
} else { } else {
while (u_cmd[i] != ' ') { end = strchr(cmd, ' ');
if (u_cmd[++i] == '\0') { cmd_end = end == NULL;
f_end = true;
break;
}
}
} }
if (!f_end) if (!cmd_end)
u_cmd[i++] = '\0'; *end++ = '\0'; /* mark end of current argument */
/* Copy from start to current position */ /* Copy from start of current arg to where we just inserted the null byte */
strcpy(args[num_args++], start); strcpy(args[num_args++], cmd);
cmd = end;
} }
/* match input to command list */ /* match input to command list */