From ec500c04ad40d7a1edba58481d2afb61e935a292 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 1 Nov 2025 10:02:42 +0000 Subject: [PATCH] cmd: remove internal usage of legacy iterator interface --- cmd/arglist.c | 195 ++++++++++++------------------- cmd/command.c | 255 ++++++++++++++++++++++++----------------- cmd/include/blue/cmd.h | 6 +- cmd/option.c | 53 +++++---- 4 files changed, 258 insertions(+), 251 deletions(-) diff --git a/cmd/arglist.c b/cmd/arglist.c index 5881731..12c889f 100644 --- a/cmd/arglist.c +++ b/cmd/arglist.c @@ -24,7 +24,7 @@ struct argv_parser { struct b_command *cmd; struct b_arglist *arglist; - b_queue_iterator arg_it; + b_queue_entry *arg_it; int nr_values_cur_arg; int argc; @@ -66,7 +66,7 @@ static void move_to_subcommand(struct argv_parser *parser, struct b_command *cmd { parser->cmd = cmd; parser->arglist->list_command = cmd; - b_queue_iterator_begin(&cmd->b_arg, &parser->arg_it); + parser->arg_it = b_queue_first(&cmd->b_arg); parser->nr_values_cur_arg = 0; } @@ -182,12 +182,12 @@ static void report_unexpected_arg(struct argv_parser *parser, const char *value) static b_status check_required_args(struct argv_parser *parser) { - if (!b_queue_iterator_is_valid(&parser->arg_it)) { + if (!parser->arg_it) { return B_SUCCESS; } struct b_command_arg *arg - = b_unbox(struct b_command_arg, parser->arg_it.entry, arg_entry); + = b_unbox(struct b_command_arg, parser->arg_it, arg_entry); if (!arg) { return B_SUCCESS; } @@ -236,7 +236,7 @@ static b_status parse_arg(struct argv_parser *parser) } struct b_command_arg *arg = b_unbox( - struct b_command_arg, parser->arg_it.entry, arg_entry); + struct b_command_arg, parser->arg_it, arg_entry); if (!arg) { b_arglist_report_unexpected_arg(parser->arglist, value); @@ -258,13 +258,13 @@ static b_status parse_arg(struct argv_parser *parser) } if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { - b_queue_iterator_next(&parser->arg_it); + parser->arg_it = b_queue_next(parser->arg_it); parser->nr_values_cur_arg = 0; continue; } if (parser->nr_values_cur_arg == arg->arg_nr_values) { - b_queue_iterator_next(&parser->arg_it); + parser->arg_it = b_queue_next(parser->arg_it); parser->nr_values_cur_arg = 0; continue; } @@ -357,25 +357,19 @@ static b_status parse_short_opt(struct argv_parser *parser) arglist_opt->opt_id = opt->opt_id; put_arglist_option(&parser->arglist->list_options, arglist_opt); - b_queue_iterator it; - b_queue_iterator_begin(&opt->opt_args, &it); + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); const char *value = flags; if (*value == '\0') { advance(parser); } - while (b_queue_iterator_is_valid(&it)) { + while (entry) { value = peek(parser); if (!value || *value == '-') { value = NULL; } - arg = b_unbox(struct b_command_arg, it.entry, arg_entry); - - if (!arg) { - b_queue_iterator_next(&it); - continue; - } + arg = b_unbox(struct b_command_arg, entry, arg_entry); if (!value || *value == '\0' || *value == '-') { value = NULL; @@ -398,20 +392,17 @@ static b_status parse_short_opt(struct argv_parser *parser) } if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { - b_queue_iterator_next(&it); nr_args_cur_opt = 0; goto next_value; } if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { - b_queue_iterator_next(&it); nr_args_cur_opt = 0; goto next_value; } if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { if (nr_args_cur_opt > 0) { - b_queue_iterator_next(&it); nr_args_cur_opt = 0; goto next_value; } @@ -423,7 +414,6 @@ static b_status parse_short_opt(struct argv_parser *parser) } if (nr_args_cur_opt == arg->arg_nr_values) { - b_queue_iterator_next(&it); nr_args_cur_opt = 0; goto next_value; } @@ -435,6 +425,7 @@ static b_status parse_short_opt(struct argv_parser *parser) return B_ERR_BAD_FORMAT; } next_value: + entry = b_queue_next(entry); value = advance(parser); } @@ -493,16 +484,11 @@ static b_status parse_long_opt(struct argv_parser *parser) arglist_opt->opt_id = opt->opt_id; put_arglist_option(&parser->arglist->list_options, arglist_opt); - b_queue_iterator it; - b_queue_iterator_begin(&opt->opt_args, &it); - struct b_command_arg *arg - = b_unbox(struct b_command_arg, it.entry, arg_entry); + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct b_command_arg *arg = NULL; - while (b_queue_iterator_is_valid(&it)) { - arg = b_unbox(struct b_command_arg, it.entry, arg_entry); - if (!arg) { - break; - } + while (entry) { + arg = b_unbox(struct b_command_arg, entry, arg_entry); const char *value = peek(parser); if (!value || value[0] == '-') { @@ -528,20 +514,20 @@ static b_status parse_long_opt(struct argv_parser *parser) } if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { - b_queue_iterator_next(&it); + entry = b_queue_next(entry); nr_args_cur_opt = 0; continue; } if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { - b_queue_iterator_next(&it); + entry = b_queue_next(entry); nr_args_cur_opt = 0; continue; } if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { if (nr_args_cur_opt > 0) { - b_queue_iterator_next(&it); + entry = b_queue_next(entry); nr_args_cur_opt = 0; continue; } @@ -553,7 +539,7 @@ static b_status parse_long_opt(struct argv_parser *parser) } if (nr_args_cur_opt == arg->arg_nr_values) { - b_queue_iterator_next(&it); + entry = b_queue_next(entry); nr_args_cur_opt = 0; continue; } @@ -666,34 +652,33 @@ static void arglist_value_destroy(struct b_arglist_value *val) void b_arglist_destroy(struct b_arglist *args) { - b_btree_iterator opt_it, args_it; + b_btree_node *opt_it, *args_it; + b_btree_node *opt_next, *args_next; - b_btree_iterator_begin(&args->list_options, &opt_it); - while (b_btree_iterator_is_valid(&opt_it)) { + opt_it = b_btree_first(&args->list_options); + while (opt_it) { struct b_arglist_option *opt - = b_unbox(struct b_arglist_option, opt_it.node, opt_node); + = b_unbox(struct b_arglist_option, opt_it, opt_node); + opt_next = b_btree_next(opt_it); - if (!opt) { - b_btree_iterator_next(&opt_it); - continue; - } - - b_btree_iterator_begin(&opt->opt_values, &args_it); - while (b_btree_iterator_is_valid(&args_it)) { + args_it = b_btree_first(&opt->opt_values); + while (args_it) { struct b_arglist_value *val = b_unbox( - struct b_arglist_value, args_it.node, val_node); + struct b_arglist_value, args_it, val_node); + args_next = b_btree_next(args_it); - if (!val) { - b_btree_iterator_next(&args_it); - continue; + if (val) { + b_btree_delete(&opt->opt_values, args_it); + arglist_value_destroy(val); } - b_btree_iterator_erase(&args_it); - arglist_value_destroy(val); + args_it = args_next; } - b_btree_iterator_erase(&opt_it); + b_btree_delete(&args->list_options, opt_it); arglist_option_destroy(opt); + + opt_it = opt_next; } free(args); @@ -775,12 +760,13 @@ b_status b_arglist_get_option( const b_arglist *args, unsigned int opt_id, unsigned int index, b_arglist_option **out) { - b_btree_iterator it = {0}; - b_btree_foreach (&it, &args->list_options) { - b_arglist_option *cur - = b_unbox(b_arglist_option, it.node, opt_node); + struct b_btree_node *node = b_btree_first(&args->list_options); + + while (node) { + b_arglist_option *cur = b_unbox(b_arglist_option, node, opt_node); if (cur->opt_id != opt_id) { + node = b_btree_next(node); continue; } @@ -789,6 +775,7 @@ b_status b_arglist_get_option( return B_SUCCESS; } + node = b_btree_next(node); index--; } @@ -813,11 +800,12 @@ b_status b_arglist_option_get_value( const b_arglist_option *opt, unsigned int arg_id, unsigned int index, b_arglist_value **out) { - b_btree_iterator it = {0}; - b_btree_foreach (&it, &opt->opt_values) { - b_arglist_value *cur = b_unbox(b_arglist_value, it.node, val_node); + struct b_btree_node *node = b_btree_first(&opt->opt_values); + while (node) { + b_arglist_value *cur = b_unbox(b_arglist_value, node, val_node); if (cur->val_id != arg_id) { + node = b_btree_next(node); continue; } @@ -826,6 +814,7 @@ b_status b_arglist_option_get_value( return B_SUCCESS; } + node = b_btree_next(node); index--; } @@ -834,24 +823,14 @@ b_status b_arglist_option_get_value( /************************ arglist iterator functions **************************/ -static bool arglist_iterator_next(struct b_iterator *it) -{ - return b_arglist_iterator_next((struct b_arglist_iterator *)it); -} - -static bool arglist_iterator_is_valid(const struct b_iterator *it) -{ - return b_arglist_iterator_is_valid((const struct b_arglist_iterator *)it); -} - static struct b_arglist_option *advance_to_next_opt(struct b_arglist_iterator *it) { struct b_arglist_option *opt; // b_btree_iterator_next(&it->_opt_it); - while (b_btree_iterator_is_valid(&it->_opt_it)) { - opt = b_unbox(struct b_arglist_option, it->_opt_it.node, opt_node); + while (it->_opt_it) { + opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); if (opt && (opt->opt_id == it->_opt_filter || it->_opt_filter == B_COMMAND_INVALID_ID)) { @@ -859,7 +838,7 @@ static struct b_arglist_option *advance_to_next_opt(struct b_arglist_iterator *i return opt; } - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); } return NULL; @@ -871,8 +850,8 @@ static struct b_arglist_value *advance_to_next_arg(struct b_arglist_iterator *it // b_btree_iterator_next(&it->_arg_it); - while (b_btree_iterator_is_valid(&it->_arg_it)) { - val = b_unbox(struct b_arglist_value, it->_arg_it.node, val_node); + while (it->_arg_it) { + val = b_unbox(struct b_arglist_value, it->_arg_it, val_node); if (val && (val->val_id == it->_arg_filter || it->_arg_filter == B_COMMAND_INVALID_ID)) { @@ -880,17 +859,12 @@ static struct b_arglist_value *advance_to_next_arg(struct b_arglist_iterator *it return val; } - b_btree_iterator_next(&it->_arg_it); + it->_arg_it = b_btree_next(it->_arg_it); } return NULL; } -static b_iterator_ops it_ops = { - .it_next = arglist_iterator_next, - .it_is_valid = arglist_iterator_is_valid, -}; - int b_arglist_iterator_begin( const struct b_arglist *args, unsigned int opt_filter, unsigned int arg_filter, struct b_arglist_iterator *it) @@ -899,33 +873,32 @@ int b_arglist_iterator_begin( it->opt_id = B_COMMAND_INVALID_ID; - it->_base.it_ops = &it_ops; it->_opt_filter = opt_filter; it->_arg_filter = arg_filter; - b_btree_iterator_begin(&args->list_options, &it->_opt_it); + it->_opt_it = b_btree_first(&args->list_options); struct b_arglist_option *opt = NULL; struct b_arglist_value *val = NULL; while (1) { - if (!b_btree_iterator_is_valid(&it->_opt_it)) { + if (!it->_opt_it) { opt = NULL; return -1; } - opt = b_unbox(struct b_arglist_option, it->_opt_it.node, opt_node); + opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); if (!opt || (opt_filter != opt->opt_id && opt_filter != B_COMMAND_INVALID_ID)) { - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); continue; } - b_btree_iterator_begin(&opt->opt_values, &it->_arg_it); + it->_arg_it = b_btree_first(&opt->opt_values); bool done = false; while (1) { - if (!b_btree_iterator_is_valid(&it->_arg_it)) { + if (!it->_arg_it) { if (arg_filter == B_COMMAND_INVALID_ID) { done = true; } @@ -933,12 +906,11 @@ int b_arglist_iterator_begin( break; } - val = b_unbox( - struct b_arglist_value, it->_arg_it.node, val_node); + val = b_unbox(struct b_arglist_value, it->_arg_it, val_node); if (!val || (arg_filter != val->val_id && arg_filter != B_COMMAND_INVALID_ID)) { - b_btree_iterator_next(&it->_arg_it); + it->_arg_it = b_btree_next(it->_arg_it); continue; } @@ -950,7 +922,7 @@ int b_arglist_iterator_begin( break; } - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); } it->opt_id = opt->opt_id; @@ -964,7 +936,7 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it) struct b_arglist_option *opt; struct b_arglist_value *val; - b_btree_iterator_next(&it->_arg_it); + it->_arg_it = b_btree_next(it->_arg_it); while (1) { val = advance_to_next_arg(it); @@ -975,7 +947,7 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it) return true; } - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); opt = advance_to_next_opt(it); if (!opt) { it->value = NULL; @@ -983,7 +955,7 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it) return false; } - b_btree_iterator_begin(&opt->opt_values, &it->_arg_it); + it->_arg_it = b_btree_first(&opt->opt_values); if (it->_arg_filter == B_COMMAND_INVALID_ID) { return true; @@ -998,27 +970,13 @@ bool b_arglist_iterator_is_valid(const struct b_arglist_iterator *it) /********************* arglist option iterator functions **********************/ -static bool arglist_option_iterator_next(struct b_iterator *it) -{ - return b_arglist_option_iterator_next( - (struct b_arglist_option_iterator *)it); -} - -static bool arglist_option_iterator_is_valid(const struct b_iterator *it) -{ - return b_arglist_option_iterator_is_valid( - (const struct b_arglist_option_iterator *)it); -} - static struct b_arglist_option *advance_to_next_opt2( struct b_arglist_option_iterator *it) { struct b_arglist_option *opt; - // b_btree_iterator_next(&it->_opt_it); - - while (b_btree_iterator_is_valid(&it->_opt_it)) { - opt = b_unbox(struct b_arglist_option, it->_opt_it.node, opt_node); + while (it->_opt_it) { + opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); if (opt && (opt->opt_id == it->_opt_filter || it->_opt_filter == B_COMMAND_INVALID_ID)) { @@ -1026,17 +984,12 @@ static struct b_arglist_option *advance_to_next_opt2( return opt; } - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); } return NULL; } -static b_iterator_ops option_it_ops = { - .it_next = arglist_option_iterator_next, - .it_is_valid = arglist_option_iterator_is_valid, -}; - int b_arglist_option_iterator_begin( const struct b_arglist *args, unsigned int opt_filter, struct b_arglist_option_iterator *it) @@ -1045,26 +998,26 @@ int b_arglist_option_iterator_begin( it->opt_id = B_COMMAND_INVALID_ID; - it->_base.it_ops = &it_ops; it->_opt_filter = opt_filter; - b_btree_iterator_begin(&args->list_options, &it->_opt_it); + it->_opt_it = b_btree_first(&args->list_options); struct b_arglist_option *opt = NULL; struct b_arglist_value *val = NULL; while (1) { - if (!b_btree_iterator_is_valid(&it->_opt_it)) { + if (!it->_opt_it) { opt = NULL; return -1; } - opt = b_unbox(struct b_arglist_option, it->_opt_it.node, opt_node); + opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); if (opt && (opt_filter == opt->opt_id || opt_filter == B_COMMAND_INVALID_ID)) { break; } - b_btree_iterator_next(&it->_opt_it); + + it->_opt_it = b_btree_next(it->_opt_it); } it->opt_id = opt->opt_id; @@ -1078,7 +1031,7 @@ bool b_arglist_option_iterator_next(struct b_arglist_option_iterator *it) struct b_arglist_option *opt; struct b_arglist_value *val; - b_btree_iterator_next(&it->_opt_it); + it->_opt_it = b_btree_next(it->_opt_it); opt = advance_to_next_opt2(it); if (!opt) { it->opt = NULL; diff --git a/cmd/command.c b/cmd/command.c index 85d9e1d..8cdafa9 100644 --- a/cmd/command.c +++ b/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; diff --git a/cmd/include/blue/cmd.h b/cmd/include/blue/cmd.h index 6d270f9..476ae57 100644 --- a/cmd/include/blue/cmd.h +++ b/cmd/include/blue/cmd.h @@ -164,24 +164,22 @@ typedef struct b_arglist_value { } b_arglist_value; typedef struct b_arglist_iterator { - b_iterator _base; size_t i; unsigned int opt_id; struct b_arglist_value *value; - b_btree_iterator _opt_it, _arg_it; + b_btree_node *_opt_it, *_arg_it; unsigned int _opt_filter, _arg_filter; } b_arglist_iterator; typedef struct b_arglist_option_iterator { - b_iterator _base; size_t i; unsigned int opt_id; struct b_arglist_option *opt; - b_btree_iterator _opt_it; + b_btree_node *_opt_it; unsigned int _opt_filter; } b_arglist_option_iterator; diff --git a/cmd/option.c b/cmd/option.c index b8b571e..7fa3ee8 100644 --- a/cmd/option.c +++ b/cmd/option.c @@ -26,18 +26,17 @@ void b_command_option_destroy(struct b_command_option *opt) free(opt->opt_description); } - b_queue_iterator it = {0}; + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); - b_queue_iterator_begin(&opt->opt_args, &it); - while (b_queue_iterator_is_valid(&it)) { + while (entry) { struct b_command_arg *arg - = b_unbox(struct b_command_arg, it.entry, arg_entry); - if (!arg) { - break; - } + = b_unbox(struct b_command_arg, entry, arg_entry); + + struct b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&opt->opt_args, entry); - b_queue_iterator_erase(&it); b_command_arg_destroy(arg); + entry = next; } free(opt); @@ -107,15 +106,16 @@ void z__b_get_option_description(struct b_command_option *opt, b_string *out) size_t nr_args = b_queue_length(&opt->opt_args); bool close_bracket = false; - b_queue_iterator it; - b_queue_foreach (&it, &opt->opt_args) { + size_t i = 0; + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + 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 || !arg->arg_allowed_values) { - continue; + goto skip; } - if (it.i > 0) { + if (i > 0) { b_string_append_cstr(out, "; "); } else { b_string_append_cstr(out, " [["); @@ -138,6 +138,10 @@ void z__b_get_option_description(struct b_command_option *opt, b_string *out) out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]); } + + skip: + i++; + entry = b_queue_next(entry); } if (close_bracket) { @@ -175,12 +179,12 @@ void z__b_get_option_usage_string( b_string_append_cstr(out, "}"); } - b_queue_iterator it; - b_queue_foreach (&it, &opt->opt_args) { + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + 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) { - continue; + goto skip; } bool optional = false, multi = false; @@ -228,23 +232,26 @@ void z__b_get_option_usage_string( if (flags & CMD_STR_COLOUR) { b_string_append_cstr(out, F_RESET); } + + skip: + entry = b_queue_next(entry); } } struct b_command_arg *b_command_option_get_arg_with_id( struct b_command_option *opt, unsigned int id) { - b_queue_iterator it; - b_queue_foreach (&it, &opt->opt_args) { + struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + + 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;