server/src/kernel/command.c
Enno Rehling 03c247c788 I am killing the core/ directory.
Lots of files are moving to a new location in src/
Some intersting CMake changes.
2014-02-18 05:45:00 +01:00

101 lines
2.3 KiB
C

/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
+-------------------+ Stefan Reich <reich@halbling.de>
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include <kernel/config.h>
#include "command.h"
#include <kernel/order.h>
#include <util/umlaut.h>
#include <util/language.h>
#include <util/log.h>
#include <util/parser.h>
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct command {
parser fun;
void *nodes;
} command;
void *stree_find(const syntaxtree * stree, const struct locale *lang)
{
while (stree) {
if (stree->lang == lang)
return stree->root;
stree = stree->next;
}
return NULL;
}
syntaxtree *stree_create(void)
{
syntaxtree *sroot = NULL;
const struct locale *lang = locales;
while (lang) {
syntaxtree *stree = (syntaxtree *) malloc(sizeof(syntaxtree));
stree->lang = lang;
stree->next = sroot;
stree->root = 0;
sroot = stree;
lang = nextlocale(lang);
}
return sroot;
}
void
add_command(void **keys, void *tnext,
const char *str, parser fun)
{
command *cmd = (command *) malloc(sizeof(command));
variant var;
cmd->fun = fun;
cmd->nodes = tnext;
var.v = cmd;
addtoken(keys, str, var);
}
static int do_command_i(const void *keys, struct unit *u, struct order *ord)
{
const char *c;
variant var;
c = getstrtoken();
if (findtoken(keys, c, &var) == E_TOK_SUCCESS) {
command *cmd = (command *) var.v;
if (cmd->nodes && *c) {
assert(!cmd->fun);
return do_command_i(cmd->nodes, u, ord);
} else if (cmd->fun) {
cmd->fun(cmd->nodes, u, ord);
return E_TOK_SUCCESS;
}
}
return E_TOK_NOMATCH;
}
void do_command(const void *keys, struct unit *u, struct order *ord)
{
init_tokens(ord);
skip_token();
if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) {
char *cmd = getcommand(ord);
log_warning("%s failed command '%s'\n", unitname(u), cmd);
free(cmd);
}
}