ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
core.h File Reference

Internal helper functions and data for registry implementation. More...

Go to the source code of this file.

Macros

#define OPTIONS_REGISTRY_CAPACITY   2048

Functions

void registry_init_from_builders (void)
 Initialize registry from category builders Populates g_options_registry by concatenating all category arrays.
asciichat_error_t registry_validate_unique_options (void)
 Validate that no short or long options appear more than once in the registry.
void registry_init_size (void)
 Initialize registry size and metadata.
const registry_entry_t * registry_find_entry_by_name (const char *long_name)
 Get a registry entry by long name.
const registry_entry_t * registry_find_entry_by_short (char short_name)
 Get a registry entry by short name.
option_descriptor_t registry_entry_to_descriptor (const registry_entry_t *entry)
bool registry_entry_applies_to_mode (const registry_entry_t *entry, asciichat_mode_t mode, bool for_binary_help)

Variables

registry_entry_t g_options_registry [2048]
size_t g_registry_size
bool g_metadata_populated
category_builder_t g_category_builders []

Detailed Description

Internal helper functions and data for registry implementation.

This header declares internal helper functions and global registry data that are used by core.c and public_api.c. These are NOT part of the public API.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file include/ascii-chat/options/registry/core.h.

Macro Definition Documentation

◆ OPTIONS_REGISTRY_CAPACITY

#define OPTIONS_REGISTRY_CAPACITY   2048

Definition at line 16 of file include/ascii-chat/options/registry/core.h.

Function Documentation

◆ registry_entry_applies_to_mode()

bool registry_entry_applies_to_mode ( const registry_entry_t * entry,
asciichat_mode_t mode,
bool for_binary_help )

Definition at line 194 of file core.c.

196 {
197 if (!entry)
198 return false;
199 if (for_binary_help)
200 return (entry->mode_bitmask & OPTION_MODE_ALL) != 0;
201 if (mode < 0 || mode >= MODE_INVALID)
202 return false;
203 const option_mode_bitmask_t mode_bit = (option_mode_bitmask_t)(1U << mode);
204 return (entry->mode_bitmask & mode_bit) != 0;
205}
option_mode_bitmask_t
Option mode bitmask.
Definition options.h:933
@ MODE_INVALID
Invalid mode.
Definition options.h:924
@ OPTION_MODE_ALL
All modes + binary.
Definition options.h:943
option_mode_bitmask_t mode_bitmask

References registry_entry_t::mode_bitmask, MODE_INVALID, and OPTION_MODE_ALL.

Referenced by options_registry_get_for_display().

◆ registry_entry_to_descriptor()

option_descriptor_t registry_entry_to_descriptor ( const registry_entry_t * entry)

Definition at line 168 of file core.c.

168 {
169 option_descriptor_t descriptor = {0};
170 if (!entry)
171 return descriptor;
172 descriptor.long_name = entry->long_name;
173 descriptor.short_name = entry->short_name;
174 descriptor.type = entry->type;
175 descriptor.offset = entry->offset;
176 descriptor.default_value = entry->default_value;
177 descriptor.help_text = entry->help_text;
178 descriptor.group = entry->group;
179 descriptor.arg_placeholder = entry->arg_placeholder;
180 descriptor.required = entry->required;
181 descriptor.env_var_name = entry->env_var_name;
182 descriptor.validate = entry->validate_fn;
183 descriptor.parse_fn = entry->parse_fn;
184 descriptor.owns_memory = entry->owns_memory;
185 descriptor.optional_arg = entry->optional_arg;
186 descriptor.mode_bitmask = entry->mode_bitmask;
187 descriptor.metadata = entry->metadata;
188 descriptor.mode_default_getter = entry->mode_default_getter;
189 descriptor.hide_from_binary_help =
191 return descriptor;
192}
@ OPTION_MODE_DISCOVERY_SVC
Discovery server mode (bit 3).
Definition options.h:938
The complete registry entry for one command-line/configuration option.
Definition builder.h:271
option_mode_bitmask_t mode_bitmask
Which modes this option applies to.
Definition builder.h:313
const char * help_text
Description for –help.
Definition builder.h:281
size_t offset
offsetof(struct, field) - where to store value
Definition builder.h:278
bool hide_from_binary_help
Definition builder.h:287
const char * env_var_name
Environment variable fallback (or NULL).
Definition builder.h:293
const char * arg_placeholder
Definition builder.h:283
const char * group
Group name for help sections (e.g., "NETWORK OPTIONS").
Definition builder.h:282
const void * default_value
Pointer to default value (or NULL if required).
Definition builder.h:291
bool required
If true, option must be provided.
Definition builder.h:292
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
Definition builder.h:300
bool owns_memory
If true, strings are strdup'd and freed on cleanup.
Definition builder.h:306
bool(* validate)(const void *options_struct)
Definition builder.h:297
const char * long_name
Long option name (e.g., "port").
Definition builder.h:273
const void *(* mode_default_getter)(asciichat_mode_t mode)
Get mode-specific default value (NULL if not mode-aware).
Definition builder.h:320
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:274
option_metadata_t metadata
Definition builder.h:316
option_type_t type
Value type.
Definition builder.h:277
option_type_t type
bool(* validate_fn)(const void *options_struct)
bool optional_arg
const char * group
bool required
bool owns_memory
const char * long_name
mode_default_getter_fn mode_default_getter
Mode-aware default getter (NULL if using default_value) - LAST for backward compat.
const char * env_var_name
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
const void * default_value
Default value (single value for all modes, or NULL if mode_default_getter is set).
const char * arg_placeholder
Custom argument placeholder (e.g., "SHELL [FILE]" instead of "STR").
option_metadata_t metadata
Enum values, numeric ranges, examples.
char short_name
const char * help_text
size_t offset

References option_descriptor_t::arg_placeholder, registry_entry_t::arg_placeholder, option_descriptor_t::default_value, registry_entry_t::default_value, option_descriptor_t::env_var_name, registry_entry_t::env_var_name, option_descriptor_t::group, registry_entry_t::group, option_descriptor_t::help_text, registry_entry_t::help_text, option_descriptor_t::hide_from_binary_help, option_descriptor_t::long_name, registry_entry_t::long_name, option_descriptor_t::metadata, registry_entry_t::metadata, option_descriptor_t::mode_bitmask, registry_entry_t::mode_bitmask, option_descriptor_t::mode_default_getter, registry_entry_t::mode_default_getter, option_descriptor_t::offset, registry_entry_t::offset, OPTION_MODE_DISCOVERY_SVC, option_descriptor_t::optional_arg, registry_entry_t::optional_arg, option_descriptor_t::owns_memory, registry_entry_t::owns_memory, option_descriptor_t::parse_fn, registry_entry_t::parse_fn, option_descriptor_t::required, registry_entry_t::required, option_descriptor_t::short_name, registry_entry_t::short_name, option_descriptor_t::type, registry_entry_t::type, option_descriptor_t::validate, and registry_entry_t::validate_fn.

Referenced by options_json_parse_value(), options_registry_add_all_to_builder(), options_registry_get_for_display(), and options_registry_get_for_mode().

◆ registry_find_entry_by_name()

const registry_entry_t * registry_find_entry_by_name ( const char * long_name)

Get a registry entry by long name.

Note
This is used internally for option lookup

Definition at line 136 of file core.c.

136 {
137 if (!long_name) {
138 SET_ERRNO(ERROR_INVALID_PARAM, "Long name is NULL");
139 return NULL;
140 }
141
142 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
143 if (strcmp(g_options_registry[i].long_name, long_name) == 0) {
144 return &g_options_registry[i];
145 }
146 }
147 return NULL;
148}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
registry_entry_t g_options_registry[2048]
Definition registry.c:41

References ERROR_INVALID_PARAM, g_options_registry, and SET_ERRNO.

Referenced by options_json_parse_value(), options_json_parse_value(), options_json_schema_for_option(), and options_json_schema_for_option().

◆ registry_find_entry_by_short()

const registry_entry_t * registry_find_entry_by_short ( char short_name)

Get a registry entry by short name.

Note
This is used internally for option lookup

Definition at line 154 of file core.c.

154 {
155 if (short_name == '\0') {
156 return NULL;
157 }
158
159 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
160 if (g_options_registry[i].short_name == short_name) {
161 return &g_options_registry[i];
162 }
163 }
164 return NULL;
165}

References g_options_registry.

◆ registry_init_from_builders()

void registry_init_from_builders ( void )

Initialize registry from category builders Populates g_options_registry by concatenating all category arrays.

Definition at line 23 of file core.c.

23 {
24 static bool initialized = false;
25 if (initialized) {
26 return;
27 }
28
29 size_t offset = 0;
30 for (int i = 0; g_category_builders[i].entries != NULL; i++) {
31 // Count entries in this category (until sentinel terminator)
32 size_t count = 0;
33 for (const registry_entry_t *e = g_category_builders[i].entries;
34 e->long_name != NULL; e++) {
35 count++;
36 }
37
38 // Copy category entries (count already excludes sentinel)
39 if (offset + count <= OPTIONS_REGISTRY_CAPACITY) {
40 memcpy(&g_options_registry[offset], g_category_builders[i].entries,
41 count * sizeof(registry_entry_t));
42 offset += count;
43 }
44 }
45
46 // Add final null terminator
47 if (offset < OPTIONS_REGISTRY_CAPACITY) {
48 g_options_registry[offset] =
49 (registry_entry_t){.long_name = NULL,
50 .short_name = '\0',
51 .type = OPTION_TYPE_BOOL,
52 .offset = 0,
53 .default_value = NULL,
54 .default_value_size = 0,
55 .help_text = NULL,
56 .group = NULL,
57 .required = false,
58 .env_var_name = NULL,
59 .validate_fn = NULL,
60 .parse_fn = NULL,
61 .owns_memory = false,
62 .optional_arg = false,
63 .mode_bitmask = OPTION_MODE_NONE,
64 .metadata = {0}};
65 }
66
67 initialized = true;
68}
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:181
@ OPTION_MODE_NONE
No modes (invalid).
Definition options.h:934
#define OPTIONS_REGISTRY_CAPACITY
category_builder_t g_category_builders[]
Definition registry.c:26
bool initialized
Definition mmap.c:42
Registry entry - stores option definition with mode bitmask and metadata.

References g_category_builders, g_options_registry, initialized, OPTION_MODE_NONE, OPTION_TYPE_BOOL, and OPTIONS_REGISTRY_CAPACITY.

Referenced by registry_init_size().

◆ registry_init_size()

void registry_init_size ( void )

Initialize registry size and metadata.

Definition at line 117 of file core.c.

117 {
119
120 if (g_registry_size == 0) {
121 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
123 }
124 // Validate that all options have unique short and long names
126 // DEPRECATED: Metadata is now initialized compile-time in registry entries
127 // registry_populate_metadata_for_critical_options();
129 }
130}
void registry_init_from_builders(void)
Initialize registry from category builders Populates g_options_registry by concatenating all category...
Definition core.c:23
asciichat_error_t registry_validate_unique_options(void)
Validate that no short or long options appear more than once in the registry.
Definition core.c:75
bool g_metadata_populated
Definition registry.c:44
size_t g_registry_size
Definition registry.c:43

References g_metadata_populated, g_options_registry, g_registry_size, registry_init_from_builders(), and registry_validate_unique_options().

Referenced by mcp_server_main(), options_get_help_text(), options_registry_add_all_to_builder(), options_registry_get_for_display(), options_registry_get_for_mode(), options_registry_get_metadata(), options_state_init(), registry_add_all_to_flags_builder(), registry_get_count(), and registry_get_raw().

◆ registry_validate_unique_options()

asciichat_error_t registry_validate_unique_options ( void )

Validate that no short or long options appear more than once in the registry.

Returns
Result of SET_ERRNO if duplicates found, ASCIICHAT_OK if valid

Definition at line 75 of file core.c.

75 {
76 // Check for duplicate long options
77 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
78 const char *long_name = g_options_registry[i].long_name;
79 if (!long_name || long_name[0] == '\0') {
80 continue; // Skip empty long names
81 }
82
83 for (size_t j = i + 1; g_options_registry[j].long_name != NULL; j++) {
84 if (strcmp(g_options_registry[j].long_name, long_name) == 0) {
85 return SET_ERRNO(
87 "Duplicate long option '--%s' at registry indices %zu and %zu",
88 long_name, i, j);
89 }
90 }
91 }
92
93 // Check for duplicate short options (skip if '\0')
94 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
95 char short_name = g_options_registry[i].short_name;
96 if (short_name == '\0') {
97 continue; // Skip if no short option
98 }
99
100 for (size_t j = i + 1; g_options_registry[j].long_name != NULL; j++) {
101 if (g_options_registry[j].short_name == short_name) {
102 return SET_ERRNO(ERROR_CONFIG,
103 "Duplicate short option '-%c' for '--%s' and '--%s' "
104 "at registry indices %zu and %zu",
105 short_name, g_options_registry[i].long_name,
106 g_options_registry[j].long_name, i, j);
107 }
108 }
109 }
110
111 return ASCIICHAT_OK;
112}
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56

References ASCIICHAT_OK, ERROR_CONFIG, g_options_registry, and SET_ERRNO.

Referenced by registry_init_size().

Variable Documentation

◆ g_category_builders

category_builder_t g_category_builders[]
extern

Definition at line 26 of file registry.c.

26 {{g_general_entries, "GENERAL"},
27 {g_logging_entries, "LOGGING"},
28 {g_terminal_entries, "TERMINAL"},
29 {g_configuration_entries, "CONFIGURATION"},
30 {g_display_entries, "DISPLAY"},
31 {g_webcam_entries, "WEBCAM"},
32 {g_audio_entries, "AUDIO"},
33 {g_media_entries, "MEDIA"},
34 {g_network_entries, "NETWORK"},
35 {g_security_entries, "SECURITY"},
36 {g_database_entries, "DATABASE"},
37 {g_debug_entries, "DEBUG"},
38 {NULL, NULL}};
const registry_entry_t g_audio_entries[]
const registry_entry_t g_configuration_entries[]
const registry_entry_t g_media_entries[]
Definition media.c:95
const registry_entry_t g_debug_entries[]
Definition debug.c:16
const registry_entry_t g_network_entries[]
const registry_entry_t g_logging_entries[]
const registry_entry_t g_general_entries[]
Definition general.c:20
const registry_entry_t g_database_entries[]
const registry_entry_t g_webcam_entries[]
const registry_entry_t g_display_entries[]
const registry_entry_t g_terminal_entries[]
const registry_entry_t g_security_entries[]

◆ g_metadata_populated

bool g_metadata_populated
extern

Definition at line 44 of file registry.c.

◆ g_options_registry

registry_entry_t g_options_registry[2048]
extern

Definition at line 41 of file registry.c.

◆ g_registry_size

size_t g_registry_size
extern

Definition at line 43 of file registry.c.