ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
common.h File Reference

Common utilities and helpers for option parsing across all modes. More...

Go to the source code of this file.

Macros

#define OPTIONS_COMMON_H

Functions

const char * find_similar_option (const char *unknown_opt, const struct option *options)
 Find a similar option name for typo suggestions.
const char * format_available_modes (option_mode_bitmask_t mode_bitmask)
 Format all available modes for an option as comma-separated list.
int strtoint_safe (const char *str)
 Safely parse string to integer with validation.
char * validate_required_argument (const char *optarg, char *argbuf, size_t argbuf_size, const char *option_name, asciichat_mode_t mode)
 Validate and retrieve required argument for an option.
bool validate_positive_int_opt (const char *value_str, int *out_value, const char *param_name)
 Validate a positive integer value.
bool validate_port_opt (const char *value_str, uint16_t *out_port)
 Validate port number (1-65535).
bool validate_fps_opt (const char *value_str, int *out_fps)
 Validate FPS value (0 for auto, otherwise 1-144).
bool validate_webcam_index (const char *value_str, unsigned short int *out_index)
 Validate webcam index using the common device index validator.
asciichat_error_t validate_options_and_report (const void *config, const void *opts)
 Validate options and report errors to stderr.
asciichat_error_t detect_default_ssh_key (char *key_path, size_t path_size)
 Detect default SSH key path for the current user.
char * strip_equals_prefix (const char *opt_value, char *buffer, size_t buffer_size)
 Strip equals sign prefix from option argument.
char * get_required_argument (const char *opt_value, char *buffer, size_t buffer_size, const char *option_name, asciichat_mode_t mode)
 Handle required arguments with consistent error messages.
char * read_password_from_stdin (const char *prompt)
 Read password from stdin with prompt.
asciichat_error_t parse_color_mode_option (const char *value_str, options_t *opts)
 Parse –color-mode option and set opts->color_mode.
asciichat_error_t parse_render_mode_option (const char *value_str, options_t *opts)
 Parse –render-mode option and set opts->render_mode.
asciichat_error_t parse_palette_option (const char *value_str, options_t *opts)
 Parse –palette option and set opt_palette_type.
asciichat_error_t parse_palette_chars_option (const char *value_str, options_t *opts)
 Parse –palette-chars option and set opt_palette_custom.
asciichat_error_t parse_width_option (const char *value_str, options_t *opts)
 Parse –width option and set opts->width.
asciichat_error_t parse_height_option (const char *value_str, options_t *opts)
 Parse –height option and set opts->height.
asciichat_error_t parse_webcam_index_option (const char *value_str, options_t *opts)
 Parse –webcam-index option and set opts->webcam_index.
asciichat_error_t parse_snapshot_delay_option (const char *value_str, options_t *opts)
 Parse –snapshot-delay option and set opts->snapshot_delay.
asciichat_error_t parse_log_level_option (const char *value_str, options_t *opts)
 Parse –log-level option and set opt_log_level.
void update_dimensions_for_full_height (options_t *opts)
 Update dimensions for full-height mode.
void update_dimensions_to_terminal_size (options_t *opts)
 Update dimensions to current terminal size.
void print_project_links (FILE *desc)
 Print project links with link emoji and colored styling.

Detailed Description

Common utilities and helpers for option parsing across all modes.

This module provides shared utilities used by the entire options system:

  • Validators for numeric ranges, file existence, formats (IP, port, etc.)
  • String parsing helpers (safe integer conversion, color mode parsing, etc.)
  • Terminal dimension management functions
  • Option lookup and typo suggestion (Levenshtein distance)
  • SSH key detection and defaults

Design Philosophy:

  • Single Responsibility: Each validator handles one specific type of validation
  • Consistent Error Reporting: All validators provide helpful error messages
  • No Side Effects: Validators are pure functions (no global state modification)
  • Reusability: These functions are used by registry, builder, and parsers modules
  • Cross-Cutting Concerns: Handles validation needs for all modes uniformly

Validator Functions:

Return conventions:

  • Numeric validators: Return parsed value on success, INT_MIN/-1 on error
  • Boolean validators: Return true/false with error message on failure
  • String validators: Validate format, write to output buffer

Typical Usage:

// Validate port number
uint16_t port;
if (!validate_port_opt("8080", &port)) {
fprintf(stderr, "Invalid port: 8080\\n");
return false;
}
// Parse color mode from string
if (err != ASCIICHAT_OK) {
fprintf(stderr, "Unknown color mode\\n");
return false;
}
// Find similar option if user misspelled
const char *suggestion = find_similar_option("prot", all_options);
if (suggestion) {
fprintf(stderr, "Did you mean: --%s?\\n", suggestion);
}
unsigned short uint16_t
Definition common.h:61
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ASCIICHAT_OK
Definition error_codes.h:46
const char * find_similar_option(const char *unknown_opt, const struct option *options)
Find a similar option name for typo suggestions.
bool validate_port_opt(const char *value_str, uint16_t *out_port)
Validate port number (1-65535).
asciichat_error_t parse_color_mode_option(const char *value_str, options_t *opts)
Parse –color-mode option and set opts->color_mode.

Option Parsing Helpers:

Display Option Parsers:

Terminal Functions:

Cryptography Helpers:

See also
options.h - Main Options Module module
registry.h - Central registry of all Options Module
builder.h - Builder API using these validators
validation.h - Additional Validation Helpers functions
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file options/common.h.

Macro Definition Documentation

◆ OPTIONS_COMMON_H

#define OPTIONS_COMMON_H

Definition at line 91 of file options/common.h.

Function Documentation

◆ detect_default_ssh_key()

asciichat_error_t detect_default_ssh_key ( char * key_path,
size_t path_size )

Detect default SSH key path for the current user.

Checks if ~/.ssh/id_ed25519 exists and is a regular file. Only supports Ed25519 keys (modern, secure, fast).

Parameters
key_pathBuffer to store detected key path
path_sizeSize of key_path buffer
Returns
ASCIICHAT_OK if found, ERROR_CRYPTO_KEY with helpful message otherwise
Note
Uses expand_path() to resolve tilde (~) in path
Prints message to stderr suggesting key generation if not found

Example:

char key_path[OPTIONS_BUFF_SIZE];
if (detect_default_ssh_key(key_path, sizeof(key_path)) == ASCIICHAT_OK) {
log_debug("Using default SSH key: %s", key_path);
SAFE_SNPRINTF(opt_encrypt_key, OPTIONS_BUFF_SIZE, "%s", key_path);
}
#define SAFE_SNPRINTF(buffer, buffer_size,...)
Definition common.h:507
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
#define OPTIONS_BUFF_SIZE
Buffer size for option string values.
Definition options.h:310
asciichat_error_t detect_default_ssh_key(char *key_path, size_t path_size)
Detect default SSH key path for the current user.

Definition at line 275 of file options/common.c.

275 {
276 // Use expand_path utility to resolve ~/.ssh/id_ed25519
277 char *full_path = expand_path("~/.ssh/id_ed25519");
278 if (!full_path) {
279 return SET_ERRNO(ERROR_CONFIG, "Could not expand SSH key path");
280 }
281
282 // Check if the Ed25519 private key file exists
283 struct stat st;
284 bool found = (stat(full_path, &st) == 0 && S_ISREG(st.st_mode));
285
286 if (found) {
287 SAFE_SNPRINTF(key_path, path_size, "%s", full_path);
288 log_debug("Found default SSH key: %s", full_path);
289 SAFE_FREE(full_path);
290 return ASCIICHAT_OK;
291 }
292
293 log_error("No Ed25519 SSH key found at %s", full_path);
294 SAFE_FREE(full_path);
295 return SET_ERRNO(
297 "Only Ed25519 keys are supported (modern, secure, fast). Generate a new key with: ssh-keygen -t ed25519");
298}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
Definition common.h:377
@ ERROR_CRYPTO_KEY
Definition error_codes.h:94
@ ERROR_CONFIG
Definition error_codes.h:52
#define log_error(...)
Log an ERROR message.
Definition log/log.h:634
char * expand_path(const char *path)
Expand path with tilde (~) support.
Definition path.c:259

References ASCIICHAT_OK, ERROR_CONFIG, ERROR_CRYPTO_KEY, expand_path(), log_debug, log_error, SAFE_FREE, SAFE_SNPRINTF, and SET_ERRNO.

◆ find_similar_option()

const char * find_similar_option ( const char * unknown_opt,
const struct option * options )

Find a similar option name for typo suggestions.

Uses Levenshtein distance to find the most similar option name from the provided options array. Only suggests options within a reasonable edit distance.

Parameters
unknown_optThe unknown/misspelled option name
optionsArray of valid option structures (must be NULL-terminated)
Returns
Suggested option name, or NULL if no good match found
Note
Uses LEVENSHTEIN_SUGGESTION_THRESHOLD to filter poor matches

Example:

const char *suggestion = find_similar_option("--colr", client_options);
if (suggestion) {
fprintf(stderr, "Did you mean '--%s'?\n", suggestion);
}

Definition at line 43 of file options/common.c.

43 {
44 if (!unknown_opt || !options) {
45 return NULL;
46 }
47
48 const char *best_match = NULL;
49 size_t best_distance = SIZE_MAX;
50
51 for (int i = 0; options[i].name != NULL; i++) {
52 size_t dist = levenshtein(unknown_opt, options[i].name);
53 if (dist < best_distance) {
54 best_distance = dist;
55 best_match = options[i].name;
56 }
57 }
58
59 // Only suggest if the distance is within our threshold
60 if (best_distance <= LEVENSHTEIN_SUGGESTION_THRESHOLD) {
61 return best_match;
62 }
63
64 return NULL;
65}
size_t levenshtein(const char *a, const char *b)
Calculate Levenshtein distance between two strings.
Definition levenshtein.c:75
#define LEVENSHTEIN_SUGGESTION_THRESHOLD
Maximum edit distance to suggest an option.
Definition levenshtein.h:32

References levenshtein(), and LEVENSHTEIN_SUGGESTION_THRESHOLD.

◆ format_available_modes()

const char * format_available_modes ( option_mode_bitmask_t mode_bitmask)

Format all available modes for an option as comma-separated list.

Converts a mode bitmask to a human-readable comma-separated list of mode names. Used in error messages to show which modes support a given option.

Parameters
mode_bitmaskBitmask of modes (OPTION_MODE_SERVER | OPTION_MODE_CLIENT, etc.)
Returns
Formatted string like "server, client, mirror" or "global options"
Note
Returns pointer to static buffer - not thread-safe

Example:

const char *modes = format_available_modes(bitmask);
// modes == "server, client"
option_mode_bitmask_t
Option mode bitmask.
Definition options.h:936
@ OPTION_MODE_CLIENT
Client mode (bit 1).
Definition options.h:939
@ OPTION_MODE_SERVER
Server mode (bit 0).
Definition options.h:938
const char * format_available_modes(option_mode_bitmask_t mode_bitmask)
Format all available modes for an option as comma-separated list.

Definition at line 69 of file options/common.c.

69 {
70 static char buffer[OPTIONS_COMMON_BUFFER_BUFFER_SIZE];
71 buffer[0] = '\0';
72
73 bool first = true;
74
75 // Check if it's a global/binary option
76 if (mode_bitmask & OPTION_MODE_BINARY) {
77 SAFE_SNPRINTF(buffer, sizeof(buffer), "global options");
78 return buffer;
79 }
80
81 // Build comma-separated list of modes (ordered: default, client, server, mirror, discovery-service)
82 if (mode_bitmask & OPTION_MODE_DISCOVERY) {
83 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sdefault", first ? "" : ", ");
84 first = false;
85 }
86 if (mode_bitmask & OPTION_MODE_CLIENT) {
87 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sclient", first ? "" : ", ");
88 first = false;
89 }
90 if (mode_bitmask & OPTION_MODE_SERVER) {
91 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sserver", first ? "" : ", ");
92 first = false;
93 }
94 if (mode_bitmask & OPTION_MODE_MIRROR) {
95 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%smirror", first ? "" : ", ");
96 first = false;
97 }
98 if (mode_bitmask & OPTION_MODE_DISCOVERY_SVC) {
99 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sdiscovery-service", first ? "" : ", ");
100 first = false;
101 }
102
103 if (mode_bitmask & OPTION_MODE_MCP_SERVER) {
104 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%smcp-server", first ? "" : ", ");
105 first = false;
106 }
107 if (mode_bitmask & OPTION_MODE_API_SERVER) {
108 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sapi-server", first ? "" : ", ");
109 }
110
111 // Fallback if no modes matched
112 if (buffer[0] == '\0') {
113 SAFE_SNPRINTF(buffer, sizeof(buffer), "unknown mode");
114 }
115
116 return buffer;
117}
@ OPTION_MODE_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:945
@ OPTION_MODE_API_SERVER
Definition options.h:944
@ OPTION_MODE_DISCOVERY
Discovery mode (bit 4).
Definition options.h:942
@ OPTION_MODE_MIRROR
Mirror mode (bit 2).
Definition options.h:940
@ OPTION_MODE_MCP_SERVER
Definition options.h:943
@ OPTION_MODE_DISCOVERY_SVC
Discovery server mode (bit 3).
Definition options.h:941
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
#define OPTIONS_COMMON_BUFFER_BUFFER_SIZE

References OPTION_MODE_API_SERVER, OPTION_MODE_BINARY, OPTION_MODE_CLIENT, OPTION_MODE_DISCOVERY, OPTION_MODE_DISCOVERY_SVC, OPTION_MODE_MCP_SERVER, OPTION_MODE_MIRROR, OPTION_MODE_SERVER, OPTIONS_COMMON_BUFFER_BUFFER_SIZE, SAFE_SNPRINTF, and safe_snprintf().

Referenced by find_similar_option_with_mode().

◆ get_required_argument()

char * get_required_argument ( const char * opt_value,
char * buffer,
size_t buffer_size,
const char * option_name,
asciichat_mode_t mode )

Handle required arguments with consistent error messages.

Validates that an option has a non-empty argument and processes it. Returns NULL on error with appropriate error message printed.

Handles edge cases:

  • NULL or empty opt_value
  • getopt_long bug where option name is returned as argument
  • Arguments with '=' prefix (GNU-style –option=value)
Parameters
opt_valueArgument value from getopt_long
bufferBuffer for storing processed argument
buffer_sizeSize of buffer
option_nameName of the option (for error messages)
modeCurrent mode (client/server/mirror) for error messages
Returns
Pointer to processed argument string in buffer, or NULL on error
Note
Prints "option '--<name>' requires an argument" to stderr on error
Used internally by validate_required_argument()

Example:

char argbuf[OPTIONS_BUFF_SIZE];
char *value = get_required_argument(optarg, argbuf, sizeof(argbuf), "key", MODE_CLIENT);
if (!value) {
return option_error_invalid();
}
SAFE_SNPRINTF(opt_encrypt_key, OPTIONS_BUFF_SIZE, "%s", value);
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:921
char * get_required_argument(const char *opt_value, char *buffer, size_t buffer_size, const char *option_name, asciichat_mode_t mode)
Handle required arguments with consistent error messages.

Definition at line 325 of file options/common.c.

326 {
327 // Check if opt_value is NULL or empty
328 if (!opt_value || strlen(opt_value) == 0) {
329 goto error;
330 }
331
332 // Check if getopt_long returned the option name itself as the argument
333 // This happens when a long option requiring an argument is at the end of argv
334 if (opt_value && option_name && strcmp(opt_value, option_name) == 0) {
335 goto error;
336 }
337
338 // Process the argument normally
339 char *value_str = strip_equals_prefix(opt_value, buffer, buffer_size);
340 if (!value_str) {
341 goto error;
342 }
343
344 return value_str;
345
346error:
347 (void)0;
348 const char *mode_name = (mode == MODE_SERVER ? "server" : (mode == MODE_MIRROR ? "mirror" : "client"));
349 log_error("%s: option '--%s' requires an argument", mode_name, option_name);
350 return NULL; // Signal error to caller
351}
int buffer_size
Size of circular buffer.
Definition grep.c:68
@ MODE_SERVER
Server mode - network server options.
Definition options.h:920
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:922
char * strip_equals_prefix(const char *opt_value, char *buffer, size_t buffer_size)
Strip equals sign prefix from option argument.

References buffer_size, log_error, MODE_MIRROR, MODE_SERVER, and strip_equals_prefix().

Referenced by validate_required_argument().

◆ parse_color_mode_option()

asciichat_error_t parse_color_mode_option ( const char * value_str,
options_t * opts )

Parse –color-mode option and set opts->color_mode.

Validates color mode string and sets opts->color_mode field. Accepts: "auto", "none", "mono", "16", "16color", "256", "256color", "truecolor", "24bit"

Parameters
value_strColor mode string from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on invalid mode
Note
Prints error message to stderr on failure
Sets opts->color_mode on success

Definition at line 398 of file options/common.c.

398 {
399 if (!value_str || !opts) {
400 return ERROR_INVALID_PARAM;
401 }
402
403 if (strcmp(value_str, OPT_VALUE_AUTO) == 0 || strcmp(value_str, "a") == 0) {
405 } else if (strcmp(value_str, OPT_COLOR_MODE_NONE) == 0 || strcmp(value_str, OPT_COLOR_MODE_MONO) == 0) {
407 } else if (strcmp(value_str, OPT_COLOR_MODE_16) == 0 || strcmp(value_str, "16color") == 0 ||
408 strcmp(value_str, "ansi") == 0) {
410 } else if (strcmp(value_str, OPT_COLOR_MODE_256) == 0 || strcmp(value_str, "256color") == 0) {
412 } else if (strcmp(value_str, OPT_COLOR_MODE_TRUECOLOR) == 0 || strcmp(value_str, "24bit") == 0 ||
413 strcmp(value_str, "tc") == 0 || strcmp(value_str, "rgb") == 0 || strcmp(value_str, OPT_VALUE_TRUE) == 0) {
415 } else {
416 log_error("Invalid color mode '%s'. Valid modes: auto, none, 16, 256, truecolor", value_str);
417 return ERROR_INVALID_PARAM;
418 }
419
420 return ASCIICHAT_OK;
421}
#define OPT_COLOR_MODE_256
Definition enums.h:101
#define OPT_VALUE_TRUE
Definition enums.h:78
#define OPT_COLOR_MODE_NONE
Definition enums.h:98
#define OPT_VALUE_AUTO
Definition enums.h:77
#define OPT_COLOR_MODE_TRUECOLOR
Definition enums.h:102
#define OPT_COLOR_MODE_MONO
Definition enums.h:99
#define OPT_COLOR_MODE_16
Definition enums.h:100
@ ERROR_INVALID_PARAM
#define COLOR_MODE_16_COLOR
16-color mode (full name)
Definition options.h:225
#define COLOR_MODE_256_COLOR
256-color mode (full name)
Definition options.h:227
#define COLOR_MODE_TRUECOLOR
24-bit truecolor mode
Definition options.h:228
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:222
#define COLOR_MODE_NONE
Monochrome mode.
Definition options.h:223
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor).
Definition options.h:1092

References ASCIICHAT_OK, options_state::color_mode, COLOR_MODE_16_COLOR, COLOR_MODE_256_COLOR, COLOR_MODE_AUTO, COLOR_MODE_NONE, COLOR_MODE_TRUECOLOR, ERROR_INVALID_PARAM, log_error, OPT_COLOR_MODE_16, OPT_COLOR_MODE_256, OPT_COLOR_MODE_MONO, OPT_COLOR_MODE_NONE, OPT_COLOR_MODE_TRUECOLOR, OPT_VALUE_AUTO, and OPT_VALUE_TRUE.

◆ parse_height_option()

asciichat_error_t parse_height_option ( const char * value_str,
options_t * opts )

Parse –height option and set opts->height.

Validates height value and sets opts->height and opts->auto_height fields.

Parameters
value_strHeight value from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if invalid
Note
Prints error message to stderr on failure
Sets opts->height and opts->auto_height = false on success

Definition at line 502 of file options/common.c.

502 {
503 if (!opts) {
504 return ERROR_INVALID_PARAM;
505 }
506
507 int height_val;
508 if (!validate_positive_int_opt(value_str, &height_val, "height")) {
509 return ERROR_INVALID_PARAM;
510 }
511
512 opts->height = height_val;
513 opts->auto_height = false;
514
515 return ASCIICHAT_OK;
516}
bool validate_positive_int_opt(const char *value_str, int *out_value, const char *param_name)
Validate a positive integer value.
int height
Terminal height in characters (int for OPTION_TYPE_INT compat).
Definition options.h:997
bool auto_height
Auto-detect height from terminal.
Definition options.h:999

References ASCIICHAT_OK, options_state::auto_height, ERROR_INVALID_PARAM, options_state::height, and validate_positive_int_opt().

◆ parse_log_level_option()

asciichat_error_t parse_log_level_option ( const char * value_str,
options_t * opts )

Parse –log-level option and set opt_log_level.

Validates log level string and sets global opt_log_level variable. Accepts: "dev", "debug", "info", "warn", "error", "fatal" (case-insensitive)

Parameters
value_strLog level string from command line
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if invalid
Note
Prints error message to stderr on failure
Sets opt_log_level global variable on success
Uses validate_opt_log_level() from validation.h

Definition at line 550 of file options/common.c.

550 {
551 if (!opts) {
552 return ERROR_INVALID_PARAM;
553 }
554
555 char error_msg[BUFFER_SIZE_SMALL];
556 int log_level = validate_opt_log_level(value_str, error_msg, sizeof(error_msg));
557
558 if (log_level == -1) {
559 log_error("%s", error_msg);
560 return ERROR_INVALID_PARAM;
561 }
562
563 opts->log_level = (log_level_t)log_level;
564
565 return ASCIICHAT_OK;
566}
#define BUFFER_SIZE_SMALL
Small buffer size (256 bytes).
log_level_t
Logging levels enumeration.
Definition types.h:29
int validate_opt_log_level(const char *value_str, char *error_msg, size_t error_msg_size)
Validate log level string.
Definition validation.c:247
log_level_t log_level
Log level threshold.
Definition options.h:1167

References ASCIICHAT_OK, BUFFER_SIZE_SMALL, ERROR_INVALID_PARAM, log_error, options_state::log_level, and validate_opt_log_level().

◆ parse_palette_chars_option()

asciichat_error_t parse_palette_chars_option ( const char * value_str,
options_t * opts )

Parse –palette-chars option and set opt_palette_custom.

Validates custom palette characters and sets global opt_palette_custom, opt_palette_custom_set, and opt_palette_type variables.

Parameters
value_strCustom palette characters from command line
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if too long
Note
Prints error message to stderr on failure
Sets opt_palette_custom, opt_palette_custom_set, and opt_palette_type on success
Maximum length is 255 characters (sizeof(opt_palette_custom) - 1)

Definition at line 467 of file options/common.c.

467 {
468 if (!value_str || !opts) {
469 return ERROR_INVALID_PARAM;
470 }
471
472 if (strlen(value_str) >= sizeof(opts->palette_custom)) {
473 log_error("Invalid palette-chars: too long (%zu chars, max %zu)", strlen(value_str),
474 sizeof(opts->palette_custom) - 1);
475 return ERROR_INVALID_PARAM;
476 }
477
478 SAFE_STRNCPY(opts->palette_custom, value_str, sizeof(opts->palette_custom));
479 opts->palette_custom[sizeof(opts->palette_custom) - 1] = '\0';
480 opts->palette_custom_set = true;
482
483 return ASCIICHAT_OK;
484}
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:423
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:104
char palette_custom[256]
Custom palette characters.
Definition options.h:1230
bool palette_custom_set
True if custom palette was set.
Definition options.h:1231
palette_type_t palette_type
Selected palette type.
Definition options.h:1229

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_error, PALETTE_CUSTOM, options_state::palette_custom, options_state::palette_custom_set, options_state::palette_type, and SAFE_STRNCPY.

◆ parse_palette_option()

asciichat_error_t parse_palette_option ( const char * value_str,
options_t * opts )

Parse –palette option and set opt_palette_type.

Validates palette type string and sets global opt_palette_type variable. Accepts: "standard", "blocks", "digital", "minimal", "cool", "custom"

Parameters
value_strPalette type string from command line
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on invalid palette
Note
Prints error message to stderr on failure
Sets opt_palette_type global variable on success

Definition at line 442 of file options/common.c.

442 {
443 if (!value_str || !opts) {
444 return ERROR_INVALID_PARAM;
445 }
446
447 if (strcmp(value_str, OPT_PALETTE_STANDARD) == 0) {
449 } else if (strcmp(value_str, OPT_PALETTE_BLOCKS) == 0) {
451 } else if (strcmp(value_str, OPT_PALETTE_DIGITAL) == 0) {
453 } else if (strcmp(value_str, OPT_PALETTE_MINIMAL) == 0) {
455 } else if (strcmp(value_str, OPT_PALETTE_COOL) == 0) {
457 } else if (strcmp(value_str, OPT_PALETTE_CUSTOM) == 0) {
459 } else {
460 log_error("Invalid palette '%s'. Valid palettes: standard, blocks, digital, minimal, cool, custom", value_str);
461 return ERROR_INVALID_PARAM;
462 }
463
464 return ASCIICHAT_OK;
465}
#define OPT_PALETTE_CUSTOM
Definition enums.h:125
#define OPT_PALETTE_COOL
Definition enums.h:124
#define OPT_PALETTE_MINIMAL
Definition enums.h:123
#define OPT_PALETTE_BLOCKS
Definition enums.h:121
#define OPT_PALETTE_STANDARD
Definition enums.h:120
#define OPT_PALETTE_DIGITAL
Definition enums.h:122
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:96
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:102
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:94
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:98
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:100

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_error, OPT_PALETTE_BLOCKS, OPT_PALETTE_COOL, OPT_PALETTE_CUSTOM, OPT_PALETTE_DIGITAL, OPT_PALETTE_MINIMAL, OPT_PALETTE_STANDARD, PALETTE_BLOCKS, PALETTE_COOL, PALETTE_CUSTOM, PALETTE_DIGITAL, PALETTE_MINIMAL, PALETTE_STANDARD, and options_state::palette_type.

◆ parse_render_mode_option()

asciichat_error_t parse_render_mode_option ( const char * value_str,
options_t * opts )

Parse –render-mode option and set opts->render_mode.

Validates render mode string and sets opts->render_mode field. Accepts: "foreground", "fg", "background", "bg", "half-block", "halfblock"

Parameters
value_strRender mode string from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on invalid mode
Note
Prints error message to stderr on failure
Sets opts->render_mode on success

Definition at line 423 of file options/common.c.

423 {
424 if (!value_str || !opts) {
425 return ERROR_INVALID_PARAM;
426 }
427
428 if (strcmp(value_str, OPT_RENDER_MODE_FOREGROUND) == 0 || strcmp(value_str, OPT_RENDER_MODE_FG) == 0) {
429 opts->render_mode = RENDER_MODE_FOREGROUND;
430 } else if (strcmp(value_str, OPT_RENDER_MODE_BACKGROUND) == 0 || strcmp(value_str, OPT_RENDER_MODE_BG) == 0) {
431 opts->render_mode = RENDER_MODE_BACKGROUND;
432 } else if (strcmp(value_str, OPT_RENDER_MODE_HALF_BLOCK) == 0 || strcmp(value_str, "halfblock") == 0) {
433 opts->render_mode = RENDER_MODE_HALF_BLOCK;
434 } else {
435 log_error("Invalid render mode '%s'. Valid modes: foreground, background, half-block", value_str);
436 return ERROR_INVALID_PARAM;
437 }
438
439 return ASCIICHAT_OK;
440}
#define OPT_RENDER_MODE_BG
Definition enums.h:131
#define OPT_RENDER_MODE_FOREGROUND
Definition enums.h:128
#define OPT_RENDER_MODE_HALF_BLOCK
Definition enums.h:132
#define OPT_RENDER_MODE_FG
Definition enums.h:129
#define OPT_RENDER_MODE_BACKGROUND
Definition enums.h:130
terminal_render_mode_t render_mode
Render mode (foreground/background/half-block).
Definition options.h:1095

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_error, OPT_RENDER_MODE_BACKGROUND, OPT_RENDER_MODE_BG, OPT_RENDER_MODE_FG, OPT_RENDER_MODE_FOREGROUND, OPT_RENDER_MODE_HALF_BLOCK, and options_state::render_mode.

◆ parse_snapshot_delay_option()

asciichat_error_t parse_snapshot_delay_option ( const char * value_str,
options_t * opts )

Parse –snapshot-delay option and set opts->snapshot_delay.

Validates snapshot delay (non-negative float) and sets opts->snapshot_delay field.

Parameters
value_strSnapshot delay in seconds from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if invalid
Note
Prints error message to stderr on failure
Sets opts->snapshot_delay on success

Definition at line 533 of file options/common.c.

533 {
534 if (!value_str || !opts) {
535 return ERROR_INVALID_PARAM;
536 }
537
538 char *endptr;
539 float delay = strtof(value_str, &endptr);
540 if (endptr == value_str || *endptr != '\0' || delay < 0.0f) {
541 log_error("Invalid snapshot delay '%s'. Must be a non-negative number.", value_str);
542 return ERROR_INVALID_PARAM;
543 }
544
545 opts->snapshot_delay = delay;
546
547 return ASCIICHAT_OK;
548}
double snapshot_delay
Snapshot delay in seconds.
Definition options.h:1150

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_error, and options_state::snapshot_delay.

◆ parse_webcam_index_option()

asciichat_error_t parse_webcam_index_option ( const char * value_str,
options_t * opts )

Parse –webcam-index option and set opts->webcam_index.

Validates webcam index and sets opts->webcam_index field.

Parameters
value_strWebcam index from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if invalid
Note
Prints error message to stderr on failure
Sets opts->webcam_index on success

Definition at line 518 of file options/common.c.

518 {
519 if (!opts) {
520 return ERROR_INVALID_PARAM;
521 }
522
523 unsigned short int index_val;
524 if (!validate_webcam_index(value_str, &index_val)) {
525 return ERROR_INVALID_PARAM;
526 }
527
528 opts->webcam_index = index_val;
529
530 return ASCIICHAT_OK;
531}
bool validate_webcam_index(const char *value_str, unsigned short int *out_index)
Validate webcam index using the common device index validator.
int webcam_index
Webcam device index (0 = first).
Definition options.h:1070

References ASCIICHAT_OK, ERROR_INVALID_PARAM, validate_webcam_index(), and options_state::webcam_index.

◆ parse_width_option()

asciichat_error_t parse_width_option ( const char * value_str,
options_t * opts )

Parse –width option and set opts->width.

Validates width value and sets opts->width and opts->auto_width fields.

Parameters
value_strWidth value from command line
optsOptions struct to update
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM if invalid
Note
Prints error message to stderr on failure
Sets opts->width and opts->auto_width = false on success

Definition at line 486 of file options/common.c.

486 {
487 if (!opts) {
488 return ERROR_INVALID_PARAM;
489 }
490
491 int width_val;
492 if (!validate_positive_int_opt(value_str, &width_val, "width")) {
493 return ERROR_INVALID_PARAM;
494 }
495
496 opts->width = width_val;
497 opts->auto_width = false;
498
499 return ASCIICHAT_OK;
500}
int width
Terminal width in characters (int for OPTION_TYPE_INT compat).
Definition options.h:996
bool auto_width
Auto-detect width from terminal.
Definition options.h:998

References ASCIICHAT_OK, options_state::auto_width, ERROR_INVALID_PARAM, validate_positive_int_opt(), and options_state::width.

◆ print_project_links()

void print_project_links ( FILE * desc)

Print project links with link emoji and colored styling.

Parameters
descOutput file stream (typically stdout)

Definition at line 773 of file options/common.c.

773 {
774 if (!desc) {
775 return;
776 }
777
778 (void)fprintf(desc, "🔗 %s\n", colored_string(LOG_COLOR_GREY, "https://ascii-chat.com"));
779 (void)fprintf(desc, "🔗 %s\n", colored_string(LOG_COLOR_GREY, "https://github.com/zfogg/ascii-chat"));
780}
@ LOG_COLOR_GREY
Definition log/log.h:154
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
Definition string.c:284

References colored_string(), and LOG_COLOR_GREY.

◆ read_password_from_stdin()

char * read_password_from_stdin ( const char * prompt)

Read password from stdin with prompt.

Parameters
promptPrompt message to display to user
Returns
Allocated password string (caller must free), or NULL on error

Prompts user for password input using prompt_password_simple() from util/password.h. Returns dynamically allocated string that must be freed by caller.

Note
Returns NULL if password input fails or if not running in a TTY.
Caller must use SAFE_FREE() to deallocate returned string.

Definition at line 354 of file options/common.c.

354 {
355 char *password_buf = SAFE_MALLOC(PASSWORD_MAX_LEN, char *);
356 if (!password_buf) {
357 return NULL;
358 }
359
360 if (prompt_password_simple(prompt, password_buf, PASSWORD_MAX_LEN) != 0) {
361 SAFE_FREE(password_buf);
362 return NULL;
363 }
364
365 return password_buf; // Caller must free
366}
#define SAFE_MALLOC(size, cast)
Definition common.h:265
#define PASSWORD_MAX_LEN
Buffer size for password input.
Definition password.h:31
int prompt_password_simple(const char *prompt, char *password, size_t max_len)
Prompt the user for a password with simple formatting.
Definition password.c:31

References PASSWORD_MAX_LEN, prompt_password_simple(), SAFE_FREE, and SAFE_MALLOC.

◆ strip_equals_prefix()

char * strip_equals_prefix ( const char * opt_value,
char * buffer,
size_t buffer_size )

Strip equals sign prefix from option argument.

Internal helper that handles GNU-style long options with = syntax (–option=value). Copies argument to buffer and returns pointer past the '=' if present.

Parameters
opt_valueRaw option value from getopt_long
bufferBuffer to store processed value
buffer_sizeSize of buffer
Returns
Pointer to value in buffer (past '=' if present), or NULL if empty
Note
Returns NULL for empty strings after stripping '='
Buffer is always null-terminated via SAFE_SNPRINTF

Example:

char argbuf[OPTIONS_BUFF_SIZE];
char *value = strip_equals_prefix("=1234", argbuf, sizeof(argbuf));
// value points to "1234" in argbuf

Definition at line 305 of file options/common.c.

305 {
306 if (!opt_value)
307 return NULL;
308
309 SAFE_SNPRINTF(buffer, buffer_size, "%s", opt_value);
310 char *value_str = buffer;
311 if (value_str[0] == '=') {
312 value_str++; // Skip the equals sign
313 }
314
315 // Return NULL for empty strings (treat as missing argument)
316 if (strlen(value_str) == 0) {
317 return NULL;
318 }
319
320 return value_str;
321}

References buffer_size, and SAFE_SNPRINTF.

Referenced by get_required_argument().

◆ strtoint_safe()

int strtoint_safe ( const char * str)

Safely parse string to integer with validation.

Parses a string to integer using parse_int32() with full range checking. Returns INT_MIN on error (NULL input, empty string, invalid format, out of range).

Parameters
strString to parse
Returns
Parsed integer value, or INT_MIN on error
Warning
INT_MIN is used as error sentinel, so cannot represent INT_MIN value

Example:

int val = strtoint_safe(optarg);
if (val == INT_MIN) {
fprintf(stderr, "Invalid integer: %s\n", optarg);
}
int strtoint_safe(const char *str)
Safely parse string to integer with validation.

Definition at line 177 of file options/common.c.

177 {
178 if (!str || *str == '\0') {
179 return INT_MIN; // Error: NULL or empty string
180 }
181
182 int32_t result = 0;
183 // Use safe parsing utility with full int32 range validation
184 if (parse_int32(str, &result, INT_MIN, INT_MAX) != ASCIICHAT_OK) {
185 return INT_MIN; // Error: invalid input or out of range
186 }
187
188 return (int)result;
189}
asciichat_error_t parse_int32(const char *str, int32_t *out_value, int32_t min_value, int32_t max_value)
Parse signed 32-bit integer with range validation.

◆ update_dimensions_for_full_height()

void update_dimensions_for_full_height ( options_t * opts)

Update dimensions for full-height mode.

Sets opt_height to terminal height when auto-detected. Used during initialization to maximize vertical space usage.

Behavior:

  • Both auto: Set both width and height to terminal size
  • Only height auto: Set height to terminal height
  • Only width auto: Set width to terminal width
  • Neither auto: No change
Note
Does not use log_debug because logging may not be initialized yet
Fails silently if terminal size detection fails (keeps defaults)

Example:

// During options_init():
}
void update_dimensions_for_full_height(options_t *opts)
Update dimensions for full-height mode.
bool auto_height
bool auto_width

Definition at line 620 of file options/common.c.

620 {
621 if (!opts) {
622 return;
623 }
624
625 unsigned short int term_width, term_height;
626
627 // Note: Logging is not available during options_init, so we can't use log_debug here
628 asciichat_error_t result = get_terminal_size(&term_width, &term_height);
629
630 if (result == ASCIICHAT_OK) {
631 // If both dimensions are auto, set height to terminal height and let
632 // aspect_ratio calculate width
633 if (opts->auto_height && opts->auto_width) {
634 opts->height = term_height;
635 opts->width = term_width; // Also set width when both are auto
636 }
637 // If only height is auto, use full terminal height
638 else if (opts->auto_height) {
639 opts->height = term_height;
640 }
641 // If only width is auto, use full terminal width
642 else if (opts->auto_width) {
643 opts->width = term_width;
644 }
645 } else {
646 // Terminal size detection failed, but we can still continue with defaults
647 }
648}

◆ update_dimensions_to_terminal_size()

void update_dimensions_to_terminal_size ( options_t * opts)

Update dimensions to current terminal size.

Updates opt_width and opt_height to current terminal size for auto-detected dimensions. Used after logging is initialized (can use log_debug).

Behavior:

  • auto_width: Set width to terminal width
  • auto_height: Set height to terminal height
  • Neither: No change
Note
Logs debug messages about dimension updates
Logs debug message if terminal size detection fails

Example:

// After logging initialization:
log_info("Terminal dimensions: %dx%d", opt_width, opt_height);
#define log_info(...)
Log an INFO message.
Definition log/log.h:608
void update_dimensions_to_terminal_size(options_t *opts)
Update dimensions to current terminal size.

Definition at line 650 of file options/common.c.

650 {
651 if (!opts) {
652 return;
653 }
654
655 unsigned short int term_width, term_height;
656 // Get current terminal size (get_terminal_size already handles ioctl first, then $COLUMNS/$LINES fallback)
657 asciichat_error_t terminal_result = get_terminal_size(&term_width, &term_height);
658 if (terminal_result == ASCIICHAT_OK) {
659 // Use INFO level so this is visible without -v flag (important for debugging dimension issues)
660 log_dev("Terminal size detected: %ux%u (auto_width=%d, auto_height=%d)", term_width, term_height, opts->auto_width,
661 opts->auto_height);
662 if (opts->auto_width) {
663 opts->width = term_width;
664 log_debug("Auto-width: set width to %u", opts->width);
665 }
666 if (opts->auto_height) {
667 opts->height = term_height;
668 log_debug("Auto-height: set height to %u", opts->height);
669 }
670 log_debug("Final dimensions: %ux%u", opts->width, opts->height);
671 } else {
672 // Terminal detection failed - keep the default values set in options_init()
673 log_warn("TERMINAL_DETECT_FAIL: Could not detect terminal size, using defaults: %ux%u", opts->width, opts->height);
674 }
675}
#define log_warn(...)
Log a WARN message.
Definition log/log.h:621
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:581

◆ validate_fps_opt()

bool validate_fps_opt ( const char * value_str,
int * out_fps )

Validate FPS value (0 for auto, otherwise 1-144).

Internal option parsing helper that validates FPS is in reasonable range. Range chosen to support 1 FPS (slideshows) to 144 FPS (high refresh monitors).

Parameters
value_strString to validate
out_fpsOutput parameter for validated FPS
Returns
true if valid, false otherwise

Example:

int fps;
if (!validate_fps_opt(optarg, &fps)) {
return option_error_invalid();
}
opt_fps = fps;
bool validate_fps_opt(const char *value_str, int *out_fps)
Validate FPS value (0 for auto, otherwise 1-144).

Definition at line 237 of file options/common.c.

237 {
238 if (!value_str || !out_fps) {
239 return false;
240 }
241
242 int fps_val = strtoint_safe(value_str);
243 if (fps_val == INT_MIN || fps_val < 0 || fps_val > 144) {
244 log_error("Invalid FPS value '%s'. FPS must be between 0 and 144.", value_str);
245 return false;
246 }
247
248 *out_fps = fps_val;
249 return true;
250}

References log_error, and strtoint_safe().

◆ validate_options_and_report()

asciichat_error_t validate_options_and_report ( const void * config,
const void * opts )

Validate options and report errors to stderr.

Calls options_config_validate() and handles error message display and cleanup. Validates all option dependencies, conflicts, and custom validators.

Parameters
configOptions configuration (opaque pointer, defined in builder.h)
optsOptions struct to validate
Returns
ASCIICHAT_OK if valid, error code otherwise
Note
Prints error message to stderr if validation fails
Frees error message internally

Example:

if (result != ASCIICHAT_OK) {
return result;
}
void options_config_destroy(options_config_t *config)
Free options config.
asciichat_error_t validate_options_and_report(const void *config, const void *opts)
Validate options and report errors to stderr.

Definition at line 735 of file options/common.c.

735 {
736 if (!config || !opts) {
737 return SET_ERRNO(ERROR_INVALID_PARAM, "Config or options is NULL");
738 }
739
740 char *error_message = NULL;
741 // Cast opaque config pointer to actual type
742 const options_config_t *config_typed = (const options_config_t *)config;
743 const options_t *options = (const options_t *)opts;
744 if (options->webrtc_relay_only && options->detected_mode == MODE_CLIENT) {
745 return SET_ERRNO(ERROR_INVALID_PARAM, "Relay only requires discovery mode; connect using a session name");
746 }
747 if (options->webrtc_relay_only && options->detected_mode == MODE_SERVER && !options->discovery) {
748 return SET_ERRNO(ERROR_INVALID_PARAM, "Server relay only requires --discovery");
749 }
750 if (options->webrtc_relay_only && (options->no_webrtc || options->webrtc_disable_turn)) {
751 return SET_ERRNO(ERROR_INVALID_PARAM, "--webrtc-relay-only conflicts with --no-webrtc or --webrtc-disable-turn");
752 }
753 if ((options->turn_username[0] != '\0') != (options->turn_credential[0] != '\0')) {
754 return SET_ERRNO(ERROR_INVALID_PARAM, "Provide both --turn-username and --turn-credential, or leave both blank");
755 }
756 if (strlen(options->turn_username) > 127 || strlen(options->turn_credential) > 127) {
757 return SET_ERRNO(ERROR_INVALID_PARAM, "TURN username and credential must each fit 127 bytes");
758 }
759 asciichat_error_t result = options_config_validate(config_typed, opts, &error_message);
760 if (result != ASCIICHAT_OK) {
761 if (error_message) {
762 log_error("%s", error_message);
763 SAFE_FREE(error_message);
764 }
765 }
766 return result;
767}
asciichat_error_t options_config_validate(const options_config_t *config, const void *options_struct, char **error_message)
Validate options struct.
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
Immutable parsing tables produced from an options builder.
Definition builder.h:414
bool discovery
Enable discovery session registration (default: false).
Definition options.h:1032
bool webrtc_relay_only
–webrtc-relay-only: Require TURN relay candidates
Definition options.h:1210
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
Definition options.h:983
bool no_webrtc
–no-webrtc: Disable WebRTC, use Direct TCP only
Definition options.h:1207
bool webrtc_disable_turn
–webrtc-disable-turn: Disable Stage 3 (TURN), use STUN only
Definition options.h:1211
char turn_credential[256]
ACDS: Credential/password for TURN authentication.
Definition options.h:1223
char turn_username[256]
ACDS: Username for TURN authentication.
Definition options.h:1222

References ASCIICHAT_OK, options_state::detected_mode, options_state::discovery, ERROR_INVALID_PARAM, log_error, MODE_CLIENT, MODE_SERVER, options_state::no_webrtc, options_config_validate(), SAFE_FREE, SET_ERRNO, options_state::turn_credential, options_state::turn_username, options_state::webrtc_disable_turn, and options_state::webrtc_relay_only.

◆ validate_port_opt()

bool validate_port_opt ( const char * value_str,
uint16_t * out_port )

Validate port number (1-65535).

Internal option parsing helper that validates a port number is in valid range. Uses parse_port() for robust validation.

Parameters
value_strString to validate
out_portOutput parameter for validated port
Returns
true if valid, false otherwise

Example:

uint16_t port;
if (!validate_port_opt(optarg, &port)) {
return option_error_invalid();
}
opt_port = port;

Definition at line 222 of file options/common.c.

222 {
223 if (!value_str || !out_port) {
224 return false;
225 }
226
227 // Use safe integer parsing with range validation
228 if (parse_port(value_str, out_port) != ASCIICHAT_OK) {
229 log_error("Invalid port value '%s'. Port must be a number between 1 and 65535.", value_str);
230 return false;
231 }
232
233 return true;
234}
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.

References ASCIICHAT_OK, log_error, and parse_port().

◆ validate_positive_int_opt()

bool validate_positive_int_opt ( const char * value_str,
int * out_value,
const char * param_name )

Validate a positive integer value.

Internal option parsing helper that validates a string represents a positive integer (> 0). Prints error message on failure.

Parameters
value_strString to validate
out_valueOutput parameter for validated integer
param_nameParameter name for error messages
Returns
true if valid, false otherwise

Example:

int fps;
if (!validate_positive_int_opt(optarg, &fps, "FPS")) {
return option_error_invalid();
}

Definition at line 206 of file options/common.c.

206 {
207 if (!value_str || !out_value) {
208 return false;
209 }
210
211 int val = strtoint_safe(value_str);
212 if (val == INT_MIN || val <= 0) {
213 log_error("Invalid %s value '%s'. %s must be a positive integer.", param_name, value_str, param_name);
214 return false;
215 }
216
217 *out_value = val;
218 return true;
219}

References log_error, and strtoint_safe().

Referenced by parse_height_option(), and parse_width_option().

◆ validate_required_argument()

char * validate_required_argument ( const char * optarg,
char * argbuf,
size_t argbuf_size,
const char * option_name,
asciichat_mode_t mode )

Validate and retrieve required argument for an option.

Wrapper around get_required_argument() that also sets error code on failure. Used for options that must have an argument.

Parameters
optargArgument value from getopt_long
argbufBuffer for storing processed argument
argbuf_sizeSize of argbuf
option_nameName of the option (for error messages)
modeCurrent mode (client/server/mirror) for error messages
Returns
Pointer to processed argument string in argbuf, or NULL on error
Note
On error, prints message to stderr and calls option_error_invalid()

Example:

char argbuf[OPTIONS_BUFF_SIZE];
char *value = validate_required_argument(optarg, argbuf, sizeof(argbuf), "port", MODE_CLIENT);
if (!value) {
return option_error_invalid();
}
char * validate_required_argument(const char *optarg, char *argbuf, size_t argbuf_size, const char *option_name, asciichat_mode_t mode)
Validate and retrieve required argument for an option.

Definition at line 196 of file options/common.c.

197 {
198 char *value = get_required_argument(optarg, argbuf, argbuf_size, option_name, mode);
199 if (!value) {
200 (void)option_error_invalid();
201 }
202 return value;
203}

References get_required_argument().

◆ validate_webcam_index()

bool validate_webcam_index ( const char * value_str,
unsigned short int * out_index )

Validate webcam index using the common device index validator.

Validates webcam index is a non-negative integer. Unlike audio device indices, webcam indices do not support -1 (default).

Parameters
value_strString to validate
out_indexOutput parameter for validated index
Returns
true if valid, false otherwise

Example:

unsigned short int webcam_idx;
if (!validate_webcam_index(optarg, &webcam_idx)) {
return option_error_invalid();
}
opt_webcam_index = webcam_idx;

Definition at line 253 of file options/common.c.

253 {
254 if (!value_str || !out_index) {
255 return false;
256 }
257
258 char error_msg[BUFFER_SIZE_SMALL];
259 int parsed_index = validate_opt_device_index(value_str, error_msg, sizeof(error_msg));
260 if (parsed_index == INT_MIN) {
261 log_error("Invalid webcam index: %s", error_msg);
262 return false;
263 }
264 // Webcam index doesn't support -1 (default), must be >= 0
265 if (parsed_index < 0) {
266 log_error("Invalid webcam index '%s'. Webcam index must be a non-negative integer.", value_str);
267 return false;
268 }
269
270 *out_index = (unsigned short int)parsed_index;
271 return true;
272}
int validate_opt_device_index(const char *value_str, char *error_msg, size_t error_msg_size)
Validate device index (-1 for default, 0+ for specific device).
Definition validation.c:469

References BUFFER_SIZE_SMALL, log_error, and validate_opt_device_index().

Referenced by parse_webcam_index_option().