server/src/common/util/command.c

99 lines
2.1 KiB
C
Raw Normal View History

2001-04-26 19:41:06 +02:00
/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea-pbem.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
2001-04-26 19:41:06 +02:00
| | 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 <config.h>
#include "command.h"
#include "umlaut.h"
2002-09-02 22:36:12 +02:00
#include "language.h"
2001-04-26 19:41:06 +02:00
/* libc includes */
2002-09-02 22:36:12 +02:00
#include <assert.h>
2001-04-26 19:41:06 +02:00
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct command {
2002-09-02 22:36:12 +02:00
parser fun;
struct tnode * nodes;
2001-04-26 19:41:06 +02:00
} command;
2002-09-02 22:36:12 +02:00
tnode *
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));
2002-09-02 22:36:12 +02:00
stree->lang = lang;
stree->next = sroot;
sroot=stree;
lang=nextlocale(lang);
}
return sroot;
}
2001-04-26 19:41:06 +02:00
void
2002-09-02 22:36:12 +02:00
add_command(struct tnode * keys, struct tnode * tnext,
const char * str, parser fun)
2001-04-26 19:41:06 +02:00
{
command * cmd = (command *)malloc(sizeof(command));
2002-02-23 12:30:41 +01:00
cmd->fun = fun;
2002-09-02 22:36:12 +02:00
cmd->nodes = tnext;
2002-02-23 12:30:41 +01:00
addtoken(keys, str, (void*)cmd);
2001-04-26 19:41:06 +02:00
}
2004-06-21 18:45:27 +02:00
static void
do_command_i(const struct tnode * keys, void * u, const char * str, struct order * ord)
2001-04-26 19:41:06 +02:00
{
int i;
char zText[16];
const char * c;
2002-02-23 12:30:41 +01:00
command * cmd;
2001-04-26 19:41:06 +02:00
while (isspace(*str)) ++str;
c = str;
while (isalnum(*c)) ++c;
i = min(16, c-str);
strncpy(zText, str, i);
zText[i]=0;
2002-02-23 12:30:41 +01:00
if (findtoken(keys, zText, (void**)&cmd)==E_TOK_SUCCESS) {
2002-09-02 22:36:12 +02:00
if (cmd->nodes) {
assert(!cmd->fun);
2004-06-21 18:45:27 +02:00
do_command_i(cmd->nodes, u, ++c, ord);
2002-09-02 22:36:12 +02:00
return;
}
assert(cmd->fun);
2004-06-21 18:45:27 +02:00
cmd->fun(cmd->nodes, ++c, u, ord);
2002-02-23 12:30:41 +01:00
}
2004-06-21 18:45:27 +02:00
}
extern char * getcommand(struct order * ord);
2004-06-21 18:45:27 +02:00
void
do_command(const struct tnode * keys, void * u, struct order * ord)
{
char * cmd = getcommand(ord);
do_command_i(keys, u, cmd, ord);
free(cmd);
2001-04-26 19:41:06 +02:00
}