cmd: remove internal usage of legacy iterator interface
This commit is contained in:
255
cmd/command.c
255
cmd/command.c
@@ -24,16 +24,18 @@ enum item_type {
|
||||
|
||||
static void command_list_cleanup(void)
|
||||
{
|
||||
struct b_btree_iterator it = {0};
|
||||
b_btree_iterator_begin(&command_list, &it);
|
||||
while (b_btree_iterator_is_valid(&it)) {
|
||||
struct b_command *cmd = b_unbox(struct b_command, it.node, b_node);
|
||||
struct b_btree_node *node = b_btree_first(&command_list);
|
||||
while (node) {
|
||||
struct b_command *cmd = b_unbox(struct b_command, node, b_node);
|
||||
if (!cmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_btree_iterator_erase(&it);
|
||||
struct b_btree_node *next = b_btree_next(node);
|
||||
b_btree_delete(&command_list, node);
|
||||
b_command_destroy(cmd);
|
||||
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,18 +55,19 @@ struct b_command *b_command_create(unsigned int id)
|
||||
|
||||
static void command_usage_destroy(struct b_command_usage *usage)
|
||||
{
|
||||
struct b_queue_iterator it = {0};
|
||||
|
||||
b_queue_iterator_begin(&usage->u_parts, &it);
|
||||
while (b_queue_iterator_is_valid(&it)) {
|
||||
struct b_command_usage_entry *arg = b_unbox(
|
||||
struct b_command_usage_entry, it.entry, e_entry);
|
||||
struct b_queue_entry *entry = b_queue_first(&usage->u_parts);
|
||||
while (entry) {
|
||||
struct b_command_usage_entry *arg
|
||||
= b_unbox(struct b_command_usage_entry, entry, e_entry);
|
||||
if (!arg) {
|
||||
continue;
|
||||
}
|
||||
|
||||
b_queue_iterator_erase(&it);
|
||||
struct b_queue_entry *next = b_queue_next(entry);
|
||||
b_queue_delete(&usage->u_parts, entry);
|
||||
free(arg);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
free(usage);
|
||||
@@ -72,8 +75,6 @@ static void command_usage_destroy(struct b_command_usage *usage)
|
||||
|
||||
void b_command_destroy(struct b_command *cmd)
|
||||
{
|
||||
struct b_queue_iterator it = {0};
|
||||
|
||||
if (cmd->b_name) {
|
||||
free(cmd->b_name);
|
||||
}
|
||||
@@ -86,52 +87,71 @@ void b_command_destroy(struct b_command *cmd)
|
||||
free(cmd->b_description);
|
||||
}
|
||||
|
||||
b_queue_iterator_begin(&cmd->b_opt, &it);
|
||||
while (b_queue_iterator_is_valid(&it)) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
|
||||
struct b_queue_entry *next = NULL;
|
||||
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
if (!opt) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_queue_iterator_erase(&it);
|
||||
next = b_queue_next(entry);
|
||||
b_queue_delete(&cmd->b_opt, entry);
|
||||
|
||||
b_command_option_destroy(opt);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
b_queue_iterator_begin(&cmd->b_arg, &it);
|
||||
while (b_queue_iterator_is_valid(&it)) {
|
||||
entry = b_queue_first(&cmd->b_arg);
|
||||
while (entry) {
|
||||
struct b_command_arg *arg
|
||||
= b_unbox(struct b_command_arg, it.entry, arg_entry);
|
||||
= b_unbox(struct b_command_arg, entry, arg_entry);
|
||||
if (!arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_queue_iterator_erase(&it);
|
||||
next = b_queue_next(entry);
|
||||
b_queue_delete(&cmd->b_arg, entry);
|
||||
|
||||
b_command_arg_destroy(arg);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
b_queue_iterator_begin(&cmd->b_arg, &it);
|
||||
while (b_queue_iterator_is_valid(&it)) {
|
||||
#if 0
|
||||
entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *subcmd
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
= b_unbox(struct b_command, entry, b_entry);
|
||||
if (!subcmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_queue_iterator_erase(&it);
|
||||
b_command_destroy(subcmd);
|
||||
}
|
||||
next = b_queue_next(entry);
|
||||
|
||||
b_queue_iterator_begin(&cmd->b_usage, &it);
|
||||
while (b_queue_iterator_is_valid(&it)) {
|
||||
b_command_destroy(subcmd);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
#endif
|
||||
|
||||
entry = b_queue_first(&cmd->b_usage);
|
||||
while (entry) {
|
||||
struct b_command_usage *usage
|
||||
= b_unbox(struct b_command_usage, it.entry, u_entry);
|
||||
= b_unbox(struct b_command_usage, entry, u_entry);
|
||||
if (!usage) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_queue_iterator_erase(&it);
|
||||
next = b_queue_next(entry);
|
||||
b_queue_delete(&cmd->b_usage, entry);
|
||||
|
||||
command_usage_destroy(usage);
|
||||
|
||||
entry = next;
|
||||
}
|
||||
|
||||
free(cmd);
|
||||
@@ -382,10 +402,10 @@ static void get_usage_string(
|
||||
|
||||
b_string *cmd_name = b_string_create();
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &usage->u_parts) {
|
||||
struct b_command_usage_entry *entry = b_unbox(
|
||||
struct b_command_usage_entry, it.entry, e_entry);
|
||||
struct b_queue_entry *q_entry = b_queue_first(&usage->u_parts);
|
||||
while (q_entry) {
|
||||
struct b_command_usage_entry *entry
|
||||
= b_unbox(struct b_command_usage_entry, q_entry, e_entry);
|
||||
|
||||
if (!entry) {
|
||||
break;
|
||||
@@ -426,6 +446,8 @@ static void get_usage_string(
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
q_entry = b_queue_next(q_entry);
|
||||
}
|
||||
|
||||
b_string_unref(cmd_name);
|
||||
@@ -445,13 +467,15 @@ b_string *z__b_command_default_usage_string(
|
||||
b_string_append_cstr(str, " [OPTIONS]");
|
||||
}
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_arg) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
|
||||
while (entry) {
|
||||
struct b_command_arg *arg
|
||||
= b_unbox(struct b_command_arg, it.entry, arg_entry);
|
||||
= b_unbox(struct b_command_arg, entry, arg_entry);
|
||||
|
||||
b_string_append_cstr(str, " ");
|
||||
z__b_get_arg_usage_string(arg, false, str);
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
if (!b_queue_empty(&cmd->b_subcommands)) {
|
||||
@@ -523,13 +547,10 @@ static void print_options_list(struct b_command *cmd)
|
||||
b_string *opt_str = b_string_create();
|
||||
b_string *desc_str = b_string_create();
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_opt) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
if (!opt) {
|
||||
continue;
|
||||
}
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
|
||||
b_string_clear(opt_str);
|
||||
b_string_clear(desc_str);
|
||||
@@ -544,12 +565,15 @@ static void print_options_list(struct b_command *cmd)
|
||||
desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
|
||||
|
||||
if (description_on_separate_line(opt_len, desc_len)) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (opt_len > desb_margin) {
|
||||
desb_margin = opt_len;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_paragraph_format format = {0};
|
||||
@@ -558,11 +582,12 @@ static void print_options_list(struct b_command *cmd)
|
||||
format.p_right_margin = 4;
|
||||
|
||||
size_t i = 0;
|
||||
b_queue_foreach (&it, &cmd->b_opt) {
|
||||
entry = b_queue_first(&cmd->b_opt);
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
if (!opt) {
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
b_string_clear(opt_str);
|
||||
@@ -609,6 +634,8 @@ static void print_options_list(struct b_command *cmd)
|
||||
if (new_paragraph) {
|
||||
b_tty_putc(OUTPUT_STREAM, 0, '\n');
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_string_unref(opt_str);
|
||||
@@ -622,10 +649,11 @@ static void print_args_list(struct b_command *cmd)
|
||||
size_t desb_margin = 0;
|
||||
b_string *str = b_string_create();
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_arg) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
|
||||
|
||||
while (entry) {
|
||||
struct b_command_arg *arg
|
||||
= b_unbox(struct b_command_arg, it.entry, arg_entry);
|
||||
= b_unbox(struct b_command_arg, entry, arg_entry);
|
||||
|
||||
b_string_clear(str);
|
||||
z__b_get_arg_usage_string(arg, true, str);
|
||||
@@ -636,6 +664,8 @@ static void print_args_list(struct b_command *cmd)
|
||||
if (len > desb_margin) {
|
||||
desb_margin = len;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_paragraph_format format = {0};
|
||||
@@ -644,9 +674,10 @@ static void print_args_list(struct b_command *cmd)
|
||||
format.p_right_margin = 4;
|
||||
|
||||
size_t i = 0;
|
||||
b_queue_foreach (&it, &cmd->b_arg) {
|
||||
entry = b_queue_first(&cmd->b_arg);
|
||||
while (entry) {
|
||||
struct b_command_arg *arg
|
||||
= b_unbox(struct b_command_arg, it.entry, arg_entry);
|
||||
= b_unbox(struct b_command_arg, entry, arg_entry);
|
||||
|
||||
b_string_clear(str);
|
||||
z__b_get_arg_usage_string(arg, true, str);
|
||||
@@ -666,6 +697,7 @@ static void print_args_list(struct b_command *cmd)
|
||||
z__b_get_arg_description(arg, str);
|
||||
|
||||
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_string_unref(str);
|
||||
@@ -678,10 +710,9 @@ static void print_commands_list(struct b_command *cmd)
|
||||
size_t desb_margin = 0;
|
||||
b_string *str = b_string_create();
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_subcommands) {
|
||||
struct b_command *sub
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *sub = b_unbox(struct b_command, entry, b_entry);
|
||||
|
||||
b_string_clear(str);
|
||||
get_command_string(sub, str);
|
||||
@@ -692,6 +723,8 @@ static void print_commands_list(struct b_command *cmd)
|
||||
if (len > desb_margin) {
|
||||
desb_margin = len;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_paragraph_format format = {0};
|
||||
@@ -700,9 +733,9 @@ static void print_commands_list(struct b_command *cmd)
|
||||
format.p_right_margin = 4;
|
||||
|
||||
size_t i = 0;
|
||||
b_queue_foreach (&it, &cmd->b_subcommands) {
|
||||
struct b_command *sub
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *sub = b_unbox(struct b_command, entry, b_entry);
|
||||
|
||||
b_string_clear(str);
|
||||
get_command_string(sub, str);
|
||||
@@ -722,6 +755,8 @@ static void print_commands_list(struct b_command *cmd)
|
||||
get_command_description(sub, str);
|
||||
|
||||
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_string_unref(str);
|
||||
@@ -730,17 +765,20 @@ static void print_commands_list(struct b_command *cmd)
|
||||
struct b_command *b_command_get_subcommand_with_name(
|
||||
struct b_command *cmd, const char *name)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_subcommands) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *subcmd
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
= b_unbox(struct b_command, entry, b_entry);
|
||||
if (!subcmd || !subcmd->b_name) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (!strcmp(subcmd->b_name, name)) {
|
||||
return subcmd;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -749,17 +787,20 @@ struct b_command *b_command_get_subcommand_with_name(
|
||||
struct b_command *b_command_get_subcommand_with_long_name(
|
||||
struct b_command *cmd, const char *long_name)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_subcommands) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *subcmd
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
= b_unbox(struct b_command, entry, b_entry);
|
||||
if (!subcmd || !subcmd->b_long_name) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (!strcmp(subcmd->b_name, long_name)) {
|
||||
return subcmd;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -768,17 +809,20 @@ struct b_command *b_command_get_subcommand_with_long_name(
|
||||
struct b_command *b_command_get_subcommand_with_short_name(
|
||||
struct b_command *cmd, char short_name)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_subcommands) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
|
||||
while (entry) {
|
||||
struct b_command *subcmd
|
||||
= b_unbox(struct b_command, it.entry, b_entry);
|
||||
= b_unbox(struct b_command, entry, b_entry);
|
||||
if (!subcmd || !subcmd->b_short_name) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (subcmd->b_short_name == short_name) {
|
||||
return subcmd;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -787,17 +831,20 @@ struct b_command *b_command_get_subcommand_with_short_name(
|
||||
struct b_command_option *b_command_get_option_with_long_name(
|
||||
struct b_command *cmd, const char *long_name)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_opt) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
if (!opt || !opt->opt_long_name) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (!strcmp(opt->opt_long_name, long_name)) {
|
||||
return opt;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -806,17 +853,20 @@ struct b_command_option *b_command_get_option_with_long_name(
|
||||
struct b_command_option *b_command_get_option_with_short_name(
|
||||
struct b_command *cmd, char short_name)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_opt) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
if (!opt || !opt->opt_long_name) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
if (opt->opt_short_name == short_name) {
|
||||
return opt;
|
||||
}
|
||||
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -825,17 +875,16 @@ struct b_command_option *b_command_get_option_with_short_name(
|
||||
struct b_command_option *b_command_get_option_with_id(
|
||||
struct b_command *cmd, unsigned int id)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_opt) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
|
||||
while (entry) {
|
||||
struct b_command_option *opt
|
||||
= b_unbox(struct b_command_option, it.entry, opt_entry);
|
||||
if (!opt) {
|
||||
continue;
|
||||
}
|
||||
= b_unbox(struct b_command_option, entry, opt_entry);
|
||||
|
||||
if (opt->opt_id == id) {
|
||||
return opt;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -844,17 +893,16 @@ struct b_command_option *b_command_get_option_with_id(
|
||||
struct b_command_arg *b_command_get_arg_with_id(
|
||||
struct b_command *cmd, unsigned int id)
|
||||
{
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_arg) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
|
||||
while (entry) {
|
||||
struct b_command_arg *arg
|
||||
= b_unbox(struct b_command_arg, it.entry, arg_entry);
|
||||
if (!arg) {
|
||||
continue;
|
||||
}
|
||||
= b_unbox(struct b_command_arg, entry, arg_entry);
|
||||
|
||||
if (arg->arg_id == id) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -876,17 +924,16 @@ static void print_usage(struct b_command *cmd, struct b_arglist *args)
|
||||
}
|
||||
|
||||
b_string *str = b_string_create();
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, &cmd->b_usage) {
|
||||
struct b_queue_entry *entry = b_queue_first(&cmd->b_usage);
|
||||
while (entry) {
|
||||
struct b_command_usage *usage
|
||||
= b_unbox(struct b_command_usage, it.entry, u_entry);
|
||||
if (!usage) {
|
||||
break;
|
||||
}
|
||||
= b_unbox(struct b_command_usage, entry, u_entry);
|
||||
|
||||
b_string_clear(str);
|
||||
get_usage_string(cmd, args, usage, str);
|
||||
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
|
||||
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
|
||||
b_string_unref(str);
|
||||
@@ -960,12 +1007,12 @@ static b_status add_subcommand(struct b_command *parent, struct b_command *child
|
||||
|
||||
static int resolve_command_parents(struct b_btree *commands)
|
||||
{
|
||||
struct b_btree_iterator it;
|
||||
b_btree_foreach (&it, commands) {
|
||||
struct b_command *cmd = b_unbox(struct b_command, it.node, b_node);
|
||||
struct b_btree_node *node = b_btree_first(commands);
|
||||
while (node) {
|
||||
struct b_command *cmd = b_unbox(struct b_command, node, b_node);
|
||||
|
||||
if (cmd->b_parent_id == B_COMMAND_INVALID_ID) {
|
||||
continue;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
cmd->b_parent = get_command(commands, cmd->b_parent_id);
|
||||
@@ -974,6 +1021,8 @@ static int resolve_command_parents(struct b_btree *commands)
|
||||
}
|
||||
|
||||
add_subcommand(cmd->b_parent, cmd);
|
||||
skip:
|
||||
node = b_btree_next(node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user