ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
options.c File Reference

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

Go to the source code of this file.

Data Structures

struct  binary_level_opts_t
 Create a new options_t struct with all defaults set. More...
 

Functions

bool has_action_flag (void)
 Check if an action flag was detected.
 
options_t options_t_new (void)
 Initialize options by parsing command-line arguments.
 
options_t options_t_new_preserve_binary (const options_t *source)
 Create new options struct, preserving binary-level fields from source.
 
asciichat_error_t options_init (int argc, char **argv)
 Initialize options by parsing command-line arguments with unified mode detection.
 

Detailed Description

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

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

Architecture:

The parsing flow is:

options_init(argc, argv)
├─ Mode Detection
│ └─ Checks for --help, --version, mode keywords, session strings
├─ Binary-Level Option Parsing
│ └─ Handles: --help, --version, --log-file, --verbose, --quiet, etc.
├─ Get Mode-Specific Preset (unified)
│ └─ Uses options_preset_unified() from presets.h
├─ Mode-Specific Option Parsing
│ └─ Builder parses remaining args for the detected mode
├─ Configuration File Loading (if --config specified)
│ └─ Loads and merges TOML config file
├─ Validation
│ └─ Applies defaults, validates ranges, cross-field checks
└─ RCU Publishing
└─ Makes options available via options_get() and GET_OPTION()
#define GET_OPTION(field)
Safely get a specific option field (lock-free read)
const options_t * options_get(void)
Get current options (lock-free read)
Definition rcu.c:496
asciichat_error_t options_init(int argc, char **argv)
Initialize options by parsing command-line arguments with unified mode detection.
action_args_t args
options_config_t * options_preset_unified(const char *program_name, const char *description)
Build unified options config with ALL options (binary + all modes)
Definition presets.c:51

Key Components:

Option Lifecycle in options_init():

  1. Create default options via options_t_new()
  2. Detect mode from argv
  3. Parse binary-level options (may exit for –help, –version)
  4. Get mode-specific preset from registry
  5. Parse mode-specific options into options struct
  6. Load config file if specified
  7. Validate all options with defaults
  8. Initialize RCU state
  9. Publish options for lock-free access

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

Definition in file lib/options/options.c.

Function Documentation

◆ options_t_new_preserve_binary()

options_t options_t_new_preserve_binary ( const options_t *  source)

Create new options struct, preserving binary-level fields from source.

This version of options_t_new() extracts binary-level options from a source struct before performing a full reset, then restores them after. This allows binary-level options (–quiet, –verbose, –log-level, etc.) to persist through multiple reset cycles without manual save/restore code.

Parameters
sourceThe options struct to extract binary-level fields from (NULL-safe)
Returns
New options_t with defaults and binary-level fields preserved

Definition at line 714 of file lib/options/options.c.

714 {
715 // Extract binary-level options before reset
716 binary_level_opts_t binary = extract_binary_level(source);
717
718 // Create fresh options with all defaults
719 options_t opts = options_t_new();
720
721 // Restore binary-level options (overwrite defaults)
722 restore_binary_level(&opts, &binary);
723
724 return opts;
725}
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Create a new options_t struct with all defaults set.
Consolidated options structure.

References options_t_new().

Referenced by options_init().