ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
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 lib/options/registry/core.h.

Macro Definition Documentation

◆ OPTIONS_REGISTRY_CAPACITY

#define OPTIONS_REGISTRY_CAPACITY   2048

Definition at line 16 of file lib/options/registry/core.h.

Referenced by registry_init_from_builders().

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 182 of file core.c.

182 {
183 if (!entry)
184 return false;
185 if (for_binary_help)
186 return (entry->mode_bitmask & OPTION_MODE_ALL) != 0;
187 if (mode < 0 || mode >= MODE_INVALID)
188 return false;
189 const option_mode_bitmask_t mode_bit = (option_mode_bitmask_t)(1U << mode);
190 return (entry->mode_bitmask & mode_bit) != 0;
191}
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

◆ registry_entry_to_descriptor()

option_descriptor_t registry_entry_to_descriptor ( const registry_entry_t * entry)

Definition at line 157 of file core.c.

157 {
158 option_descriptor_t descriptor = {0};
159 if (!entry)
160 return descriptor;
161 descriptor.long_name = entry->long_name;
162 descriptor.short_name = entry->short_name;
163 descriptor.type = entry->type;
164 descriptor.offset = entry->offset;
165 descriptor.default_value = entry->default_value;
166 descriptor.help_text = entry->help_text;
167 descriptor.group = entry->group;
168 descriptor.arg_placeholder = entry->arg_placeholder;
169 descriptor.required = entry->required;
170 descriptor.env_var_name = entry->env_var_name;
171 descriptor.validate = entry->validate_fn;
172 descriptor.parse_fn = entry->parse_fn;
173 descriptor.owns_memory = entry->owns_memory;
174 descriptor.optional_arg = entry->optional_arg;
175 descriptor.mode_bitmask = entry->mode_bitmask;
176 descriptor.metadata = entry->metadata;
177 descriptor.mode_default_getter = entry->mode_default_getter;
179 return descriptor;
180}
@ 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:251
option_mode_bitmask_t mode_bitmask
Which modes this option applies to.
Definition builder.h:288
const char * help_text
Description for –help.
Definition builder.h:261
size_t offset
offsetof(struct, field) - where to store value
Definition builder.h:258
bool hide_from_binary_help
If true, don't show in binary-level help (e.g., in release builds).
Definition builder.h:265
const char * env_var_name
Environment variable fallback (or NULL).
Definition builder.h:270
const char * arg_placeholder
Custom argument placeholder (e.g., "SHELL [FILE]" instead of "STR").
Definition builder.h:263
const char * group
Group name for help sections (e.g., "NETWORK OPTIONS").
Definition builder.h:262
const void * default_value
Pointer to default value (or NULL if required).
Definition builder.h:268
bool required
If true, option must be provided.
Definition builder.h:269
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
Definition builder.h:276
bool owns_memory
If true, strings are strdup'd and freed on cleanup.
Definition builder.h:282
bool(* validate)(const void *options_struct)
Definition builder.h:273
const char * long_name
Long option name (e.g., "port").
Definition builder.h:253
const void *(* mode_default_getter)(asciichat_mode_t mode)
Get mode-specific default value (NULL if not mode-aware).
Definition builder.h:294
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:254
option_metadata_t metadata
Metadata for shell completions (enums, ranges, examples, etc.).
Definition builder.h:291
option_type_t type
Value type.
Definition builder.h:257
bool optional_arg
If true, argument is optional (for callbacks like –verbose).
Definition builder.h:285
option_type_t type
const char * group
bool optional_arg
bool required
bool owns_memory
bool(* validate_fn)(const void *options_struct)
mode_default_getter_fn mode_default_getter
Mode-aware default getter (NULL if using default_value) - LAST for backward compat.
const char * help_text
const char * arg_placeholder
Custom argument placeholder (e.g., "SHELL [FILE]" instead of "STR").
const void * default_value
Default value (single value for all modes, or NULL if mode_default_getter is set).
const char * env_var_name
option_metadata_t metadata
Enum values, numeric ranges, examples.
char short_name
const char * long_name
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
size_t offset

◆ 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 126 of file core.c.

126 {
127 if (!long_name) {
128 SET_ERRNO(ERROR_INVALID_PARAM, "Long name is NULL");
129 return NULL;
130 }
131
132 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
133 if (strcmp(g_options_registry[i].long_name, long_name) == 0) {
134 return &g_options_registry[i];
135 }
136 }
137 return NULL;
138}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
registry_entry_t g_options_registry[2048]
Definition registry.c:41

◆ 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 144 of file core.c.

144 {
145 if (short_name == '\0') {
146 return NULL;
147 }
148
149 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
150 if (g_options_registry[i].short_name == short_name) {
151 return &g_options_registry[i];
152 }
153 }
154 return NULL;
155}

◆ 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; e->long_name != NULL; e++) {
34 count++;
35 }
36
37 // Copy category entries (count already excludes sentinel)
38 if (offset + count <= OPTIONS_REGISTRY_CAPACITY) {
39 memcpy(&g_options_registry[offset], g_category_builders[i].entries, count * sizeof(registry_entry_t));
40 offset += count;
41 }
42 }
43
44 // Add final null terminator
45 if (offset < OPTIONS_REGISTRY_CAPACITY) {
46 g_options_registry[offset] = (registry_entry_t){.long_name = NULL,
47 .short_name = '\0',
48 .type = OPTION_TYPE_BOOL,
49 .offset = 0,
50 .default_value = NULL,
51 .default_value_size = 0,
52 .help_text = NULL,
53 .group = NULL,
54 .required = false,
55 .env_var_name = NULL,
56 .validate_fn = NULL,
57 .parse_fn = NULL,
58 .owns_memory = false,
59 .optional_arg = false,
60 .mode_bitmask = OPTION_MODE_NONE,
61 .metadata = {0}};
62 }
63
64 initialized = true;
65}
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
@ 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:41
Registry entry - stores option definition with mode bitmask and metadata.

◆ registry_init_size()

void registry_init_size ( void )

Initialize registry size and metadata.

Definition at line 107 of file core.c.

107 {
109
110 if (g_registry_size == 0) {
111 for (size_t i = 0; g_options_registry[i].long_name != NULL; i++) {
113 }
114 // Validate that all options have unique short and long names
116 // DEPRECATED: Metadata is now initialized compile-time in registry entries
117 // registry_populate_metadata_for_critical_options();
119 }
120}
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:71
bool g_metadata_populated
Definition registry.c:44
size_t g_registry_size
Definition registry.c:43

◆ 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 71 of file core.c.

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

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[]

Referenced by registry_init_from_builders().

◆ g_metadata_populated

bool g_metadata_populated
extern

Definition at line 44 of file registry.c.

Referenced by registry_init_size().

◆ g_options_registry

◆ g_registry_size