fx.cmdline: convert to new assembly build system
This commit is contained in:
+133
-53
@@ -1,8 +1,8 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/core/misc.h>
|
||||
#include <fx/ds/string.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/term/print.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdarg.h>
|
||||
@@ -11,14 +11,28 @@
|
||||
#include <string.h>
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(
|
||||
struct fx_arglist_option, opt_node, opt_id, put_arglist_option)
|
||||
struct fx_arglist_option,
|
||||
opt_node,
|
||||
opt_id,
|
||||
put_arglist_option)
|
||||
FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct fx_arglist_option, unsigned int, opt_node, opt_id, get_arglist_option)
|
||||
struct fx_arglist_option,
|
||||
unsigned int,
|
||||
opt_node,
|
||||
opt_id,
|
||||
get_arglist_option)
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(
|
||||
struct fx_arglist_value, val_node, val_id, put_arglist_value)
|
||||
struct fx_arglist_value,
|
||||
val_node,
|
||||
val_id,
|
||||
put_arglist_value)
|
||||
FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct fx_arglist_value, unsigned int, val_node, val_id, get_arglist_value)
|
||||
struct fx_arglist_value,
|
||||
unsigned int,
|
||||
val_node,
|
||||
val_id,
|
||||
get_arglist_value)
|
||||
|
||||
struct argv_parser {
|
||||
struct fx_command *cmd;
|
||||
@@ -62,7 +76,9 @@ struct fx_arglist *fx_arglist_create(void)
|
||||
return out;
|
||||
}
|
||||
|
||||
static void move_to_subcommand(struct argv_parser *parser, struct fx_command *cmd)
|
||||
static void move_to_subcommand(
|
||||
struct argv_parser *parser,
|
||||
struct fx_command *cmd)
|
||||
{
|
||||
parser->cmd = cmd;
|
||||
parser->arglist->list_command = cmd;
|
||||
@@ -86,10 +102,13 @@ static fx_status set_opt(struct fx_arglist *args, struct fx_command_option *opt)
|
||||
}
|
||||
|
||||
static fx_status put_arg(
|
||||
struct fx_arglist *args, struct fx_command_arg *arg, const char *value)
|
||||
struct fx_arglist *args,
|
||||
struct fx_command_arg *arg,
|
||||
const char *value)
|
||||
{
|
||||
struct fx_arglist_option *arglist_opt
|
||||
= get_arglist_option(&args->list_options, FX_COMMAND_INVALID_ID);
|
||||
struct fx_arglist_option *arglist_opt = get_arglist_option(
|
||||
&args->list_options,
|
||||
FX_COMMAND_INVALID_ID);
|
||||
if (!arglist_opt) {
|
||||
arglist_opt = malloc(sizeof *arglist_opt);
|
||||
|
||||
@@ -134,8 +153,10 @@ static fx_status put_arg(
|
||||
}
|
||||
|
||||
static fx_status put_opt_arg(
|
||||
struct fx_arglist_option *arglist_opt, struct fx_command_option *opt,
|
||||
struct fx_command_arg *arg, const char *value)
|
||||
struct fx_arglist_option *arglist_opt,
|
||||
struct fx_command_option *opt,
|
||||
struct fx_command_arg *arg,
|
||||
const char *value)
|
||||
{
|
||||
|
||||
if (arg->arg_allowed_values) {
|
||||
@@ -202,14 +223,18 @@ static fx_status check_required_args(struct argv_parser *parser)
|
||||
}
|
||||
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id,
|
||||
parser->arglist,
|
||||
FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id,
|
||||
parser->nr_values_cur_arg);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
if (parser->nr_values_cur_arg != arg->arg_nr_values) {
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id,
|
||||
parser->arglist,
|
||||
FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id,
|
||||
parser->nr_values_cur_arg);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
@@ -225,8 +250,9 @@ static fx_status parse_arg(struct argv_parser *parser)
|
||||
break;
|
||||
}
|
||||
|
||||
struct fx_command *subcmd
|
||||
= fx_command_get_subcommand_with_name(parser->cmd, value);
|
||||
struct fx_command *subcmd = fx_command_get_subcommand_with_name(
|
||||
parser->cmd,
|
||||
value);
|
||||
if (subcmd) {
|
||||
move_to_subcommand(parser, subcmd);
|
||||
parser->arglist->list_argv_last_command
|
||||
@@ -236,10 +262,14 @@ static fx_status parse_arg(struct argv_parser *parser)
|
||||
}
|
||||
|
||||
struct fx_command_arg *arg = fx_unbox(
|
||||
struct fx_command_arg, parser->arg_it, arg_entry);
|
||||
struct fx_command_arg,
|
||||
parser->arg_it,
|
||||
arg_entry);
|
||||
|
||||
if (!arg) {
|
||||
fx_arglist_report_unexpected_arg(parser->arglist, value);
|
||||
fx_arglist_report_unexpected_arg(
|
||||
parser->arglist,
|
||||
value);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
@@ -249,8 +279,10 @@ static fx_status parse_arg(struct argv_parser *parser)
|
||||
|
||||
if (status == FX_ERR_INVALID_ARGUMENT) {
|
||||
fx_arglist_report_invalid_arg_value(
|
||||
parser->arglist, FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id, value);
|
||||
parser->arglist,
|
||||
FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id,
|
||||
value);
|
||||
}
|
||||
|
||||
if (FX_ERR(status)) {
|
||||
@@ -306,7 +338,8 @@ static fx_status parse_short_opt(struct argv_parser *parser)
|
||||
opt = fx_command_get_option_with_short_name(parser->cmd, flag);
|
||||
if (!opt) {
|
||||
subcmd = fx_command_get_subcommand_with_short_name(
|
||||
parser->cmd, flag);
|
||||
parser->cmd,
|
||||
flag);
|
||||
}
|
||||
|
||||
if (subcmd) {
|
||||
@@ -319,10 +352,13 @@ static fx_status parse_short_opt(struct argv_parser *parser)
|
||||
|
||||
if (!opt) {
|
||||
fx_string *usage = z__fx_command_default_usage_string(
|
||||
parser->cmd, NULL, parser->arglist);
|
||||
parser->cmd,
|
||||
NULL,
|
||||
parser->arglist);
|
||||
fx_err("unrecognised argument '" F_YELLOW "-%c" F_RESET
|
||||
"'\n\n",
|
||||
flag, fx_string_get_cstr(usage));
|
||||
flag,
|
||||
fx_string_get_cstr(usage));
|
||||
fx_i("usage: %s", fx_string_get_cstr(usage));
|
||||
fx_i("for more information, use '" F_YELLOW
|
||||
"--help" F_RESET "'\n");
|
||||
@@ -382,8 +418,10 @@ static fx_status parse_short_opt(struct argv_parser *parser)
|
||||
|
||||
if (status == FX_ERR_INVALID_ARGUMENT) {
|
||||
fx_arglist_report_invalid_arg_value(
|
||||
parser->arglist, FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id, value);
|
||||
parser->arglist,
|
||||
FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id,
|
||||
value);
|
||||
}
|
||||
|
||||
if (FX_ERR(status)) {
|
||||
@@ -408,7 +446,9 @@ static fx_status parse_short_opt(struct argv_parser *parser)
|
||||
}
|
||||
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, opt->opt_id, arg->arg_id,
|
||||
parser->arglist,
|
||||
opt->opt_id,
|
||||
arg->arg_id,
|
||||
nr_args_cur_opt);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
@@ -420,7 +460,9 @@ static fx_status parse_short_opt(struct argv_parser *parser)
|
||||
|
||||
if (!value) {
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, opt->opt_id, arg->arg_id,
|
||||
parser->arglist,
|
||||
opt->opt_id,
|
||||
arg->arg_id,
|
||||
nr_args_cur_opt);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
@@ -443,16 +485,20 @@ static fx_status parse_long_opt(struct argv_parser *parser)
|
||||
opt = fx_command_get_option_with_long_name(parser->cmd, opt_name);
|
||||
if (!opt) {
|
||||
subcmd = fx_command_get_subcommand_with_long_name(
|
||||
parser->cmd, opt_name);
|
||||
parser->cmd,
|
||||
opt_name);
|
||||
}
|
||||
|
||||
if (!opt && !subcmd) {
|
||||
fx_string *usage = z__fx_command_default_usage_string(
|
||||
parser->cmd, NULL, parser->arglist);
|
||||
parser->cmd,
|
||||
NULL,
|
||||
parser->arglist);
|
||||
fx_err("unrecognised argument '" F_YELLOW "--%s" F_RESET
|
||||
"'\n\nusage: %s\n\nfor more information, use '" F_YELLOW
|
||||
"--help" F_RESET "'\n",
|
||||
opt_name, fx_string_get_cstr(usage));
|
||||
opt_name,
|
||||
fx_string_get_cstr(usage));
|
||||
fx_string_unref(usage);
|
||||
|
||||
return FX_ERR_NO_ENTRY;
|
||||
@@ -504,8 +550,10 @@ static fx_status parse_long_opt(struct argv_parser *parser)
|
||||
|
||||
if (status == FX_ERR_INVALID_ARGUMENT) {
|
||||
fx_arglist_report_invalid_arg_value(
|
||||
parser->arglist, FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id, value);
|
||||
parser->arglist,
|
||||
FX_COMMAND_INVALID_ID,
|
||||
arg->arg_id,
|
||||
value);
|
||||
}
|
||||
|
||||
if (FX_ERR(status)) {
|
||||
@@ -533,7 +581,9 @@ static fx_status parse_long_opt(struct argv_parser *parser)
|
||||
}
|
||||
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, opt->opt_id, arg->arg_id,
|
||||
parser->arglist,
|
||||
opt->opt_id,
|
||||
arg->arg_id,
|
||||
nr_args_cur_opt);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
@@ -546,7 +596,9 @@ static fx_status parse_long_opt(struct argv_parser *parser)
|
||||
|
||||
if (!value) {
|
||||
fx_arglist_report_missing_args(
|
||||
parser->arglist, opt->opt_id, arg->arg_id,
|
||||
parser->arglist,
|
||||
opt->opt_id,
|
||||
arg->arg_id,
|
||||
nr_args_cur_opt);
|
||||
return FX_ERR_BAD_FORMAT;
|
||||
}
|
||||
@@ -580,7 +632,9 @@ static bool should_show_help(struct fx_command *cmd, struct fx_arglist *args)
|
||||
}
|
||||
|
||||
fx_status fx_arglist_parse(
|
||||
struct fx_arglist *args, struct fx_command **cmd, int argc,
|
||||
struct fx_arglist *args,
|
||||
struct fx_command **cmd,
|
||||
int argc,
|
||||
const char **argv)
|
||||
{
|
||||
struct argv_parser parser = {
|
||||
@@ -665,7 +719,9 @@ void fx_arglist_destroy(struct fx_arglist *args)
|
||||
args_it = fx_bst_first(&opt->opt_values);
|
||||
while (args_it) {
|
||||
struct fx_arglist_value *val = fx_unbox(
|
||||
struct fx_arglist_value, args_it, val_node);
|
||||
struct fx_arglist_value,
|
||||
args_it,
|
||||
val_node);
|
||||
args_next = fx_bst_next(args_it);
|
||||
|
||||
if (val) {
|
||||
@@ -686,8 +742,11 @@ void fx_arglist_destroy(struct fx_arglist *args)
|
||||
}
|
||||
|
||||
fx_status fx_arglist_get_string(
|
||||
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
|
||||
unsigned int index, const char **out)
|
||||
const fx_arglist *args,
|
||||
unsigned int opt_id,
|
||||
unsigned int arg_id,
|
||||
unsigned int index,
|
||||
const char **out)
|
||||
{
|
||||
fx_arglist_iterator it = {0};
|
||||
fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
|
||||
@@ -710,8 +769,11 @@ fx_status fx_arglist_get_string(
|
||||
}
|
||||
|
||||
fx_status fx_arglist_get_int(
|
||||
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
|
||||
unsigned int index, long long *out)
|
||||
const fx_arglist *args,
|
||||
unsigned int opt_id,
|
||||
unsigned int arg_id,
|
||||
unsigned int index,
|
||||
long long *out)
|
||||
{
|
||||
fx_arglist_iterator it = {0};
|
||||
fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
|
||||
@@ -734,8 +796,11 @@ fx_status fx_arglist_get_int(
|
||||
}
|
||||
|
||||
fx_status fx_arglist_get_uint(
|
||||
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
|
||||
unsigned int index, unsigned long long *out)
|
||||
const fx_arglist *args,
|
||||
unsigned int opt_id,
|
||||
unsigned int arg_id,
|
||||
unsigned int index,
|
||||
unsigned long long *out)
|
||||
{
|
||||
fx_arglist_iterator it = {0};
|
||||
fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
|
||||
@@ -758,7 +823,9 @@ fx_status fx_arglist_get_uint(
|
||||
}
|
||||
|
||||
fx_status fx_arglist_get_option(
|
||||
const fx_arglist *args, unsigned int opt_id, unsigned int index,
|
||||
const fx_arglist *args,
|
||||
unsigned int opt_id,
|
||||
unsigned int index,
|
||||
fx_arglist_option **out)
|
||||
{
|
||||
struct fx_bst_node *node = fx_bst_first(&args->list_options);
|
||||
@@ -785,7 +852,9 @@ fx_status fx_arglist_get_option(
|
||||
}
|
||||
|
||||
size_t fx_arglist_get_count(
|
||||
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id)
|
||||
const fx_arglist *args,
|
||||
unsigned int opt_id,
|
||||
unsigned int arg_id)
|
||||
{
|
||||
size_t count = 0;
|
||||
fx_arglist_iterator it;
|
||||
@@ -799,12 +868,15 @@ size_t fx_arglist_get_count(
|
||||
}
|
||||
|
||||
fx_status fx_arglist_option_get_value(
|
||||
const fx_arglist_option *opt, unsigned int arg_id, unsigned int index,
|
||||
const fx_arglist_option *opt,
|
||||
unsigned int arg_id,
|
||||
unsigned int index,
|
||||
fx_arglist_value **out)
|
||||
{
|
||||
struct fx_bst_node *node = fx_bst_first(&opt->opt_values);
|
||||
while (node) {
|
||||
fx_arglist_value *cur = fx_unbox(fx_arglist_value, node, val_node);
|
||||
fx_arglist_value *cur
|
||||
= fx_unbox(fx_arglist_value, node, val_node);
|
||||
|
||||
if (cur->val_id != arg_id) {
|
||||
node = fx_bst_next(node);
|
||||
@@ -825,7 +897,8 @@ fx_status fx_arglist_option_get_value(
|
||||
|
||||
/************************ arglist iterator functions **************************/
|
||||
|
||||
static struct fx_arglist_option *advance_to_next_opt(struct fx_arglist_iterator *it)
|
||||
static struct fx_arglist_option *advance_to_next_opt(
|
||||
struct fx_arglist_iterator *it)
|
||||
{
|
||||
struct fx_arglist_option *opt;
|
||||
|
||||
@@ -846,7 +919,8 @@ static struct fx_arglist_option *advance_to_next_opt(struct fx_arglist_iterator
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct fx_arglist_value *advance_to_next_arg(struct fx_arglist_iterator *it)
|
||||
static struct fx_arglist_value *advance_to_next_arg(
|
||||
struct fx_arglist_iterator *it)
|
||||
{
|
||||
struct fx_arglist_value *val;
|
||||
|
||||
@@ -868,8 +942,10 @@ static struct fx_arglist_value *advance_to_next_arg(struct fx_arglist_iterator *
|
||||
}
|
||||
|
||||
int fx_arglist_iterator_begin(
|
||||
const struct fx_arglist *args, unsigned int opt_filter,
|
||||
unsigned int arg_filter, struct fx_arglist_iterator *it)
|
||||
const struct fx_arglist *args,
|
||||
unsigned int opt_filter,
|
||||
unsigned int arg_filter,
|
||||
struct fx_arglist_iterator *it)
|
||||
{
|
||||
memset(it, 0x0, sizeof *it);
|
||||
|
||||
@@ -909,7 +985,9 @@ int fx_arglist_iterator_begin(
|
||||
}
|
||||
|
||||
val = fx_unbox(
|
||||
struct fx_arglist_value, it->_arg_it, val_node);
|
||||
struct fx_arglist_value,
|
||||
it->_arg_it,
|
||||
val_node);
|
||||
if (!val
|
||||
|| (arg_filter != val->val_id
|
||||
&& arg_filter != FX_COMMAND_INVALID_ID)) {
|
||||
@@ -994,7 +1072,8 @@ static struct fx_arglist_option *advance_to_next_opt2(
|
||||
}
|
||||
|
||||
int fx_arglist_option_iterator_begin(
|
||||
const struct fx_arglist *args, unsigned int opt_filter,
|
||||
const struct fx_arglist *args,
|
||||
unsigned int opt_filter,
|
||||
struct fx_arglist_option_iterator *it)
|
||||
{
|
||||
memset(it, 0x0, sizeof *it);
|
||||
@@ -1047,7 +1126,8 @@ bool fx_arglist_option_iterator_next(struct fx_arglist_option_iterator *it)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fx_arglist_option_iterator_is_valid(const struct fx_arglist_option_iterator *it)
|
||||
bool fx_arglist_option_iterator_is_valid(
|
||||
const struct fx_arglist_option_iterator *it)
|
||||
{
|
||||
return it->opt_id != FX_COMMAND_INVALID_ID || it->opt != NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user