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 File Reference

⚙️ Main entry point for unified options parsing with mode detection More...

Go to the source code of this file.

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)

Detailed Description

⚙️ Main entry point for unified options parsing with mode detection

Internal helper functions for registry implementation.

Purpose: Implements options_init() - the unified entry point that:

  1. Detects mode from command-line arguments (server, client, mirror, acds, etc.)
  2. Parses binary-level options (–help, –version, –log-file, etc.)
  3. Routes to mode-specific parsing using preset configurations
  4. Loads configuration files if –config specified
  5. Validates all options with cross-field checks
  6. Publishes options via RCU for lock-free thread-safe access

Initialization order:

  1. Validate argv and initialize RCU state.
  2. Handle help, scan binary actions, and detect the mode.
  3. Parse binary options and build argv for the selected mode.
  4. Initialize mode defaults, build the schema, and load configuration files.
  5. Parse mode options while preserving binary settings and track explicit flags.
  6. Publish options for cleanup, validate them, and apply mode defaults.
  7. Finalize identity keys, dimensions, and media settings.
  8. Publish final options, apply the logging color scheme, and run deferred actions.

The stage helpers borrow options and parser state. options_init() owns the unified configuration and filtered argv array and releases both on errors. The configuration is released after validation; argv survives deferred actions.

Error Handling:

  • Invalid options → ERROR_USAGE (usage already printed to stderr)
  • File errors → ERROR_IO or ERROR_CONFIG
  • Validation failures → ERROR_VALIDATION with context message
  • Success → ASCIICHAT_OK

Thread Safety:

  • options_init() must be called exactly once before worker threads
  • After options_init() completes, options are read-only
  • Access via lock-free GET_OPTION() macro from worker threads
  • Optional updates via options_set_*() functions
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

This file contains all the static helper functions used internally by the registry implementation. These functions are not part of the public API and are only used by public_api.c.

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

Definition in file core.c.

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

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

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

References ERROR_INVALID_PARAM, g_options_registry, and SET_ERRNO.

Referenced by main(), 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 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}

References g_options_registry.

Referenced by main().

◆ 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.

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

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

Referenced by main(), 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 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

References ASCIICHAT_OK, ERROR_CONFIG, g_options_registry, and SET_ERRNO.

Referenced by registry_init_size().