352 lines
13 KiB
C
352 lines
13 KiB
C
#ifndef FX_CMD_H_
|
|
#define FX_CMD_H_
|
|
|
|
#include <fx/bst.h>
|
|
#include <fx/collections/array.h>
|
|
#include <fx/init.h>
|
|
#include <fx/iterator.h>
|
|
#include <fx/queue.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#define fx_arglist_foreach(it, q) \
|
|
for (int z__fx_unique_name() = fx_arglist_iterator_begin( \
|
|
q, \
|
|
FX_COMMAND_INVALID_ID, \
|
|
FX_COMMAND_INVALID_ID, \
|
|
(it)); \
|
|
fx_arglist_iterator_is_valid(it); \
|
|
fx_arglist_iterator_next(it))
|
|
|
|
#define fx_arglist_foreach_filtered(it, q, opt_id, arg_id) \
|
|
for (int z__fx_unique_name() \
|
|
= fx_arglist_iterator_begin(q, opt_id, arg_id, (it)); \
|
|
fx_arglist_iterator_is_valid(it); \
|
|
fx_arglist_iterator_next(it))
|
|
|
|
#define fx_arglist_option_foreach(it, q) \
|
|
for (int z__fx_unique_name() = fx_arglist_option_iterator_begin( \
|
|
q, \
|
|
FX_COMMAND_INVALID_ID, \
|
|
(it)); \
|
|
fx_arglist_option_iterator_is_valid(it); \
|
|
fx_arglist_option_iterator_next(it))
|
|
|
|
#define fx_arglist_option_foreach_filtered(it, q, opt_id) \
|
|
for (int z__fx_unique_name() \
|
|
= fx_arglist_option_iterator_begin(q, opt_id, (it)); \
|
|
fx_arglist_option_iterator_is_valid(it); \
|
|
fx_arglist_option_iterator_next(it))
|
|
|
|
#define FX_COMMAND(id, parent_id) \
|
|
static fx_command *command_##id = NULL; \
|
|
static void __init_##id( \
|
|
fx_command *, \
|
|
fx_command_option *, \
|
|
fx_command_arg *, \
|
|
fx_command_usage *); \
|
|
FX_INIT(init_##id) \
|
|
{ \
|
|
command_##id = fx_command_create(id); \
|
|
if ((parent_id) != FX_COMMAND_INVALID_ID) { \
|
|
fx_command_set_parent(command_##id, parent_id); \
|
|
} \
|
|
__init_##id(command_##id, NULL, NULL, NULL); \
|
|
fx_command_register(command_##id); \
|
|
} \
|
|
static void __init_##id( \
|
|
fx_command *this_cmd, \
|
|
fx_command_option *this_opt, \
|
|
fx_command_arg *this_arg, \
|
|
fx_command_usage *this_usage)
|
|
|
|
#define FX_COMMAND_NAME(name) fx_command_set_name(this_cmd, (name))
|
|
#define FX_COMMAND_LONG_NAME(name) fx_command_set_long_name(this_cmd, (name))
|
|
#define FX_COMMAND_SHORT_NAME(name) fx_command_set_short_name(this_cmd, (name))
|
|
#define FX_COMMAND_DESC(desc) fx_command_set_description(this_cmd, (desc))
|
|
#define FX_COMMAND_FLAGS(flags) fx_command_set_flags(this_cmd, (flags))
|
|
#define FX_COMMAND_FUNCTION(fn) fx_command_set_callback(this_cmd, (fn))
|
|
|
|
#define FX_COMMAND_OPTION(id) \
|
|
fx_command_option *opt_##id = fx_command_add_option(this_cmd, (id)); \
|
|
this_opt = opt_##id; \
|
|
if (this_opt)
|
|
|
|
#define FX_COMMAND_OPTION_GEN(id) \
|
|
fx_command_option *z__fx_unique_name() \
|
|
= fx_command_add_option(this_cmd, (id)); \
|
|
this_opt = z__fx_unique_name(); \
|
|
if (this_opt)
|
|
|
|
#define FX_COMMAND_HELP_OPTION() \
|
|
do { \
|
|
fx_command_option *opt = fx_command_add_option( \
|
|
this_cmd, \
|
|
FX_COMMAND_OPTION_HELP); \
|
|
fx_command_option_set_description( \
|
|
opt, \
|
|
"Show this help message"); \
|
|
fx_command_option_set_short_name(opt, 'h'); \
|
|
fx_command_option_set_long_name(opt, "help"); \
|
|
} while (0)
|
|
|
|
#define FX_OPTION_LONG_NAME(name) \
|
|
fx_command_option_set_long_name(this_opt, (name))
|
|
|
|
#define FX_OPTION_SHORT_NAME(name) \
|
|
fx_command_option_set_short_name(this_opt, (name))
|
|
|
|
#define FX_OPTION_DESC(desc) fx_command_option_set_description(this_opt, (desc))
|
|
|
|
#define FX_OPTION_ARG(id) \
|
|
fx_command_arg *arg_##id = fx_command_option_add_arg(this_opt, (id)); \
|
|
this_arg = arg_##id; \
|
|
if (this_arg)
|
|
|
|
#define FX_COMMAND_ARG(id) \
|
|
fx_command_arg *arg_##id = fx_command_add_arg(this_cmd, (id)); \
|
|
this_arg = arg_##id; \
|
|
if (this_arg)
|
|
|
|
#define FX_ARG_NAME(name) fx_command_arg_set_name(this_arg, (name))
|
|
|
|
#define FX_ARG_DESC(desc) fx_command_arg_set_description(this_arg, (desc))
|
|
|
|
#define FX_ARG_NR_VALUES(nr_values) \
|
|
fx_command_arg_set_nr_values(this_arg, (nr_values))
|
|
|
|
#define FX_ARG_ALLOWED_VALUES(...) \
|
|
static const char *allowed_values[] = { \
|
|
__VA_ARGS__, \
|
|
NULL, \
|
|
}; \
|
|
fx_command_arg_set_allowed_values(this_arg, allowed_values)
|
|
|
|
#define FX_COMMAND_USAGE() \
|
|
fx_command_usage *z__fx_unique_name() \
|
|
= fx_command_add_usage(this_cmd); \
|
|
this_usage = z__fx_unique_name(); \
|
|
if (this_usage)
|
|
|
|
#define FX_COMMAND_USAGE_COMMAND(cmd_id) \
|
|
fx_command_usage_add_command(this_usage, cmd_id)
|
|
|
|
#define FX_COMMAND_USAGE_COMMAND_PLACEHOLDER() \
|
|
fx_command_usage_add_command(this_usage, FX_COMMAND_INVALID_ID)
|
|
|
|
#define FX_COMMAND_USAGE_OPT(opt_id) \
|
|
fx_command_usage_add_option(this_usage, opt_##opt_id)
|
|
|
|
#define FX_COMMAND_USAGE_OPT_PLACEHOLDER() \
|
|
fx_command_usage_add_option(this_usage, NULL)
|
|
|
|
#define FX_COMMAND_USAGE_ARG(opt_id) \
|
|
fx_command_usage_add_arg(this_usage, arg_##opt_id)
|
|
|
|
#define FX_COMMAND_USAGE_ARG_PLACEHOLDER() \
|
|
fx_command_usage_add_arg(this_usage, NULL)
|
|
|
|
#define FX_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001)
|
|
#define FX_COMMAND_INVALID_ID ((uintptr_t)0xFFFFFFFF)
|
|
|
|
typedef enum fx_command_arg_value_count {
|
|
FX_ARG_0_OR_1_VALUES = -1,
|
|
FX_ARG_0_OR_MORE_VALUES = -2,
|
|
FX_ARG_1_OR_MORE_VALUES = -3,
|
|
} fx_command_arg_value_count;
|
|
|
|
typedef enum fx_command_arg_type {
|
|
FX_COMMAND_ARG_NONE = 0,
|
|
FX_COMMAND_ARG_STRING,
|
|
FX_COMMAND_ARG_SIGNED_INT,
|
|
FX_COMMAND_ARG_UNSIGNED_INT,
|
|
|
|
FX_COMMAND_ARG_INT = FX_COMMAND_ARG_SIGNED_INT,
|
|
} fx_command_arg_type;
|
|
|
|
typedef enum fx_command_flags {
|
|
FX_COMMAND_SHOW_HELP_BY_DEFAULT = 0x01u,
|
|
} fx_command_flags;
|
|
|
|
typedef enum fx_command_usage_flags {
|
|
FX_COMMAND_USAGE_SHOW_OPTIONS = 0x01u,
|
|
} fx_command_usage_flags;
|
|
|
|
typedef struct fx_arglist_value {
|
|
unsigned int val_id;
|
|
fx_command_arg_type val_type;
|
|
struct fx_bst_node val_node;
|
|
|
|
union {
|
|
char *val_str;
|
|
long long val_int;
|
|
unsigned long long val_uint;
|
|
};
|
|
} fx_arglist_value;
|
|
|
|
typedef struct fx_arglist_iterator {
|
|
size_t i;
|
|
|
|
unsigned int opt_id;
|
|
struct fx_arglist_value *value;
|
|
|
|
fx_bst_node *_opt_it, *_arg_it;
|
|
unsigned int _opt_filter, _arg_filter;
|
|
} fx_arglist_iterator;
|
|
|
|
typedef struct fx_arglist_option_iterator {
|
|
size_t i;
|
|
|
|
unsigned int opt_id;
|
|
struct fx_arglist_option *opt;
|
|
|
|
fx_bst_node *_opt_it;
|
|
unsigned int _opt_filter;
|
|
} fx_arglist_option_iterator;
|
|
|
|
typedef struct fx_command fx_command;
|
|
typedef struct fx_command_option fx_command_option;
|
|
typedef struct fx_command_arg fx_command_arg;
|
|
typedef struct fx_command_usage fx_command_usage;
|
|
typedef struct fx_arglist fx_arglist;
|
|
typedef struct fx_arglist_option fx_arglist_option;
|
|
|
|
typedef int (*fx_command_callback)(
|
|
const fx_command *,
|
|
const fx_arglist *,
|
|
const fx_array *);
|
|
|
|
FX_API fx_command *fx_command_create(unsigned int id);
|
|
FX_API void fx_command_destroy(fx_command *cmd);
|
|
FX_API fx_status fx_command_register(fx_command *cmd);
|
|
FX_API int fx_command_dispatch(
|
|
unsigned int cmd_id,
|
|
int argc,
|
|
const char **argv);
|
|
|
|
FX_API fx_status fx_command_set_name(fx_command *cmd, const char *name);
|
|
FX_API fx_status fx_command_set_long_name(fx_command *cmd, const char *name);
|
|
FX_API fx_status fx_command_set_short_name(fx_command *cmd, char name);
|
|
FX_API fx_status fx_command_set_flags(fx_command *cmd, fx_command_flags flags);
|
|
FX_API fx_status fx_command_set_description(
|
|
fx_command *cmd,
|
|
const char *description);
|
|
FX_API fx_status fx_command_set_callback(
|
|
fx_command *cmd,
|
|
fx_command_callback callback);
|
|
FX_API fx_status fx_command_set_parent(fx_command *cmd, unsigned int parent_id);
|
|
FX_API fx_command_option *fx_command_add_option(fx_command *cmd, int id);
|
|
FX_API fx_command_arg *fx_command_add_arg(fx_command *cmd, int id);
|
|
FX_API fx_command_usage *fx_command_add_usage(fx_command *cmd);
|
|
|
|
FX_API const fx_command_option *fx_command_get_option(
|
|
const fx_command *cmd,
|
|
int id);
|
|
|
|
FX_API const char *fx_command_option_get_long_name(
|
|
const fx_command_option *opt);
|
|
FX_API char fx_command_option_get_short_name(const fx_command_option *opt);
|
|
FX_API const char *fx_command_option_get_description(fx_command_option *opt);
|
|
FX_API fx_status fx_command_option_set_long_name(
|
|
fx_command_option *opt,
|
|
const char *name);
|
|
FX_API fx_status fx_command_option_set_short_name(
|
|
fx_command_option *opt,
|
|
char name);
|
|
FX_API fx_status fx_command_option_set_description(
|
|
fx_command_option *opt,
|
|
const char *description);
|
|
FX_API fx_command_arg *fx_command_option_add_arg(
|
|
fx_command_option *opt,
|
|
int id);
|
|
|
|
FX_API fx_status fx_command_arg_set_name(fx_command_arg *arg, const char *name);
|
|
FX_API fx_status fx_command_arg_set_description(
|
|
fx_command_arg *arg,
|
|
const char *description);
|
|
FX_API fx_status fx_command_arg_set_nr_values(
|
|
fx_command_arg *arg,
|
|
fx_command_arg_value_count nr_values);
|
|
FX_API fx_status fx_command_arg_set_allowed_values(
|
|
fx_command_arg *arg,
|
|
const char **allowed_values);
|
|
|
|
FX_API fx_status fx_command_usage_add_option(
|
|
fx_command_usage *usage,
|
|
fx_command_option *opt);
|
|
FX_API fx_status fx_command_usage_add_arg(
|
|
fx_command_usage *usage,
|
|
fx_command_arg *opt);
|
|
FX_API fx_status fx_command_usage_add_command(
|
|
fx_command_usage *usage,
|
|
unsigned int cmd_id);
|
|
|
|
FX_API fx_status fx_arglist_get_string(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int arg_id,
|
|
unsigned int index,
|
|
const char **out);
|
|
FX_API fx_status fx_arglist_get_int(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int arg_id,
|
|
unsigned int index,
|
|
long long *out);
|
|
FX_API 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);
|
|
FX_API fx_status fx_arglist_get_option(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int index,
|
|
fx_arglist_option **out);
|
|
FX_API size_t fx_arglist_get_count(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int arg_id);
|
|
|
|
FX_API fx_status fx_arglist_report_missing_option(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id);
|
|
FX_API fx_status fx_arglist_report_unexpected_arg(
|
|
const fx_arglist *args,
|
|
const char *value);
|
|
FX_API fx_status fx_arglist_report_invalid_arg_value(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int arg_id,
|
|
const char *value);
|
|
FX_API fx_status fx_arglist_report_missing_args(
|
|
const fx_arglist *args,
|
|
unsigned int opt_id,
|
|
unsigned int arg_id,
|
|
size_t nr_supplied);
|
|
|
|
FX_API fx_status fx_arglist_option_get_value(
|
|
const fx_arglist_option *opt,
|
|
unsigned int arg_id,
|
|
unsigned int index,
|
|
fx_arglist_value **out);
|
|
|
|
FX_API int fx_arglist_iterator_begin(
|
|
const fx_arglist *args,
|
|
unsigned int opt_filter,
|
|
unsigned int arg_filter,
|
|
fx_arglist_iterator *it);
|
|
FX_API bool fx_arglist_iterator_next(fx_arglist_iterator *it);
|
|
FX_API bool fx_arglist_iterator_is_valid(const fx_arglist_iterator *it);
|
|
|
|
FX_API int fx_arglist_option_iterator_begin(
|
|
const fx_arglist *args,
|
|
unsigned int opt_filter,
|
|
fx_arglist_option_iterator *it);
|
|
FX_API bool fx_arglist_option_iterator_next(fx_arglist_option_iterator *it);
|
|
FX_API bool fx_arglist_option_iterator_is_valid(
|
|
const fx_arglist_option_iterator *it);
|
|
|
|
#endif
|