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

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...
struct  binary_actions_t
struct  mode_arguments_t
 The useful part of binary actions is in show_version, create_config, create_manpage, has_action: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. More...

Macros

#define OPTIONS_OPT_BUFFER_SIZE   64
#define OPTIONS_LOG_FILENAME_BUFFER_SIZE   256
#define OPTIONS_RESULT_BUFFER_SIZE   512
#define OPTIONS_LONG_FORM_BUFFER_SIZE   256
#define OPTIONS_LONG_FORM_EQ_BUFFER_SIZE   256
#define OPTIONS_NORMALIZED_URL_BUFFER_SIZE   2048

Functions

yyjson_mut_val * options_json_schema_for_option (yyjson_mut_doc *document, const char *name)
asciichat_error_t options_json_parse_value (const char *name, yyjson_val *value, char *output, size_t output_size, bool *omitted)
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 a new options_t struct with all defaults set.
asciichat_error_t options_validate_libflags (options_t *options)
asciichat_error_t options_init (int argc, char **argv)
 Initialize options by parsing command-line arguments with unified mode detection.
void options_resolve_flip_x (options_t *opts, bool explicitly_set)

Macro Definition Documentation

◆ OPTIONS_LOG_FILENAME_BUFFER_SIZE

#define OPTIONS_LOG_FILENAME_BUFFER_SIZE   256

Definition at line 54 of file options.c.

◆ OPTIONS_LONG_FORM_BUFFER_SIZE

#define OPTIONS_LONG_FORM_BUFFER_SIZE   256

Definition at line 56 of file options.c.

◆ OPTIONS_LONG_FORM_EQ_BUFFER_SIZE

#define OPTIONS_LONG_FORM_EQ_BUFFER_SIZE   256

Definition at line 57 of file options.c.

◆ OPTIONS_NORMALIZED_URL_BUFFER_SIZE

#define OPTIONS_NORMALIZED_URL_BUFFER_SIZE   2048

Definition at line 58 of file options.c.

◆ OPTIONS_OPT_BUFFER_SIZE

#define OPTIONS_OPT_BUFFER_SIZE   64

Definition at line 53 of file options.c.

◆ OPTIONS_RESULT_BUFFER_SIZE

#define OPTIONS_RESULT_BUFFER_SIZE   512

Definition at line 55 of file options.c.

Function Documentation

◆ options_json_parse_value()

asciichat_error_t options_json_parse_value ( const char * name,
yyjson_val * value,
char * output,
size_t output_size,
bool * omitted )

Convert and parse a JSON value with the CLI handler, without publishing global options. omitted is set for an explicitly disabled optional flag such as test-pattern=false. Full cross-option validation remains in the normal CLI worker initialization.

Definition at line 200 of file options.c.

202 {
203 if (!name || !value || !output || !output_size || !omitted)
204 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing JSON option argument");
205 *omitted = false;
207 if (!entry || entry->type == OPTION_TYPE_ACTION || entry->owns_memory)
208 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported value option: %s", name);
209 asciichat_error_t error = options_json_validate_value(entry, value);
210 if (error != ASCIICHAT_OK)
211 return error;
212 if (yyjson_is_str(value)) {
213 size_t length = yyjson_get_len(value);
214 if (length >= output_size ||
215 (entry->type == OPTION_TYPE_STRING && entry->default_value_size &&
216 length >= entry->default_value_size))
217 return SET_ERRNO(ERROR_INVALID_PARAM, "Option value too long: %s", name);
218 SAFE_STRNCPY(output, yyjson_get_str(value), output_size);
219 } else if (yyjson_is_bool(value)) {
220 SAFE_SNPRINTF(output, output_size, "%s",
221 yyjson_get_bool(value) ? "true" : "false");
222 } else {
223 SAFE_SNPRINTF(output, output_size, "%.17g", yyjson_get_num(value));
224 }
225 if (!strcmp(name, "test-pattern") && yyjson_is_bool(value)) {
226 if (!yyjson_get_bool(value)) {
227 *omitted = true;
228 return ASCIICHAT_OK;
229 }
230 SAFE_SNPRINTF(output, output_size, "0");
231 }
232 flags_schema_builder_t *builder = flags_schema_builder_create();
233 if (!builder)
234 return SET_ERRNO(ERROR_MEMORY, "Failed to build JSON validation schema");
236 ASCIICHAT_OK) {
237 flags_schema_builder_destroy(builder);
238 return SET_ERRNO(ERROR_CONFIG, "Failed to populate JSON validation schema");
239 }
240 options_t *scratch = SAFE_CALLOC(1, sizeof(*scratch), options_t *);
241 flags_context_t *context = flags_create(flags_schema_builder_schema(builder),
242 scratch, sizeof(*scratch));
243 if (!context) {
244 SAFE_FREE(scratch);
245 flags_schema_builder_destroy(builder);
246 return SET_ERRNO(ERROR_MEMORY, "Failed to create JSON validation context");
247 }
248 const bool valid =
249 flags_set_text(context, name, output, FLAGS_SOURCE_COMMAND_LINE);
250 flags_destroy_preserving_storage(context);
251 SAFE_FREE(scratch);
252 flags_schema_builder_destroy(builder);
253 return valid ? ASCIICHAT_OK
254 : SET_ERRNO(ERROR_INVALID_PARAM, "Invalid value for option: %s",
255 name);
256}
#define SET_ERRNO(code,...)
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:183
@ OPTION_TYPE_ACTION
Definition builder.h:186
const registry_entry_t * registry_find_entry_by_name(const char *long_name)
Get a registry entry by long name.
Definition core.c:136
bool valid
Definition fft.c:29
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
@ OPTION_MODE_ALL
All modes + binary.
Definition options.h:943
struct flags_schema_builder flags_schema_builder_t
asciichat_error_t registry_add_all_to_flags_builder(flags_schema_builder_t *builder, option_mode_bitmask_t mode_mask)
Definition public_api.c:84
Registry entry - stores option definition with mode bitmask and metadata.
option_type_t type
bool owns_memory
size_t default_value_size

References ASCIICHAT_OK, registry_entry_t::default_value_size, ERROR_CONFIG, ERROR_INVALID_PARAM, ERROR_MEMORY, OPTION_MODE_ALL, OPTION_TYPE_ACTION, OPTION_TYPE_STRING, registry_entry_t::owns_memory, registry_add_all_to_flags_builder(), registry_find_entry_by_name(), SAFE_CALLOC, SAFE_FREE, SAFE_SNPRINTF, SAFE_STRNCPY, SET_ERRNO, registry_entry_t::type, and valid.

◆ options_json_schema_for_option()

yyjson_mut_val * options_json_schema_for_option ( yyjson_mut_doc * doc,
const char * name )

Generate a JSON property from a registered option's metadata; document owns the result.

Definition at line 158 of file options.c.

159 {
160 if (!document || !name) {
161 SET_ERRNO(ERROR_INVALID_PARAM, "Missing option schema argument");
162 return NULL;
163 }
165 if (!entry || entry->type == OPTION_TYPE_ACTION) {
166 SET_ERRNO(ERROR_INVALID_PARAM, "Unknown value option: %s", name);
167 return NULL;
168 }
169 yyjson_mut_val *schema = yyjson_mut_obj(document);
170 yyjson_mut_obj_add_str(document, schema, "type", options_json_type(entry));
171 yyjson_mut_obj_add_str(document, schema, "description", entry->help_text);
172 if (entry->metadata.enum_values) {
173 yyjson_mut_val *values = yyjson_mut_arr(document);
174 for (size_t i = 0; entry->metadata.enum_values[i]; ++i)
175 yyjson_mut_arr_add_str(document, values, entry->metadata.enum_values[i]);
176 yyjson_mut_obj_add_val(document, schema, "enum", values);
177 }
178 if (!strcmp(name, "test-pattern")) {
179 yyjson_mut_obj_remove_str(schema, "type");
180 yyjson_mut_obj_remove_str(schema, "enum");
181 yyjson_mut_val *values = yyjson_mut_arr(document);
182 yyjson_mut_arr_add_bool(document, values, true);
183 yyjson_mut_arr_add_bool(document, values, false);
184 yyjson_mut_arr_add_int(document, values, 0);
185 yyjson_mut_arr_add_int(document, values, 1);
186 yyjson_mut_arr_add_str(document, values, "0");
187 yyjson_mut_arr_add_str(document, values, "1");
188 yyjson_mut_obj_add_val(document, schema, "enum", values);
189 }
190 if (!strcmp(name, "seek")) {
191 yyjson_mut_obj_remove_str(schema, "type");
192 yyjson_mut_val *types = yyjson_mut_arr(document);
193 yyjson_mut_arr_add_str(document, types, "string");
194 yyjson_mut_arr_add_str(document, types, "number");
195 yyjson_mut_obj_add_val(document, schema, "type", types);
196 }
197 return schema;
198}
const char ** enum_values
Definition builder.h:216
option_metadata_t metadata
Enum values, numeric ranges, examples.
const char * help_text

References option_metadata_t::enum_values, ERROR_INVALID_PARAM, registry_entry_t::help_text, registry_entry_t::metadata, OPTION_TYPE_ACTION, registry_find_entry_by_name(), SET_ERRNO, and registry_entry_t::type.

◆ options_resolve_flip_x()

void options_resolve_flip_x ( options_t * opts,
bool explicitly_set )

Resolve source defaults without turning them into a user selection.

Definition at line 2692 of file options.c.

2692 {
2693 if (!opts)
2694 return;
2695 if (explicitly_set)
2696 return;
2697 if (opts->test_pattern)
2698 opts->flip_x = false;
2699 else
2700 opts->flip_x = !opts->media_input && !opts->waveform && !opts->fft;
2701}
bool fft
Replace the video image with a live audio frequency spectrogram.
Definition options.h:1117
bool media_input
Active input is a file/URL rather than the webcam.
Definition options.h:1097
bool waveform
Replace the video image with a live audio waveform.
Definition options.h:1116
bool test_pattern
Use test pattern instead of webcam.
Definition options.h:1063
bool flip_x
Effective horizontal orientation.
Definition options.h:1095

References options_state::fft, options_state::flip_x, options_state::media_input, options_state::test_pattern, and options_state::waveform.

Referenced by config_load_and_apply().

◆ options_t_new_preserve_binary()

options_t options_t_new_preserve_binary ( const options_t * source)

Create a new options_t struct with all defaults set.

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 911 of file options.c.

911 {
912 // Extract binary-level options before reset
913 binary_level_opts_t binary = extract_binary_level(source);
914
915 // Create fresh options with all defaults
916 options_t opts = options_t_new();
917
918 // Restore binary-level options (overwrite defaults)
919 restore_binary_level(&opts, &binary);
920
921 return opts;
922}
audio_visualization_source_t source
Definition fft.c:25
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Definition options.c:688
Create a new options_t struct with all defaults set.
Definition options.c:592

References options_t_new(), and source.

◆ options_validate_libflags()

asciichat_error_t options_validate_libflags ( options_t * options)

Definition at line 1762 of file options.c.

1762 {
1763 if (!options)
1764 return SET_ERRNO(ERROR_INVALID_PARAM, "Options storage is NULL");
1765 const option_mode_bitmask_t mode_mask =
1766 options->detected_mode == MODE_DISCOVERY
1769 (1U << options->detected_mode));
1770 flags_schema_builder_t *builder = flags_schema_builder_create();
1771 if (!builder)
1772 return SET_ERRNO(ERROR_MEMORY,
1773 "Failed to create libflags validation schema");
1774 if (registry_add_all_to_flags_builder(builder, mode_mask) != ASCIICHAT_OK ||
1775 !add_ascii_chat_dependencies(builder)) {
1776 flags_schema_builder_destroy(builder);
1777 return SET_ERRNO(ERROR_CONFIG,
1778 "Failed to build libflags validation schema");
1779 }
1780 flags_context_t *context = flags_create(flags_schema_builder_schema(builder),
1781 options, sizeof(*options));
1782 if (!context) {
1783 flags_schema_builder_destroy(builder);
1784 return SET_ERRNO(ERROR_MEMORY,
1785 "Failed to create libflags validation context");
1786 }
1787 const bool valid = flags_validate(context);
1788 flags_destroy_preserving_storage(context);
1789 flags_schema_builder_destroy(builder);
1790 if (!valid)
1792 "One or more options failed libflags validation");
1793 return ASCIICHAT_OK;
1794}
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_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:942
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
Definition options.h:978

References ASCIICHAT_OK, options_state::detected_mode, ERROR_CONFIG, ERROR_INVALID_PARAM, ERROR_MEMORY, MODE_DISCOVERY, OPTION_MODE_ALL, OPTION_MODE_BINARY, registry_add_all_to_flags_builder(), SET_ERRNO, and valid.

Referenced by validate_options_and_report().