mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 02:13:01 +01:00
generalize command matching loop
This commit is contained in:
parent
5a7f26cfea
commit
bc4e089a65
@ -11,11 +11,11 @@
|
|||||||
|
|
||||||
#include "toxic_windows.h"
|
#include "toxic_windows.h"
|
||||||
#include "misc_tools.h"
|
#include "misc_tools.h"
|
||||||
#include "chat_commands.h"
|
#include "execute.h"
|
||||||
|
|
||||||
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
||||||
|
|
||||||
void cmd_help(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
void cmd_chat_help(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||||
{
|
{
|
||||||
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
|
wattron(window, COLOR_PAIR(CYAN) | A_BOLD);
|
||||||
wprintw(window, "Chat commands:\n");
|
wprintw(window, "Chat commands:\n");
|
||||||
|
@ -2,22 +2,8 @@
|
|||||||
* Toxic -- Tox Curses Client
|
* Toxic -- Tox Curses Client
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* commands */
|
void cmd_chat_help(WINDOW *w, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_help(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_groupinvite(WINDOW *w, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_groupinvite(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_join_group(WINDOW *w, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_join_group(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_savefile(WINDOW *w, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_savefile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_sendfile(WINDOW *w, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_sendfile(WINDOW *window, ToxWindow *chatwin, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
|
||||||
|
|
||||||
#define CHAT_NUM_COMMANDS 5
|
|
||||||
|
|
||||||
static struct {
|
|
||||||
char *name;
|
|
||||||
void (*func)(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
|
||||||
} chat_commands[] = {
|
|
||||||
{ "/help", cmd_help },
|
|
||||||
{ "/invite", cmd_groupinvite },
|
|
||||||
{ "/join", cmd_join_group },
|
|
||||||
{ "/savefile", cmd_savefile },
|
|
||||||
{ "/sendfile", cmd_sendfile },
|
|
||||||
};
|
|
||||||
|
@ -6,13 +6,45 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "toxic_windows.h"
|
#include "toxic_windows.h"
|
||||||
#include "global_commands.h"
|
|
||||||
#include "chat_commands.h"
|
|
||||||
#include "execute.h"
|
#include "execute.h"
|
||||||
|
#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 },
|
||||||
|
};
|
||||||
|
|
||||||
/* Parses input command and puts args into arg array.
|
/* Parses input command and puts args into arg array.
|
||||||
Returns number of arguments on success, -1 on failure. */
|
Returns number of arguments on success, -1 on failure. */
|
||||||
static int parse_command(WINDOW *window, char *cmd, char (*args)[MAX_STR_SIZE])
|
static int parse_command(WINDOW *w, char *cmd, char (*args)[MAX_STR_SIZE])
|
||||||
{
|
{
|
||||||
int num_args = 0;
|
int num_args = 0;
|
||||||
bool cmd_end = false; // flags when we get to the end of cmd
|
bool cmd_end = false; // flags when we get to the end of cmd
|
||||||
@ -24,7 +56,7 @@ static int parse_command(WINDOW *window, char *cmd, char (*args)[MAX_STR_SIZE])
|
|||||||
end = strchr(cmd+1, '\"');
|
end = strchr(cmd+1, '\"');
|
||||||
|
|
||||||
if (end++ == NULL) { /* Increment past the end quote */
|
if (end++ == NULL) { /* Increment past the end quote */
|
||||||
wprintw(window, "Invalid argument. Did you forget a closing \"?\n");
|
wprintw(w, "Invalid argument. Did you forget a closing \"?\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,43 +77,48 @@ static int parse_command(WINDOW *window, char *cmd, char (*args)[MAX_STR_SIZE])
|
|||||||
return num_args;
|
return num_args;
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd, int mode)
|
/* 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)
|
||||||
{
|
{
|
||||||
if (string_is_empty(cmd))
|
if (string_is_empty(cmd))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
|
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
|
||||||
int num_args = parse_command(window, cmd, args);
|
int num_args = parse_command(w, cmd, args);
|
||||||
|
|
||||||
if (num_args == -1)
|
if (num_args == -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Attempt to match input to command functions. If non-global command mode is specified,
|
/* Try to match input command to command functions. If non-global command mode is specified,
|
||||||
try the specified mode's commands first, then upon failure try global commands.
|
try specified mode's commands first, then upon failure try global commands. */
|
||||||
|
|
||||||
TODO: Generalize command matching loop in a separate function */
|
|
||||||
int i;
|
|
||||||
|
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case CHAT_COMMAND_MODE:
|
case CHAT_COMMAND_MODE:
|
||||||
for (i = 0; i < CHAT_NUM_COMMANDS; ++i) {
|
if (do_command(w, prompt, m, num_args, CHAT_NUM_COMMANDS, chat_commands, args) == 0)
|
||||||
if (strcmp(args[0], chat_commands[i].name) == 0) {
|
return;
|
||||||
(chat_commands[i].func)(window, prompt, m, num_args-1, args);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case GROUPCHAT_COMMAND_MODE:
|
case GROUPCHAT_COMMAND_MODE:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < GLOBAL_NUM_COMMANDS; ++i) {
|
if (do_command(w, prompt, m, num_args, GLOBAL_NUM_COMMANDS, global_commands, args) == 0)
|
||||||
if (strcmp(args[0], global_commands[i].name) == 0) {
|
return;
|
||||||
(global_commands[i].func)(window, prompt, m, num_args-1, args);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wprintw(window, "Invalid command.\n");
|
|
||||||
|
wprintw(w, "Invalid command.\n");
|
||||||
}
|
}
|
||||||
|
@ -10,4 +10,4 @@ enum {
|
|||||||
GROUPCHAT_COMMAND_MODE,
|
GROUPCHAT_COMMAND_MODE,
|
||||||
};
|
};
|
||||||
|
|
||||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd, int mode);
|
void execute(WINDOW *w, ToxWindow *prompt, Tox *m, char *cmd, int mode);
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#include "toxic_windows.h"
|
#include "toxic_windows.h"
|
||||||
#include "misc_tools.h"
|
#include "misc_tools.h"
|
||||||
#include "global_commands.h"
|
#include "execute.h"
|
||||||
|
|
||||||
extern char *DATA_FILE;
|
extern char *DATA_FILE;
|
||||||
|
|
||||||
|
@ -2,34 +2,13 @@
|
|||||||
* Toxic -- Tox Curses Client
|
* Toxic -- Tox Curses Client
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* commands */
|
void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
|
|
||||||
|
|
||||||
#define GLOBAL_NUM_COMMANDS 12
|
|
||||||
|
|
||||||
static struct {
|
|
||||||
char *name;
|
|
||||||
void (*func)(WINDOW *, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
|
||||||
} 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 },
|
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user