Command Line
include/cmdline.h View source View on GitHubStructs
Section titled “Structs”cmdline_range
Section titled “cmdline_range”cmdline_list
Section titled “cmdline_list” struct cmdline_list (2 fields) source
§
size_t count §
struct cmdline_value *items cmdline_value
Section titled “cmdline_value” struct cmdline_value (4 fields) source
cmdline_entry
Section titled “cmdline_entry” 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 cmdline_value value Regarding the variable to write to
§
const char *const *choices §
const struct cmdline_map *mappings §
const struct cmdline_flag *flags_table cmdline_map
Section titled “cmdline_map”cmdline_flag
Section titled “cmdline_flag”Schema-Driven Subsystem Definitions
cmdline_schema_prop
Section titled “cmdline_schema_prop” struct cmdline_schema_prop (10 fields) source
cmdline_schema
Section titled “cmdline_schema” struct cmdline_schema (6 fields) source
§
const char *prefix §
const char *path_hint §
const char *desc §
cmdline_instance_resolver_t resolve §
const struct cmdline_schema_prop *props §
size_t prop_count cmdline_entry_status
Section titled “cmdline_entry_status” enum cmdline_entry_status (3 variants) source
cmdline_entry_flags
Section titled “cmdline_entry_flags” enum cmdline_entry_flags (4 variants) source
cmdline_value_mode
Section titled “cmdline_value_mode” 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 cmdline_type
Section titled “cmdline_type” 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 Type Aliases
Section titled “Type Aliases”cmdline_instance_resolver_t
Section titled “cmdline_instance_resolver_t”typedef void * (*cmdline_instance_resolver_t)(
const char * path,
size_t path_len
) source Functions
Section titled “Functions”cmdline_parse
Section titled “cmdline_parse”void cmdline_parse(const char *input) source cmdline_wants_help
Section titled “cmdline_wants_help”bool cmdline_wants_help(const char *input) source cmdline_dump_help
Section titled “cmdline_dump_help”cc_noreturn void cmdline_dump_help(void) source cmdline_debug_hook
Section titled “cmdline_debug_hook”void cmdline_debug_hook(void) source cmdline_lookup
Section titled “cmdline_lookup”struct cmdline_entry * cmdline_lookup(const char *key) source cmdline_entry_value_u64
Section titled “cmdline_entry_value_u64”uint64_t cmdline_entry_value_u64(const struct cmdline_entry *e) source Macros
Section titled “Macros”CMDLINE_TYPE_OFFSET
Section titled “CMDLINE_TYPE_OFFSET”#define CMDLINE_TYPE_OFFSET 6 source CMDLINE_TYPES
Section titled “CMDLINE_TYPES”#define CMDLINE_TYPES(...) source Expansion
#define CMDLINE_TYPES(...) PP_CALL(CMDLINE_IMPL_TYPE_BIT, __VA_ARGS__) CMDLINE_MAP
Section titled “CMDLINE_MAP”#define CMDLINE_MAP(str, enum_val) source Expansion
#define CMDLINE_MAP(str, enum_val) \
{.name = (str), .value = (uint64_t) (enum_val)} CMDLINE_MAPPINGS
Section titled “CMDLINE_MAPPINGS”#define CMDLINE_MAPPINGS(...) source Expansion
#define CMDLINE_MAPPINGS(...) \
((const struct cmdline_map[]) {__VA_ARGS__, {NULL, 0}}) CMDLINE_FLAG
Section titled “CMDLINE_FLAG”#define CMDLINE_FLAG(str, bit_val) source Expansion
#define CMDLINE_FLAG(str, bit_val) \
{.name = (str), .value = (uint64_t) (bit_val)} CMDLINE_FLAGS
Section titled “CMDLINE_FLAGS”#define CMDLINE_FLAGS(...) source Expansion
#define CMDLINE_FLAGS(...) \
((const struct cmdline_flag[]) {__VA_ARGS__, {NULL, 0}}) CMDLINE_CHOICES
Section titled “CMDLINE_CHOICES”#define CMDLINE_CHOICES(...) source Expansion
#define CMDLINE_CHOICES(...) ((const char *const[]) {__VA_ARGS__, NULL}) CMDLINE_PARSER
Section titled “CMDLINE_PARSER”#define CMDLINE_PARSER(fn) source Expansion
#define CMDLINE_PARSER(fn) .value.parse = (fn) _CMDLINE_DEFINE
Section titled “_CMDLINE_DEFINE”#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__} _CMDLINE_DEFINE_VAR
Section titled “_CMDLINE_DEFINE_VAR”#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__} CMDLINE_DEFINE
Section titled “CMDLINE_DEFINE”#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 CMDLINE_DEFINE_VAR
Section titled “CMDLINE_DEFINE_VAR”#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 CMDLINE_DEFINE_STATIC
Section titled “CMDLINE_DEFINE_STATIC”#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 CMDLINE_DEFINE_VAR_STATIC
Section titled “CMDLINE_DEFINE_VAR_STATIC”#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 CMDLINE_CHILD_DEFINE
Section titled “CMDLINE_CHILD_DEFINE”#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__) CMDLINE_CHILD_DEFINE_VAR
Section titled “CMDLINE_CHILD_DEFINE_VAR”#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__) CMDLINE_INNER
Section titled “CMDLINE_INNER”#define CMDLINE_INNER(n, ...) source Expansion
#define CMDLINE_INNER(n, ...) (_CMDLINE_CHILD_KIND_POLY, n, 0, ##__VA_ARGS__) CMDLINE_INNER_VAR
Section titled “CMDLINE_INNER_VAR”#define CMDLINE_INNER_VAR(n, var, ...) source Expansion
#define CMDLINE_INNER_VAR(n, var, ...) \
(_CMDLINE_CHILD_KIND_VAR, n, var, ##__VA_ARGS__) CMDLINE_CHILDREN_DEFINE
Section titled “CMDLINE_CHILDREN_DEFINE”#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__) CMDLINE_EXTRACT
Section titled “CMDLINE_EXTRACT”#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))) CMDLINE_EXTRACT_LIST
Section titled “CMDLINE_EXTRACT_LIST”#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; \
}) CMDLINE_NODE_1
Section titled “CMDLINE_NODE_1”#define CMDLINE_NODE_1(a) source Expansion
#define CMDLINE_NODE_1(a) a CMDLINE_NODE_2
Section titled “CMDLINE_NODE_2”#define CMDLINE_NODE_2(a, b) source Expansion
#define CMDLINE_NODE_2(a, b) a##_##b CMDLINE_NODE_3
Section titled “CMDLINE_NODE_3”#define CMDLINE_NODE_3(a, b, c) source Expansion
#define CMDLINE_NODE_3(a, b, c) a##_##b##_##c CMDLINE_NODE_4
Section titled “CMDLINE_NODE_4”#define CMDLINE_NODE_4(a, b, c, d) source Expansion
#define CMDLINE_NODE_4(a, b, c, d) a##_##b##_##c##_##d CMDLINE_NODE
Section titled “CMDLINE_NODE”#define CMDLINE_NODE(...) source Expansion
#define CMDLINE_NODE(...) PP_CALL(CMDLINE_NODE, __VA_ARGS__) CMDLINE_EXTERN
Section titled “CMDLINE_EXTERN”#define CMDLINE_EXTERN(n) source Expansion
#define CMDLINE_EXTERN(n) extern struct cmdline_entry PP_CONCAT(__cmdline_, n) CMDLINE
Section titled “CMDLINE”CMDLINE_VALUE
Section titled “CMDLINE_VALUE”#define CMDLINE_VALUE(n) source Expansion
#define CMDLINE_VALUE(n) cmdline_entry_value_u64(CMDLINE(n)) CMDLINE_CHILD_EXTERN
Section titled “CMDLINE_CHILD_EXTERN”#define CMDLINE_CHILD_EXTERN(...) source Expansion
#define CMDLINE_CHILD_EXTERN(...) CMDLINE_EXTERN(CMDLINE_NODE(__VA_ARGS__)) CMDLINE_CHILD
Section titled “CMDLINE_CHILD”#define CMDLINE_CHILD(...) source Expansion
#define CMDLINE_CHILD(...) CMDLINE(CMDLINE_NODE(__VA_ARGS__)) CMDLINE_CHILD_VALUE
Section titled “CMDLINE_CHILD_VALUE”#define CMDLINE_CHILD_VALUE(...) source Expansion
#define CMDLINE_CHILD_VALUE(...) \
cmdline_entry_value_u64(CMDLINE_CHILD(__VA_ARGS__)) CMDLINE_ENTRY_NAME_LEN_MAX
Section titled “CMDLINE_ENTRY_NAME_LEN_MAX”#define CMDLINE_ENTRY_NAME_LEN_MAX 256 source cmdline_list_for_each
Section titled “cmdline_list_for_each”#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++) CMDLINE_SCHEMA_PROP_PARSER
Section titled “CMDLINE_SCHEMA_PROP_PARSER”#define CMDLINE_SCHEMA_PROP_PARSER(fn) source Expansion
#define CMDLINE_SCHEMA_PROP_PARSER(fn) .parse = (fn) CMDLINE_SCHEMA_PROP
Section titled “CMDLINE_SCHEMA_PROP”#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__} CMDLINE_SCHEMA_DEFINE
Section titled “CMDLINE_SCHEMA_DEFINE”#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 CMDLINE_GET
Section titled “CMDLINE_GET”#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; \
}) cmdline_read_or
Section titled “cmdline_read_or”#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; \
})