From c9b39deea9c44bd1217272a5f882fc796fdaa69c Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 3 Dec 2024 16:07:17 +0100 Subject: [PATCH 1/2] Remove more RCmd lefotvers --- libr/core/cmd_api.c | 206 +----------------- libr/include/r_cmd.h | 162 +------------- test/unit/test_cmd.c | 489 ------------------------------------------- 3 files changed, 3 insertions(+), 854 deletions(-) delete mode 100644 test/unit/test_cmd.c diff --git a/libr/core/cmd_api.c b/libr/core/cmd_api.c index 24a1107e21f40..3f6d6b98daa32 100644 --- a/libr/core/cmd_api.c +++ b/libr/core/cmd_api.c @@ -6,60 +6,6 @@ #define NCMDS (sizeof (cmd->cmds) / sizeof (*cmd->cmds)) -static const RCmdDescHelp not_defined_help = { - .usage = "Usage not defined", - .summary = "Help summary not defined", - .description = "Help description not defined.", -}; - -static const RCmdDescHelp root_help = { - .usage = "[.][times][cmd][~grep][@[@iter]addr!size][|>pipe] ; ...", - .description = "", -}; - -static void cmd_desc_free(RCmdDesc *cd) { - if (!cd) { - return; - } - - r_pvector_clear (&cd->children); - free (cd->name); - free (cd); -} - -static bool cmd_desc_set_parent(RCmdDesc *cd, RCmdDesc *parent) { - R_RETURN_VAL_IF_FAIL (cd && !cd->parent, false); - if (parent) { - cd->parent = parent; - r_pvector_push (&parent->children, cd); - parent->n_children++; - } - return true; -} - -static RCmdDesc *create_cmd_desc(RCmd *cmd, RCmdDesc *parent, RCmdDescType type, const char *name, const RCmdDescHelp *help, bool ht_insert) { - RCmdDesc *res = R_NEW0 (RCmdDesc); - if (!res) { - return NULL; - } - res->type = type; - res->name = strdup (name); - if (!res->name) { - goto err; - } - res->n_children = 0; - res->help = help? help: ¬_defined_help; - r_pvector_init (&res->children, (RPVectorFree)cmd_desc_free); - if (ht_insert && !ht_pp_insert (cmd->ht_cmds, name, res)) { - goto err; - } - cmd_desc_set_parent (res, parent); - return res; -err: - cmd_desc_free (res); - return NULL; -} - static void alias_freefn(HtPPKv *kv) { if (kv) { char *k = kv->key; @@ -145,7 +91,7 @@ R_API RCmd *r_cmd_new(void) { } cmd->nullcallback = cmd->data = NULL; cmd->ht_cmds = ht_pp_new0 (); - cmd->root_cmd_desc = create_cmd_desc (cmd, NULL, R_CMD_DESC_TYPE_ARGV, "", &root_help, true); + // cmd->root_cmd_desc = create_cmd_desc (cmd, NULL, R_CMD_DESC_TYPE_ARGV, "", &root_help, true); r_core_plugin_init (cmd); r_cmd_macro_init (&cmd->macro); r_cmd_alias_init (cmd); @@ -170,48 +116,11 @@ R_API RCmd *r_cmd_free(RCmd *cmd) { R_FREE (cmd->cmds[i]); } } - cmd_desc_free (cmd->root_cmd_desc); + // cmd_desc_free (cmd->root_cmd_desc); free (cmd); return NULL; } -R_API RCmdDesc *r_cmd_get_root(RCmd *cmd) { - return cmd->root_cmd_desc; -} - -R_API RCmdDesc *r_cmd_get_desc(RCmd *cmd, const char *cmd_identifier) { - R_RETURN_VAL_IF_FAIL (cmd && cmd_identifier, NULL); - char *cmdid = strdup (cmd_identifier); - char *end_cmdid = cmdid + strlen (cmdid); - RCmdDesc *res = NULL; - bool is_exact_match = true; - // match longer commands first - while (*cmdid) { - RCmdDesc *cd = ht_pp_find (cmd->ht_cmds, cmdid, NULL); - if (cd) { - switch (cd->type) { - case R_CMD_DESC_TYPE_ARGV: - if (!is_exact_match) { - break; - } - // fallthrough - case R_CMD_DESC_TYPE_GROUP: - // fallthrough - case R_CMD_DESC_TYPE_OLDINPUT: - res = cd; - goto out; - case R_CMD_DESC_TYPE_INNER: - break; - } - } - is_exact_match = false; - *(--end_cmdid) = '\0'; - } -out: - free (cmdid); - return res; -} - // This struct exists to store the index during hashtable foreach. typedef struct { const char **keys; @@ -1011,114 +920,3 @@ R_API int r_cmd_macro_break(RCmdMacro *mac, const char *value) { } return 0; } - -/* RCmdParsedArgs */ - -R_API RCmdParsedArgs *r_cmd_parsed_args_new(const char *cmd, int n_args, char **args) { - R_RETURN_VAL_IF_FAIL (cmd && n_args >= 0, NULL); - RCmdParsedArgs *res = R_NEW0 (RCmdParsedArgs); - res->has_space_after_cmd = true; - res->argc = n_args + 1; - res->argv = R_NEWS0 (char *, res->argc); - res->argv[0] = strdup (cmd); - int i; - for (i = 1; i < res->argc; i++) { - res->argv[i] = strdup (args[i - 1]); - } - return res; -} - -R_API RCmdParsedArgs *r_cmd_parsed_args_newcmd(const char *cmd) { - return r_cmd_parsed_args_new (cmd, 0, NULL); -} - -R_API RCmdParsedArgs *r_cmd_parsed_args_newargs(int n_args, char **args) { - return r_cmd_parsed_args_new ("", n_args, args); -} - -R_API void r_cmd_parsed_args_free(RCmdParsedArgs *a) { - if (!a) { - return; - } - - int i; - for (i = 0; i < a->argc; i++) { - free (a->argv[i]); - } - free (a->argv); - free (a); -} - -static void free_array(char **arr, int n) { - int i; - for (i = 0; i < n; i++) { - free (arr[i]); - } - free (arr); -} - -R_API bool r_cmd_parsed_args_setargs(RCmdParsedArgs *a, int n_args, char **args) { - R_RETURN_VAL_IF_FAIL (a && a->argv && a->argv[0], false); - char **tmp = R_NEWS0 (char *, n_args + 1); - if (!tmp) { - return false; - } - tmp[0] = strdup (a->argv[0]); - int i; - for (i = 1; i < n_args + 1; i++) { - tmp[i] = strdup (args[i - 1]); - if (!tmp[i]) { - goto err; - } - } - free_array (a->argv, a->argc); - a->argv = tmp; - a->argc = n_args + 1; - return true; -err: - free_array (tmp, n_args + 1); - return false; -} - -R_API bool r_cmd_parsed_args_setcmd(RCmdParsedArgs *a, const char *cmd) { - R_RETURN_VAL_IF_FAIL (a && a->argv && a->argv[0], false); - char *tmp = strdup (cmd); - if (!tmp) { - return false; - } - free (a->argv[0]); - a->argv[0] = tmp; - return true; -} - -static void parsed_args_iterateargs(RCmdParsedArgs *a, RStrBuf *sb) { - int i; - for (i = 1; i < a->argc; i++) { - if (i > 1) { - r_strbuf_append (sb, " "); - } - r_strbuf_append (sb, a->argv[i]); - } -} - -R_API char *r_cmd_parsed_args_argstr(RCmdParsedArgs *a) { - R_RETURN_VAL_IF_FAIL (a && a->argv && a->argv[0], NULL); - RStrBuf *sb = r_strbuf_new (""); - parsed_args_iterateargs (a, sb); - return r_strbuf_drain (sb); -} - -R_API char *r_cmd_parsed_args_execstr(RCmdParsedArgs *a) { - R_RETURN_VAL_IF_FAIL (a && a->argv && a->argv[0], NULL); - RStrBuf *sb = r_strbuf_new (a->argv[0]); - if (a->argc > 1 && a->has_space_after_cmd) { - r_strbuf_append (sb, " "); - } - parsed_args_iterateargs (a, sb); - return r_strbuf_drain (sb); -} - -R_API const char *r_cmd_parsed_args_cmd(RCmdParsedArgs *a) { - R_RETURN_VAL_IF_FAIL (a && a->argv && a->argv[0], NULL); - return a->argv[0]; -} diff --git a/libr/include/r_cmd.h b/libr/include/r_cmd.h index a7a3a07211ea4..f387eedcb4358 100644 --- a/libr/include/r_cmd.h +++ b/libr/include/r_cmd.h @@ -75,101 +75,6 @@ typedef struct r_cmd_alias_val_t { bool is_data; // Is the buffer data or a command? (if false, is_str must be true - commands can't be raw) } RCmdAliasVal; -typedef struct r_cmd_desc_example_t { - const char *example; - const char *comment; -} RCmdDescExample; - -/** - * Define how the command looks like in the help. - */ -typedef struct r_cmd_desc_help_t { - /** - * Short-sentence explaining what the command does. - * This is shown, for example, when the list of sub-commands is printed - * and each sub-command has a very short description on the right, - * explaining what it does. - */ - const char *summary; - /** - * Long description of what the command does. It can be as long as you - * want and it should explain well how the command behaves. - * This is shown, for example, when `??` is appended on command or `?` - * is appended and the command has no children to show. In that case, - * the short summary is extended with this longer description. - * - * Optional. - */ - const char *description; - /** - * String used to identify the arguments. This usually comes together - * with the summary. - * TODO: explain how to differentiate between required and optional arguments - */ - const char *args_str; - /** - * String that overrides the name+args_str usually used to describe the - * command. - * - * Optional. - */ - const char *usage; - /** - * String to use as sub-commands suggestions instead of the - * auto-generated one (e.g. [abcd] or [?] that you can see near command - * names when doing `w?`). If not provided, the options will be - * auto-generated. - * - * Optional. - */ - const char *options; - /** - * List of examples used to better explain how to use the command. This - * is shown together with the long description. - * - * Optional. - */ - const RCmdDescExample *examples; -} RCmdDescHelp; - -typedef enum { - // for old handlers that parse their own input and accept a single string - R_CMD_DESC_TYPE_OLDINPUT = 0, - // for handlers that accept argc/argv - R_CMD_DESC_TYPE_ARGV, - // for cmd descriptors that are just used to group together related - // sub-commands. Do not use this if the command can be used by itself or - // if it's necessary to show its help, because this descriptor is not - // stored in the hashtable and cannot be retrieved except by listing the - // children of its parent. - R_CMD_DESC_TYPE_INNER, - // for cmd descriptors that are parent of other sub-commands but that - // may also have a sub-command with the same name. For example, `wc` is - // both the parent of `wci`, `wc*`, etc. but there is also `wc` as a - // sub-command. - R_CMD_DESC_TYPE_GROUP, -} RCmdDescType; - -typedef struct r_cmd_desc_t { - RCmdDescType type; - char *name; - struct r_cmd_desc_t *parent; - int n_children; - RPVector children; - const RCmdDescHelp *help; - - union { - struct { - RCmdCb cb; - } oldinput_data; - struct { - RCmdArgvCb cb; - } argv_data; - struct { - struct r_cmd_desc_t *exec_cd; - } group_data; - } d; -} RCmdDesc; typedef struct r_cmd_t { void *data; @@ -181,7 +86,7 @@ typedef struct r_cmd_t { RCmdAlias aliases; void *language; // used to store TSLanguage * HtUP *ts_symbols_ht; - RCmdDesc *root_cmd_desc; + // RCmdDesc *root_cmd_desc; HtPP *ht_cmds; } RCmd; @@ -194,23 +99,6 @@ typedef struct r_cmd_descriptor_t { struct r_cmd_descriptor_t *sub[127]; } RCmdDescriptor; -#define DEFINE_CMD_ARGV_DESC_DETAIL(core, name, c_name, parent, handler, help) \ - RCmdDesc *c_name##_cd = r_cmd_desc_argv_new (core->rcmd, parent, #name, handler, help); \ - r_warn_if_fail (c_name##_cd) -#define DEFINE_CMD_ARGV_DESC_SPECIAL(core, name, c_name, parent) \ - DEFINE_CMD_ARGV_DESC_DETAIL (core, name, c_name, parent, c_name##_handler, &c_name##_help) -#define DEFINE_CMD_ARGV_DESC_INNER(core, name, c_name, parent) \ - RCmdDesc *c_name##_cd = r_cmd_desc_inner_new (core->rcmd, parent, #name, &c_name##_help); \ - r_warn_if_fail (c_name##_cd) -#define DEFINE_CMD_ARGV_GROUP_WITH_CHILD(core, name, parent) \ - RCmdDesc *name##_cd = r_cmd_desc_group_new (core->rcmd, parent, #name, name##_handler, &name##_help, &name##_group_help); \ - r_warn_if_fail (name##_cd) -#define DEFINE_CMD_ARGV_DESC(core, name, parent) \ - DEFINE_CMD_ARGV_DESC_SPECIAL (core, name, name, parent) -#define DEFINE_CMD_OLDINPUT_DESC(core, name, parent) \ - RCmdDesc *name##_cd = r_cmd_desc_oldinput_new (core->rcmd, parent, #name, name##_handler_old, &name##_help); \ - r_warn_if_fail (name##_cd) - #ifdef R_API R_API RCmd *r_cmd_new(void); R_API RCmd *r_cmd_free(RCmd *cmd); @@ -218,54 +106,6 @@ R_API int r_cmd_call(RCmd *cmd, const char *command); R_API void r_cmd_set_data(RCmd *cmd, void *data); R_API bool r_cmd_add(RCmd *cmd, const char *command, RCmdCb callback); -static inline RCmdStatus r_cmd_int2status(int v) { - if (v == -2) { - return R_CMD_STATUS_EXIT; - } else if (v < 0) { - return R_CMD_STATUS_ERROR; - } else { - return R_CMD_STATUS_OK; - } -} - -static inline int r_cmd_status2int(RCmdStatus s) { - switch (s) { - case R_CMD_STATUS_OK: - return 0; - case R_CMD_STATUS_ERROR: - case R_CMD_STATUS_WRONG_ARGS: - case R_CMD_STATUS_INVALID: - return -1; - case R_CMD_STATUS_EXIT: - default: - return -2; - } -} - -/* RCmdDescriptor */ -R_API RCmdDesc *r_cmd_desc_argv_new(RCmd *cmd, RCmdDesc *parent, const char *name, RCmdArgvCb cb, const RCmdDescHelp *help); -R_API RCmdDesc *r_cmd_desc_inner_new(RCmd *cmd, RCmdDesc *parent, const char *name, const RCmdDescHelp *help); -R_API RCmdDesc *r_cmd_desc_group_new(RCmd *cmd, RCmdDesc *parent, const char *name, RCmdArgvCb cb, const RCmdDescHelp *help, const RCmdDescHelp *group_help); -R_API RCmdDesc *r_cmd_desc_oldinput_new(RCmd *cmd, RCmdDesc *parent, const char *name, RCmdCb cb, const RCmdDescHelp *help); -R_API RCmdDesc *r_cmd_desc_parent(RCmdDesc *cd); -R_API bool r_cmd_desc_has_handler(RCmdDesc *cd); -R_API bool r_cmd_desc_remove(RCmd *cmd, RCmdDesc *cd); - -#define r_cmd_desc_children_foreach(root, it_cd) r_pvector_foreach (&root->children, it_cd) - -/* RCmdParsedArgs */ -R_API RCmdParsedArgs *r_cmd_parsed_args_new(const char *cmd, int n_args, char **args); -R_API RCmdParsedArgs *r_cmd_parsed_args_newcmd(const char *cmd); -R_API RCmdParsedArgs *r_cmd_parsed_args_newargs(int n_args, char **args); -R_API void r_cmd_parsed_args_free(RCmdParsedArgs *args); -R_API bool r_cmd_parsed_args_setargs(RCmdParsedArgs *arg, int n_args, char **args); -R_API bool r_cmd_parsed_args_setcmd(RCmdParsedArgs *arg, const char *cmd); -R_API char *r_cmd_parsed_args_argstr(RCmdParsedArgs *arg); -R_API char *r_cmd_parsed_args_execstr(RCmdParsedArgs *arg); -R_API const char *r_cmd_parsed_args_cmd(RCmdParsedArgs *arg); - -#define r_cmd_parsed_args_foreach_arg(args, i, arg) for ((i) = 1; (i) < (args->argc) && ((arg) = (args)->argv[i]); (i)++) - /* r_cmd_macro */ R_API RCmdMacroItem *r_cmd_macro_item_new(void); R_API void r_cmd_macro_item_free(RCmdMacroItem *item); diff --git a/test/unit/test_cmd.c b/test/unit/test_cmd.c deleted file mode 100644 index c3170fb531a65..0000000000000 --- a/test/unit/test_cmd.c +++ /dev/null @@ -1,489 +0,0 @@ -#if 0 -#include -#include -#include -#include "minunit.h" - -bool test_parsed_args_noargs(void) { - RCmdParsedArgs *a = r_cmd_parsed_args_new ("pd", 0, NULL); - mu_assert_streq (a->argv[0], "pd", "pd is the command"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "", "empty arguments"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "pd", "only command"); - r_cmd_parsed_args_free (a); - mu_end; -} - -bool test_parsed_args_onearg(void) { - char *args[] = {"10"}; - RCmdParsedArgs *a = r_cmd_parsed_args_new ("pd", 1, args); - mu_assert_streq (a->argv[0], "pd", "pd is the command"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "10", "one argument"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "pd 10", "cmd + arg"); - r_cmd_parsed_args_free (a); - mu_end; -} - -bool test_parsed_args_args(void) { - char *args[] = { "d", "0" }; - RCmdParsedArgs *a = r_cmd_parsed_args_new ("wA", 2, args); - mu_assert_streq (a->argv[0], "wA", "wA is the command"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "d 0", "two args"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "wA d 0", "cmd + args"); - r_cmd_parsed_args_free (a); - mu_end; -} - -bool test_parsed_args_nospace(void) { - char *args[] = { "dr*" }; - RCmdParsedArgs *a = r_cmd_parsed_args_new (".", 1, args); - a->has_space_after_cmd = false; - mu_assert_streq (a->argv[0], ".", ". is the command"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "dr*", "arg"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), ".dr*", "cmd + args without space"); - r_cmd_parsed_args_free (a); - mu_end; -} - -bool test_parsed_args_newcmd(void) { - RCmdParsedArgs *a = r_cmd_parsed_args_newcmd ("pd"); - mu_assert_streq (a->argv[0], "pd", "pd is the command"); - char *args[] = { "10" }; - bool res = r_cmd_parsed_args_setargs (a, 1, args); - mu_assert ("args should be added", res); - mu_assert_eq (a->argc, 2, "argc == 2"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "10", "arg"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "pd 10", "cmd + args"); - - char *args2[] = { "2", "3" }; - res = r_cmd_parsed_args_setargs (a, 2, args2); - mu_assert_eq (a->argc, 3, "argc == 3"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "2 3", "arg"); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "pd 2 3", "cmd + args"); - - r_cmd_parsed_args_free (a); - mu_end; -} - -bool test_parsed_args_newargs(void) { - char *args[] = { "0", "1", "2" }; - RCmdParsedArgs *a = r_cmd_parsed_args_newargs (3, args); - mu_assert_eq (a->argc, 4, "argc == 4"); - mu_assert_streq_free (r_cmd_parsed_args_argstr (a), "0 1 2", "args"); - mu_assert_streq (a->argv[1], "0", "first arg"); - mu_assert_streq (a->argv[2], "1", "second arg"); - - bool res = r_cmd_parsed_args_setcmd (a, "pd"); - mu_assert ("cmd should be added", res); - mu_assert_streq_free (r_cmd_parsed_args_execstr (a), "pd 0 1 2", "cmd + args"); - r_cmd_parsed_args_free (a); - mu_end; -} - -static RCmdStatus afl_argv_handler(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -bool test_cmd_descriptor_argv(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *cd = r_cmd_desc_argv_new (cmd, root, "afl", afl_argv_handler, NULL); - mu_assert_notnull (cd, "cmddesc created"); - mu_assert_streq (cd->name, "afl", "command descriptor name is afl"); - mu_assert_eq (cd->type, R_CMD_DESC_TYPE_ARGV, "type of command descriptor is argv"); - mu_assert_ptreq (r_cmd_desc_parent (cd), root, "root parent descriptor"); - mu_assert_eq (root->n_children, 1, "root has 1 child"); - mu_assert_eq (cd->n_children, 0, "no children"); - r_cmd_free (cmd); - mu_end; -} - -bool test_cmd_descriptor_argv_nested(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *af_cd = r_cmd_desc_argv_new (cmd, root, "af", NULL, NULL); - r_cmd_desc_argv_new (cmd, root, "af2", NULL, NULL); - RCmdDesc *cd = r_cmd_desc_argv_new (cmd, af_cd, "afl", afl_argv_handler, NULL); - mu_assert_ptreq (r_cmd_desc_parent (cd), af_cd, "parent of afl is af"); - mu_assert_true (r_pvector_contains (&af_cd->children, cd), "afl is child of af"); - r_cmd_free (cmd); - mu_end; -} - -static int a_oldinput_cb(void *user, const char *input) { - return 0; -} - -bool test_cmd_descriptor_oldinput(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *cd = r_cmd_desc_oldinput_new (cmd, root, "a", a_oldinput_cb, NULL); - mu_assert_notnull (cd, "cmddesc created"); - mu_assert_streq (cd->name, "a", "command descriptor name is a"); - mu_assert_eq (cd->type, R_CMD_DESC_TYPE_OLDINPUT, "type of command descriptor is oldinput"); - mu_assert_ptreq (r_cmd_desc_parent (cd), root, "root parent descriptor"); - mu_assert_eq (cd->n_children, 0, "no children"); - r_cmd_free (cmd); - mu_end; -} - -static RCmdStatus a_exec_cb(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -static RCmdStatus ab_cb(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -bool test_cmd_descriptor_group(void) { - const RCmdDescHelp ab_help = { .summary = "ab help" }; - const RCmdDescHelp a_exec_help = { .summary = "a exec help" }; - const RCmdDescHelp a_group_help = { .summary = "a group help" }; - - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *cd = r_cmd_desc_group_new (cmd, root, "a", a_exec_cb, &a_exec_help, &a_group_help); - r_cmd_desc_argv_new (cmd, cd, "ab", ab_cb, &ab_help); - mu_assert_notnull (cd, "cmddesc created"); - mu_assert_streq (cd->name, "a", "command descriptor name is a"); - mu_assert_eq (cd->type, R_CMD_DESC_TYPE_GROUP, "type of command descriptor is group"); - mu_assert_ptreq (r_cmd_desc_parent (cd), root, "root parent descriptor"); - mu_assert_eq (cd->n_children, 2, "no children"); - mu_assert_true (r_cmd_desc_has_handler (cd), "a_exec_cb is the handler for this"); - - mu_assert_ptreq (r_cmd_get_desc (cmd, "a"), cd, "cd is the desc for `a`"); - - RCmdParsedArgs *pa = r_cmd_parsed_args_newcmd("a??"); - char *h = r_cmd_get_help (cmd, pa, false); - mu_assert_streq (h, "Usage: a # a exec help\n", "detailed help for a is a_exec_help"); - r_cmd_parsed_args_free (pa); - free (h); - - pa = r_cmd_parsed_args_newcmd ("a?"); - h = r_cmd_get_help (cmd, pa, false); - const char *exp_h = "Usage: a[b] # a group help\n" - "| a # a exec help\n" - "| ab # ab help\n"; - mu_assert_streq (h, exp_h, "regular help for a is a_group_help"); - free (h); - - r_cmd_free (cmd); - r_cmd_parsed_args_free (pa); - mu_end; -} - -static RCmdStatus ap_handler(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -static RCmdStatus aeir_handler(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -static int ae_handler(void *user, const char *input) { - return 0; -} - -static int w_handler(void *user, const char *input) { - return 0; -} - -bool test_cmd_descriptor_tree(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *a_cd = r_cmd_desc_argv_new (cmd, root, "a", NULL, NULL); - r_cmd_desc_argv_new (cmd, a_cd, "ap", ap_handler, NULL); - r_cmd_desc_oldinput_new (cmd, root, "w", w_handler, NULL); - - void **it_cd; - r_cmd_desc_children_foreach (root, it_cd) { - RCmdDesc *cd = *it_cd; - mu_assert_ptreq (r_cmd_desc_parent (cd), root, "root is the parent"); - } - - r_cmd_free (cmd); - mu_end; -} - -bool test_cmd_get_desc(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *a_cd = r_cmd_desc_argv_new (cmd, root, "a", NULL, NULL); - RCmdDesc *ap_cd = r_cmd_desc_argv_new (cmd, a_cd, "ap", ap_handler, NULL); - RCmdDesc *apd_cd = r_cmd_desc_argv_new (cmd, ap_cd, "apd", ap_handler, NULL); - RCmdDesc *ae_cd = r_cmd_desc_oldinput_new (cmd, a_cd, "ae", ae_handler, NULL); - RCmdDesc *aeir_cd = r_cmd_desc_argv_new (cmd, ae_cd, "aeir", aeir_handler, NULL); - RCmdDesc *w_cd = r_cmd_desc_oldinput_new (cmd, root, "w", w_handler, NULL); - - mu_assert_null (r_cmd_get_desc (cmd, "afl"), "afl does not have any handler"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "ap"), ap_cd, "ap will be handled by ap"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "wx"), w_cd, "wx will be handled by w"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "wao"), w_cd, "wao will be handled by w"); - mu_assert_null (r_cmd_get_desc (cmd, "apx"), "apx does not have any handler"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "apd"), apd_cd, "apd will be handled by apd"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "ae"), ae_cd, "ae will be handled by ae"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "aeim"), ae_cd, "aeim will be handled by ae"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "aeir"), aeir_cd, "aeir will be handled by aeir"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "aei"), ae_cd, "aei will be handled by ae"); - - r_cmd_free (cmd); - mu_end; -} - -static RCmdStatus pd_handler(RCore *core, int argc, const char **argv) { - mu_assert_eq (argc, 2, "pd_handler called with 2 arguments (name and arg)"); - mu_assert_streq (argv[0], "pd", "pd is argv[0]"); - mu_assert_streq (argv[1], "10", "10 is argv[1]"); - return R_CMD_STATUS_OK; -} - -static RCmdStatus p_handler_argv(RCore *core, int argc, const char **argv) { - return R_CMD_STATUS_OK; -} - -static int p_handler(void *user, const char *input) { - mu_assert_streq (input, "x 10", "input is +1"); - return -1; -} - -static int px_handler(void *user, const char *input) { - if (*input == '?') { - r_cons_printf ("Free format px help\n"); - } - return 0; -} - -static int wv_handler(void *user, const char *input) { - mu_assert_streq (input, "8 0xdeadbeef", "input is +2"); - return 1; -} - -static int q_handler(void *user, const char *input) { - return -2; -} - -bool test_cmd_call_desc(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *p_cd = r_cmd_desc_argv_new (cmd, root, "p", NULL, NULL); - r_cmd_desc_argv_new (cmd, p_cd, "pd", pd_handler, NULL); - r_cmd_desc_oldinput_new (cmd, p_cd, "p", p_handler, NULL); - r_cmd_desc_oldinput_new (cmd, root, "wv", wv_handler, NULL); - r_cmd_desc_oldinput_new (cmd, root, "q", q_handler, NULL); - - char *pd_args[] = {"10"}; - char *px_args[] = {"10"}; - char *wv8_args[] = {"0xdeadbeef"}; - - RCmdParsedArgs *a = r_cmd_parsed_args_new ("pd", 1, pd_args); - mu_assert_eq(r_cmd_call_parsed_args (cmd, a), R_CMD_STATUS_OK, "pd was called correctly"); - r_cmd_parsed_args_free (a); - - a = r_cmd_parsed_args_new ("px", 1, px_args); - mu_assert_eq(r_cmd_call_parsed_args (cmd, a), R_CMD_STATUS_INVALID, "p was called correctly"); - r_cmd_parsed_args_free (a); - - a = r_cmd_parsed_args_new ("wv8", 1, wv8_args); - mu_assert_eq(r_cmd_call_parsed_args (cmd, a), R_CMD_STATUS_OK, "wv was called correctly"); - r_cmd_parsed_args_free (a); - - a = r_cmd_parsed_args_new ("quit", 0, NULL); - mu_assert_eq (r_cmd_call_parsed_args (cmd, a), R_CMD_STATUS_EXIT, "quit is going to exit"); - r_cmd_parsed_args_free (a); - - r_cmd_free (cmd); - mu_end; -} - -bool test_cmd_help(void) { - const RCmdDescHelp p_help = { - .summary = "p summary", - .usage = "p-usage", - .args_str = "", - .description = NULL, - .examples = NULL, - }; - - const RCmdDescExample pd_help_examples[] = { - { .example = "pd 10", .comment = "print 10 disassembled instructions" }, - {0}, - }; - - const RCmdDescHelp pd_help = { - .summary = "pd summary", - .usage = NULL, - .args_str = " ", - .description = "pd long description", - .examples = pd_help_examples, - }; - - const RCmdDescHelp px_help = { - .summary = "px summary", - .usage = "px-usage", - .args_str = " ", - .description = "px long description", - .examples = NULL, - }; - - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *p_cd = r_cmd_desc_argv_new (cmd, root, "p", NULL, &p_help); - r_cmd_desc_argv_new (cmd, p_cd, "pd", pd_handler, &pd_help); - r_cmd_desc_oldinput_new (cmd, p_cd, "px", px_handler, &px_help); - - const char *p_help_exp = "Usage: p-usage # p summary\n" - "| pd # pd summary\n" - "| px[?] # px summary\n"; - RCmdParsedArgs *a = r_cmd_parsed_args_newcmd ("p?"); - char *h = r_cmd_get_help (cmd, a, false); - mu_assert_notnull (h, "help is not null"); - mu_assert_streq (h, p_help_exp, "wrong help for p?"); - free (h); - r_cmd_parsed_args_free (a); - - const char *pd_help_exp = "Usage: pd # pd summary\n"; - a = r_cmd_parsed_args_newcmd ("pd?"); - h = r_cmd_get_help (cmd, a, false); - mu_assert_notnull (h, "help is not null"); - mu_assert_streq (h, pd_help_exp, "wrong help for pd?"); - free (h); - r_cmd_parsed_args_free (a); - - const char *pd_long_help_exp = "Usage: pd # pd summary\n" - "\n" - "pd long description\n" - "\n" - "Examples:\n" - "| pd 10 # print 10 disassembled instructions\n"; - a = r_cmd_parsed_args_newcmd ("pd??"); - h = r_cmd_get_help (cmd, a, false); - mu_assert_notnull (h, "help is not null"); - mu_assert_streq (h, pd_long_help_exp, "wrong help for pd??"); - free (h); - r_cmd_parsed_args_free (a); - - r_cmd_free (cmd); - mu_end; -} - -bool test_cmd_group_help(void) { - const RCmdDescHelp p_help = { - .summary = "p summary", - .usage = "p-usage", - .args_str = "", - .description = NULL, - .examples = NULL, - }; - - const RCmdDescHelp p_group_help = { - .usage = "p-usage", - .summary = "p group-summary", - .args_str = NULL, - .description = NULL, - .examples = NULL, - }; - - const RCmdDescHelp pd_help = { - .summary = "pd summary", - .usage = NULL, - .args_str = " ", - .description = "pd long description", - .examples = NULL, - }; - - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *p_cd = r_cmd_desc_group_new (cmd, root, "p", p_handler_argv, &p_help, &p_group_help); - r_cmd_desc_argv_new (cmd, p_cd, "pd", pd_handler, &pd_help); - - const char *p_help_exp = "Usage: p-usage # p group-summary\n" - "| p # p summary\n" - "| pd # pd summary\n"; - RCmdParsedArgs *a = r_cmd_parsed_args_newcmd ("p?"); - char *h = r_cmd_get_help (cmd, a, false); - mu_assert_notnull (h, "help is not null"); - mu_assert_streq (h, p_help_exp, "wrong help for p?"); - free (h); - r_cmd_parsed_args_free (a); - - r_cmd_free (cmd); - mu_end; -} - -bool test_cmd_oldinput_help(void) { - r_cons_new (); - - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *p_cd = r_cmd_desc_argv_new (cmd, root, "p", NULL, NULL); - r_cmd_desc_argv_new (cmd, p_cd, "pd", pd_handler, NULL); - r_cmd_desc_oldinput_new (cmd, p_cd, "px", px_handler, NULL); - - RCmdParsedArgs *a = r_cmd_parsed_args_newcmd ("px?"); - const char *px_help_exp = "Free format px help\n"; - char *h = r_cmd_get_help (cmd, a, false); - mu_assert_notnull (h, "help is not null"); - mu_assert_streq (h, px_help_exp, "wrong help for px?"); - free (h); - r_cmd_parsed_args_free (a); - - r_cmd_free (cmd); - r_cons_free (); - mu_end; -} - -bool test_remove_cmd(void) { - RCmd *cmd = r_cmd_new (); - RCmdDesc *root = r_cmd_get_root (cmd); - RCmdDesc *x_cd = r_cmd_desc_argv_new (cmd, root, "x", NULL, NULL); - RCmdDesc *p_cd = r_cmd_desc_argv_new (cmd, root, "p", NULL, NULL); - RCmdDesc *pd_cd = r_cmd_desc_argv_new (cmd, p_cd, "pd", pd_handler, NULL); - r_cmd_desc_argv_new (cmd, p_cd, "px", pd_handler, NULL); - - mu_assert_ptreq (r_cmd_get_desc (cmd, "x"), x_cd, "x is found"); - mu_assert_ptreq (r_cmd_get_desc (cmd, "pd"), pd_cd, "pd is found"); - mu_assert_eq (root->n_children, 2, "root has 2 commands as children"); - r_cmd_desc_remove (cmd, p_cd); - mu_assert_eq (root->n_children, 1, "p was removed, now root has 1 command as children"); - mu_assert_null (r_cmd_get_desc (cmd, "p"), "p should not be found anymore"); - mu_assert_null (r_cmd_get_desc (cmd, "pd"), "pd should not be found anymore"); - mu_assert_null (r_cmd_get_desc (cmd, "px"), "px should not be found anymore"); - - void **it_cd; - r_cmd_desc_children_foreach (root, it_cd) { - RCmdDesc *cd = *it_cd; - mu_assert_ptrneq (cd, p_cd, "p should not be found anymore"); - } - - r_cmd_free (cmd); - r_cons_free (); - mu_end; -} -#endif - -int all_tests(void) { -#if 0 - mu_run_test (test_parsed_args_noargs); - mu_run_test (test_parsed_args_onearg); - mu_run_test (test_parsed_args_args); - mu_run_test (test_parsed_args_nospace); - mu_run_test (test_parsed_args_newcmd); - mu_run_test (test_parsed_args_newargs); - mu_run_test (test_cmd_descriptor_argv); - mu_run_test (test_cmd_descriptor_argv_nested); - mu_run_test (test_cmd_descriptor_oldinput); - mu_run_test (test_cmd_descriptor_tree); - mu_run_test (test_cmd_descriptor_group); - mu_run_test (test_cmd_get_desc); - mu_run_test (test_cmd_call_desc); - mu_run_test (test_cmd_help); - mu_run_test (test_cmd_group_help); - mu_run_test (test_cmd_oldinput_help); - mu_run_test (test_remove_cmd); - return tests_passed != tests_run; -#endif - return 0; -} - -int main(int argc, char **argv) { - return all_tests (); -} From 5266bc72ed9350700d97588bceda0c186ef48f4c Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 3 Dec 2024 16:15:52 +0100 Subject: [PATCH 2/2] fm --- test/unit/meson.build | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/meson.build b/test/unit/meson.build index 7d449d5ec77f4..5caed253e28be 100644 --- a/test/unit/meson.build +++ b/test/unit/meson.build @@ -18,7 +18,6 @@ if get_option('enable_tests') 'bin', 'bitmap', 'buf', - 'cmd', 'cmd_str', 'cons', 'debruijn',