/* vi: set ts=2: +-------------------+ Christian Schlittchen | | Enno Rehling | Eressea PBEM host | Katja Zedel | (c) 1998 - 2003 | Henning Peters | | Ingo Wilken +-------------------+ Stefan Reich This program may not be used, modified or distributed without prior permission by the authors of Eressea. */ #include #include #include "command.h" #include #include #include #include #include /* libc includes */ #include #include #include #include typedef struct command { parser fun; struct tnode *nodes; } command; 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)); stree->lang = lang; stree->next = sroot; sroot = stree; lang = nextlocale(lang); } return sroot; } void add_command(struct tnode *keys, struct tnode *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 struct tnode *keys, void *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 struct tnode *keys, void *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); } }