server/src/kernel/command.c

117 lines
2.9 KiB
C
Raw Normal View History

/*
2010-08-08 10:06:34 +02:00
+-------------------+ 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
2010-08-08 10:06:34 +02:00
without prior permission by the authors of Eressea.
2010-08-08 10:06:34 +02:00
*/
#include <platform.h>
#include <kernel/config.h>
#include "command.h"
2017-10-08 17:07:42 +02:00
#include <kernel/faction.h>
2010-08-08 10:06:34 +02:00
#include <kernel/order.h>
#include <kernel/unit.h>
2010-08-08 10:06:34 +02:00
#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;
struct command *next;
2010-08-08 10:06:34 +02:00
} command;
void *stree_find(const syntaxtree * stree, const struct locale *lang)
2010-08-08 10:06:34 +02:00
{
while (stree) {
if (stree->lang == lang)
return stree->root;
stree = stree->next;
}
return NULL;
2010-08-08 10:06:34 +02:00
}
void stree_free(syntaxtree *stree) {
while (stree->cmds) {
command *next = stree->cmds->next;
free(stree->cmds);
stree->cmds = next;
}
while (stree) {
syntaxtree *snext = stree->next;
freetokens(stree->root);
free(stree);
stree = snext;
}
}
2011-03-07 08:02:35 +01:00
syntaxtree *stree_create(void)
2010-08-08 10:06:34 +02:00
{
syntaxtree *sroot = NULL;
const struct locale *lang = locales;
while (lang) {
syntaxtree *stree = (syntaxtree *)malloc(sizeof(syntaxtree));
2018-12-15 20:01:51 +01:00
if (!stree) abort();
stree->lang = lang;
stree->next = sroot;
stree->root = 0;
2016-09-07 17:48:27 +02:00
stree->cmds = 0;
sroot = stree;
lang = nextlocale(lang);
}
return sroot;
2010-08-08 10:06:34 +02:00
}
void stree_add(struct syntaxtree *stree, const char *str, parser fun) {
command *cmd = (command *)malloc(sizeof(command));
variant var;
assert(str);
2018-12-15 20:01:51 +01:00
if (!cmd) abort();
cmd->fun = fun;
var.v = cmd;
cmd->next = stree->cmds;
stree->cmds = cmd;
addtoken(&stree->root, str, var);
}
static int do_command_i(const struct tnode *keys, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
char token[128];
const char *c;
variant var;
2010-08-08 10:06:34 +02:00
c = gettoken(token, sizeof(token));
if (findtoken(keys, c, &var) == E_TOK_SUCCESS) {
command *cmd = (command *)var.v;
2016-09-07 15:36:48 +02:00
if (cmd->fun) {
cmd->fun(0, u, ord);
return E_TOK_SUCCESS;
}
2010-08-08 10:06:34 +02:00
}
return E_TOK_NOMATCH;
2010-08-08 10:06:34 +02:00
}
void do_command(const struct tnode *keys, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
2017-10-09 20:33:47 +02:00
init_order_depr(ord);
if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) {
char cmd[ORDERSIZE];
2017-10-08 17:07:42 +02:00
get_command(ord, u->faction->locale, cmd, sizeof(cmd));
log_warning("%s failed command '%s'\n", unitname(u), cmd);
}
2010-08-08 10:06:34 +02:00
}