diff options
Diffstat (limited to 'testing/toxic/prompt.c')
-rw-r--r-- | testing/toxic/prompt.c | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index e4eb259b..ab44e960 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -40,7 +40,7 @@ static struct { | |||
40 | void (*func)(ToxWindow *, char **); | 40 | void (*func)(ToxWindow *, char **); |
41 | } commands[] = { | 41 | } commands[] = { |
42 | { "accept", 1, cmd_accept }, | 42 | { "accept", 1, cmd_accept }, |
43 | { "add", 2, cmd_add }, | 43 | { "add", 1, cmd_add }, |
44 | { "clear", 0, cmd_clear }, | 44 | { "clear", 0, cmd_clear }, |
45 | { "connect", 3, cmd_connect }, | 45 | { "connect", 3, cmd_connect }, |
46 | { "exit", 0, cmd_quit }, | 46 | { "exit", 0, cmd_quit }, |
@@ -300,36 +300,54 @@ static void execute(ToxWindow *self, char *u_cmd) | |||
300 | break; | 300 | break; |
301 | cmd[cmd_end + 1] = '\0'; | 301 | cmd[cmd_end + 1] = '\0'; |
302 | 302 | ||
303 | char *args[4]; | 303 | /* insert \0 at argument boundaries */ |
304 | args[0] = strtok(cmd, " "); | 304 | int numargs = 0; |
305 | for (i = 0; i < MAX_STR_SIZE; i++) { | ||
306 | if (cmd[i] == '\"') | ||
307 | while (cmd[++i] != '\"'); /* skip over strings */ | ||
308 | if (cmd[i] == ' ') { | ||
309 | cmd[i] = '\0'; | ||
310 | numargs++; | ||
311 | } | ||
312 | } | ||
313 | |||
314 | /* excessive arguments */ | ||
315 | if (numargs > 3) { | ||
316 | wprintw(self->window, "Invalid command: too many arguments.\n"); | ||
317 | return; | ||
318 | } | ||
319 | |||
320 | /* read arguments into array */ | ||
321 | char *cmdargs[5]; | ||
322 | int pos = 0; | ||
323 | for (i = 0; i < 5; i++) { | ||
324 | cmdargs[i] = cmd + pos; | ||
325 | pos += strlen(cmdargs[i]) + 1; | ||
326 | } | ||
305 | 327 | ||
306 | /* no input */ | 328 | /* no input */ |
307 | if (!args[0]) | 329 | if (strlen(cmdargs[0]) == 0) |
308 | return; | 330 | return; |
309 | 331 | ||
310 | /* match input to command list */ | 332 | /* match input to command list */ |
311 | for (i = 0; i < NUM_COMMANDS; i++) { | 333 | for (i = 0; i < NUM_COMMANDS; i++) { |
312 | if (!strcmp(args[0], commands[i].name)) { | 334 | if (!strcmp(cmdargs[0], commands[i].name)) { |
313 | /* read in arguments */ | 335 | /* check for missing arguments */ |
314 | int j; | 336 | int j; |
315 | for (j = 1; j <= commands[i].numargs; j++) { | 337 | for (j = 0; j <= commands[i].numargs; j++) { |
316 | args[j] = strtok(NULL, " "); | 338 | if (strlen(cmdargs[j]) == 0) { |
317 | /* check for missing arguments */ | ||
318 | /* add is special because it can take either 1 or 2 arguments */ | ||
319 | if (strcmp(args[0], "add") != 0 && args[j] == NULL) { | ||
320 | wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n", | 339 | wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n", |
321 | commands[i].name, commands[i].numargs, j - 1); | 340 | commands[i].name, commands[i].numargs, j - 1); |
322 | return; | 341 | return; |
323 | } | 342 | } |
324 | } | 343 | } |
325 | /* check for excess arguments */ | 344 | /* check for excess arguments */ |
326 | /* add is special because the add message may contain spaces */ | 345 | if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) { |
327 | if (strcmp(args[0], "add") != 0 && strtok(NULL, " ") != NULL) { | ||
328 | wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); | 346 | wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); |
329 | return; | 347 | return; |
330 | } | 348 | } |
331 | /* pass arguments to command function */ | 349 | /* pass arguments to command function */ |
332 | (commands[i].func)(self, args); | 350 | (commands[i].func)(self, cmdargs); |
333 | return; | 351 | return; |
334 | } | 352 | } |
335 | } | 353 | } |