1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 02:13:01 +01:00
toxic/src/execute.c

125 lines
3.5 KiB
C
Raw Normal View History

2013-11-10 03:43:56 +01:00
/*
* Toxic -- Tox Curses Client
*/
#include <stdlib.h>
#include <string.h>
#include "toxic_windows.h"
#include "execute.h"
2013-11-10 19:23:47 +01:00
#include "chat_commands.h"
#include "global_commands.h"
struct cmd_func {
const char *name;
void (*func)(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
};
#define GLOBAL_NUM_COMMANDS 12
static struct cmd_func global_commands[] = {
{ "/accept", cmd_accept },
{ "/add", cmd_add },
{ "/clear", cmd_clear },
{ "/connect", cmd_connect },
{ "/exit", cmd_quit },
{ "/groupchat", cmd_groupchat },
{ "/myid", cmd_myid },
{ "/nick", cmd_nick },
{ "/note", cmd_note },
{ "/q", cmd_quit },
{ "/quit", cmd_quit },
{ "/status", cmd_status },
};
#define CHAT_NUM_COMMANDS 5
static struct cmd_func chat_commands[] = {
{ "/help", cmd_chat_help },
{ "/invite", cmd_groupinvite },
{ "/join", cmd_join_group },
{ "/savefile", cmd_savefile },
{ "/sendfile", cmd_sendfile },
};
2013-11-10 03:43:56 +01:00
2013-11-10 04:08:25 +01:00
/* Parses input command and puts args into arg array.
Returns number of arguments on success, -1 on failure. */
2013-11-10 19:23:47 +01:00
static int parse_command(WINDOW *w, char *cmd, char (*args)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
int num_args = 0;
bool cmd_end = false; // flags when we get to the end of cmd
char *end; // points to the end of the current arg
2013-11-10 04:08:25 +01:00
/* characters wrapped in double quotes count as one arg */
2013-11-10 03:43:56 +01:00
while (!cmd_end && num_args < MAX_NUM_ARGS) {
if (*cmd == '\"') {
end = strchr(cmd+1, '\"');
if (end++ == NULL) { /* Increment past the end quote */
2013-11-10 19:23:47 +01:00
wprintw(w, "Invalid argument. Did you forget a closing \"?\n");
2013-11-10 04:08:25 +01:00
return -1;
2013-11-10 03:43:56 +01:00
}
cmd_end = *end == '\0';
} else {
end = strchr(cmd, ' ');
cmd_end = end == NULL;
}
if (!cmd_end)
*end++ = '\0'; /* mark end of current argument */
/* Copy from start of current arg to where we just inserted the null byte */
strcpy(args[num_args++], cmd);
cmd = end;
}
return num_args;
}
2013-11-10 19:23:47 +01:00
/* Matches command to respective function. Returns 0 on match, 1 on no match */
static int do_command(WINDOW *w, ToxWindow *prompt, Tox *m, int num_args,
int num_cmds, struct cmd_func *commands, char (*args)[MAX_STR_SIZE])
{
int i;
for (i = 0; i < num_cmds; ++i) {
if (strcmp(args[0], commands[i].name) == 0) {
(commands[i].func)(w, prompt, m, num_args-1, args);
return 0;
}
}
return 1;
}
void execute(WINDOW *w, ToxWindow *prompt, Tox *m, char *cmd, int mode)
2013-11-10 03:43:56 +01:00
{
if (string_is_empty(cmd))
return;
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
2013-11-10 19:23:47 +01:00
int num_args = parse_command(w, cmd, args);
2013-11-10 03:43:56 +01:00
2013-11-10 04:08:25 +01:00
if (num_args == -1)
return;
2013-11-10 19:23:47 +01:00
/* Try to match input command to command functions. If non-global command mode is specified,
try specified mode's commands first, then upon failure try global commands. */
2013-11-10 03:43:56 +01:00
switch(mode) {
case CHAT_COMMAND_MODE:
2013-11-10 19:23:47 +01:00
if (do_command(w, prompt, m, num_args, CHAT_NUM_COMMANDS, chat_commands, args) == 0)
return;
2013-11-10 03:43:56 +01:00
break;
case GROUPCHAT_COMMAND_MODE:
break;
}
2013-11-10 19:23:47 +01:00
if (do_command(w, prompt, m, num_args, GLOBAL_NUM_COMMANDS, global_commands, args) == 0)
return;
2013-11-10 03:43:56 +01:00
2013-11-10 19:23:47 +01:00
wprintw(w, "Invalid command.\n");
2013-11-10 03:43:56 +01:00
}