Skip to content

Command Line

struct cmdline_range (2 fields) source
§ uint64_t start
§ uint64_t end
struct cmdline_list (2 fields) source
§ size_t count
§ struct cmdline_value *items
struct cmdline_value (4 fields) source
§ union
§ enum type_enum c_type
§ enum cmdline_type type
§ union
§ void *write_to
§ void *data
§ uint64_t u64
§ int64_t i64
§ bool b
§ time_ns_t duration
§ enum err (*parse)(void *write_to, const char *text)

this is used when mode == CMDLINE_MODE_TYPED or CMDLINE_MODE_CUSTOM

struct cmdline_entry (13 fields) source

The idea with parent-child relationships:

The dot is used as a namespace marker. With a given cmdline_entry, we trace its lineage to build out what name it really has.

For instance, if we have

apple->parent = orange orange->parent = pineapple pineapple->parent = NULL

the “functional name” of apple is

pineapple.orange.apple

§ const char *name
§ const char *desc

human readable description

§ const char *arg

value format hint e.g. “<hex bytes>”, “<device>”

§ const char *default_val
§ enum cmdline_entry_flags flags
§ enum cmdline_entry_status status
§ struct cmdline_entry *parent
§ uint64_t types
§ struct range range
§ struct cmdline_value value

Regarding the variable to write to

§ const char *const *choices
§ const struct cmdline_map *mappings
§ const struct cmdline_flag *flags_table
struct cmdline_map (2 fields) source
§ const char *name
§ uint64_t value
struct cmdline_flag (2 fields) source
§ const char *name
§ uint64_t value

Schema-Driven Subsystem Definitions

struct cmdline_schema_prop (10 fields) source
§ const char *name
§ const char *desc
§ size_t offset
§ enum type_enum c_type
§ uint64_t types
§ enum err (*parse)(void *dst, const char *text)
§ struct range range
§ const char *const *choices
§ const struct cmdline_map *mappings
§ const struct cmdline_flag *flags_table
struct cmdline_schema (6 fields) source
§ const char *prefix
§ const char *path_hint
§ const char *desc
§ const struct cmdline_schema_prop *props
§ size_t prop_count
enum cmdline_entry_status (3 variants) source
§ CMDLINE_ENTRY_NOT_FOUND = 0
§ CMDLINE_ENTRY_DEFAULTED
§ CMDLINE_ENTRY_FOUND
enum cmdline_entry_flags (4 variants) source
§ CMDLINE_ENTRY_FLAGS_NONE = 0
§ CMDLINE_ENTRY_SYMBOLIC = 1

for namespace parenting purposes

§ CMDLINE_ENTRY_REQUIRED = 1 << 1
§ CMDLINE_ENTRY_HIDDEN = 1 << 2

exclude from help message

enum cmdline_value_mode (4 variants) source
§ CMDLINE_MODE_POLYMORPHIC = 0
§ CMDLINE_MODE_VAR
§ CMDLINE_MODE_TYPED = CMDLINE_MODE_VAR
§ CMDLINE_MODE_CUSTOM = CMDLINE_MODE_VAR
enum cmdline_type (14 variants) source
§ CMDLINE_TYPE_FIRST_ = CMDLINE_TYPE_OFFSET - 1
§ CMDLINE_TYPE_BOOL = CMDLINE_TYPE_OFFSET
§ CMDLINE_TYPE_INT
§ CMDLINE_TYPE_UINT
§ CMDLINE_TYPE_FX
§ CMDLINE_TYPE_DURATION
§ CMDLINE_TYPE_DATA_SIZE
§ CMDLINE_TYPE_RANGE
§ CMDLINE_TYPE_CPU_MASK
§ CMDLINE_TYPE_MAC
§ CMDLINE_TYPE_STRING
§ CMDLINE_TYPE_LIST
§ CMDLINE_TYPE_ERR
§ CMDLINE_TYPE_NONE
typedef void * (*cmdline_instance_resolver_t)(
    const char * path,
    size_t path_len
)
source
void cmdline_parse(const char *input)
source
bool cmdline_wants_help(const char *input)
source
cc_noreturn void cmdline_dump_help(void)
source
void cmdline_debug_hook(void)
source
struct cmdline_entry * cmdline_lookup(const char *key)
source
uint64_t cmdline_entry_value_u64(const struct cmdline_entry *e)
source
#define CMDLINE_TYPE_OFFSET 6
source
#define CMDLINE_TYPES(...)
source
Expansion
#define CMDLINE_TYPES(...) PP_CALL(CMDLINE_IMPL_TYPE_BIT, __VA_ARGS__)
#define CMDLINE_MAP(str, enum_val)
source
Expansion
#define CMDLINE_MAP(str, enum_val) \
    {.name = (str), .value = (uint64_t) (enum_val)}
#define CMDLINE_MAPPINGS(...)
source
Expansion
#define CMDLINE_MAPPINGS(...) \
    ((const struct cmdline_map[]) {__VA_ARGS__, {NULL, 0}})
#define CMDLINE_FLAG(str, bit_val)
source
Expansion
#define CMDLINE_FLAG(str, bit_val) \
    {.name = (str), .value = (uint64_t) (bit_val)}
#define CMDLINE_FLAGS(...)
source
Expansion
#define CMDLINE_FLAGS(...) \
    ((const struct cmdline_flag[]) {__VA_ARGS__, {NULL, 0}})
#define CMDLINE_CHOICES(...)
source
Expansion
#define CMDLINE_CHOICES(...) ((const char *const[]) {__VA_ARGS__, NULL})
#define CMDLINE_PARSER(fn)
source
Expansion
#define CMDLINE_PARSER(fn) .value.parse = (fn)
#define _CMDLINE_DEFINE(n, storage, ...)
source
Expansion
#define _CMDLINE_DEFINE(n, storage, ...) \
    storage LINKER_SECTION_OBJECT(struct cmdline_entry, cmdline_entries) \
        __cmdline_##n = {.name = #n, \
                         .status = CMDLINE_ENTRY_NOT_FOUND, \
                         .types = 0, \
                         .range = RANGE(1, 0), \
                         .choices = NULL, \
                         .mappings = NULL, \
                         .flags_table = NULL, \
                         .value.mode = CMDLINE_MODE_POLYMORPHIC, \
                         .value.type = CMDLINE_TYPE_NONE, \
                         .flags = CMDLINE_ENTRY_FLAGS_NONE, \
                         __VA_ARGS__}
#define _CMDLINE_DEFINE_VAR(n, storage, var, ...)
source
Expansion
#define _CMDLINE_DEFINE_VAR(n, storage, var, ...) \
    storage LINKER_SECTION_OBJECT(struct cmdline_entry, cmdline_entries) \
        __cmdline_##n = {.name = #n, \
                         .status = CMDLINE_ENTRY_NOT_FOUND, \
                         .types = 0, \
                         .range = RANGE(1, 0), \
                         .choices = NULL, \
                         .mappings = NULL, \
                         .flags_table = NULL, \
                         .value.mode = CMDLINE_MODE_VAR, \
                         .value.write_to = &(var), \
                         .value.c_type = TYPE_TO_ENUM((var)), \
                         .value.parse = NULL, \
                         .flags = CMDLINE_ENTRY_FLAGS_NONE, \
                         __VA_ARGS__}
#define CMDLINE_DEFINE(n, ...)
source
Expansion
#define CMDLINE_DEFINE(n, ...) \
    cc_wno_override_init_start _CMDLINE_DEFINE(n, , __VA_ARGS__); \
    cc_wno_override_init_end
#define CMDLINE_DEFINE_VAR(n, var, ...)
source
Expansion
#define CMDLINE_DEFINE_VAR(n, var, ...) \
    cc_wno_override_init_start _CMDLINE_DEFINE_VAR(n, , var, __VA_ARGS__); \
    cc_wno_override_init_end
#define CMDLINE_DEFINE_STATIC(n, ...)
source
Expansion
#define CMDLINE_DEFINE_STATIC(n, ...) \
    cc_wno_override_init_start _CMDLINE_DEFINE(n, static, __VA_ARGS__); \
    cc_wno_override_init_end
#define CMDLINE_DEFINE_VAR_STATIC(n, var, ...)
source
Expansion
#define CMDLINE_DEFINE_VAR_STATIC(n, var, ...) \
    cc_wno_override_init_start _CMDLINE_DEFINE_VAR(n, static, var, \
                                                   __VA_ARGS__); \
    cc_wno_override_init_end
#define CMDLINE_CHILD_DEFINE(parent_n, n, ...)
source
Expansion
#define CMDLINE_CHILD_DEFINE(parent_n, n, ...) \
    CMDLINE_DEFINE(parent_n##_##n, .name = #n, .parent = CMDLINE(parent_n), \
                   __VA_ARGS__)
#define CMDLINE_CHILD_DEFINE_VAR(parent_n, n, var, ...)
source
Expansion
#define CMDLINE_CHILD_DEFINE_VAR(parent_n, n, var, ...) \
    CMDLINE_DEFINE_VAR(parent_n##_##n, var, .name = #n, \
                       .parent = CMDLINE(parent_n), __VA_ARGS__)
#define CMDLINE_INNER(n, ...)
source
Expansion
#define CMDLINE_INNER(n, ...) (_CMDLINE_CHILD_KIND_POLY, n, 0, ##__VA_ARGS__)
#define CMDLINE_INNER_VAR(n, var, ...)
source
Expansion
#define CMDLINE_INNER_VAR(n, var, ...) \
    (_CMDLINE_CHILD_KIND_VAR, n, var, ##__VA_ARGS__)
#define CMDLINE_CHILDREN_DEFINE(parent_n, ...)
source
Expansion
#define CMDLINE_CHILDREN_DEFINE(parent_n, ...) \
    PP_OVERLOAD(_CMDLINE_CHILDREN_MAP, __VA_ARGS__)(parent_n, __VA_ARGS__)
#define CMDLINE_EXTRACT(val, var)
source
Expansion
#define CMDLINE_EXTRACT(val, var) \
    cc_generic(&(var), bool * : cmdline_extract_bool((val), (bool *) &(var)), \
               uint64_t * : cmdline_extract_u64((val), (uint64_t *) &(var)), \
               int64_t * : cmdline_extract_i64((val), (int64_t *) &(var)), \
               uint32_t * : cmdline_extract_u32((val), (uint32_t *) &(var)), \
               int32_t * : cmdline_extract_i32((val), (int32_t *) &(var)), \
               uint16_t * : cmdline_extract_u16((val), (uint16_t *) &(var)), \
               int16_t * : cmdline_extract_i16((val), (int16_t *) &(var)), \
               uint8_t * : cmdline_extract_u8((val), (uint8_t *) &(var)), \
               int8_t * : cmdline_extract_i8((val), (int8_t *) &(var)), \
               struct cmdline_range * : cmdline_extract_range( \
                   (val), (struct cmdline_range *) &(var)), \
               char ** : cmdline_extract_string((val), (char **) &(var)), \
               struct cpu_mask * : cmdline_extract_cpu_mask( \
                   (val), (struct cpu_mask *) &(var)), \
               struct cmdline_list * : cmdline_extract_list( \
                   (val), (struct cmdline_list *) &(var)), \
               const char ** : cmdline_extract_const_string( \
                   (val), (const char **) &(var)))
#define CMDLINE_EXTRACT_LIST(list, type, out_buf, max_count, parse_fn)
source
Expansion
#define CMDLINE_EXTRACT_LIST(list, type, out_buf, max_count, parse_fn) \
    ({ \
        size_t __cmdl_n = 0; \
        const struct cmdline_list *__cmdl_l = (list); \
        const size_t __cmdl_max = (max_count); \
        for (size_t __cmdl_i = 0; \
             __cmdl_i < __cmdl_l->count && __cmdl_n < __cmdl_max; \
             __cmdl_i++) { \
            type __cmdl_v = (type) {0}; \
            if ((parse_fn) (&__cmdl_l->items[__cmdl_i], &__cmdl_v) == ERR_OK) \
                (out_buf)[__cmdl_n++] = __cmdl_v; \
        } \
        __cmdl_n; \
    })
#define CMDLINE_NODE_1(a)
source
Expansion
#define CMDLINE_NODE_1(a) a
#define CMDLINE_NODE_2(a, b)
source
Expansion
#define CMDLINE_NODE_2(a, b) a##_##b
#define CMDLINE_NODE_3(a, b, c)
source
Expansion
#define CMDLINE_NODE_3(a, b, c) a##_##b##_##c
#define CMDLINE_NODE_4(a, b, c, d)
source
Expansion
#define CMDLINE_NODE_4(a, b, c, d) a##_##b##_##c##_##d
#define CMDLINE_NODE(...)
source
Expansion
#define CMDLINE_NODE(...) PP_CALL(CMDLINE_NODE, __VA_ARGS__)
#define CMDLINE_EXTERN(n)
source
Expansion
#define CMDLINE_EXTERN(n) extern struct cmdline_entry PP_CONCAT(__cmdline_, n)
#define CMDLINE(n)
source
Expansion
#define CMDLINE(n) (&PP_CONCAT(__cmdline_, n))
#define CMDLINE_VALUE(n)
source
#define CMDLINE_CHILD_EXTERN(...)
source
Expansion
#define CMDLINE_CHILD_EXTERN(...) CMDLINE_EXTERN(CMDLINE_NODE(__VA_ARGS__))
#define CMDLINE_CHILD(...)
source
Expansion
#define CMDLINE_CHILD(...) CMDLINE(CMDLINE_NODE(__VA_ARGS__))
#define CMDLINE_CHILD_VALUE(...)
source
Expansion
#define CMDLINE_CHILD_VALUE(...) \
    cmdline_entry_value_u64(CMDLINE_CHILD(__VA_ARGS__))
#define CMDLINE_ENTRY_NAME_LEN_MAX 256
source
#define cmdline_list_for_each(val_, list_)
source
Expansion
#define cmdline_list_for_each(val_, list_) \
    for (size_t __cmdl_idx = 0, __cmdl_count = (list_)->count; \
         __cmdl_idx < __cmdl_count && \
         (((val_) = (list_)->items[__cmdl_idx]), true); \
         __cmdl_idx++)
#define CMDLINE_SCHEMA_PROP_PARSER(fn)
source
Expansion
#define CMDLINE_SCHEMA_PROP_PARSER(fn) .parse = (fn)
#define CMDLINE_SCHEMA_PROP(struct_type, member, ...)
source
Expansion
#define CMDLINE_SCHEMA_PROP(struct_type, member, ...) \
    {.name = #member, \
     .offset = offsetof(struct_type, member), \
     .c_type = TYPE_TO_ENUM(((struct_type *) 0)->member), \
     .types = 0, \
     .parse = NULL, \
     .range = RANGE(1, 0), \
     .choices = NULL, \
     .mappings = NULL, \
     .flags_table = NULL, \
     __VA_ARGS__}
#define CMDLINE_SCHEMA_DEFINE(n, prefix_str, path_hint_str, desc_str, \ resolver_fn, ...)
source
Expansion
#define CMDLINE_SCHEMA_DEFINE(n, prefix_str, path_hint_str, desc_str, \
                              resolver_fn, ...) \
    cc_wno_override_init_start static const struct cmdline_schema_prop \
        __cmdline_schema_props_##n[] = {__VA_ARGS__}; \
    LINKER_SECTION_OBJECT(struct cmdline_schema, cmdline_schemas) \
    __cmdline_schema_##n = { \
        .prefix = (prefix_str), \
        .path_hint = (path_hint_str), \
        .desc = (desc_str), \
        .resolve = (resolver_fn), \
        .props = __cmdline_schema_props_##n, \
        .prop_count = ct_array_size(__cmdline_schema_props_##n), \
    }; \
    cc_wno_override_init_end
#define CMDLINE_GET(key, type, fallback)
source
Expansion
#define CMDLINE_GET(key, type, fallback) \
    ({ \
        type __out = (fallback); \
        struct cmdline_entry *__e = cmdline_lookup(key); \
        if (__e && (__e->status == CMDLINE_ENTRY_FOUND || \
                    __e->status == CMDLINE_ENTRY_DEFAULTED)) { \
            CMDLINE_EXTRACT(&__e->value, __out); \
        } \
        __out; \
    })
#define cmdline_read_or(key, target_var, fallback)
source
Expansion
#define cmdline_read_or(key, target_var, fallback) \
    ({ \
        bool __cmdl_overridden = false; \
        struct cmdline_entry *__cmdl_e = cmdline_lookup(key); \
        if (__cmdl_e && __cmdl_e->status == CMDLINE_ENTRY_FOUND && \
            CMDLINE_EXTRACT(&__cmdl_e->value, target_var) == ERR_OK) { \
            __cmdl_overridden = true; \
        } else { \
            (target_var) = (fallback); \
        } \
        __cmdl_overridden; \
    })