diff options
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/cmumble.c | 1 | ||||
-rw-r--r-- | src/cmumble.h | 3 | ||||
-rw-r--r-- | src/commands.c | 60 | ||||
-rw-r--r-- | src/commands.h | 15 | ||||
-rw-r--r-- | src/io.c | 18 |
6 files changed, 93 insertions, 10 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 3dec946..40d19e8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,11 @@ bin_PROGRAMS = cmumble -noinst_HEADERS = cmumble.h message.h varint.h io.h connection.h audio.h +noinst_HEADERS = cmumble.h message.h varint.h io.h \ + connection.h audio.h commands.h nodist_noinst_HEADERS = mumble.pb-c.h message_list.h -cmumble_SOURCES = cmumble.c message.c varint.c io.c connection.c audio.c +cmumble_SOURCES = cmumble.c message.c varint.c io.c \ + connection.c audio.c commands.c nodist_cmumble_SOURCES = mumble.pb-c.c cmumble_LDADD = $(PROTOBUF_LIBS) $(GLIB_LIBS) $(GIO_LIBS) \ diff --git a/src/cmumble.c b/src/cmumble.c index 33ba9d4..d56256b 100644 --- a/src/cmumble.c +++ b/src/cmumble.c @@ -207,6 +207,7 @@ int main(int argc, char **argv) ctx.loop = g_main_loop_new(NULL, FALSE); ctx.callbacks = (const callback_t *) &callbacks; + cmumble_commands_init(&ctx); if (cmumble_connection_init(&ctx, host, port) < 0) return 1; diff --git a/src/cmumble.h b/src/cmumble.h index 2ea1203..63b332a 100644 --- a/src/cmumble.h +++ b/src/cmumble.h @@ -10,6 +10,7 @@ #include "io.h" #include "connection.h" #include "audio.h" +#include "commands.h" typedef void (*callback_t)(ProtobufCMessage *msg, struct context *); @@ -18,6 +19,8 @@ struct context { struct cmumble_io io; struct cmumble_audio audio; const callback_t *callbacks; + const struct cmumble_command *commands; + GMainLoop *loop; uint32_t session; diff --git a/src/commands.c b/src/commands.c new file mode 100644 index 0000000..be78a69 --- /dev/null +++ b/src/commands.c @@ -0,0 +1,60 @@ +#include "../config.h" +#include "commands.h" +#include "cmumble.h" + +#include <glib.h> + +#include <readline/readline.h> +#include <readline/history.h> + +static void +list_users(struct context *ctx) +{ + struct user *user = NULL; + GList *l; + + for (l = ctx->users; l; l = l->next) { + user = l->data; + + g_print("%4d: %s\n", user->user_id, user->name); + } +} + +static void +quit(struct context *ctx) +{ + rl_already_prompted = 1; + g_main_loop_quit(ctx->loop); +} + +static void +clear(struct context *ctx) +{ + rl_clear_screen(0,0); + rl_reset_line_state(); + g_print("\n"); +} + +static void +help(struct context *ctx) +{ + int i; + + for (i = 0; ctx->commands[i].name; ++i) + g_print("%s\t%s\n", + ctx->commands[i].name, ctx->commands[i].description); +} + +static const struct cmumble_command commands[] = { + { "ls", list_users, "list users" }, + { "clear", clear, "clear screen" }, + { "help", help, "show this help" }, + { "quit", quit, "quit " PACKAGE }, + { NULL, NULL , NULL} +}; + +void +cmumble_commands_init(struct context *ctx) +{ + ctx->commands = commands; +} diff --git a/src/commands.h b/src/commands.h new file mode 100644 index 0000000..2b1ad66 --- /dev/null +++ b/src/commands.h @@ -0,0 +1,15 @@ +#ifndef _COMMANDS_H_ +#define _COMMANDS_H_ + +struct context; + +struct cmumble_command { + const char *name; + void (*callback)(struct context *); + const char *description; +}; + +void +cmumble_commands_init(struct context *ctx); + +#endif /* _COMMANDS_H_ */ @@ -66,6 +66,7 @@ static void process_line(char *line) { struct context *ctx = global_rl_user_data; + int i; g_assert(global_rl_user_data); @@ -79,14 +80,15 @@ process_line(char *line) return; } - if (strcmp(line, "quit") == 0) { - rl_already_prompted = 1; - g_main_loop_quit(ctx->loop); - } else if (strcmp(line, "clear") == 0) { - rl_clear_screen(0,0); - rl_reset_line_state(); - } else - g_print("readline: %s\n", line); + for (i = 0; ctx->commands[i].name; ++i) { + if (strcmp(line, ctx->commands[i].name) == 0) { + ctx->commands[i].callback(ctx); + break; + } + } + + if (ctx->commands[i].name == NULL) + g_print("Unknown command: %s\n", line); if (strlen(line)) add_history(line); |