diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index c351ba5ee..7a65c4db5 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -288,14 +288,12 @@ static void perform_join(void) static syntaxtree * build_syntax(void) { syntaxtree *slang, *stree = stree_create(); for (slang = stree; slang; slang = slang->next) { - struct tnode *leaf = 0; - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick); - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave); - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer); - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new); - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite); - add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_JOIN]), &cmd_join); - slang->root = leaf; + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick); + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave); + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer); + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new); + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite); + stree_add(slang, LOC(slang->lang, alliance_kwd[ALLIANCE_JOIN]), &cmd_join); } return stree; } diff --git a/src/kernel/command.c b/src/kernel/command.c index 6f1c18809..912f21f7d 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -30,7 +30,7 @@ typedef struct command { parser fun; - void *nodes; + struct command *next; } command; void *stree_find(const syntaxtree * stree, const struct locale *lang) @@ -44,6 +44,11 @@ void *stree_find(const syntaxtree * stree, const struct locale *lang) } 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); @@ -61,24 +66,23 @@ syntaxtree *stree_create(void) stree->lang = lang; stree->next = sroot; stree->root = 0; + stree->cmds = 0; sroot = stree; lang = nextlocale(lang); } return sroot; } -void -add_command(struct tnode **keys, void *tnext, -const char *str, parser fun) -{ +void stree_add(struct syntaxtree *stree, const char *str, parser fun) { command *cmd = (command *)malloc(sizeof(command)); variant var; assert(str); cmd->fun = fun; - cmd->nodes = tnext; var.v = cmd; - addtoken(keys, str, var); + 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) @@ -90,12 +94,8 @@ static int do_command_i(const struct tnode *keys, struct unit *u, struct order * c = gettoken(token, sizeof(token)); 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); + if (cmd->fun) { + cmd->fun(0, u, ord); return E_TOK_SUCCESS; } } diff --git a/src/kernel/command.h b/src/kernel/command.h index e8d6bbaf2..8fba94b7b 100644 --- a/src/kernel/command.h +++ b/src/kernel/command.h @@ -20,19 +20,20 @@ extern "C" { struct order; struct unit; struct tnode; + struct command; typedef struct syntaxtree { const struct locale *lang; struct tnode *root; struct syntaxtree *next; + struct command *cmds; } syntaxtree; typedef void(*parser) (const void *nodes, struct unit * u, struct order *); - void add_command(struct tnode **troot, void *tnext, - const char *str, parser fun); void do_command(const struct tnode *troot, struct unit *u, struct order *); struct syntaxtree *stree_create(void); + void stree_add(struct syntaxtree *, const char *str, parser fun); void stree_free(struct syntaxtree *); void *stree_find(const struct syntaxtree *stree, const struct locale *lang); diff --git a/src/kernel/command.test.c b/src/kernel/command.test.c index edf521c81..f38c7e443 100644 --- a/src/kernel/command.test.c +++ b/src/kernel/command.test.c @@ -37,8 +37,8 @@ static void test_command(CuTest * tc) { CuAssertPtrEquals(tc, loc, (struct locale *)st->lang); CuAssertPtrEquals(tc, 0, st->root); CuAssertPtrEquals(tc, 0, st->next); - add_command(&st->root, 0, "two", parser_two); - add_command(&st->root, 0, "six", parser_six); + stree_add(st, "two", parser_two); + stree_add(st, "six", parser_six); CuAssertPtrNotNull(tc, st->root); CuAssertPtrEquals(tc, st->root, stree_find(st, loc)); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); @@ -54,4 +54,4 @@ CuSuite *get_command_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_command); return suite; -} \ No newline at end of file +}