From d962dd110a9b368de085cc4d6bae9874ece79cbc Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 15:29:40 +0200 Subject: [PATCH 1/7] correct type. --- src/kernel/command.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/command.c b/src/kernel/command.c index 6f1c18809..317d3fc2a 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -30,7 +30,7 @@ typedef struct command { parser fun; - void *nodes; + struct tnode *nodes; } command; void *stree_find(const syntaxtree * stree, const struct locale *lang) From eed6e1d88d2b83550342933a6cb6900c4b6f2957 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 15:36:48 +0200 Subject: [PATCH 2/7] add_command has an unused argument --- src/kernel/command.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/kernel/command.c b/src/kernel/command.c index 317d3fc2a..180c9068c 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -30,7 +30,6 @@ typedef struct command { parser fun; - struct tnode *nodes; } command; void *stree_find(const syntaxtree * stree, const struct locale *lang) @@ -69,14 +68,13 @@ syntaxtree *stree_create(void) void add_command(struct tnode **keys, void *tnext, -const char *str, parser fun) + 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); } @@ -90,12 +88,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; } } From 1ad2775f5138ca3108404320b3e18b023e47a428 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 15:43:04 +0200 Subject: [PATCH 3/7] simplify add_command --- src/kernel/alliance.c | 12 ++++++------ src/kernel/command.c | 2 +- src/kernel/command.h | 2 +- src/kernel/command.test.c | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index c351ba5ee..121e118c7 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -289,12 +289,12 @@ 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); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite); + add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_JOIN]), &cmd_join); slang->root = leaf; } return stree; diff --git a/src/kernel/command.c b/src/kernel/command.c index 180c9068c..fe2f2449e 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -67,7 +67,7 @@ syntaxtree *stree_create(void) } void -add_command(struct tnode **keys, void *tnext, +add_command(struct tnode **keys, const char *str, parser fun) { command *cmd = (command *)malloc(sizeof(command)); diff --git a/src/kernel/command.h b/src/kernel/command.h index e8d6bbaf2..94e38bcab 100644 --- a/src/kernel/command.h +++ b/src/kernel/command.h @@ -28,7 +28,7 @@ extern "C" { } syntaxtree; typedef void(*parser) (const void *nodes, struct unit * u, struct order *); - void add_command(struct tnode **troot, void *tnext, + void add_command(struct tnode **troot, const char *str, parser fun); void do_command(const struct tnode *troot, struct unit *u, struct order *); diff --git a/src/kernel/command.test.c b/src/kernel/command.test.c index edf521c81..76ef034c1 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); + add_command(&st->root, "two", parser_two); + add_command(&st->root, "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 +} From 632f25d42993f0173eeddcb2be447acaeaf6b8c6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 16:39:54 +0100 Subject: [PATCH 4/7] try to eliminate memory leak in command.test --- src/kernel/command.c | 18 ++++++++++++++++++ src/kernel/command.h | 3 +++ src/kernel/command.test.c | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/kernel/command.c b/src/kernel/command.c index fe2f2449e..15a226a80 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -30,6 +30,7 @@ typedef struct command { parser fun; + struct command *next; } command; void *stree_find(const syntaxtree * stree, const struct locale *lang) @@ -43,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); @@ -66,6 +72,18 @@ syntaxtree *stree_create(void) return sroot; } +void stree_add(struct syntaxtree *stree, const char *str, parser fun) { + command *cmd = (command *)malloc(sizeof(command)); + variant var; + + assert(str); + cmd->fun = fun; + var.v = cmd; + cmd->next = stree->cmds; + stree->cmds = cmd; + addtoken(&stree->root, str, var); +} + void add_command(struct tnode **keys, const char *str, parser fun) diff --git a/src/kernel/command.h b/src/kernel/command.h index 94e38bcab..17aa3e949 100644 --- a/src/kernel/command.h +++ b/src/kernel/command.h @@ -20,11 +20,13 @@ 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 *); @@ -33,6 +35,7 @@ extern "C" { 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 76ef034c1..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, "two", parser_two); - add_command(&st->root, "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)); From 9499919387a2eb88a487360f03320ec93794f300 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 17:48:27 +0200 Subject: [PATCH 5/7] fix uninitialized member --- src/kernel/command.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kernel/command.c b/src/kernel/command.c index 15a226a80..13e02ee2e 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -66,6 +66,7 @@ syntaxtree *stree_create(void) stree->lang = lang; stree->next = sroot; stree->root = 0; + stree->cmds = 0; sroot = stree; lang = nextlocale(lang); } From 4115bff79be4f43125b63f638748451ba1146120 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 17:50:51 +0200 Subject: [PATCH 6/7] eliminate leak from alliance.test --- src/kernel/alliance.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index 121e118c7..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, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick); - add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave); - add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer); - add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new); - add_command(&leaf, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite); - add_command(&leaf, 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; } From af310d48d662d5b20266c45bdced5026f6cce1d3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 7 Sep 2016 17:52:12 +0200 Subject: [PATCH 7/7] remove unused function after refactoring --- src/kernel/command.c | 13 ------------- src/kernel/command.h | 2 -- 2 files changed, 15 deletions(-) diff --git a/src/kernel/command.c b/src/kernel/command.c index 13e02ee2e..912f21f7d 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -85,19 +85,6 @@ void stree_add(struct syntaxtree *stree, const char *str, parser fun) { addtoken(&stree->root, str, var); } -void -add_command(struct tnode **keys, - const char *str, parser fun) -{ - command *cmd = (command *)malloc(sizeof(command)); - variant var; - - assert(str); - cmd->fun = fun; - var.v = cmd; - addtoken(keys, str, var); -} - static int do_command_i(const struct tnode *keys, struct unit *u, struct order *ord) { char token[128]; diff --git a/src/kernel/command.h b/src/kernel/command.h index 17aa3e949..8fba94b7b 100644 --- a/src/kernel/command.h +++ b/src/kernel/command.h @@ -30,8 +30,6 @@ extern "C" { } syntaxtree; typedef void(*parser) (const void *nodes, struct unit * u, struct order *); - void add_command(struct tnode **troot, - const char *str, parser fun); void do_command(const struct tnode *troot, struct unit *u, struct order *); struct syntaxtree *stree_create(void);