ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
common.c File Reference

Common utilities and helpers for option parsing. More...

Go to the source code of this file.

Functions

const char * find_similar_option (const char *unknown_opt, const struct option *options)
 Find a similar option name for typo suggestions.
 
int strtoint_safe (const char *str)
 Safely parse string to integer with validation.
 
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 * 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 (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 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 * 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 usage (FILE *desc, asciichat_mode_t mode)
 Print usage information for client, server, or mirror mode.
 

Variables

ASCIICHAT_API unsigned short int opt_width = OPT_WIDTH_DEFAULT
 
ASCIICHAT_API unsigned short int opt_height = OPT_HEIGHT_DEFAULT
 
ASCIICHAT_API bool auto_width = true
 
ASCIICHAT_API bool auto_height = true
 
bool port_explicitly_set_via_flag = false
 
ASCIICHAT_API char opt_address [OPTIONS_BUFF_SIZE] = "localhost"
 
ASCIICHAT_API char opt_address6 [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API char opt_port [OPTIONS_BUFF_SIZE] = "27224"
 
ASCIICHAT_API int opt_max_clients = 10
 
ASCIICHAT_API int opt_compression_level = 1
 
ASCIICHAT_API bool opt_no_compress = false
 
ASCIICHAT_API bool opt_encode_audio = true
 
ASCIICHAT_API int opt_reconnect_attempts = -1
 
ASCIICHAT_API unsigned short int opt_webcam_index = 0
 
ASCIICHAT_API bool opt_webcam_flip = true
 
ASCIICHAT_API bool opt_test_pattern = false
 
ASCIICHAT_API bool opt_no_audio_mixer = false
 
ASCIICHAT_API terminal_color_mode_t opt_color_mode = COLOR_MODE_AUTO
 
ASCIICHAT_API render_mode_t opt_render_mode = RENDER_MODE_FOREGROUND
 
ASCIICHAT_API unsigned short int opt_show_capabilities = 0
 
ASCIICHAT_API unsigned short int opt_force_utf8 = 0
 
ASCIICHAT_API unsigned short int opt_audio_enabled = 0
 
ASCIICHAT_API int opt_microphone_index = -1
 
ASCIICHAT_API int opt_speakers_index = -1
 
ASCIICHAT_API unsigned short int opt_audio_analysis_enabled = 0
 
ASCIICHAT_API unsigned short int opt_audio_no_playback = 0
 
ASCIICHAT_API unsigned short int opt_stretch = 0
 
ASCIICHAT_API unsigned short int opt_quiet = 0
 
ASCIICHAT_API unsigned short int opt_verbose_level = 0
 
ASCIICHAT_API unsigned short int opt_snapshot_mode = 0
 
ASCIICHAT_API float opt_snapshot_delay = SNAPSHOT_DELAY_DEFAULT
 
ASCIICHAT_API unsigned short int opt_strip_ansi = 0
 
ASCIICHAT_API char opt_log_file [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API log_level_t opt_log_level = LOG_DEBUG
 
ASCIICHAT_API unsigned short int opt_encrypt_enabled = 0
 
ASCIICHAT_API char opt_encrypt_key [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API char opt_password [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API char opt_encrypt_keyfile [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API unsigned short int opt_no_encrypt = 0
 
ASCIICHAT_API char opt_server_key [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API char opt_client_keys [OPTIONS_BUFF_SIZE] = ""
 
ASCIICHAT_API palette_type_t opt_palette_type = PALETTE_STANDARD
 
ASCIICHAT_API char opt_palette_custom [256] = ""
 
ASCIICHAT_API bool opt_palette_custom_set = false
 
const float weight_red = 0.2989f
 
const float weight_green = 0.5866f
 Green weight for luminance calculation.
 
const float weight_blue = 0.1145f
 Blue weight for luminance calculation.
 
unsigned short int RED [256]
 Red channel lookup table.
 
unsigned short int GREEN [256]
 Green channel lookup table.
 
unsigned short int BLUE [256]
 Blue channel lookup table.
 
unsigned short int GRAY [256]
 Grayscale lookup table.
 

Detailed Description

Common utilities and helpers for option parsing.

Shared helper functions, validators, and global variables used by all option parsing modules (client.c, server.c, mirror.c).

Definition in file options/common.c.

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);
}
#define SAFE_SNPRINTF(buffer, buffer_size,...)
Definition common.h:412
@ ASCIICHAT_OK
Definition error_codes.h:48
#define log_debug(...)
Log a DEBUG message.
#define OPTIONS_BUFF_SIZE
Buffer size for option string values.
Definition options.h:176
ASCIICHAT_API char opt_encrypt_key[OPTIONS_BUFF_SIZE]
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 165 of file options/common.c.

165 {
166 // Use expand_path utility to resolve ~/.ssh/id_ed25519
167 char *full_path = expand_path("~/.ssh/id_ed25519");
168 if (!full_path) {
169 return SET_ERRNO(ERROR_CONFIG, "Could not expand SSH key path");
170 }
171
172 // Check if the Ed25519 private key file exists
173 struct stat st;
174 bool found = (stat(full_path, &st) == 0 && S_ISREG(st.st_mode));
175
176 if (found) {
177 SAFE_SNPRINTF(key_path, path_size, "%s", full_path);
178 log_debug("Found default SSH key: %s", full_path);
179 SAFE_FREE(full_path);
180 return ASCIICHAT_OK;
181 }
182
183 (void)fprintf(stderr, "No Ed25519 SSH key found at %s\n", full_path);
184 SAFE_FREE(full_path);
185 return SET_ERRNO(
187 "Only Ed25519 keys are supported (modern, secure, fast). Generate a new key with: ssh-keygen -t ed25519");
188}
#define SAFE_FREE(ptr)
Definition common.h:320
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_CRYPTO_KEY
Definition error_codes.h:89
@ ERROR_CONFIG
Definition error_codes.h:54
char * expand_path(const char *path)
Expand path with tilde (~) support.
Definition path.c:183

References ASCIICHAT_OK, ERROR_CONFIG, ERROR_CRYPTO_KEY, expand_path(), log_debug, 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);
}
const char * find_similar_option(const char *unknown_opt, const struct option *options)
Find a similar option name for typo suggestions.

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

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

References levenshtein(), and LEVENSHTEIN_SUGGESTION_THRESHOLD.

◆ 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();
}
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:428
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 215 of file options/common.c.

216 {
217 // Check if opt_value is NULL or empty
218 if (!opt_value || strlen(opt_value) == 0) {
219 goto error;
220 }
221
222 // Check if getopt_long returned the option name itself as the argument
223 // This happens when a long option requiring an argument is at the end of argv
224 if (opt_value && option_name && strcmp(opt_value, option_name) == 0) {
225 goto error;
226 }
227
228 // Process the argument normally
229 char *value_str = strip_equals_prefix(opt_value, buffer, buffer_size);
230 if (!value_str) {
231 goto error;
232 }
233
234 return value_str;
235
236error:
237 (void)fprintf(stderr, "%s: option '--%s' requires an argument\n",
238 mode == MODE_SERVER ? "server" : (mode == MODE_MIRROR ? "mirror" : "client"), option_name);
239 (void)fflush(stderr);
240 return NULL; // Signal error to caller
241}
@ MODE_SERVER
Server mode - network server options.
Definition options.h:427
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network)
Definition options.h:429
char * strip_equals_prefix(const char *opt_value, char *buffer, size_t buffer_size)
Strip equals sign prefix from option argument.

References 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 361 of file options/common.c.

361 {
362 if (!value_str || !opts) {
363 return ERROR_INVALID_PARAM;
364 }
365
366 if (strcmp(value_str, "auto") == 0) {
368 } else if (strcmp(value_str, "none") == 0 || strcmp(value_str, "mono") == 0) {
370 } else if (strcmp(value_str, "16") == 0 || strcmp(value_str, "16color") == 0) {
372 } else if (strcmp(value_str, "256") == 0 || strcmp(value_str, "256color") == 0) {
374 } else if (strcmp(value_str, "truecolor") == 0 || strcmp(value_str, "24bit") == 0) {
376 } else {
377 (void)fprintf(stderr, "Error: Invalid color mode '%s'. Valid modes: auto, none, 16, 256, truecolor\n", value_str);
378 return ERROR_INVALID_PARAM;
379 }
380
381 return ASCIICHAT_OK;
382}
@ ERROR_INVALID_PARAM
#define COLOR_MODE_16_COLOR
16-color mode (full name)
Definition options.h:159
#define COLOR_MODE_256_COLOR
256-color mode (full name)
Definition options.h:161
#define COLOR_MODE_TRUECOLOR
24-bit truecolor mode
Definition options.h:162
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:156
#define COLOR_MODE_NONE
Monochrome mode.
Definition options.h:157
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor)
Definition options.h:507

References ASCIICHAT_OK, options_state::color_mode, COLOR_MODE_16_COLOR, COLOR_MODE_256_COLOR, COLOR_MODE_AUTO, COLOR_MODE_NONE, COLOR_MODE_TRUECOLOR, and ERROR_INVALID_PARAM.

◆ 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 465 of file options/common.c.

465 {
466 if (!opts) {
467 return ERROR_INVALID_PARAM;
468 }
469
470 int height_val;
471 if (!validate_positive_int_opt(value_str, &height_val, "height")) {
472 return ERROR_INVALID_PARAM;
473 }
474
475 opts->height = (unsigned short int)height_val;
476 opts->auto_height = false;
477
478 return ASCIICHAT_OK;
479}
bool validate_positive_int_opt(const char *value_str, int *out_value, const char *param_name)
Validate a positive integer value.
bool auto_height
Auto-detect height from terminal.
Definition options.h:457
unsigned short int height
Terminal height in characters.
Definition options.h:455

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 513 of file options/common.c.

513 {
514 if (!opts) {
515 return ERROR_INVALID_PARAM;
516 }
517
518 char error_msg[256];
519 int log_level = validate_opt_log_level(value_str, error_msg, sizeof(error_msg));
520
521 if (log_level == -1) {
522 (void)fprintf(stderr, "Error: %s\n", error_msg);
523 return ERROR_INVALID_PARAM;
524 }
525
526 opts->log_level = (log_level_t)log_level;
527
528 return ASCIICHAT_OK;
529}
log_level_t
Logging levels enumeration.
Definition log/logging.h:59
int validate_opt_log_level(const char *value_str, char *error_msg, size_t error_msg_size)
Validate log level string.
Definition validation.c:190
log_level_t log_level
Log level threshold.
Definition options.h:536

References ASCIICHAT_OK, ERROR_INVALID_PARAM, 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 430 of file options/common.c.

430 {
431 if (!value_str || !opts) {
432 return ERROR_INVALID_PARAM;
433 }
434
435 if (strlen(value_str) >= sizeof(opts->palette_custom)) {
436 (void)fprintf(stderr, "Invalid palette-chars: too long (%zu chars, max %zu)\n", strlen(value_str),
437 sizeof(opts->palette_custom) - 1);
438 return ERROR_INVALID_PARAM;
439 }
440
441 SAFE_STRNCPY(opts->palette_custom, value_str, sizeof(opts->palette_custom));
442 opts->palette_custom[sizeof(opts->palette_custom) - 1] = '\0';
443 opts->palette_custom_set = true;
445
446 return ASCIICHAT_OK;
447}
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:358
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:96
char palette_custom[256]
Custom palette characters.
Definition options.h:582
bool palette_custom_set
True if custom palette was set.
Definition options.h:583
palette_type_t palette_type
Selected palette type.
Definition options.h:581

References ASCIICHAT_OK, ERROR_INVALID_PARAM, options_state::palette_custom, 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 404 of file options/common.c.

404 {
405 if (!value_str || !opts) {
406 return ERROR_INVALID_PARAM;
407 }
408
409 if (strcmp(value_str, "standard") == 0) {
411 } else if (strcmp(value_str, "blocks") == 0) {
413 } else if (strcmp(value_str, "digital") == 0) {
415 } else if (strcmp(value_str, "minimal") == 0) {
417 } else if (strcmp(value_str, "cool") == 0) {
419 } else if (strcmp(value_str, "custom") == 0) {
421 } else {
422 (void)fprintf(stderr, "Invalid palette '%s'. Valid palettes: standard, blocks, digital, minimal, cool, custom\n",
423 value_str);
424 return ERROR_INVALID_PARAM;
425 }
426
427 return ASCIICHAT_OK;
428}
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:88
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:94
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:86
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:90
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:92

References ASCIICHAT_OK, ERROR_INVALID_PARAM, 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 384 of file options/common.c.

384 {
385 if (!value_str || !opts) {
386 return ERROR_INVALID_PARAM;
387 }
388
389 if (strcmp(value_str, "foreground") == 0 || strcmp(value_str, "fg") == 0) {
391 } else if (strcmp(value_str, "background") == 0 || strcmp(value_str, "bg") == 0) {
393 } else if (strcmp(value_str, "half-block") == 0 || strcmp(value_str, "halfblock") == 0) {
395 } else {
396 (void)fprintf(stderr, "Error: Invalid render mode '%s'. Valid modes: foreground, background, half-block\n",
397 value_str);
398 return ERROR_INVALID_PARAM;
399 }
400
401 return ASCIICHAT_OK;
402}
@ RENDER_MODE_FOREGROUND
Foreground colors only (text color)
Definition terminal.h:469
@ RENDER_MODE_BACKGROUND
Background colors (block colors)
Definition terminal.h:471
@ RENDER_MODE_HALF_BLOCK
Unicode half-block characters (mixed foreground/background)
Definition terminal.h:473
render_mode_t render_mode
Render mode (foreground/background/half-block)
Definition options.h:508

References ASCIICHAT_OK, ERROR_INVALID_PARAM, options_state::render_mode, RENDER_MODE_BACKGROUND, RENDER_MODE_FOREGROUND, and RENDER_MODE_HALF_BLOCK.

◆ 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 496 of file options/common.c.

496 {
497 if (!value_str || !opts) {
498 return ERROR_INVALID_PARAM;
499 }
500
501 char *endptr;
502 float delay = strtof(value_str, &endptr);
503 if (endptr == value_str || *endptr != '\0' || delay < 0.0f) {
504 (void)fprintf(stderr, "Invalid snapshot delay '%s'. Must be a non-negative number.\n", value_str);
505 return ERROR_INVALID_PARAM;
506 }
507
508 opts->snapshot_delay = delay;
509
510 return ASCIICHAT_OK;
511}
double snapshot_delay
Snapshot delay in seconds.
Definition options.h:533

References ASCIICHAT_OK, ERROR_INVALID_PARAM, 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 481 of file options/common.c.

481 {
482 if (!opts) {
483 return ERROR_INVALID_PARAM;
484 }
485
486 unsigned short int index_val;
487 if (!validate_webcam_index(value_str, &index_val)) {
488 return ERROR_INVALID_PARAM;
489 }
490
491 opts->webcam_index = index_val;
492
493 return ASCIICHAT_OK;
494}
bool validate_webcam_index(const char *value_str, unsigned short int *out_index)
Validate webcam index using the common device index validator.
unsigned short int webcam_index
Webcam device index (0 = first)
Definition options.h:499

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 449 of file options/common.c.

449 {
450 if (!opts) {
451 return ERROR_INVALID_PARAM;
452 }
453
454 int width_val;
455 if (!validate_positive_int_opt(value_str, &width_val, "width")) {
456 return ERROR_INVALID_PARAM;
457 }
458
459 opts->width = (unsigned short int)width_val;
460 opts->auto_width = false;
461
462 return ASCIICHAT_OK;
463}
unsigned short int width
Terminal width in characters.
Definition options.h:454
bool auto_width
Auto-detect width from terminal.
Definition options.h:456

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

◆ 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 244 of file options/common.c.

244 {
245 char *password_buf = SAFE_MALLOC(PASSWORD_MAX_LEN, char *);
246 if (!password_buf) {
247 return NULL;
248 }
249
250 if (prompt_password_simple(prompt, password_buf, PASSWORD_MAX_LEN) != 0) {
251 SAFE_FREE(password_buf);
252 return NULL;
253 }
254
255 return password_buf; // Caller must free
256}
#define SAFE_MALLOC(size, cast)
Definition common.h:208
#define PASSWORD_MAX_LEN
Buffer size for password input.
Definition password.h:30
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:46

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 195 of file options/common.c.

195 {
196 if (!opt_value)
197 return NULL;
198
199 SAFE_SNPRINTF(buffer, buffer_size, "%s", opt_value);
200 char *value_str = buffer;
201 if (value_str[0] == '=') {
202 value_str++; // Skip the equals sign
203 }
204
205 // Return NULL for empty strings (treat as missing argument)
206 if (strlen(value_str) == 0) {
207 return NULL;
208 }
209
210 return value_str;
211}

References SAFE_SNPRINTF.

Referenced by get_required_argument().

◆ validate_fps_opt()

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

Validate FPS value (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 (1-144)

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

127 {
128 if (!value_str || !out_fps) {
129 return false;
130 }
131
132 int fps_val = strtoint_safe(value_str);
133 if (fps_val == INT_MIN || fps_val < 1 || fps_val > 144) {
134 (void)fprintf(stderr, "Invalid FPS value '%s'. FPS must be between 1 and 144.\n", value_str);
135 return false;
136 }
137
138 *out_fps = fps_val;
139 return true;
140}
int strtoint_safe(const char *str)
Safely parse string to integer with validation.

References strtoint_safe().

◆ 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;
unsigned short uint16_t
Definition common.h:57
ASCIICHAT_API char opt_port[OPTIONS_BUFF_SIZE]
bool validate_port_opt(const char *value_str, uint16_t *out_port)
Validate port number (1-65535)

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

112 {
113 if (!value_str || !out_port) {
114 return false;
115 }
116
117 // Use safe integer parsing with range validation
118 if (parse_port(value_str, out_port) != ASCIICHAT_OK) {
119 (void)fprintf(stderr, "Invalid port value '%s'. Port must be a number between 1 and 65535.\n", value_str);
120 return false;
121 }
122
123 return true;
124}
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.
Definition parsing.c:221

References ASCIICHAT_OK, 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 96 of file options/common.c.

96 {
97 if (!value_str || !out_value) {
98 return false;
99 }
100
101 int val = strtoint_safe(value_str);
102 if (val == INT_MIN || val <= 0) {
103 (void)fprintf(stderr, "Invalid %s value '%s'. %s must be a positive integer.\n", param_name, value_str, param_name);
104 return false;
105 }
106
107 *out_value = val;
108 return true;
109}

References 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 86 of file options/common.c.

87 {
88 char *value = get_required_argument(optarg, argbuf, argbuf_size, option_name, mode);
89 if (!value) {
90 (void)option_error_invalid();
91 }
92 return value;
93}

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;
ASCIICHAT_API unsigned short int opt_webcam_index

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

143 {
144 if (!value_str || !out_index) {
145 return false;
146 }
147
148 char error_msg[256];
149 int parsed_index = validate_opt_device_index(value_str, error_msg, sizeof(error_msg));
150 if (parsed_index == INT_MIN) {
151 (void)fprintf(stderr, "Invalid webcam index: %s\n", error_msg);
152 return false;
153 }
154 // Webcam index doesn't support -1 (default), must be >= 0
155 if (parsed_index < 0) {
156 (void)fprintf(stderr, "Invalid webcam index '%s'. Webcam index must be a non-negative integer.\n", value_str);
157 return false;
158 }
159
160 *out_index = (unsigned short int)parsed_index;
161 return true;
162}
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:422

References validate_opt_device_index().

Referenced by parse_webcam_index_option().

Variable Documentation

◆ auto_height

ASCIICHAT_API bool auto_height = true

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

◆ auto_width

ASCIICHAT_API bool auto_width = true

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

◆ opt_address

ASCIICHAT_API char opt_address[OPTIONS_BUFF_SIZE] = "localhost"

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

◆ opt_address6

ASCIICHAT_API char opt_address6[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_audio_analysis_enabled

ASCIICHAT_API unsigned short int opt_audio_analysis_enabled = 0

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

◆ opt_audio_enabled

ASCIICHAT_API unsigned short int opt_audio_enabled = 0

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

◆ opt_audio_no_playback

ASCIICHAT_API unsigned short int opt_audio_no_playback = 0

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

◆ opt_client_keys

ASCIICHAT_API char opt_client_keys[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_color_mode

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

◆ opt_compression_level

ASCIICHAT_API int opt_compression_level = 1

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

◆ opt_encode_audio

ASCIICHAT_API bool opt_encode_audio = true

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

◆ opt_encrypt_enabled

ASCIICHAT_API unsigned short int opt_encrypt_enabled = 0

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

◆ opt_encrypt_key

ASCIICHAT_API char opt_encrypt_key[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_encrypt_keyfile

ASCIICHAT_API char opt_encrypt_keyfile[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_force_utf8

ASCIICHAT_API unsigned short int opt_force_utf8 = 0

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

◆ opt_height

ASCIICHAT_API unsigned short int opt_height = OPT_HEIGHT_DEFAULT

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

◆ opt_log_file

ASCIICHAT_API char opt_log_file[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_log_level

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

◆ opt_max_clients

ASCIICHAT_API int opt_max_clients = 10

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

◆ opt_microphone_index

ASCIICHAT_API int opt_microphone_index = -1

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

◆ opt_no_audio_mixer

ASCIICHAT_API bool opt_no_audio_mixer = false

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

◆ opt_no_compress

ASCIICHAT_API bool opt_no_compress = false

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

◆ opt_no_encrypt

ASCIICHAT_API unsigned short int opt_no_encrypt = 0

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

◆ opt_palette_custom

ASCIICHAT_API char opt_palette_custom[256] = ""

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

◆ opt_palette_custom_set

ASCIICHAT_API bool opt_palette_custom_set = false

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

◆ opt_palette_type

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

◆ opt_password

ASCIICHAT_API char opt_password[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_port

ASCIICHAT_API char opt_port[OPTIONS_BUFF_SIZE] = "27224"

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

◆ opt_quiet

ASCIICHAT_API unsigned short int opt_quiet = 0

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

◆ opt_reconnect_attempts

ASCIICHAT_API int opt_reconnect_attempts = -1

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

◆ opt_render_mode

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

◆ opt_server_key

ASCIICHAT_API char opt_server_key[OPTIONS_BUFF_SIZE] = ""

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

◆ opt_show_capabilities

ASCIICHAT_API unsigned short int opt_show_capabilities = 0

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

◆ opt_snapshot_delay

ASCIICHAT_API float opt_snapshot_delay = SNAPSHOT_DELAY_DEFAULT

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

◆ opt_snapshot_mode

ASCIICHAT_API unsigned short int opt_snapshot_mode = 0

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

◆ opt_speakers_index

ASCIICHAT_API int opt_speakers_index = -1

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

◆ opt_stretch

ASCIICHAT_API unsigned short int opt_stretch = 0

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

◆ opt_strip_ansi

ASCIICHAT_API unsigned short int opt_strip_ansi = 0

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

◆ opt_test_pattern

ASCIICHAT_API bool opt_test_pattern = false

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

◆ opt_verbose_level

ASCIICHAT_API unsigned short int opt_verbose_level = 0

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

◆ opt_webcam_flip

ASCIICHAT_API bool opt_webcam_flip = true

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

◆ opt_webcam_index

ASCIICHAT_API unsigned short int opt_webcam_index = 0

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

◆ opt_width

ASCIICHAT_API unsigned short int opt_width = OPT_WIDTH_DEFAULT

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

◆ port_explicitly_set_via_flag

bool port_explicitly_set_via_flag = false

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