cmd: remove internal usage of legacy iterator interface

This commit is contained in:
2025-11-01 10:02:42 +00:00
parent c94f976751
commit ec500c04ad
4 changed files with 258 additions and 251 deletions

View File

@@ -24,7 +24,7 @@ struct argv_parser {
struct b_command *cmd; struct b_command *cmd;
struct b_arglist *arglist; struct b_arglist *arglist;
b_queue_iterator arg_it; b_queue_entry *arg_it;
int nr_values_cur_arg; int nr_values_cur_arg;
int argc; int argc;
@@ -66,7 +66,7 @@ static void move_to_subcommand(struct argv_parser *parser, struct b_command *cmd
{ {
parser->cmd = cmd; parser->cmd = cmd;
parser->arglist->list_command = 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; 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) 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; return B_SUCCESS;
} }
struct b_command_arg *arg 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) { if (!arg) {
return B_SUCCESS; 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 *arg = b_unbox(
struct b_command_arg, parser->arg_it.entry, arg_entry); struct b_command_arg, parser->arg_it, arg_entry);
if (!arg) { if (!arg) {
b_arglist_report_unexpected_arg(parser->arglist, value); 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) { 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; parser->nr_values_cur_arg = 0;
continue; continue;
} }
if (parser->nr_values_cur_arg == arg->arg_nr_values) { 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; parser->nr_values_cur_arg = 0;
continue; continue;
} }
@@ -357,25 +357,19 @@ static b_status parse_short_opt(struct argv_parser *parser)
arglist_opt->opt_id = opt->opt_id; arglist_opt->opt_id = opt->opt_id;
put_arglist_option(&parser->arglist->list_options, arglist_opt); put_arglist_option(&parser->arglist->list_options, arglist_opt);
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
b_queue_iterator_begin(&opt->opt_args, &it);
const char *value = flags; const char *value = flags;
if (*value == '\0') { if (*value == '\0') {
advance(parser); advance(parser);
} }
while (b_queue_iterator_is_valid(&it)) { while (entry) {
value = peek(parser); value = peek(parser);
if (!value || *value == '-') { if (!value || *value == '-') {
value = NULL; value = NULL;
} }
arg = b_unbox(struct b_command_arg, it.entry, arg_entry); arg = b_unbox(struct b_command_arg, entry, arg_entry);
if (!arg) {
b_queue_iterator_next(&it);
continue;
}
if (!value || *value == '\0' || *value == '-') { if (!value || *value == '\0' || *value == '-') {
value = NULL; 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) { if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) {
b_queue_iterator_next(&it);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
goto next_value; goto next_value;
} }
if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) {
b_queue_iterator_next(&it);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
goto next_value; goto next_value;
} }
if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) {
if (nr_args_cur_opt > 0) { if (nr_args_cur_opt > 0) {
b_queue_iterator_next(&it);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
goto next_value; 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) { if (nr_args_cur_opt == arg->arg_nr_values) {
b_queue_iterator_next(&it);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
goto next_value; goto next_value;
} }
@@ -435,6 +425,7 @@ static b_status parse_short_opt(struct argv_parser *parser)
return B_ERR_BAD_FORMAT; return B_ERR_BAD_FORMAT;
} }
next_value: next_value:
entry = b_queue_next(entry);
value = advance(parser); value = advance(parser);
} }
@@ -493,16 +484,11 @@ static b_status parse_long_opt(struct argv_parser *parser)
arglist_opt->opt_id = opt->opt_id; arglist_opt->opt_id = opt->opt_id;
put_arglist_option(&parser->arglist->list_options, arglist_opt); put_arglist_option(&parser->arglist->list_options, arglist_opt);
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
b_queue_iterator_begin(&opt->opt_args, &it); struct b_command_arg *arg = NULL;
struct b_command_arg *arg
= b_unbox(struct b_command_arg, it.entry, arg_entry);
while (b_queue_iterator_is_valid(&it)) { while (entry) {
arg = b_unbox(struct b_command_arg, it.entry, arg_entry); arg = b_unbox(struct b_command_arg, entry, arg_entry);
if (!arg) {
break;
}
const char *value = peek(parser); const char *value = peek(parser);
if (!value || value[0] == '-') { 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) { 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; nr_args_cur_opt = 0;
continue; continue;
} }
if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { 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; nr_args_cur_opt = 0;
continue; continue;
} }
if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) {
if (nr_args_cur_opt > 0) { if (nr_args_cur_opt > 0) {
b_queue_iterator_next(&it); entry = b_queue_next(entry);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
continue; continue;
} }
@@ -553,7 +539,7 @@ static b_status parse_long_opt(struct argv_parser *parser)
} }
if (nr_args_cur_opt == arg->arg_nr_values) { if (nr_args_cur_opt == arg->arg_nr_values) {
b_queue_iterator_next(&it); entry = b_queue_next(entry);
nr_args_cur_opt = 0; nr_args_cur_opt = 0;
continue; continue;
} }
@@ -666,34 +652,33 @@ static void arglist_value_destroy(struct b_arglist_value *val)
void b_arglist_destroy(struct b_arglist *args) 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); opt_it = b_btree_first(&args->list_options);
while (b_btree_iterator_is_valid(&opt_it)) { while (opt_it) {
struct b_arglist_option *opt 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) { args_it = b_btree_first(&opt->opt_values);
b_btree_iterator_next(&opt_it); while (args_it) {
continue;
}
b_btree_iterator_begin(&opt->opt_values, &args_it);
while (b_btree_iterator_is_valid(&args_it)) {
struct b_arglist_value *val = b_unbox( 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) { if (val) {
b_btree_iterator_next(&args_it); b_btree_delete(&opt->opt_values, args_it);
continue; arglist_value_destroy(val);
} }
b_btree_iterator_erase(&args_it); args_it = args_next;
arglist_value_destroy(val);
} }
b_btree_iterator_erase(&opt_it); b_btree_delete(&args->list_options, opt_it);
arglist_option_destroy(opt); arglist_option_destroy(opt);
opt_it = opt_next;
} }
free(args); free(args);
@@ -775,12 +760,13 @@ b_status b_arglist_get_option(
const b_arglist *args, unsigned int opt_id, unsigned int index, const b_arglist *args, unsigned int opt_id, unsigned int index,
b_arglist_option **out) b_arglist_option **out)
{ {
b_btree_iterator it = {0}; struct b_btree_node *node = b_btree_first(&args->list_options);
b_btree_foreach (&it, &args->list_options) {
b_arglist_option *cur while (node) {
= b_unbox(b_arglist_option, it.node, opt_node); b_arglist_option *cur = b_unbox(b_arglist_option, node, opt_node);
if (cur->opt_id != opt_id) { if (cur->opt_id != opt_id) {
node = b_btree_next(node);
continue; continue;
} }
@@ -789,6 +775,7 @@ b_status b_arglist_get_option(
return B_SUCCESS; return B_SUCCESS;
} }
node = b_btree_next(node);
index--; index--;
} }
@@ -813,11 +800,12 @@ b_status b_arglist_option_get_value(
const b_arglist_option *opt, unsigned int arg_id, unsigned int index, const b_arglist_option *opt, unsigned int arg_id, unsigned int index,
b_arglist_value **out) b_arglist_value **out)
{ {
b_btree_iterator it = {0}; struct b_btree_node *node = b_btree_first(&opt->opt_values);
b_btree_foreach (&it, &opt->opt_values) { while (node) {
b_arglist_value *cur = b_unbox(b_arglist_value, it.node, val_node); b_arglist_value *cur = b_unbox(b_arglist_value, node, val_node);
if (cur->val_id != arg_id) { if (cur->val_id != arg_id) {
node = b_btree_next(node);
continue; continue;
} }
@@ -826,6 +814,7 @@ b_status b_arglist_option_get_value(
return B_SUCCESS; return B_SUCCESS;
} }
node = b_btree_next(node);
index--; index--;
} }
@@ -834,24 +823,14 @@ b_status b_arglist_option_get_value(
/************************ arglist iterator functions **************************/ /************************ 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) static struct b_arglist_option *advance_to_next_opt(struct b_arglist_iterator *it)
{ {
struct b_arglist_option *opt; struct b_arglist_option *opt;
// b_btree_iterator_next(&it->_opt_it); // b_btree_iterator_next(&it->_opt_it);
while (b_btree_iterator_is_valid(&it->_opt_it)) { while (it->_opt_it) {
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 if (opt
&& (opt->opt_id == it->_opt_filter && (opt->opt_id == it->_opt_filter
|| it->_opt_filter == B_COMMAND_INVALID_ID)) { || 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; return opt;
} }
b_btree_iterator_next(&it->_opt_it); it->_opt_it = b_btree_next(it->_opt_it);
} }
return NULL; 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); // b_btree_iterator_next(&it->_arg_it);
while (b_btree_iterator_is_valid(&it->_arg_it)) { while (it->_arg_it) {
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 if (val
&& (val->val_id == it->_arg_filter && (val->val_id == it->_arg_filter
|| it->_arg_filter == B_COMMAND_INVALID_ID)) { || 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; return val;
} }
b_btree_iterator_next(&it->_arg_it); it->_arg_it = b_btree_next(it->_arg_it);
} }
return NULL; 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( int b_arglist_iterator_begin(
const struct b_arglist *args, unsigned int opt_filter, const struct b_arglist *args, unsigned int opt_filter,
unsigned int arg_filter, struct b_arglist_iterator *it) 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->opt_id = B_COMMAND_INVALID_ID;
it->_base.it_ops = &it_ops;
it->_opt_filter = opt_filter; it->_opt_filter = opt_filter;
it->_arg_filter = arg_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_option *opt = NULL;
struct b_arglist_value *val = NULL; struct b_arglist_value *val = NULL;
while (1) { while (1) {
if (!b_btree_iterator_is_valid(&it->_opt_it)) { if (!it->_opt_it) {
opt = NULL; opt = NULL;
return -1; 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 if (!opt
|| (opt_filter != opt->opt_id || (opt_filter != opt->opt_id
&& opt_filter != B_COMMAND_INVALID_ID)) { && opt_filter != B_COMMAND_INVALID_ID)) {
b_btree_iterator_next(&it->_opt_it); it->_opt_it = b_btree_next(it->_opt_it);
continue; continue;
} }
b_btree_iterator_begin(&opt->opt_values, &it->_arg_it); it->_arg_it = b_btree_first(&opt->opt_values);
bool done = false; bool done = false;
while (1) { while (1) {
if (!b_btree_iterator_is_valid(&it->_arg_it)) { if (!it->_arg_it) {
if (arg_filter == B_COMMAND_INVALID_ID) { if (arg_filter == B_COMMAND_INVALID_ID) {
done = true; done = true;
} }
@@ -933,12 +906,11 @@ int b_arglist_iterator_begin(
break; break;
} }
val = b_unbox( val = b_unbox(struct b_arglist_value, it->_arg_it, val_node);
struct b_arglist_value, it->_arg_it.node, val_node);
if (!val if (!val
|| (arg_filter != val->val_id || (arg_filter != val->val_id
&& arg_filter != B_COMMAND_INVALID_ID)) { && arg_filter != B_COMMAND_INVALID_ID)) {
b_btree_iterator_next(&it->_arg_it); it->_arg_it = b_btree_next(it->_arg_it);
continue; continue;
} }
@@ -950,7 +922,7 @@ int b_arglist_iterator_begin(
break; break;
} }
b_btree_iterator_next(&it->_opt_it); it->_opt_it = b_btree_next(it->_opt_it);
} }
it->opt_id = opt->opt_id; 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_option *opt;
struct b_arglist_value *val; struct b_arglist_value *val;
b_btree_iterator_next(&it->_arg_it); it->_arg_it = b_btree_next(it->_arg_it);
while (1) { while (1) {
val = advance_to_next_arg(it); val = advance_to_next_arg(it);
@@ -975,7 +947,7 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it)
return true; return true;
} }
b_btree_iterator_next(&it->_opt_it); it->_opt_it = b_btree_next(it->_opt_it);
opt = advance_to_next_opt(it); opt = advance_to_next_opt(it);
if (!opt) { if (!opt) {
it->value = NULL; it->value = NULL;
@@ -983,7 +955,7 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it)
return false; 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) { if (it->_arg_filter == B_COMMAND_INVALID_ID) {
return true; return true;
@@ -998,27 +970,13 @@ bool b_arglist_iterator_is_valid(const struct b_arglist_iterator *it)
/********************* arglist option iterator functions **********************/ /********************* 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( static struct b_arglist_option *advance_to_next_opt2(
struct b_arglist_option_iterator *it) struct b_arglist_option_iterator *it)
{ {
struct b_arglist_option *opt; struct b_arglist_option *opt;
// b_btree_iterator_next(&it->_opt_it); while (it->_opt_it) {
opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node);
while (b_btree_iterator_is_valid(&it->_opt_it)) {
opt = b_unbox(struct b_arglist_option, it->_opt_it.node, opt_node);
if (opt if (opt
&& (opt->opt_id == it->_opt_filter && (opt->opt_id == it->_opt_filter
|| it->_opt_filter == B_COMMAND_INVALID_ID)) { || it->_opt_filter == B_COMMAND_INVALID_ID)) {
@@ -1026,17 +984,12 @@ static struct b_arglist_option *advance_to_next_opt2(
return opt; return opt;
} }
b_btree_iterator_next(&it->_opt_it); it->_opt_it = b_btree_next(it->_opt_it);
} }
return NULL; 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( int b_arglist_option_iterator_begin(
const struct b_arglist *args, unsigned int opt_filter, const struct b_arglist *args, unsigned int opt_filter,
struct b_arglist_option_iterator *it) struct b_arglist_option_iterator *it)
@@ -1045,26 +998,26 @@ int b_arglist_option_iterator_begin(
it->opt_id = B_COMMAND_INVALID_ID; it->opt_id = B_COMMAND_INVALID_ID;
it->_base.it_ops = &it_ops;
it->_opt_filter = opt_filter; 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_option *opt = NULL;
struct b_arglist_value *val = NULL; struct b_arglist_value *val = NULL;
while (1) { while (1) {
if (!b_btree_iterator_is_valid(&it->_opt_it)) { if (!it->_opt_it) {
opt = NULL; opt = NULL;
return -1; 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 if (opt
&& (opt_filter == opt->opt_id && (opt_filter == opt->opt_id
|| opt_filter == B_COMMAND_INVALID_ID)) { || opt_filter == B_COMMAND_INVALID_ID)) {
break; break;
} }
b_btree_iterator_next(&it->_opt_it);
it->_opt_it = b_btree_next(it->_opt_it);
} }
it->opt_id = opt->opt_id; 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_option *opt;
struct b_arglist_value *val; 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); opt = advance_to_next_opt2(it);
if (!opt) { if (!opt) {
it->opt = NULL; it->opt = NULL;

View File

@@ -24,16 +24,18 @@ enum item_type {
static void command_list_cleanup(void) static void command_list_cleanup(void)
{ {
struct b_btree_iterator it = {0}; struct b_btree_node *node = b_btree_first(&command_list);
b_btree_iterator_begin(&command_list, &it); while (node) {
while (b_btree_iterator_is_valid(&it)) { struct b_command *cmd = b_unbox(struct b_command, node, b_node);
struct b_command *cmd = b_unbox(struct b_command, it.node, b_node);
if (!cmd) { if (!cmd) {
break; 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); 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) static void command_usage_destroy(struct b_command_usage *usage)
{ {
struct b_queue_iterator it = {0}; struct b_queue_entry *entry = b_queue_first(&usage->u_parts);
while (entry) {
b_queue_iterator_begin(&usage->u_parts, &it); struct b_command_usage_entry *arg
while (b_queue_iterator_is_valid(&it)) { = b_unbox(struct b_command_usage_entry, entry, e_entry);
struct b_command_usage_entry *arg = b_unbox(
struct b_command_usage_entry, it.entry, e_entry);
if (!arg) { if (!arg) {
continue; continue;
} }
b_queue_iterator_erase(&it); struct b_queue_entry *next = b_queue_next(entry);
b_queue_delete(&usage->u_parts, entry);
free(arg); free(arg);
entry = next;
} }
free(usage); free(usage);
@@ -72,8 +75,6 @@ static void command_usage_destroy(struct b_command_usage *usage)
void b_command_destroy(struct b_command *cmd) void b_command_destroy(struct b_command *cmd)
{ {
struct b_queue_iterator it = {0};
if (cmd->b_name) { if (cmd->b_name) {
free(cmd->b_name); free(cmd->b_name);
} }
@@ -86,52 +87,71 @@ void b_command_destroy(struct b_command *cmd)
free(cmd->b_description); free(cmd->b_description);
} }
b_queue_iterator_begin(&cmd->b_opt, &it); struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
while (b_queue_iterator_is_valid(&it)) { struct b_queue_entry *next = NULL;
while (entry) {
struct b_command_option *opt 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) { if (!opt) {
break; break;
} }
b_queue_iterator_erase(&it); next = b_queue_next(entry);
b_queue_delete(&cmd->b_opt, entry);
b_command_option_destroy(opt); b_command_option_destroy(opt);
entry = next;
} }
b_queue_iterator_begin(&cmd->b_arg, &it); entry = b_queue_first(&cmd->b_arg);
while (b_queue_iterator_is_valid(&it)) { while (entry) {
struct b_command_arg *arg 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) { if (!arg) {
break; break;
} }
b_queue_iterator_erase(&it); next = b_queue_next(entry);
b_queue_delete(&cmd->b_arg, entry);
b_command_arg_destroy(arg); b_command_arg_destroy(arg);
entry = next;
} }
b_queue_iterator_begin(&cmd->b_arg, &it); #if 0
while (b_queue_iterator_is_valid(&it)) { entry = b_queue_first(&cmd->b_subcommands);
while (entry) {
struct b_command *subcmd struct b_command *subcmd
= b_unbox(struct b_command, it.entry, b_entry); = b_unbox(struct b_command, entry, b_entry);
if (!subcmd) { if (!subcmd) {
break; break;
} }
b_queue_iterator_erase(&it); next = b_queue_next(entry);
b_command_destroy(subcmd);
}
b_queue_iterator_begin(&cmd->b_usage, &it); b_command_destroy(subcmd);
while (b_queue_iterator_is_valid(&it)) {
entry = next;
}
#endif
entry = b_queue_first(&cmd->b_usage);
while (entry) {
struct b_command_usage *usage 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) { if (!usage) {
break; break;
} }
b_queue_iterator_erase(&it); next = b_queue_next(entry);
b_queue_delete(&cmd->b_usage, entry);
command_usage_destroy(usage); command_usage_destroy(usage);
entry = next;
} }
free(cmd); free(cmd);
@@ -382,10 +402,10 @@ static void get_usage_string(
b_string *cmd_name = b_string_create(); b_string *cmd_name = b_string_create();
b_queue_iterator it; struct b_queue_entry *q_entry = b_queue_first(&usage->u_parts);
b_queue_foreach (&it, &usage->u_parts) { while (q_entry) {
struct b_command_usage_entry *entry = b_unbox( struct b_command_usage_entry *entry
struct b_command_usage_entry, it.entry, e_entry); = b_unbox(struct b_command_usage_entry, q_entry, e_entry);
if (!entry) { if (!entry) {
break; break;
@@ -426,6 +446,8 @@ static void get_usage_string(
default: default:
break; break;
} }
q_entry = b_queue_next(q_entry);
} }
b_string_unref(cmd_name); b_string_unref(cmd_name);
@@ -445,13 +467,15 @@ b_string *z__b_command_default_usage_string(
b_string_append_cstr(str, " [OPTIONS]"); b_string_append_cstr(str, " [OPTIONS]");
} }
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
b_queue_foreach (&it, &cmd->b_arg) { while (entry) {
struct b_command_arg *arg 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, " "); b_string_append_cstr(str, " ");
z__b_get_arg_usage_string(arg, false, str); z__b_get_arg_usage_string(arg, false, str);
entry = b_queue_next(entry);
} }
if (!b_queue_empty(&cmd->b_subcommands)) { 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 *opt_str = b_string_create();
b_string *desc_str = b_string_create(); b_string *desc_str = b_string_create();
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
b_queue_foreach (&it, &cmd->b_opt) { while (entry) {
struct b_command_option *opt 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;
}
b_string_clear(opt_str); b_string_clear(opt_str);
b_string_clear(desc_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); desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
if (description_on_separate_line(opt_len, desc_len)) { if (description_on_separate_line(opt_len, desc_len)) {
continue; goto skip;
} }
if (opt_len > desb_margin) { if (opt_len > desb_margin) {
desb_margin = opt_len; desb_margin = opt_len;
} }
skip:
entry = b_queue_next(entry);
} }
b_paragraph_format format = {0}; b_paragraph_format format = {0};
@@ -558,11 +582,12 @@ static void print_options_list(struct b_command *cmd)
format.p_right_margin = 4; format.p_right_margin = 4;
size_t i = 0; 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 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) { if (!opt) {
continue; break;
} }
b_string_clear(opt_str); b_string_clear(opt_str);
@@ -609,6 +634,8 @@ static void print_options_list(struct b_command *cmd)
if (new_paragraph) { if (new_paragraph) {
b_tty_putc(OUTPUT_STREAM, 0, '\n'); b_tty_putc(OUTPUT_STREAM, 0, '\n');
} }
entry = b_queue_next(entry);
} }
b_string_unref(opt_str); b_string_unref(opt_str);
@@ -622,10 +649,11 @@ static void print_args_list(struct b_command *cmd)
size_t desb_margin = 0; size_t desb_margin = 0;
b_string *str = b_string_create(); b_string *str = b_string_create();
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
b_queue_foreach (&it, &cmd->b_arg) {
while (entry) {
struct b_command_arg *arg 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); b_string_clear(str);
z__b_get_arg_usage_string(arg, true, 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) { if (len > desb_margin) {
desb_margin = len; desb_margin = len;
} }
entry = b_queue_next(entry);
} }
b_paragraph_format format = {0}; b_paragraph_format format = {0};
@@ -644,9 +674,10 @@ static void print_args_list(struct b_command *cmd)
format.p_right_margin = 4; format.p_right_margin = 4;
size_t i = 0; 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 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); b_string_clear(str);
z__b_get_arg_usage_string(arg, true, 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); z__b_get_arg_description(arg, str);
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
entry = b_queue_next(entry);
} }
b_string_unref(str); b_string_unref(str);
@@ -678,10 +710,9 @@ static void print_commands_list(struct b_command *cmd)
size_t desb_margin = 0; size_t desb_margin = 0;
b_string *str = b_string_create(); b_string *str = b_string_create();
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
b_queue_foreach (&it, &cmd->b_subcommands) { while (entry) {
struct b_command *sub struct b_command *sub = b_unbox(struct b_command, entry, b_entry);
= b_unbox(struct b_command, it.entry, b_entry);
b_string_clear(str); b_string_clear(str);
get_command_string(sub, str); get_command_string(sub, str);
@@ -692,6 +723,8 @@ static void print_commands_list(struct b_command *cmd)
if (len > desb_margin) { if (len > desb_margin) {
desb_margin = len; desb_margin = len;
} }
entry = b_queue_next(entry);
} }
b_paragraph_format format = {0}; b_paragraph_format format = {0};
@@ -700,9 +733,9 @@ static void print_commands_list(struct b_command *cmd)
format.p_right_margin = 4; format.p_right_margin = 4;
size_t i = 0; size_t i = 0;
b_queue_foreach (&it, &cmd->b_subcommands) { entry = b_queue_first(&cmd->b_subcommands);
struct b_command *sub while (entry) {
= b_unbox(struct b_command, it.entry, b_entry); struct b_command *sub = b_unbox(struct b_command, entry, b_entry);
b_string_clear(str); b_string_clear(str);
get_command_string(sub, str); get_command_string(sub, str);
@@ -722,6 +755,8 @@ static void print_commands_list(struct b_command *cmd)
get_command_description(sub, str); get_command_description(sub, str);
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
entry = b_queue_next(entry);
} }
b_string_unref(str); 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 *b_command_get_subcommand_with_name(
struct b_command *cmd, const char *name) struct b_command *cmd, const char *name)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
b_queue_foreach (&it, &cmd->b_subcommands) { while (entry) {
struct b_command *subcmd 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) { if (!subcmd || !subcmd->b_name) {
continue; goto skip;
} }
if (!strcmp(subcmd->b_name, name)) { if (!strcmp(subcmd->b_name, name)) {
return subcmd; return subcmd;
} }
skip:
entry = b_queue_next(entry);
} }
return NULL; 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 *b_command_get_subcommand_with_long_name(
struct b_command *cmd, const char *long_name) struct b_command *cmd, const char *long_name)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
b_queue_foreach (&it, &cmd->b_subcommands) { while (entry) {
struct b_command *subcmd 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) { if (!subcmd || !subcmd->b_long_name) {
continue; goto skip;
} }
if (!strcmp(subcmd->b_name, long_name)) { if (!strcmp(subcmd->b_name, long_name)) {
return subcmd; return subcmd;
} }
skip:
entry = b_queue_next(entry);
} }
return NULL; 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 *b_command_get_subcommand_with_short_name(
struct b_command *cmd, char short_name) struct b_command *cmd, char short_name)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands);
b_queue_foreach (&it, &cmd->b_subcommands) { while (entry) {
struct b_command *subcmd 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) { if (!subcmd || !subcmd->b_short_name) {
continue; goto skip;
} }
if (subcmd->b_short_name == short_name) { if (subcmd->b_short_name == short_name) {
return subcmd; return subcmd;
} }
skip:
entry = b_queue_next(entry);
} }
return NULL; 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_option *b_command_get_option_with_long_name(
struct b_command *cmd, const char *long_name) struct b_command *cmd, const char *long_name)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
b_queue_foreach (&it, &cmd->b_opt) { while (entry) {
struct b_command_option *opt 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) { if (!opt || !opt->opt_long_name) {
continue; goto skip;
} }
if (!strcmp(opt->opt_long_name, long_name)) { if (!strcmp(opt->opt_long_name, long_name)) {
return opt; return opt;
} }
skip:
entry = b_queue_next(entry);
} }
return NULL; 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_option *b_command_get_option_with_short_name(
struct b_command *cmd, char short_name) struct b_command *cmd, char short_name)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
b_queue_foreach (&it, &cmd->b_opt) { while (entry) {
struct b_command_option *opt 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) { if (!opt || !opt->opt_long_name) {
continue; goto skip;
} }
if (opt->opt_short_name == short_name) { if (opt->opt_short_name == short_name) {
return opt; return opt;
} }
skip:
entry = b_queue_next(entry);
} }
return NULL; 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_option *b_command_get_option_with_id(
struct b_command *cmd, unsigned int id) struct b_command *cmd, unsigned int id)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_opt);
b_queue_foreach (&it, &cmd->b_opt) { while (entry) {
struct b_command_option *opt 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;
}
if (opt->opt_id == id) { if (opt->opt_id == id) {
return opt; return opt;
} }
entry = b_queue_next(entry);
} }
return NULL; 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_arg *b_command_get_arg_with_id(
struct b_command *cmd, unsigned int id) struct b_command *cmd, unsigned int id)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_arg);
b_queue_foreach (&it, &cmd->b_arg) { while (entry) {
struct b_command_arg *arg 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;
}
if (arg->arg_id == id) { if (arg->arg_id == id) {
return arg; return arg;
} }
entry = b_queue_next(entry);
} }
return NULL; 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_string *str = b_string_create();
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&cmd->b_usage);
b_queue_foreach (&it, &cmd->b_usage) { while (entry) {
struct b_command_usage *usage 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_string_clear(str); b_string_clear(str);
get_usage_string(cmd, args, usage, str); get_usage_string(cmd, args, usage, str);
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
entry = b_queue_next(entry);
} }
b_string_unref(str); 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) static int resolve_command_parents(struct b_btree *commands)
{ {
struct b_btree_iterator it; struct b_btree_node *node = b_btree_first(commands);
b_btree_foreach (&it, commands) { while (node) {
struct b_command *cmd = b_unbox(struct b_command, it.node, b_node); struct b_command *cmd = b_unbox(struct b_command, node, b_node);
if (cmd->b_parent_id == B_COMMAND_INVALID_ID) { if (cmd->b_parent_id == B_COMMAND_INVALID_ID) {
continue; goto skip;
} }
cmd->b_parent = get_command(commands, cmd->b_parent_id); 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); add_subcommand(cmd->b_parent, cmd);
skip:
node = b_btree_next(node);
} }
return 0; return 0;

View File

@@ -164,24 +164,22 @@ typedef struct b_arglist_value {
} b_arglist_value; } b_arglist_value;
typedef struct b_arglist_iterator { typedef struct b_arglist_iterator {
b_iterator _base;
size_t i; size_t i;
unsigned int opt_id; unsigned int opt_id;
struct b_arglist_value *value; 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; unsigned int _opt_filter, _arg_filter;
} b_arglist_iterator; } b_arglist_iterator;
typedef struct b_arglist_option_iterator { typedef struct b_arglist_option_iterator {
b_iterator _base;
size_t i; size_t i;
unsigned int opt_id; unsigned int opt_id;
struct b_arglist_option *opt; struct b_arglist_option *opt;
b_btree_iterator _opt_it; b_btree_node *_opt_it;
unsigned int _opt_filter; unsigned int _opt_filter;
} b_arglist_option_iterator; } b_arglist_option_iterator;

View File

@@ -26,18 +26,17 @@ void b_command_option_destroy(struct b_command_option *opt)
free(opt->opt_description); 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 (entry) {
while (b_queue_iterator_is_valid(&it)) {
struct b_command_arg *arg 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; 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); b_command_arg_destroy(arg);
entry = next;
} }
free(opt); 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); size_t nr_args = b_queue_length(&opt->opt_args);
bool close_bracket = false; bool close_bracket = false;
b_queue_iterator it; size_t i = 0;
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 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) { if (!arg || !arg->arg_allowed_values) {
continue; goto skip;
} }
if (it.i > 0) { if (i > 0) {
b_string_append_cstr(out, "; "); b_string_append_cstr(out, "; ");
} else { } else {
b_string_append_cstr(out, " [["); 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, out, " " F_GREEN "%s" F_RESET,
arg->arg_allowed_values[i]); arg->arg_allowed_values[i]);
} }
skip:
i++;
entry = b_queue_next(entry);
} }
if (close_bracket) { if (close_bracket) {
@@ -175,12 +179,12 @@ void z__b_get_option_usage_string(
b_string_append_cstr(out, "}"); b_string_append_cstr(out, "}");
} }
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
b_queue_foreach (&it, &opt->opt_args) { while (entry) {
struct b_command_arg *arg 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) { if (!arg) {
continue; goto skip;
} }
bool optional = false, multi = false; bool optional = false, multi = false;
@@ -228,23 +232,26 @@ void z__b_get_option_usage_string(
if (flags & CMD_STR_COLOUR) { if (flags & CMD_STR_COLOUR) {
b_string_append_cstr(out, F_RESET); 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_arg *b_command_option_get_arg_with_id(
struct b_command_option *opt, unsigned int id) struct b_command_option *opt, unsigned int id)
{ {
b_queue_iterator it; struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
b_queue_foreach (&it, &opt->opt_args) {
while (entry) {
struct b_command_arg *arg 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;
}
if (arg->arg_id == id) { if (arg->arg_id == id) {
return arg; return arg;
} }
entry = b_queue_next(entry);
} }
return NULL; return NULL;