diff --git a/src/Makefile.am b/src/Makefile.am index d957249..1cc5b09 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = wmud AM_CFLAGS = $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) -wmud_SOURCES = main.c networking.c +wmud_SOURCES = main.c networking.c interpreter.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) diff --git a/src/interpreter.c b/src/interpreter.c new file mode 100644 index 0000000..d52125d --- /dev/null +++ b/src/interpreter.c @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "networking.h" + +static void +command_part_print(GString *data, gpointer user_data) +{ + g_print("Part: %s\n", data->str); +} + +void +wmud_interpret_game_command(wmudClient *client) +{ + GSList *command_parts = NULL; + gchar *a, + *last_start = NULL; + gboolean in_string = FALSE; + gchar string_delim = 0; + gboolean prev_space = TRUE; + + g_print("Interpreting '%s' as a client command\n", client->buffer->str); + + for (a = client->buffer->str; (a - client->buffer->str) < client->buffer->len + 1; a++) + { + if ((!g_ascii_isspace(*a) && *a) || in_string) + { + if (prev_space) + { + last_start = a; + g_print("New token starts here: %s\n", a); + } + + prev_space = FALSE; + } + + if ((!in_string && ((*a == '\'') || (*a == '"'))) || (in_string && (*a == string_delim))) + { + g_print("String %s\n", (in_string) ? "ended" : "started"); + in_string = !in_string; + if (!in_string) + string_delim = 0; + prev_space = FALSE; + continue; + } + + if (in_string) + continue; + + if (g_ascii_isspace(*a) || !*a) + { + g_print("Space found.\n"); + if (!prev_space) + { + GString *token = g_string_new_len(last_start, a - last_start); + command_parts = g_slist_prepend(command_parts, token); + g_print("Found new token, %d long\n", a - last_start); + } + prev_space = TRUE; + } + } + + command_parts = g_slist_reverse(command_parts); + g_slist_foreach(command_parts, (GFunc)command_part_print, NULL); +} + +gboolean +wmud_interpreter_init(void) +{ + return TRUE; +} + diff --git a/src/interpreter.h b/src/interpreter.h new file mode 100644 index 0000000..9bb9cd8 --- /dev/null +++ b/src/interpreter.h @@ -0,0 +1,10 @@ +#ifndef __WMUD_INTERPRETER_H__ +# define __WMUD_INTERPRETER_H__ + +#include "networking.h" + +gboolean wmud_interpreter_init(void); +void wmud_interpret_game_command(wmudClient *client); + +#endif /* __WMUD_INTERPRETER_H__ */ + diff --git a/src/main.c b/src/main.c index 7f5e69b..f63247b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "main.h" #include "networking.h" +#include "interpreter.h" #define MAX_RECV_LEN 1024 @@ -70,6 +71,7 @@ main(int argc, char **argv) game_listener = g_socket_listener_new(); + wmud_interpreter_init(); wmud_networking_init(4000); g_print("Startup finished\n"); diff --git a/src/networking.c b/src/networking.c index 43b0979..30f6a46 100644 --- a/src/networking.c +++ b/src/networking.c @@ -4,6 +4,7 @@ #include "main.h" #include "networking.h" +#include "interpreter.h" #define MAX_RECV_LEN 1024 @@ -74,8 +75,7 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data buf2 = n; } - g_print("Will process input '%s'\n", client_data->buffer->str); - /* TODO: interpret command before erasing */ + wmud_interpret_game_command(client_data); g_string_erase(client_data->buffer, 0, -1); for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++);