fx.cmdline: convert to new assembly build system

This commit is contained in:
2026-05-03 16:49:33 +01:00
parent 44fed67d43
commit f4469c9eb0
10 changed files with 591 additions and 279 deletions
+142 -64
View File
@@ -1,8 +1,8 @@
#include "command.h"
#include <fx/bst.h>
#include <fx/cmd.h>
#include <fx/core/bst.h>
#include <fx/ds/string.h>
#include <fx/string.h>
#include <fx/term/print.h>
#include <fx/term/tty.h>
#include <stdio.h>
@@ -13,7 +13,12 @@
static struct fx_bst command_list = {0};
FX_BST_DEFINE_SIMPLE_GET(struct fx_command, unsigned int, c_node, c_id, get_command)
FX_BST_DEFINE_SIMPLE_GET(
struct fx_command,
unsigned int,
c_node,
c_id,
get_command)
FX_BST_DEFINE_SIMPLE_INSERT(struct fx_command, c_node, c_id, put_command)
enum item_type {
@@ -26,7 +31,8 @@ static void command_list_cleanup(void)
{
struct fx_bst_node *node = fx_bst_first(&command_list);
while (node) {
struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node);
struct fx_command *cmd
= fx_unbox(struct fx_command, node, c_node);
if (!cmd) {
break;
}
@@ -57,8 +63,10 @@ static void command_usage_destroy(struct fx_command_usage *usage)
{
struct fx_queue_entry *entry = fx_queue_first(&usage->u_parts);
while (entry) {
struct fx_command_usage_entry *arg
= fx_unbox(struct fx_command_usage_entry, entry, e_entry);
struct fx_command_usage_entry *arg = fx_unbox(
struct fx_command_usage_entry,
entry,
e_entry);
if (!arg) {
continue;
}
@@ -216,7 +224,9 @@ fx_status fx_command_set_flags(struct fx_command *cmd, fx_command_flags flags)
return FX_SUCCESS;
}
fx_status fx_command_set_description(struct fx_command *cmd, const char *description)
fx_status fx_command_set_description(
struct fx_command *cmd,
const char *description)
{
char *desc = fx_strdup(description);
if (!desc) {
@@ -233,7 +243,8 @@ fx_status fx_command_set_description(struct fx_command *cmd, const char *descrip
}
fx_status fx_command_set_callback(
struct fx_command *cmd, fx_command_callback callback)
struct fx_command *cmd,
fx_command_callback callback)
{
cmd->c_callback = callback;
return FX_SUCCESS;
@@ -284,7 +295,8 @@ struct fx_command_usage *fx_command_add_usage(struct fx_command *cmd)
}
const struct fx_command_option *fx_command_get_option(
const struct fx_command *cmd, int id)
const struct fx_command *cmd,
int id)
{
struct fx_queue_entry *cur = fx_queue_first(&cmd->c_opt);
while (cur) {
@@ -302,7 +314,8 @@ const struct fx_command_option *fx_command_get_option(
}
fx_status fx_command_usage_add_option(
struct fx_command_usage *usage, struct fx_command_option *opt)
struct fx_command_usage *usage,
struct fx_command_option *opt)
{
struct fx_command_usage_entry *u_opt = malloc(sizeof *u_opt);
if (!u_opt) {
@@ -319,7 +332,8 @@ fx_status fx_command_usage_add_option(
}
fx_status fx_command_usage_add_arg(
struct fx_command_usage *usage, struct fx_command_arg *arg)
struct fx_command_usage *usage,
struct fx_command_arg *arg)
{
struct fx_command_usage_entry *u_arg = malloc(sizeof *u_arg);
if (!u_arg) {
@@ -335,7 +349,9 @@ fx_status fx_command_usage_add_arg(
return FX_SUCCESS;
}
fx_status fx_command_usage_add_command(fx_command_usage *usage, unsigned int cmd_id)
fx_status fx_command_usage_add_command(
fx_command_usage *usage,
unsigned int cmd_id)
{
struct fx_command_usage_entry *u_cmd = malloc(sizeof *u_cmd);
if (!u_cmd) {
@@ -392,7 +408,9 @@ static void prepend_command_name(struct fx_command *cmd, fx_string *out)
}
static void get_qualified_command_name(
struct fx_command *cmd, const struct fx_arglist *args, fx_string *out)
struct fx_command *cmd,
const struct fx_arglist *args,
fx_string *out)
{
for (unsigned int i = 0; i <= args->list_argv_last_command; i++) {
if (i > 0) {
@@ -414,8 +432,10 @@ static void get_qualified_command_name(
}
static void get_usage_string(
struct fx_command *cmd, struct fx_arglist *args,
struct fx_command_usage *usage, fx_string *out)
struct fx_command *cmd,
struct fx_arglist *args,
struct fx_command_usage *usage,
fx_string *out)
{
get_qualified_command_name(cmd, args, out);
@@ -424,7 +444,9 @@ static void get_usage_string(
struct fx_queue_entry *q_entry = fx_queue_first(&usage->u_parts);
while (q_entry) {
struct fx_command_usage_entry *entry = fx_unbox(
struct fx_command_usage_entry, q_entry, e_entry);
struct fx_command_usage_entry,
q_entry,
e_entry);
if (!entry) {
break;
@@ -436,7 +458,10 @@ static void get_usage_string(
switch (entry->e_type) {
case CMD_USAGE_ARG:
if (entry->e_arg) {
z__fx_get_arg_usage_string(entry->e_arg, false, out);
z__fx_get_arg_usage_string(
entry->e_arg,
false,
out);
} else {
fx_string_append_cstr(out, "[[ARGS]");
}
@@ -444,7 +469,9 @@ static void get_usage_string(
case CMD_USAGE_OPT:
if (entry->e_opt) {
z__fx_get_option_usage_string(
entry->e_opt, CMD_STR_DIRECT_USAGE, out);
entry->e_opt,
CMD_STR_DIRECT_USAGE,
out);
} else {
fx_string_append_cstr(out, "[[OPTIONS]");
}
@@ -473,7 +500,8 @@ static void get_usage_string(
}
fx_string *z__fx_command_default_usage_string(
struct fx_command *cmd, struct fx_command_option *with_opt,
struct fx_command *cmd,
struct fx_command_option *with_opt,
const struct fx_arglist *args)
{
fx_string *str = fx_string_create();
@@ -481,7 +509,10 @@ fx_string *z__fx_command_default_usage_string(
if (with_opt) {
fx_string_append_cstr(str, " ");
z__fx_get_option_usage_string(with_opt, CMD_STR_DIRECT_USAGE, str);
z__fx_get_option_usage_string(
with_opt,
CMD_STR_DIRECT_USAGE,
str);
} else if (!fx_queue_empty(&cmd->c_opt)) {
fx_string_append_cstr(str, " [OPTIONS]");
}
@@ -519,7 +550,9 @@ static void get_command_string(struct fx_command *cmd, fx_string *out)
}
fx_string_append_cstrf(
out, F_GREEN "-%c" F_RESET, cmd->c_short_name);
out,
F_GREEN "-%c" F_RESET,
cmd->c_short_name);
r++;
}
@@ -529,7 +562,9 @@ static void get_command_string(struct fx_command *cmd, fx_string *out)
}
fx_string_append_cstrf(
out, F_GREEN "--%s" F_RESET, cmd->c_long_name);
out,
F_GREEN "--%s" F_RESET,
cmd->c_long_name);
r++;
}
}
@@ -544,7 +579,8 @@ static void get_command_description(struct fx_command *cmd, fx_string *out)
* 2) the length of the description string exceeds the remaining line length
* (once the usage string has been printed.
* or,
* 3) the length of the description string is more than three terminal lines.
* 3) the length of the description string is more than three terminal
* lines.
*/
#define description_on_separate_line(opt_len, desc_len) \
((opt_len >= newline_threshold \
@@ -578,12 +614,14 @@ static void print_options_list(struct fx_command *cmd)
z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__fx_get_option_description(opt, desc_str);
size_t opt_len = fx_string_get_size(
opt_str, FX_STRLEN_IGNORE_ESC
| FX_STRLEN_IGNORE_MOD)
+ 4;
size_t opt_len
= fx_string_get_size(
opt_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
size_t desc_len = fx_string_get_size(
desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
desc_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
if (description_on_separate_line(opt_len, desc_len)) {
goto skip;
@@ -616,12 +654,14 @@ static void print_options_list(struct fx_command *cmd)
z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__fx_get_option_description(opt, desc_str);
size_t opt_len = fx_string_get_size(
opt_str, FX_STRLEN_IGNORE_ESC
| FX_STRLEN_IGNORE_MOD)
+ 4;
size_t opt_len
= fx_string_get_size(
opt_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
size_t desc_len = fx_string_get_size(
desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
desc_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
bool new_paragraph
= description_on_separate_line(opt_len, desc_len);
@@ -650,7 +690,10 @@ static void print_options_list(struct fx_command *cmd)
len++;
}
fx_print_paragraph(fx_string_get_cstr(desc_str), OUTPUT_STREAM, &format);
fx_print_paragraph(
fx_string_get_cstr(desc_str),
OUTPUT_STREAM,
&format);
if (new_paragraph) {
fx_tty_putc(OUTPUT_STREAM, 0, '\n');
@@ -678,9 +721,11 @@ static void print_args_list(struct fx_command *cmd)
fx_string_clear(str);
z__fx_get_arg_usage_string(arg, true, str);
size_t len = fx_string_get_size(
str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
size_t len
= fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
if (len > desc_margin) {
desc_margin = len;
@@ -705,10 +750,11 @@ static void print_args_list(struct fx_command *cmd)
fx_tty_puts(OUTPUT_STREAM, 0, " ");
fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str));
unsigned int len = fx_string_get_size(
str, FX_STRLEN_IGNORE_ESC
| FX_STRLEN_IGNORE_MOD)
+ 4;
unsigned int len
= fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
while (len < format.p_left_margin) {
fx_tty_putc(OUTPUT_STREAM, 0, ' ');
len++;
@@ -717,7 +763,10 @@ static void print_args_list(struct fx_command *cmd)
fx_string_clear(str);
z__fx_get_arg_description(arg, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format);
fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry);
}
@@ -738,9 +787,11 @@ static void print_commands_list(struct fx_command *cmd)
fx_string_clear(str);
get_command_string(sub, str);
size_t len = fx_string_get_size(
str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
size_t len
= fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
if (len > desc_margin) {
desc_margin = len;
@@ -765,10 +816,11 @@ static void print_commands_list(struct fx_command *cmd)
fx_tty_puts(OUTPUT_STREAM, 0, " ");
fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str));
unsigned int len = fx_string_get_size(
str, FX_STRLEN_IGNORE_ESC
| FX_STRLEN_IGNORE_MOD)
+ 4;
unsigned int len
= fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
while (len < format.p_left_margin) {
fx_tty_putc(OUTPUT_STREAM, 0, ' ');
len++;
@@ -777,7 +829,10 @@ static void print_commands_list(struct fx_command *cmd)
fx_string_clear(str);
get_command_description(sub, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format);
fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry);
}
@@ -786,7 +841,8 @@ static void print_commands_list(struct fx_command *cmd)
}
struct fx_command *fx_command_get_subcommand_with_name(
struct fx_command *cmd, const char *name)
struct fx_command *cmd,
const char *name)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) {
@@ -808,7 +864,8 @@ struct fx_command *fx_command_get_subcommand_with_name(
}
struct fx_command *fx_command_get_subcommand_with_long_name(
struct fx_command *cmd, const char *long_name)
struct fx_command *cmd,
const char *long_name)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) {
@@ -830,7 +887,8 @@ struct fx_command *fx_command_get_subcommand_with_long_name(
}
struct fx_command *fx_command_get_subcommand_with_short_name(
struct fx_command *cmd, char short_name)
struct fx_command *cmd,
char short_name)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) {
@@ -852,7 +910,8 @@ struct fx_command *fx_command_get_subcommand_with_short_name(
}
struct fx_command_option *fx_command_get_option_with_long_name(
struct fx_command *cmd, const char *long_name)
struct fx_command *cmd,
const char *long_name)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) {
@@ -874,7 +933,8 @@ struct fx_command_option *fx_command_get_option_with_long_name(
}
struct fx_command_option *fx_command_get_option_with_short_name(
struct fx_command *cmd, char short_name)
struct fx_command *cmd,
char short_name)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) {
@@ -896,7 +956,8 @@ struct fx_command_option *fx_command_get_option_with_short_name(
}
struct fx_command_option *fx_command_get_option_with_id(
struct fx_command *cmd, unsigned int id)
struct fx_command *cmd,
unsigned int id)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) {
@@ -914,7 +975,8 @@ struct fx_command_option *fx_command_get_option_with_id(
}
struct fx_command_arg *fx_command_get_arg_with_id(
struct fx_command *cmd, unsigned int id)
struct fx_command *cmd,
unsigned int id)
{
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg);
while (entry) {
@@ -941,7 +1003,10 @@ static void print_usage(struct fx_command *cmd, struct fx_arglist *args)
if (fx_queue_empty(&cmd->c_usage)) {
fx_string *usage
= z__fx_command_default_usage_string(cmd, NULL, args);
fx_print_paragraph(fx_string_get_cstr(usage), OUTPUT_STREAM, &format);
fx_print_paragraph(
fx_string_get_cstr(usage),
OUTPUT_STREAM,
&format);
fx_string_unref(usage);
return;
}
@@ -954,7 +1019,10 @@ static void print_usage(struct fx_command *cmd, struct fx_arglist *args)
fx_string_clear(str);
get_usage_string(cmd, args, usage, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format);
fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry);
}
@@ -968,7 +1036,10 @@ static void print_help(struct fx_command *cmd, struct fx_arglist *args)
if (!cmd->c_parent) {
fx_tty_printf(
OUTPUT_STREAM, 0, F_GREEN "%s" F_RESET "\n", cmd->c_name);
OUTPUT_STREAM,
0,
F_GREEN "%s" F_RESET "\n",
cmd->c_name);
}
if (cmd->c_description) {
@@ -999,9 +1070,13 @@ static int execute_command(struct fx_command *cmd, struct fx_arglist *args)
}
size_t nr_items = fx_arglist_get_count(
args, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID);
args,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID);
size_t nr_help = fx_arglist_get_count(
args, FX_COMMAND_OPTION_HELP, FX_COMMAND_INVALID_ID);
args,
FX_COMMAND_OPTION_HELP,
FX_COMMAND_INVALID_ID);
if ((cmd->c_flags & FX_COMMAND_SHOW_HELP_BY_DEFAULT) && nr_items == 0) {
print_help(cmd, args);
@@ -1020,7 +1095,9 @@ static int execute_command(struct fx_command *cmd, struct fx_arglist *args)
return -1;
}
static fx_status add_subcommand(struct fx_command *parent, struct fx_command *child)
static fx_status add_subcommand(
struct fx_command *parent,
struct fx_command *child)
{
fx_queue_push_back(&parent->c_subcommands, &child->c_entry);
child->c_parent = parent;
@@ -1032,7 +1109,8 @@ static int resolve_command_parents(struct fx_bst *commands)
{
struct fx_bst_node *node = fx_bst_first(commands);
while (node) {
struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node);
struct fx_command *cmd
= fx_unbox(struct fx_command, node, c_node);
if (cmd->c_parent_id == FX_COMMAND_INVALID_ID) {
goto skip;