diff options
-rw-r--r-- | include/baresip.h | 8 | ||||
-rw-r--r-- | src/baresip.c | 6 | ||||
-rw-r--r-- | src/cmd.c | 33 | ||||
-rw-r--r-- | test/cmd.c | 32 |
4 files changed, 43 insertions, 36 deletions
diff --git a/include/baresip.h b/include/baresip.h index 08ea908..5256701 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -680,14 +680,10 @@ struct cmd { }; struct cmd_ctx; - -struct commands { - struct list cmdl; /**< List of command blocks (struct cmds) */ -}; +struct commands; -int cmd_init(struct commands *commands); -void cmd_close(struct commands *commands); +int cmd_init(struct commands **commandsp); int cmd_register(struct commands *commands, const struct cmd *cmdv, size_t cmdc); void cmd_unregister(struct commands *commands, const struct cmd *cmdv); diff --git a/src/baresip.c b/src/baresip.c index 3ef745e..07dfbca 100644 --- a/src/baresip.c +++ b/src/baresip.c @@ -15,7 +15,7 @@ static struct baresip { struct network *net; struct contacts contacts; - struct commands commands; + struct commands *commands; struct player *player; struct list mnatl; struct list mencl; @@ -61,7 +61,7 @@ int baresip_init(struct config *cfg, bool prefer_ipv6) void baresip_close(void) { baresip.player = mem_deref(baresip.player); - cmd_close(&baresip.commands); + baresip.commands = mem_deref(baresip.commands); contact_close(&baresip.contacts); baresip.net = mem_deref(baresip.net); @@ -82,7 +82,7 @@ struct contacts *baresip_contacts(void) struct commands *baresip_commands(void) { - return &baresip.commands; + return baresip.commands; } @@ -28,6 +28,10 @@ struct cmd_ctx { bool is_long; }; +struct commands { + struct list cmdl; /**< List of command blocks (struct cmds) */ +}; + static int cmd_print_all(struct re_printf *pf, const struct commands *commands, @@ -51,6 +55,14 @@ static void ctx_destructor(void *arg) } +static void commands_destructor(void *data) +{ + struct commands *commands = data; + + list_flush(&commands->cmdl); +} + + static int ctx_alloc(struct cmd_ctx **ctxp, const struct cmd *cmd) { struct cmd_ctx *ctx; @@ -703,21 +715,20 @@ int cmd_print(struct re_printf *pf, const struct commands *commands) } -int cmd_init(struct commands *commands) +int cmd_init(struct commands **commandsp) { - if (!commands) - return EINVAL; + struct commands *commands; - list_init(&commands->cmdl); + if (!commandsp) + return EINVAL; - return 0; -} + commands = mem_zalloc(sizeof(*commands), commands_destructor); + if (!commands) + return ENOMEM; + list_init(&commands->cmdl); -void cmd_close(struct commands *commands) -{ - if (!commands) - return; + *commandsp = commands; - list_flush(&commands->cmdl); + return 0; } @@ -50,7 +50,7 @@ static struct re_printf pf_null = {vprintf_null, 0}; int test_cmd(void) { - struct commands commands; + struct commands *commands = NULL; struct cmd_ctx *ctx = 0; struct test test; int err = 0; @@ -60,29 +60,29 @@ int test_cmd(void) err = cmd_init(&commands); ASSERT_EQ(0, err); - err = cmd_register(&commands, cmdv, ARRAY_SIZE(cmdv)); + err = cmd_register(commands, cmdv, ARRAY_SIZE(cmdv)); ASSERT_EQ(0, err); /* it is not possible to register same block twice */ - ASSERT_EQ(EALREADY, cmd_register(&commands, cmdv, ARRAY_SIZE(cmdv))); + ASSERT_EQ(EALREADY, cmd_register(commands, cmdv, ARRAY_SIZE(cmdv))); /* issue a different command */ - err = cmd_process(&commands, &ctx, 'h', &pf_null, &test); + err = cmd_process(commands, &ctx, 'h', &pf_null, &test); ASSERT_EQ(0, err); ASSERT_EQ(0, test.cmd_called); /* issue our command, expect handler to be called */ - err = cmd_process(&commands, &ctx, '@', &pf_null, &test); + err = cmd_process(commands, &ctx, '@', &pf_null, &test); ASSERT_EQ(0, err); ASSERT_EQ(1, test.cmd_called); - cmd_unregister(&commands, cmdv); + cmd_unregister(commands, cmdv); /* verify that context was not created */ ASSERT_TRUE(NULL == ctx); out: - cmd_close(&commands); + mem_deref(commands); return err; } @@ -110,7 +110,7 @@ static const struct cmd longcmdv[] = { int test_cmd_long(void) { - struct commands commands; + struct commands *commands = NULL; struct test test; const struct cmd *cmd; static const char *input_str = "/test 123\n"; @@ -124,38 +124,38 @@ int test_cmd_long(void) ASSERT_EQ(0, err); /* Verify that the command does not exist */ - cmd = cmd_find_long(&commands, "test"); + cmd = cmd_find_long(commands, "test"); ASSERT_TRUE(cmd == NULL); /* Register and verify command */ - err = cmd_register(&commands, longcmdv, ARRAY_SIZE(longcmdv)); + err = cmd_register(commands, longcmdv, ARRAY_SIZE(longcmdv)); ASSERT_EQ(0, err); - cmd = cmd_find_long(&commands, "test"); + cmd = cmd_find_long(commands, "test"); ASSERT_TRUE(cmd != NULL); /* Feed it some input data .. */ for (i=0; i<strlen(input_str); i++) { - err = cmd_process(&commands, &ctx, input_str[i], + err = cmd_process(commands, &ctx, input_str[i], &pf_null, &test); ASSERT_EQ(0, err); } - err = cmd_process_long(&commands, "test 123", 8, &pf_null, &test); + err = cmd_process_long(commands, "test 123", 8, &pf_null, &test); ASSERT_EQ(0, err); ASSERT_EQ(2, test.cmd_called); /* Cleanup .. */ - cmd_unregister(&commands, longcmdv); + cmd_unregister(commands, longcmdv); - cmd = cmd_find_long(&commands, "test"); + cmd = cmd_find_long(commands, "test"); ASSERT_TRUE(cmd == NULL); out: - cmd_close(&commands); + mem_deref(commands); return err; } |