ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
config.c File Reference

📋 TOML configuration file parser with schema validation and CLI override support More...

Go to the source code of this file.

Macros

#define OPTIONS_CONFIG_QUESTION_BUFFER_SIZE   4096
#define CONFIG_WARN(fmt, ...)
 Print configuration warning using the logging system.
#define CONFIG_DEBUG(fmt, ...)
 Print configuration debug message.

Functions

flags_schema_builder_t * options_config_build_flags_schema (option_mode_bitmask_t mode_mask)
asciichat_error_t config_create_default (const char *config_path)
 Create default configuration file with all default values.
asciichat_error_t config_load_and_apply (asciichat_mode_t detected_mode, const char *config_path, bool strict, options_t *opts)
 Load configuration from TOML file and apply to global options.
asciichat_error_t config_load_system_and_user (asciichat_mode_t detected_mode, bool strict, options_t *opts)
 Load system config first, then user config (user config overrides system).

Detailed Description

📋 TOML configuration file parser with schema validation and CLI override support

Definition in file options/config.c.

Macro Definition Documentation

◆ CONFIG_DEBUG

#define CONFIG_DEBUG ( fmt,
... )
Value:
do { \
/* Debug messages are only shown in debug builds after logging is initialized */ \
/* Use log_dev which safely checks initialization itself */ \
log_dev(fmt, ##__VA_ARGS__); \
} while (0)

Print configuration debug message.

Debug messages use the logging system if it's initialized, otherwise they are silently dropped.

Definition at line 79 of file options/config.c.

79#define CONFIG_DEBUG(fmt, ...) \
80 do { \
81 /* Debug messages are only shown in debug builds after logging is initialized */ \
82 /* Use log_dev which safely checks initialization itself */ \
83 log_dev(fmt, ##__VA_ARGS__); \
84 } while (0)

Referenced by config_load_system_and_user().

◆ CONFIG_WARN

#define CONFIG_WARN ( fmt,
... )
Value:
do { \
log_warn("Config file: " fmt, ##__VA_ARGS__); \
} while (0)

Print configuration warning using the logging system.

Uses log_warn so that config warnings respect the –quiet flag and are routed through the logging system for proper filtering.

Definition at line 68 of file options/config.c.

68#define CONFIG_WARN(fmt, ...) \
69 do { \
70 log_warn("Config file: " fmt, ##__VA_ARGS__); \
71 } while (0)

◆ OPTIONS_CONFIG_QUESTION_BUFFER_SIZE

#define OPTIONS_CONFIG_QUESTION_BUFFER_SIZE   4096

Definition at line 8 of file options/config.c.

Referenced by config_create_default().

Function Documentation

◆ config_load_and_apply()

asciichat_error_t config_load_and_apply ( asciichat_mode_t detected_mode,
const char * config_path,
bool strict,
options_t * opts )

Load configuration from TOML file and apply to global options.

Parameters
is_clienttrue if loading client configuration, false for server configuration
config_pathOptional path to config file (NULL uses default location)
strictIf true, errors are fatal; if false, errors are non-fatal warnings
Returns
ASCIICHAT_OK on success, error code on failure (if strict) or non-fatal (if !strict)

Loads configuration from the specified path (or default location if config_path is NULL) and applies values to global options.

Default config file location (when config_path is NULL):

  • Unix: $XDG_CONFIG_HOME/ascii-chat/config.toml if set, otherwise ~/.ascii-chat/config.toml
  • Windows: APPDATA%\ascii-chat\config.toml if set, otherwise ~.ascii-chat\config.toml

Only applies configuration values that haven't already been set (though in practice, CLI arguments will override config values anyway since this is called before CLI parsing).

Supported configuration sections:

Note
This function should be called before options_init() parses command-line arguments, so that CLI arguments can override config file values.
If strict is false and the config file doesn't exist, is not a regular file, or fails to parse, the function returns ASCIICHAT_OK (non-fatal). Individual invalid values are skipped with warnings, but valid values are still applied.
If strict is true, any error (file not found, parse error, etc.) causes the function to return an error code immediately.
Warning
Configuration warnings are printed directly to stderr because logging may not be initialized yet when this function is called.

Definition at line 156 of file options/config.c.

156 {
157 if (!opts)
158 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing configuration path or destination");
159 if (!config_path)
160 return strict ? SET_ERRNO(ERROR_CONFIG, "Missing configuration path") : ASCIICHAT_OK;
161
162 struct stat file_info;
163 if (strcmp(config_path, "-") != 0 && stat(config_path, &file_info) != 0) {
164 if (!strict && (errno == ENOENT || errno == ENOTDIR))
165 return ASCIICHAT_OK;
166 return SET_ERRNO(ERROR_CONFIG, "Unable to access configuration file '%s': %s", config_path, platform_strerror(errno));
167 }
168
169 const option_mode_bitmask_t mode_mask = detected_mode == MODE_DISCOVERY ? OPTION_MODE_ALL : (option_mode_bitmask_t)(OPTION_MODE_BINARY | (1U << detected_mode));
171 if (!builder)
172 return SET_ERRNO(ERROR_CONFIG, "Failed to build configuration schema");
173 flags_context_t *context = flags_create(flags_schema_builder_schema(builder), opts, sizeof(*opts));
174 if (!context) {
175 flags_schema_builder_destroy(builder);
176 return SET_ERRNO(ERROR_MEMORY, "Failed to create configuration context");
177 }
178
179 const bool loaded = flags_config_load_toml(context, config_path);
180 if (loaded && !flags_explicit_state_merge(context)) {
181 flags_destroy_preserving_storage(context);
182 flags_schema_builder_destroy(builder);
183 return SET_ERRNO(ERROR_MEMORY, "Failed to record explicit option state");
184 }
185 if (loaded)
186 options_resolve_flip_x(opts, flags_was_explicitly_set(context, "flip-x"));
187 flags_destroy_preserving_storage(context);
188 flags_schema_builder_destroy(builder);
189 if (!loaded && !strict)
190 return ASCIICHAT_OK;
191 if (!loaded)
192 return SET_ERRNO(ERROR_CONFIG, "Failed to load configuration file '%s'", config_path);
193 return ASCIICHAT_OK;
194}
#define SET_ERRNO(code,...)
flags_schema_builder_t * options_config_build_flags_schema(option_mode_bitmask_t mode_mask)
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
option_mode_bitmask_t
Option mode bitmask.
Definition options.h:933
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
@ OPTION_MODE_ALL
All modes + binary.
Definition options.h:943
@ OPTION_MODE_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:942
struct flags_schema_builder flags_schema_builder_t
void options_resolve_flip_x(options_t *opts, bool explicitly_set)
Definition options.c:2435
const char * platform_strerror(int errnum)
Definition system.c:52

References ASCIICHAT_OK, ERROR_CONFIG, ERROR_INVALID_PARAM, ERROR_MEMORY, MODE_DISCOVERY, OPTION_MODE_ALL, OPTION_MODE_BINARY, options_config_build_flags_schema(), options_resolve_flip_x(), platform_strerror(), and SET_ERRNO.

Referenced by config_load_system_and_user().