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

Schema metadata for config file options. More...

Go to the source code of this file.

Data Structures

 Option metadata for config file parsing. More...

Typedefs

typedef bool(* builder_validate_fn_t) (const void *options_struct)
 Validation function from options builder.

Enumerations

enum  option_context_t { OPTION_CONTEXT_CLI , OPTION_CONTEXT_CONFIG , OPTION_CONTEXT_BOTH }
 Context where an option can appear. More...

Functions

const config_option_metadata_t * config_schema_get_by_toml_key (const char *toml_key)
 Get option metadata by TOML key.
const config_option_metadata_t ** config_schema_get_by_category (const char *category, size_t *count)
 Get all option metadata for a category.
const config_option_metadata_t * config_schema_get_all (size_t *count)
 Get all option metadata.
asciichat_error_t config_schema_build_from_configs (const options_config_t **configs, size_t num_configs)
 Build schema dynamically from options builder configs.
void config_schema_destroy (void)
 Clean up dynamically allocated schema resources.

Detailed Description

Schema metadata for config file options.

This module provides declarative metadata for all configurable options that can appear in TOML configuration files. The schema drives the generic config parser, eliminating duplicate validation code.

Note
This is separate from builder.h's option_descriptor_t which is for CLI parsing. This schema is specifically for TOML config files.

Definition in file schema.h.

Typedef Documentation

◆ builder_validate_fn_t

typedef bool(* builder_validate_fn_t) (const void *options_struct)

Validation function from options builder.

This is the same validation function signature used by the options builder. It receives the full options struct and can perform cross-field validation.

Parameters
options_structFull options struct (cast to void* for generic use)
Returns
true if valid, false after setting error context with SET_ERRNO() if invalid

Definition at line 45 of file schema.h.

Enumeration Type Documentation

◆ option_context_t

Context where an option can appear.

Enumerator
OPTION_CONTEXT_CLI 

CLI-only option (cannot appear in config).

OPTION_CONTEXT_CONFIG 

Config-only option (never on CLI).

OPTION_CONTEXT_BOTH 

Can appear in both CLI and config.

Definition at line 29 of file schema.h.

29 {
option_context_t
Context where an option can appear.
Definition schema.h:29
@ OPTION_CONTEXT_BOTH
Can appear in both CLI and config.
Definition schema.h:32
@ OPTION_CONTEXT_CONFIG
Config-only option (never on CLI).
Definition schema.h:31
@ OPTION_CONTEXT_CLI
CLI-only option (cannot appear in config).
Definition schema.h:30

Function Documentation

◆ config_schema_build_from_configs()

asciichat_error_t config_schema_build_from_configs ( const options_config_t ** configs,
size_t num_configs )

Build schema dynamically from options builder configs.

This function builds the config schema by merging all mode configs (server, client, mirror, etc.). It generates TOML keys, CLI flags, categories, and types from the builder's option descriptors.

Parameters
configsArray of option configs (can contain NULL entries)
num_configsNumber of configs in the array
Returns
ASCIICHAT_OK on success, error code on failure
Note
This should be called once during initialization before any config parsing

Definition at line 250 of file schema.c.

250 {
251 // Free existing dynamic schema if any
252 if (g_dynamic_schema) {
253 SAFE_FREE(g_dynamic_schema);
254 g_dynamic_schema = NULL;
255 g_dynamic_schema_count = 0;
256 }
257
258 // Free existing string storage
259 if (g_dynamic_strings) {
260 for (size_t i = 0; i < g_dynamic_strings_count; i++) {
261 SAFE_FREE(g_dynamic_strings[i]);
262 }
263 SAFE_FREE(g_dynamic_strings);
264 g_dynamic_strings = NULL;
265 g_dynamic_strings_count = 0;
266 g_dynamic_strings_capacity = 0;
267 }
268
269 // Count total unique descriptors (by offset) across all configs
270 // Use a simple approach: collect all descriptors, then deduplicate by offset
271 const option_descriptor_t *all_descriptors[SCHEMA_OPTION_CAPACITY] = {0}; // Max expected options
272 size_t descriptor_count = 0;
273
274 // Collect descriptors from all configs
275 for (size_t cfg_idx = 0; cfg_idx < num_configs; cfg_idx++) {
276 const options_config_t *config = configs[cfg_idx];
277 if (!config) {
278 log_debug("Config %zu is NULL, skipping", cfg_idx);
279 continue;
280 }
281
282 if (!config->descriptors || config->num_descriptors == 0) {
283 log_debug("Config %zu has no descriptors", cfg_idx);
284 continue;
285 }
286
287 for (size_t i = 0; i < config->num_descriptors && descriptor_count < SCHEMA_OPTION_CAPACITY; i++) {
288 const option_descriptor_t *desc = &config->descriptors[i];
289 if (!desc) {
290 log_warn("Descriptor at index %zu in config %zu is NULL", i, cfg_idx);
291 continue;
292 }
293 if (should_add_descriptor(desc, all_descriptors, descriptor_count)) {
294 all_descriptors[descriptor_count++] = desc;
295 }
296 }
297 }
298
299 // Allocate schema array
300 g_dynamic_schema = SAFE_MALLOC(descriptor_count * sizeof(config_option_metadata_t), config_option_metadata_t *);
301 if (!g_dynamic_schema) {
302 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate dynamic schema");
303 }
304
305 // Build schema entries
306 g_dynamic_schema_count = 0;
307 char toml_key_buffer[BUFFER_SIZE_SMALL];
308 char cli_flag_buffer[OPTIONS_CONFIG_SCHEMA_CLI_FLAG_BUFFER_SIZE];
309 char category_buffer[OPTIONS_CONFIG_SCHEMA_CATEGORY_BUFFER_SIZE];
310
311 for (size_t i = 0; i < descriptor_count; i++) {
312 const option_descriptor_t *desc = all_descriptors[i];
313 if (!desc || !desc->long_name || !desc->group) {
314 continue;
315 }
316
317 config_option_metadata_t *meta = &g_dynamic_schema[g_dynamic_schema_count++];
318 // Zero-initialize the metadata struct to ensure all fields are initialized
319 memset(meta, 0, sizeof(*meta));
320
321 // Use builder's type directly
322 meta->type = desc->type;
323
324 // Generate category: lowercase the group name from builder
325 SAFE_STRNCPY(category_buffer, desc->group, sizeof(category_buffer) - 1);
326 category_buffer[sizeof(category_buffer) - 1] = '\0';
327 str_tolower(category_buffer);
328
329 // Generate TOML key: "category.field_name" (category is lowercase group, field_name is long_name with
330 // dashes->underscores)
331 const char *field_name = desc->long_name;
332 if (strcmp(category_buffer, "otel") == 0 && strncmp(field_name, "otel-", 5) == 0)
333 field_name += 5;
334 if (!generate_toml_key(category_buffer, field_name, toml_key_buffer, sizeof(toml_key_buffer))) {
335 g_dynamic_schema_count--; // Skip this entry
336 continue;
337 }
338
339 // Store generated strings (allocate and store)
340 meta->toml_key = store_dynamic_string(toml_key_buffer);
341 meta->category = store_dynamic_string(category_buffer);
342 if (!meta->toml_key || !meta->category) {
343 // Failed to allocate strings, skip this entry
344 g_dynamic_schema_count--;
345 continue;
346 }
347
348 // Generate CLI flag: "--long-name"
349 if (generate_cli_flag(desc->long_name, cli_flag_buffer, sizeof(cli_flag_buffer))) {
350 meta->cli_flag = store_dynamic_string(cli_flag_buffer);
351 if (!meta->cli_flag) {
352 // Failed to allocate, but continue (cli_flag can be NULL)
353 }
354 } else {
355 meta->cli_flag = NULL;
356 }
357
358 // Set other fields
359 meta->context = OPTION_CONTEXT_BOTH; // Most options can appear in both CLI and config
360 meta->field_offset = desc->offset;
361 meta->field_size = get_field_size(meta->type, desc->offset);
362 // Use builder's validate function directly - it receives the full options struct
363 meta->validate_fn = desc->validate;
364 // Use builder's parse function for CALLBACK types
365 meta->parse_fn = desc->parse_fn;
366 meta->description = desc->help_text;
367
368 // Set mode_bitmask from option descriptor
369 // The descriptor should have mode_bitmask set from the registry
370 meta->mode_bitmask = desc->mode_bitmask;
371
372 // Copy mode_default_getter function pointer
374
375 // Copy constraints from descriptor's metadata
376 // For integer types, copy numeric_range to int_range (always copy, even if min is 0)
377 memset(&meta->constraints, 0, sizeof(meta->constraints));
378 // Always copy numeric_range if descriptor has it (check max or min)
379 if (desc && desc->metadata.numeric_range.max > 0) {
382 }
383 }
384
385 g_schema_built = true;
386 return ASCIICHAT_OK;
387}
#define SET_ERRNO(code,...)
#define BUFFER_SIZE_SMALL
Small buffer size (256 bytes).
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define OPTIONS_CONFIG_SCHEMA_CLI_FLAG_BUFFER_SIZE
Definition schema.c:8
#define OPTIONS_CONFIG_SCHEMA_CATEGORY_BUFFER_SIZE
Definition schema.c:9
#define SCHEMA_OPTION_CAPACITY
Definition schema.c:12
Option metadata for config file parsing.
Definition schema.h:53
option_context_t context
Where this option can appear.
Definition schema.h:59
option_type_t type
Value type (from builder).
Definition schema.h:58
size_t field_size
Size of field in options_t.
Definition schema.h:62
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
Custom parser for callbacks (or NULL).
Definition schema.h:69
struct config_option_metadata_t::@047245164140222033316365214353336112047172131121::@271224077242344302200333333005050320172070077212 int_range
option_mode_bitmask_t mode_bitmask
Which modes this option applies to.
Definition schema.h:73
const void *(* mode_default_getter)(asciichat_mode_t mode)
Get mode-specific default value (NULL if not mode-aware).
Definition schema.h:91
size_t field_offset
offsetof(options_t, field) - where to store value
Definition schema.h:61
const char * description
Description for docs/help generation.
Definition schema.h:76
const char * category
Category name (e.g., "network", "client", "audio").
Definition schema.h:60
builder_validate_fn_t validate_fn
Definition schema.h:65
const char * toml_key
TOML key path (e.g., "network.port", "client.address").
Definition schema.h:55
const char * cli_flag
CLI flag name (e.g., "--port") or NULL if no CLI flag.
Definition schema.h:57
union config_option_metadata_t::@047245164140222033316365214353336112047172131121 constraints
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
const char * group
Group name for help sections (e.g., "NETWORK OPTIONS").
Definition builder.h:282
bool(* parse_fn)(const char *arg, void *dest, char **error_msg)
Definition builder.h:300
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
option_metadata_t metadata
Definition builder.h:316
option_type_t type
Value type.
Definition builder.h:277
int max
Maximum value (or 0 if no limit).
Definition builder.h:227
int min
Minimum value (or 0 if no limit).
Definition builder.h:226
struct option_metadata_t::@061372117173071240020206164036331043262170062306 numeric_range
Immutable parsing tables produced from an options builder.
Definition builder.h:456
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:457
size_t num_descriptors
Number of descriptors.
Definition builder.h:458

References ASCIICHAT_OK, BUFFER_SIZE_SMALL, config_option_metadata_t::category, config_option_metadata_t::cli_flag, config_option_metadata_t::constraints, config_option_metadata_t::context, config_option_metadata_t::description, options_config_t::descriptors, ERROR_MEMORY, config_option_metadata_t::field_offset, config_option_metadata_t::field_size, option_descriptor_t::group, option_descriptor_t::help_text, config_option_metadata_t::int_range, log_debug, log_warn, option_descriptor_t::long_name, config_option_metadata_t::max, option_metadata_t::max, option_descriptor_t::metadata, config_option_metadata_t::min, option_metadata_t::min, config_option_metadata_t::mode_bitmask, option_descriptor_t::mode_bitmask, config_option_metadata_t::mode_default_getter, option_descriptor_t::mode_default_getter, options_config_t::num_descriptors, option_metadata_t::numeric_range, option_descriptor_t::offset, OPTION_CONTEXT_BOTH, OPTIONS_CONFIG_SCHEMA_CATEGORY_BUFFER_SIZE, OPTIONS_CONFIG_SCHEMA_CLI_FLAG_BUFFER_SIZE, config_option_metadata_t::parse_fn, option_descriptor_t::parse_fn, SAFE_FREE, SAFE_MALLOC, SAFE_STRNCPY, SCHEMA_OPTION_CAPACITY, SET_ERRNO, config_option_metadata_t::toml_key, config_option_metadata_t::type, option_descriptor_t::type, option_descriptor_t::validate, and config_option_metadata_t::validate_fn.

Referenced by config_create_default().

◆ config_schema_destroy()

void config_schema_destroy ( void )

Clean up dynamically allocated schema resources.

Frees all memory allocated by config_schema_build_from_configs(), including the dynamic schema array and all associated strings. Safe to call multiple times or if schema was never built.

Note
This should be called during shutdown to prevent memory leaks

Definition at line 467 of file schema.c.

467 {
468 if (!g_schema_built) {
469 return; // Nothing to clean up
470 }
471
472 // Free all dynamically allocated strings (toml_key, cli_flag, category)
473 if (g_dynamic_strings) {
474 for (size_t i = 0; i < g_dynamic_strings_count; i++) {
475 SAFE_FREE(g_dynamic_strings[i]);
476 }
477 SAFE_FREE(g_dynamic_strings);
478 g_dynamic_strings = NULL;
479 g_dynamic_strings_count = 0;
480 g_dynamic_strings_capacity = 0;
481 }
482
483 // Free the schema array itself
484 if (g_dynamic_schema) {
485 SAFE_FREE(g_dynamic_schema);
486 g_dynamic_schema = NULL;
487 g_dynamic_schema_count = 0;
488 }
489
490 g_schema_built = false;
491}

References SAFE_FREE.

◆ config_schema_get_all()

const config_option_metadata_t * config_schema_get_all ( size_t * count)

Get all option metadata.

Parameters
countOutput: total number of options
Returns
Array of all metadata (NULL-terminated)

Definition at line 447 of file schema.c.

447 {
448 // Schema must be built before use
449 if (!g_schema_built || !g_dynamic_schema) {
450 if (count) {
451 *count = 0;
452 }
453 SET_ERRNO(ERROR_INVALID_STATE, "Schema not built");
454 return NULL;
455 }
456
457 if (count) {
458 *count = g_dynamic_schema_count;
459 }
460 return g_dynamic_schema;
461}
@ ERROR_INVALID_STATE

References ERROR_INVALID_STATE, and SET_ERRNO.

Referenced by config_create_default().

◆ config_schema_get_by_category()

const config_option_metadata_t ** config_schema_get_by_category ( const char * category,
size_t * count )

Get all option metadata for a category.

Parameters
categoryCategory name (e.g., "network")
countOutput: number of options in category
Returns
Array of metadata pointers (NULL-terminated)

Definition at line 414 of file schema.c.

414 {
415 static const config_option_metadata_t *results[SCHEMA_CATEGORY_OPTION_CAPACITY]; // Max options per category
416 size_t result_count = 0;
417
418 if (!category) {
419 if (count) {
420 *count = 0;
421 }
422 return NULL;
423 }
424
425 // Schema must be built before use
426 if (!g_schema_built || !g_dynamic_schema) {
427 if (count) {
428 *count = 0;
429 }
430 SET_ERRNO(ERROR_INVALID_STATE, "Schema not built");
431 return NULL;
432 }
433
434 for (size_t i = 0; i < g_dynamic_schema_count && result_count < SCHEMA_CATEGORY_OPTION_CAPACITY; i++) {
435 if (g_dynamic_schema[i].category && strcmp(g_dynamic_schema[i].category, category) == 0) {
436 results[result_count++] = &g_dynamic_schema[i];
437 }
438 }
439
440 if (count) {
441 *count = result_count;
442 }
443
444 return (result_count > 0) ? results : NULL;
445}
#define SCHEMA_CATEGORY_OPTION_CAPACITY
Definition schema.c:13

References ERROR_INVALID_STATE, SCHEMA_CATEGORY_OPTION_CAPACITY, and SET_ERRNO.

Referenced by config_create_default().

◆ config_schema_get_by_toml_key()

const config_option_metadata_t * config_schema_get_by_toml_key ( const char * toml_key)

Get option metadata by TOML key.

Parameters
toml_keyTOML key path (e.g., "network.port")
Returns
Pointer to metadata or NULL if not found

Definition at line 393 of file schema.c.

393 {
394 if (!toml_key) {
395 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid arguments");
396 return NULL;
397 }
398
399 // Schema must be built before use
400 if (!g_schema_built || !g_dynamic_schema) {
401 SET_ERRNO(ERROR_INVALID_STATE, "Schema not built");
402 return NULL;
403 }
404
405 for (size_t i = 0; i < g_dynamic_schema_count; i++) {
406 if (g_dynamic_schema[i].toml_key && strcmp(g_dynamic_schema[i].toml_key, toml_key) == 0) {
407 return &g_dynamic_schema[i];
408 }
409 }
410
411 return NULL;
412}
@ ERROR_INVALID_PARAM

References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.