Added a basic (non-working) command interpreter.

Signed-off-by: Polonkai Gergely <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely 2012-03-13 02:22:22 +01:00
parent 84ba83c8ba
commit 74fe14660a
5 changed files with 88 additions and 3 deletions

View File

@ -1,5 +1,5 @@
bin_PROGRAMS = wmud bin_PROGRAMS = wmud
AM_CFLAGS = $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) 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) wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS)

73
src/interpreter.c Normal file
View File

@ -0,0 +1,73 @@
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
#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;
}

10
src/interpreter.h Normal file
View File

@ -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__ */

View File

@ -6,6 +6,7 @@
#include "main.h" #include "main.h"
#include "networking.h" #include "networking.h"
#include "interpreter.h"
#define MAX_RECV_LEN 1024 #define MAX_RECV_LEN 1024
@ -70,6 +71,7 @@ main(int argc, char **argv)
game_listener = g_socket_listener_new(); game_listener = g_socket_listener_new();
wmud_interpreter_init();
wmud_networking_init(4000); wmud_networking_init(4000);
g_print("Startup finished\n"); g_print("Startup finished\n");

View File

@ -4,6 +4,7 @@
#include "main.h" #include "main.h"
#include "networking.h" #include "networking.h"
#include "interpreter.h"
#define MAX_RECV_LEN 1024 #define MAX_RECV_LEN 1024
@ -74,8 +75,7 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data
buf2 = n; buf2 = n;
} }
g_print("Will process input '%s'\n", client_data->buffer->str); wmud_interpret_game_command(client_data);
/* TODO: interpret command before erasing */
g_string_erase(client_data->buffer, 0, -1); g_string_erase(client_data->buffer, 0, -1);
for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++); for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++);