Command parsing rewritten

* The command parsing code is completely rewritten. I'm almost sure it's not as
  effective as it could be, but it's kinda fast. Also, a basic QUIT command is
  already added to the system, which does nothing yet.
This commit is contained in:
Polonkai Gergely 2012-03-14 19:06:02 +00:00
parent be3e17fd07
commit c715d19b2d

View File

@ -7,15 +7,17 @@
#define IS_SPACE(c) (g_ascii_isspace((c)) || (!(c)))
WMUD_COMMAND(gcmd_quit);
static wmudCommand command_list[] = {
{ "quit", gcmd_quit },
{ NULL, NULL },
};
static void
command_part_print(GString *data, gpointer user_data)
destroy_string(GString *string)
{
g_print("Part: %s\n", data->str);
g_string_free(string, TRUE);
}
void
@ -23,60 +25,122 @@ 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;
*start,
*end;
gchar str_delim = 0;
wmudCommand *cmd;
int command_parts_count = 0,
match_count = 0;
GSList *matches = NULL;
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 (strchr(client->buffer->str, '\r') || strchr(client->buffer->str, '\n'))
{
if (IS_SPACE(*a) && prev_space)
continue;
if (IS_SPACE(*a) && !prev_space && !in_string)
{
GString *token = g_string_new_len(last_start, a - last_start);
command_parts = g_slist_prepend(command_parts, token);
prev_space = TRUE;
continue;
}
if (!IS_SPACE(*a) && prev_space)
{
last_start = a;
prev_space = FALSE;
}
if (((*a == '\'') || (*a == '"')) && !in_string)
{
in_string = TRUE;
string_delim = *a;
prev_space = TRUE;
continue;
}
if (in_string && (*a == string_delim))
{
in_string = FALSE;
string_delim = 0;
prev_space = TRUE;
continue;
}
/* TODO: We should NEVER reach this point! */
return;
}
if (in_string)
a = client->buffer->str;
GString *token;
while (*a)
{
GString *buf = g_string_new("");
g_string_printf(buf, "You should close quotation characters, like %c...\r\n", string_delim);
/* TODO: error checking */
g_socket_send(client->socket, buf->str, buf->len, NULL, NULL);
g_string_free(buf, TRUE);
for (start = a; *start; start++)
{
if (!str_delim)
{
if ((*start == '"') || (*start == '\''))
{
str_delim = *start;
start++;
break;
}
else if (!IS_SPACE(*start))
{
break;
}
}
}
for (end = start; *end; end++)
{
if (!str_delim && strchr("'\" \t", *end))
{
break;
}
else if (str_delim && (*end == str_delim))
{
str_delim = 0;
break;
}
else if (!*end)
{
break;
}
}
if (*start)
{
token = g_string_new_len(start, end - start);
command_parts = g_slist_prepend(command_parts, token);
command_parts_count++;
}
a = end;
if (((*a == '"') || (*a == '\'')) && str_delim)
a++;
}
if (str_delim)
{
wmud_client_send(client, "You should close quotes of any kind, like %c, shouldn't you?\r\n", str_delim);
#if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(command_parts, destroy_string);
#else
g_slist_foreach(command_parts, (GFunc)destroy_string, NULL);
g_slist_free(command_parts);
#endif
return;
}
if (command_parts_count == 0)
{
/* TODO: handle empty command */
return;
}
command_parts = g_slist_reverse(command_parts);
g_slist_foreach(command_parts, (GFunc)command_part_print, NULL);
for (cmd = command_list; cmd->command; cmd++)
{
GString *input = (GString *)(command_parts->data);
gint cmp;
if (((cmp = g_ascii_strncasecmp(input->str, cmd->command, input->len)) == 0) && !cmd->command[input->len])
{
g_slist_free(matches);
match_count = 1;
matches = NULL;
matches = g_slist_prepend(matches, cmd);
break;
}
else if (cmp == 0)
{
matches = g_slist_prepend(matches, cmd);
match_count++;
}
}
if (match_count == 1)
{
((wmudCommand *)(matches->data))->commandFunc(client, ((GString *)(command_parts->data))->str, command_parts->next);
}
else
{
wmud_client_send(client, "This command could mean several things, please try a more exact form!\r\n");
}
g_slist_free(matches);
}
gboolean
@ -85,3 +149,8 @@ wmud_interpreter_init(void)
return TRUE;
}
WMUD_COMMAND(gcmd_quit)
{
wmud_client_send(client, "Are you sure you want to get back to that freaky other reality? [y/N] ");
}