cmd: remove internal usage of legacy iterator interface
This commit is contained in:
195
cmd/arglist.c
195
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;
|
||||
|
||||
Reference in New Issue
Block a user