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

rewrote execute function

This commit is contained in:
Jfreegman 2013-10-20 00:50:08 -04:00
parent 7a0951afc6
commit 4ad6853e2e
3 changed files with 57 additions and 131 deletions

View File

@ -21,7 +21,7 @@ extern uint8_t num_frnd_requests;
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];
/* command functions */ /* command functions */
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
/* check arguments */ /* check arguments */
if (argc != 1) { if (argc != 1) {
@ -62,7 +62,7 @@ void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv
num_frnd_requests = i; num_frnd_requests = i;
} }
void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
if (argc < 1) { if (argc < 1) {
wprintw(window, "Invalid syntax.\n"); wprintw(window, "Invalid syntax.\n");
@ -70,22 +70,11 @@ void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
} }
char *id = argv[1]; char *id = argv[1];
if (id == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
uint8_t *msg; uint8_t *msg;
if (argc > 1) { if (argc > 1) {
msg = argv[2]; msg = argv[2];
if (msg == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
if (msg[0] != '\"') { if (msg[0] != '\"') {
wprintw(window, "Message must be enclosed in quotes.\n"); wprintw(window, "Message must be enclosed in quotes.\n");
return; return;
@ -161,13 +150,13 @@ void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
} }
} }
void cmd_clear(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_clear(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
wclear(window); wclear(window);
wprintw(window, "\n\n"); wprintw(window, "\n\n");
} }
void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
/* check arguments */ /* check arguments */
if (argc != 3) { if (argc != 3) {
@ -180,11 +169,6 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
char *port = argv[2]; char *port = argv[2];
char *key = argv[3]; char *key = argv[3];
if (ip == NULL || port == NULL || key == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
if (atoi(port) == 0) { if (atoi(port) == 0) {
wprintw(window, "Invalid syntax.\n"); wprintw(window, "Invalid syntax.\n");
return; return;
@ -196,7 +180,7 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
free(binary_string); free(binary_string);
} }
void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
int ngc = get_num_groupchats(); int ngc = get_num_groupchats();
@ -221,18 +205,13 @@ void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **a
wprintw(window, "Group chat created as %d.\n", groupnum); wprintw(window, "Group chat created as %d.\n", groupnum);
} }
void cmd_join(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_join(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
if (argc != 1) { if (argc != 1) {
wprintw(window, "Invalid syntax.\n"); wprintw(window, "Invalid syntax.\n");
return; return;
} }
if (argv[1] == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
int num = atoi(argv[1]); int num = atoi(argv[1]);
if (num < 0 || num >= MAX_FRIENDS_NUM) { if (num < 0 || num >= MAX_FRIENDS_NUM) {
@ -261,7 +240,7 @@ void cmd_join(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
} }
} }
void cmd_myid(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_myid(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
@ -278,7 +257,7 @@ void cmd_myid(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
wprintw(window, "%s\n", id); wprintw(window, "%s\n", id);
} }
void cmd_nick(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_nick(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
/* check arguments */ /* check arguments */
if (argc != 1) { if (argc != 1) {
@ -287,12 +266,6 @@ void cmd_nick(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
} }
uint8_t *nick = argv[1]; uint8_t *nick = argv[1];
if (nick == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
int len = strlen(nick); int len = strlen(nick);
if (nick[0] == '\"') { if (nick[0] == '\"') {
@ -312,7 +285,7 @@ void cmd_nick(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
} }
void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
if (argc < 1) { if (argc < 1) {
wprintw(window, "Wrong number of arguments.\n"); wprintw(window, "Wrong number of arguments.\n");
@ -321,11 +294,6 @@ void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
uint8_t *msg = argv[1]; uint8_t *msg = argv[1];
if (msg == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
if (msg[0] != '\"') { if (msg[0] != '\"') {
wprintw(window, "Note must be enclosed in quotes.\n"); wprintw(window, "Note must be enclosed in quotes.\n");
return; return;
@ -337,24 +305,18 @@ void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
prompt_update_statusmessage(prompt, msg, len); prompt_update_statusmessage(prompt, msg, len);
} }
void cmd_quit(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_quit(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
exit_toxic(m); exit_toxic(m);
} }
void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
uint8_t *msg = NULL; uint8_t *msg = NULL;
if (argc == 2) { if (argc == 2) {
msg = argv[2]; msg = argv[2];
if (msg == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
if (msg[0] != '\"') { if (msg[0] != '\"') {
wprintw(window, "Note must be enclosed in quotes.\n"); wprintw(window, "Note must be enclosed in quotes.\n");
return; return;
@ -365,12 +327,6 @@ void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv
} }
char *status = argv[1]; char *status = argv[1];
if (status == NULL) {
wprintw(window, "Invalid syntax.\n");
return;
}
int len = strlen(status); int len = strlen(status);
char l_status[len+1]; char l_status[len+1];
int i; int i;
@ -405,85 +361,55 @@ void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv
} }
} }
#define MAX_NUM_ARGS 4 /* Includes command */
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len) void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len)
{ {
int newlines = 0; char args[MAX_NUM_ARGS][MAX_STR_SIZE];
char cmd[MAX_STR_SIZE] = {'\0'}; int num_args = 0;
size_t i; int i = 0;
bool f_end = false;
char *start;
for (i = 0; i < buf_len; ++i) { /* Put arguments into args array (characters wrapped in double quotes count as one arg) */
if (u_cmd[i] == '\n') while (u_cmd[i] != '\0' && num_args < MAX_NUM_ARGS) {
++newlines; start = &u_cmd[i];
else
cmd[i - newlines] = u_cmd[i]; if (u_cmd[i] == '\"') {
while (u_cmd[++i] != '\"') {
if (u_cmd[i] == '\0') {
wprintw(window, "Invalid command. Did you forget a closing \"?\n");
return;
}
} }
int leading_spc = 0; if (u_cmd[++i] == '\0')
f_end = true;
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) } else {
leading_spc++; while (u_cmd[i] != ' ') {
if (u_cmd[++i] == '\0') {
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); f_end = true;
int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--)
if (!isspace(cmd[cmd_end]))
break; break;
cmd[cmd_end + 1] = '\0';
/* insert \0 at argument boundaries */
int numargs = 0;
for (i = 0; i < MAX_STR_SIZE; i++) {
if (cmd[i] == ' ') {
cmd[i] = '\0';
numargs++;
}
/* skip over strings */
else if (cmd[i] == '\"') {
while (cmd[++i] != '\"') {
if (cmd[i] == '\0') {
wprintw(window, "Invalid command: did you forget an opening or closing \"?\n");
return;
}
} }
} }
} }
/* read arguments into array */ /* Copy from start to current position */
char **cmdargs = malloc((numargs + 1) * sizeof(char *)); u_cmd[i] = '\0';
if (!cmdargs) { strcpy(args[num_args++], start);
wprintw(window, "Invalid command: too many arguments.\n");
return;
}
int pos = 0; if (!f_end)
++i;
for (i = 0; i < numargs + 1; i++) {
cmdargs[i] = cmd + pos;
pos += strlen(cmdargs[i]) + 1;
/* replace empty strings with NULL for easier error checking */
if (strlen(cmdargs[i]) == 0)
cmdargs[i] = NULL;
}
/* no input */
if (!cmdargs[0]) {
free(cmdargs);
return;
} }
/* match input to command list */ /* match input to command list */
for (i = 0; i < NUM_COMMANDS; i++) { for (i = 0; i < NUM_COMMANDS; ++i) {
if (!strcmp(cmdargs[0], commands[i].name)) { if (strcmp(args[0], commands[i].name) == 0) {
(commands[i].func)(window, prompt, m, numargs, cmdargs); (commands[i].func)(window, prompt, m, num_args-1, args);
free(cmdargs);
return; return;
} }
} }
/* no match */
free(cmdargs);
wprintw(window, "Invalid command.\n"); wprintw(window, "Invalid command.\n");
} }

View File

@ -3,17 +3,17 @@
*/ */
/* commands */ /* commands */
void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len); void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
@ -21,7 +21,7 @@ void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len
static struct { static struct {
char *name; char *name;
void (*func)(WINDOW *, ToxWindow *, Tox *m, int, char **); void (*func)(WINDOW *, ToxWindow *, Tox *m, int, char (*argv)[MAX_STR_SIZE]);
} commands[] = { } commands[] = {
{ "/accept", cmd_accept }, { "/accept", cmd_accept },
{ "/add", cmd_add }, { "/add", cmd_add },

View File

@ -11,7 +11,7 @@ struct tm *get_time(void);
/* Prints the time to given window */ /* Prints the time to given window */
void print_time(WINDOW *window); void print_time(WINDOW *window);
/* check that the string has one non-space character */ /* Returns 1 if the string is empty, 0 otherwise */
int string_is_empty(char *string); int string_is_empty(char *string);
/* convert wide characters to null terminated string */ /* convert wide characters to null terminated string */