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.c
Go to the documentation of this file.
1
13
15#include <options/registry/core.h>
17#include <string.h>
18
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}
66
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}
103
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}
121
126const registry_entry_t *registry_find_entry_by_name(const char *long_name) {
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}
139
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}
156
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}
181
182bool registry_entry_applies_to_mode(const registry_entry_t *entry, asciichat_mode_t mode, bool for_binary_help) {
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}
#define SET_ERRNO(code,...)
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
option_descriptor_t registry_entry_to_descriptor(const registry_entry_t *entry)
Definition core.c:157
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
const registry_entry_t * registry_find_entry_by_name(const char *long_name)
Get a registry entry by long name.
Definition core.c:126
bool registry_entry_applies_to_mode(const registry_entry_t *entry, asciichat_mode_t mode, bool for_binary_help)
Definition core.c:182
const registry_entry_t * registry_find_entry_by_short(char short_name)
Get a registry entry by short name.
Definition core.c:144
void registry_init_size(void)
Initialize registry size and metadata.
Definition core.c:107
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
asciichat_mode_t
Mode type for options parsing.
Definition options.h:916
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_NONE
No modes (invalid).
Definition options.h:934
@ OPTION_MODE_DISCOVERY_SVC
Discovery server mode (bit 3).
Definition options.h:938
Shared structures and macros for registry implementation.
registry_entry_t g_options_registry[2048]
Definition registry.c:41
bool g_metadata_populated
Definition registry.c:44
#define OPTIONS_REGISTRY_CAPACITY
category_builder_t g_category_builders[]
Definition registry.c:26
size_t g_registry_size
Definition registry.c:43
Central registry of all command-line options with mode applicability.
bool initialized
Definition mmap.c:41
🔤 String Manipulation and Shell Escaping Utilities
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
Registry entry - stores option definition with mode bitmask and metadata.
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
option_mode_bitmask_t mode_bitmask
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