ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
options/common.c
Go to the documentation of this file.
1
9
10/* Storage capacities in elements; text buffers include the terminating NUL. */
11#define OPTIONS_COMMON_BUFFER_BUFFER_SIZE 256
12#define OPTIONS_COMMON_SUGGESTION_BUFFER_SIZE 256
13
14#include <options/enums.h>
15#include <options/common.h>
17#include <libflags/builder.h>
18#include <libflags/help.h>
19#include <libflags/format.h>
20#include <libflags/values.h>
21#include <errno.h>
22#include <limits.h>
23#include <stdlib.h>
24
26#include <ascii-chat/common.h>
27#include <ascii-chat/log/log.h>
28#include <options/validation.h>
29#include <options/levenshtein.h>
30
31int strtoint_safe(const char *str) {
32 if (!str || !*str)
33 return INT_MIN;
34 errno = 0;
35 char *end = NULL;
36 long value = strtol(str, &end, 10);
37 if (errno == ERANGE || end == str || *end != '\0' || value < INT_MIN || value > INT_MAX)
38 return INT_MIN;
39 return (int)value;
40}
41
42bool validate_port_opt(const char *value_str, uint16_t *out_port) {
43 if (!out_port || !value_str)
44 return false;
45 return flags_parse_port(value_str, out_port);
46}
47
48bool validate_fps_opt(const char *value_str, int *out_fps) {
49 int32_t fps = 0;
50 if (!out_fps || !flags_parse_int32(value_str, &fps) || fps <= 0)
51 return false;
52 *out_fps = fps;
53 return true;
54}
55
57 switch (type) {
58 case OPTION_TYPE_INT: return "INTEGER";
59 case OPTION_TYPE_DOUBLE: return "NUMBER";
60 case OPTION_TYPE_STRING: return "STRING";
61 case OPTION_TYPE_CALLBACK: return "VALUE";
62 case OPTION_TYPE_BOOL: return "BOOLEAN";
63 default: return "";
64 }
65}
66
67int options_format_default_value(option_type_t type, const void *default_value, char *buf, size_t bufsize) {
68 if (!default_value || !buf || !bufsize)
69 return 0;
70 switch (type) {
71 case OPTION_TYPE_BOOL: return safe_snprintf(buf, bufsize, "%s", *(const bool *)default_value ? OPT_VALUE_TRUE : OPT_VALUE_FALSE);
72 case OPTION_TYPE_INT: return safe_snprintf(buf, bufsize, "%d", *(const int *)default_value);
73 case OPTION_TYPE_STRING: return safe_snprintf(buf, bufsize, "%s", *(const char *const *)default_value);
74 case OPTION_TYPE_DOUBLE: return safe_snprintf(buf, bufsize, "%.2f", *(const double *)default_value);
75 default: return 0;
76 }
77}
78
79const char *find_similar_option_with_mode(const char *unknown_opt, const options_config_t *config, option_mode_bitmask_t current_mode_bitmask) {
80 if (!unknown_opt || !config)
81 return NULL;
82 const char *name = unknown_opt + (unknown_opt[0] == '-' ? (unknown_opt[1] == '-' ? 2 : 1) : 0);
83 const option_descriptor_t *best = NULL;
84 size_t distance = SIZE_MAX;
85 for (size_t i = 0; i < config->num_descriptors; ++i) {
86 const option_descriptor_t *candidate = &config->descriptors[i];
87 if (!candidate->long_name)
88 continue;
89 size_t d = levenshtein(name, candidate->long_name);
90 if (d < distance) { distance = d; best = candidate; }
91 }
92 if (!best || distance > LEVENSHTEIN_SUGGESTION_THRESHOLD)
93 return NULL;
94 static char suggestion[OPTIONS_COMMON_SUGGESTION_BUFFER_SIZE];
95 safe_snprintf(suggestion, sizeof(suggestion), "Did you mean '--%s'?", best->long_name);
96 (void)current_mode_bitmask;
97 return suggestion;
98}
99#include <libplatform/terminal.h>
100#include <sys/stat.h>
103#include <ascii-chat/util/path.h>
105
106#include <limits.h>
107#include <stdio.h>
108#include <stdlib.h>
109#include <string.h>
110#include <sys/stat.h>
111
112// ============================================================================
113// Helper Functions
114// ============================================================================
115
116// Helper to format all available modes for an option (used in error messages)
117// Returns comma-separated list like "server, client, mirror" or "global options"
119 static char buffer[OPTIONS_COMMON_BUFFER_BUFFER_SIZE];
120 buffer[0] = '\0';
121
122 bool first = true;
123
124 // Check if it's a global/binary option
125 if (mode_bitmask & OPTION_MODE_BINARY) {
126 SAFE_SNPRINTF(buffer, sizeof(buffer), "global options");
127 return buffer;
128 }
129
130 // Build comma-separated list of modes (ordered: default, client, server, mirror, discovery-service)
131 if (mode_bitmask & OPTION_MODE_DISCOVERY) {
132 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sdefault", first ? "" : ", ");
133 first = false;
134 }
135 if (mode_bitmask & OPTION_MODE_CLIENT) {
136 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sclient", first ? "" : ", ");
137 first = false;
138 }
139 if (mode_bitmask & OPTION_MODE_SERVER) {
140 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sserver", first ? "" : ", ");
141 first = false;
142 }
143 if (mode_bitmask & OPTION_MODE_MIRROR) {
144 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%smirror", first ? "" : ", ");
145 first = false;
146 }
147 if (mode_bitmask & OPTION_MODE_DISCOVERY_SVC) {
148 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sdiscovery-service", first ? "" : ", ");
149 first = false;
150 }
151
152 if (mode_bitmask & OPTION_MODE_MCP_SERVER) {
153 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%smcp-server", first ? "" : ", ");
154 first = false;
155 }
156 if (mode_bitmask & OPTION_MODE_API_SERVER) {
157 safe_snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%sapi-server", first ? "" : ", ");
158 }
159
160 // Fallback if no modes matched
161 if (buffer[0] == '\0') {
162 SAFE_SNPRINTF(buffer, sizeof(buffer), "unknown mode");
163 }
164
165 return buffer;
166}
167
168// Forward declaration for get_required_argument (defined later in file)
169char *get_required_argument(const char *opt_value, char *buffer, size_t buffer_size, const char *option_name, asciichat_mode_t mode);
170
171// Validate and retrieve required argument for an option
172char *validate_required_argument(const char *optarg, char *argbuf, size_t argbuf_size, const char *option_name, asciichat_mode_t mode) {
173 char *value = get_required_argument(optarg, argbuf, argbuf_size, option_name, mode);
174 if (!value) {
175 (void)option_error_invalid();
176 }
177 return value;
178}
179
180// Detect default SSH key path for the current user
181asciichat_error_t detect_default_ssh_key(char *key_path, size_t path_size) {
182 // Use expand_path utility to resolve ~/.ssh/id_ed25519
183 char *full_path = expand_path("~/.ssh/id_ed25519");
184 if (!full_path) {
185 return SET_ERRNO(ERROR_CONFIG, "Could not expand SSH key path");
186 }
187
188 // Check if the Ed25519 private key file exists
189 struct stat st;
190 bool found = (stat(full_path, &st) == 0 && S_ISREG(st.st_mode));
191
192 if (found) {
193 SAFE_SNPRINTF(key_path, path_size, "%s", full_path);
194 log_debug("Found default SSH key: %s", full_path);
195 SAFE_FREE(full_path);
196 return ASCIICHAT_OK;
197 }
198
199 log_error("No Ed25519 SSH key found at %s", full_path);
200 SAFE_FREE(full_path);
201 return SET_ERRNO(ERROR_CRYPTO_KEY, "Only Ed25519 keys are supported (modern, secure, fast). Generate a new key with: ssh-keygen -t ed25519");
202}
203
204// ============================================================================
205// Argument Processing Helpers
206// ============================================================================
207
208// Helper function to strip equals sign from optarg if present
209char *strip_equals_prefix(const char *opt_value, char *buffer, size_t buffer_size) {
210 if (!opt_value)
211 return NULL;
212
213 SAFE_SNPRINTF(buffer, buffer_size, "%s", opt_value);
214 char *value_str = buffer;
215 if (value_str[0] == '=') {
216 value_str++; // Skip the equals sign
217 }
218
219 // Return NULL for empty strings (treat as missing argument)
220 if (strlen(value_str) == 0) {
221 return NULL;
222 }
223
224 return value_str;
225}
226
227// Helper function to handle required arguments with consistent error messages
228// Returns NULL on error (caller should check and return error code)
229char *get_required_argument(const char *opt_value, char *buffer, size_t buffer_size, const char *option_name, asciichat_mode_t mode) {
230 // Check if opt_value is NULL or empty
231 if (!opt_value || strlen(opt_value) == 0) {
232 goto error;
233 }
234
235 // Check if getopt_long returned the option name itself as the argument
236 // This happens when a long option requiring an argument is at the end of argv
237 if (opt_value && option_name && strcmp(opt_value, option_name) == 0) {
238 goto error;
239 }
240
241 // Process the argument normally
242 char *value_str = strip_equals_prefix(opt_value, buffer, buffer_size);
243 if (!value_str) {
244 goto error;
245 }
246
247 return value_str;
248
249error:
250 (void)0;
251 const char *mode_name = (mode == MODE_SERVER ? "server" : (mode == MODE_MIRROR ? "mirror" : "client"));
252 log_error("%s: option '--%s' requires an argument", mode_name, option_name);
253 return NULL; // Signal error to caller
254}
255
256// Read password from stdin with prompt (returns allocated string or NULL on error)
257char *read_password_from_stdin(const char *prompt) {
258 char *password_buf = SAFE_MALLOC(PASSWORD_MAX_LEN, char *);
259 if (!password_buf) {
260 return NULL;
261 }
262
263 if (prompt_password_simple(prompt, password_buf, PASSWORD_MAX_LEN) != 0) {
264 SAFE_FREE(password_buf);
265 return NULL;
266 }
267
268 return password_buf; // Caller must free
269}
270
271// ============================================================================
272// Global Variable Definitions
273// ============================================================================
274// Removed: All opt_* global variables moved to RCU options_t struct
275// Access these via GET_OPTION(field_name) for thread-safe lock-free reads
276// Modify via options_set_*() functions for thread-safe updates
277// ============================================================================
278
279// These flags are used during parsing, not part of RCU options
281
282// Default weights; must add up to 1.0
283ASCIICHAT_API const float weight_red = 0.2989f;
284ASCIICHAT_API const float weight_green = 0.5866f;
285ASCIICHAT_API const float weight_blue = 0.1145f;
286
287// Color channel lookup tables (used by image.c for palette precomputation)
288// Size must match ASCII_LUMINANCE_LEVELS (256) from ascii.h
289unsigned short int RED[256];
290unsigned short int GREEN[256];
291unsigned short int BLUE[256];
292unsigned short int GRAY[256];
293
294// ============================================================================
295// Shared Option Parsers (Client + Mirror Common Options)
296// ============================================================================
297
299 if (!value_str || !opts) {
300 return ERROR_INVALID_PARAM;
301 }
302
303 if (strcmp(value_str, OPT_VALUE_AUTO) == 0 || strcmp(value_str, "a") == 0) {
305 } else if (strcmp(value_str, OPT_COLOR_MODE_NONE) == 0 || strcmp(value_str, OPT_COLOR_MODE_MONO) == 0) {
307 } else if (strcmp(value_str, OPT_COLOR_MODE_16) == 0 || strcmp(value_str, "16color") == 0 || strcmp(value_str, "ansi") == 0) {
309 } else if (strcmp(value_str, OPT_COLOR_MODE_256) == 0 || strcmp(value_str, "256color") == 0) {
311 } else if (strcmp(value_str, OPT_COLOR_MODE_TRUECOLOR) == 0 || strcmp(value_str, "24bit") == 0 || strcmp(value_str, "tc") == 0 || strcmp(value_str, "rgb") == 0 ||
312 strcmp(value_str, OPT_VALUE_TRUE) == 0) {
314 } else {
315 log_error("Invalid color mode '%s'. Valid modes: auto, none, 16, 256, truecolor", value_str);
316 return ERROR_INVALID_PARAM;
317 }
318
319 return ASCIICHAT_OK;
320}
321
323 if (!value_str || !opts) {
324 return ERROR_INVALID_PARAM;
325 }
326
327 if (strcmp(value_str, OPT_RENDER_MODE_FOREGROUND) == 0 || strcmp(value_str, OPT_RENDER_MODE_FG) == 0) {
328 opts->render_mode = RENDER_MODE_FOREGROUND;
329 } else if (strcmp(value_str, OPT_RENDER_MODE_BACKGROUND) == 0 || strcmp(value_str, OPT_RENDER_MODE_BG) == 0) {
330 opts->render_mode = RENDER_MODE_BACKGROUND;
331 } else if (strcmp(value_str, OPT_RENDER_MODE_HALF_BLOCK) == 0 || strcmp(value_str, "halfblock") == 0) {
332 opts->render_mode = RENDER_MODE_HALF_BLOCK;
333 } else {
334 log_error("Invalid render mode '%s'. Valid modes: foreground, background, half-block", value_str);
335 return ERROR_INVALID_PARAM;
336 }
337
338 return ASCIICHAT_OK;
339}
340
341asciichat_error_t parse_palette_option(const char *value_str, options_t *opts) {
342 if (!value_str || !opts) {
343 return ERROR_INVALID_PARAM;
344 }
345
346 if (strcmp(value_str, OPT_PALETTE_STANDARD) == 0) {
348 } else if (strcmp(value_str, OPT_PALETTE_BLOCKS) == 0) {
350 } else if (strcmp(value_str, OPT_PALETTE_DIGITAL) == 0) {
352 } else if (strcmp(value_str, OPT_PALETTE_MINIMAL) == 0) {
354 } else if (strcmp(value_str, OPT_PALETTE_COOL) == 0) {
356 } else if (strcmp(value_str, OPT_PALETTE_CUSTOM) == 0) {
358 } else {
359 log_error("Invalid palette '%s'. Valid palettes: standard, blocks, digital, minimal, cool, custom", value_str);
360 return ERROR_INVALID_PARAM;
361 }
362
363 return ASCIICHAT_OK;
364}
365
367 if (!value_str || !opts) {
368 return ERROR_INVALID_PARAM;
369 }
370
371 if (strlen(value_str) >= sizeof(opts->palette_custom)) {
372 log_error("Invalid palette-chars: too long (%zu chars, max %zu)", strlen(value_str), sizeof(opts->palette_custom) - 1);
373 return ERROR_INVALID_PARAM;
374 }
375
376 SAFE_STRNCPY(opts->palette_custom, value_str, sizeof(opts->palette_custom));
377 opts->palette_custom[sizeof(opts->palette_custom) - 1] = '\0';
378 opts->palette_custom_set = true;
380
381 return ASCIICHAT_OK;
382}
383
384asciichat_error_t parse_width_option(const char *value_str, options_t *opts) {
385 if (!opts) {
386 return ERROR_INVALID_PARAM;
387 }
388
389 int32_t width_val;
390 if (!flags_parse_int32(value_str, &width_val) || width_val <= 0) {
391 log_error("Invalid width value '%s'. Width must be a positive integer.", value_str ? value_str : "(null)");
392 return ERROR_INVALID_PARAM;
393 }
394
395 opts->width = width_val;
396 opts->auto_width = false;
397
398 return ASCIICHAT_OK;
399}
400
401asciichat_error_t parse_height_option(const char *value_str, options_t *opts) {
402 if (!opts) {
403 return ERROR_INVALID_PARAM;
404 }
405
406 int32_t height_val;
407 if (!flags_parse_int32(value_str, &height_val) || height_val <= 0) {
408 log_error("Invalid height value '%s'. Height must be a positive integer.", value_str ? value_str : "(null)");
409 return ERROR_INVALID_PARAM;
410 }
411
412 opts->height = height_val;
413 opts->auto_height = false;
414
415 return ASCIICHAT_OK;
416}
417
419 if (!opts) {
420 return ERROR_INVALID_PARAM;
421 }
422
423 int32_t index_val;
424 if (!flags_parse_int32(value_str, &index_val) || index_val < 0 || index_val > USHRT_MAX) {
425 log_error("Invalid webcam index '%s'. Webcam index must be a non-negative integer.", value_str ? value_str : "(null)");
426 return ERROR_INVALID_PARAM;
427 }
428
429 opts->webcam_index = (unsigned short int)index_val;
430
431 return ASCIICHAT_OK;
432}
433
435 if (!value_str || !opts) {
436 return ERROR_INVALID_PARAM;
437 }
438
439 char *endptr;
440 float delay = strtof(value_str, &endptr);
441 if (endptr == value_str || *endptr != '\0' || delay < 0.0f) {
442 log_error("Invalid snapshot delay '%s'. Must be a non-negative number.", value_str);
443 return ERROR_INVALID_PARAM;
444 }
445
446 opts->snapshot_delay = delay;
447
448 return ASCIICHAT_OK;
449}
450
452 if (!opts) {
453 return ERROR_INVALID_PARAM;
454 }
455
456 char error_msg[BUFFER_SIZE_SMALL];
457 int log_level = validate_opt_log_level(value_str, error_msg, sizeof(error_msg));
458
459 if (log_level == -1) {
460 log_error("%s", error_msg);
461 return ERROR_INVALID_PARAM;
462 }
463
464 opts->log_level = (log_level_t)log_level;
465
466 return ASCIICHAT_OK;
467}
468
469// ============================================================================
470// Option Formatting Utilities
471// ============================================================================
472
473// ============================================================================
474// Terminal Dimension Utilities
475// ============================================================================
476
478 if (!opts) {
479 return;
480 }
481
482 unsigned short int term_width, term_height;
483
484 // Note: Logging is not available during options_init, so we can't use log_debug here
485 asciichat_error_t result = get_terminal_size(&term_width, &term_height);
486
487 if (result == ASCIICHAT_OK) {
488 // If both dimensions are auto, set height to terminal height and let
489 // aspect_ratio calculate width
490 if (opts->auto_height && opts->auto_width) {
491 opts->height = term_height;
492 opts->width = term_width; // Also set width when both are auto
493 }
494 // If only height is auto, use full terminal height
495 else if (opts->auto_height) {
496 opts->height = term_height;
497 }
498 // If only width is auto, use full terminal width
499 else if (opts->auto_width) {
500 opts->width = term_width;
501 }
502 } else {
503 // Terminal size detection failed, but we can still continue with defaults
504 }
505}
506
508 if (!opts) {
509 return;
510 }
511
512 unsigned short int term_width, term_height;
513 // Get current terminal size (get_terminal_size already handles ioctl first, then $COLUMNS/$LINES fallback)
514 asciichat_error_t terminal_result = get_terminal_size(&term_width, &term_height);
515 if (terminal_result == ASCIICHAT_OK) {
516 // Use INFO level so this is visible without -v flag (important for debugging dimension issues)
517 log_dev("Terminal size detected: %ux%u (auto_width=%d, auto_height=%d)", term_width, term_height, opts->auto_width, opts->auto_height);
518 if (opts->auto_width) {
519 opts->width = term_width;
520 log_dev("Auto-width: set width to %u", opts->width);
521 }
522 if (opts->auto_height) {
523 opts->height = term_height;
524 log_dev("Auto-height: set height to %u", opts->height);
525 }
526 log_dev("Final dimensions: %ux%u", opts->width, opts->height);
527 } else {
528 // Terminal detection failed - keep the default values set in options_init()
529 log_warn("TERMINAL_DETECT_FAIL: Could not detect terminal size, using defaults: %ux%u", opts->width, opts->height);
530 }
531}
532
533// ============================================================================
534// Generic Usage Function (Unified Implementation)
535// ============================================================================
536
541typedef struct {
543 const char *program_name;
544 const char *description;
546
547static const mode_metadata_t mode_info[] = {
548 {MODE_API_SERVER, "ascii-chat api-server", "HTTP image-to-ASCII API"},
549 {MODE_MCP_SERVER, "ascii-chat mcp-server", "MCP rendering tools over stdio or HTTP"},
550 {MODE_SERVER, "ascii-chat server", "host a server mixing video and audio for ascii-chat clients"},
551 {MODE_CLIENT, "ascii-chat client", "connect to an ascii-chat server"},
552 {MODE_MIRROR, "ascii-chat mirror", "use the webcam or files or urls without network connections"},
553 {MODE_DISCOVERY_SERVICE, "ascii-chat discovery-service", "secure p2p session signalling"},
554 {MODE_DISCOVERY, "💻📸 ascii-chat 🔡💬", "Video chat in your terminal"},
555};
556
557void usage(FILE *desc, asciichat_mode_t mode) {
558 if (!desc) {
559 return;
560 }
561
562 // Find mode metadata
563 const mode_metadata_t *metadata = NULL;
564 for (size_t i = 0; i < sizeof(mode_info) / sizeof(mode_info[0]); i++) {
565 if (mode_info[i].mode == mode) {
566 metadata = &mode_info[i];
567 break;
568 }
569 }
570
571 if (!metadata) {
572 (void)fprintf(desc, "error: Unknown mode\n");
573 return;
574 }
575
576 flags_schema_builder_t *builder = flags_schema_builder_create();
577 if (!builder) {
578 (void)fprintf(desc, "Error: Failed to create libflags schema\n");
579 return;
580 }
582 if (registry_add_all_to_flags_builder(builder, mode_mask) != ASCIICHAT_OK || !flags_help_write_options(flags_schema_builder_schema(builder), metadata->program_name, metadata->description, desc)) {
583 (void)fprintf(desc, "Error: Failed to render options\n");
584 }
585 flags_schema_builder_destroy(builder);
586}
587
588// ============================================================================
589// Options Validation Helper
590// ============================================================================
591
592asciichat_error_t validate_options_and_report(const void *config, const void *opts) {
593 if (!opts) {
594 return SET_ERRNO(ERROR_INVALID_PARAM, "Options are NULL");
595 }
596
597 const options_t *options = (const options_t *)opts;
598 if (options->webrtc_relay_only && options->detected_mode == MODE_CLIENT) {
599 return SET_ERRNO(ERROR_INVALID_PARAM, "Relay only requires discovery mode; connect using a session name");
600 }
601 if (options->webrtc_relay_only && options->detected_mode == MODE_SERVER && !options->discovery) {
602 return SET_ERRNO(ERROR_INVALID_PARAM, "Server relay only requires --discovery");
603 }
604 if (options->webrtc_relay_only && (options->no_webrtc || options->webrtc_disable_turn)) {
605 return SET_ERRNO(ERROR_INVALID_PARAM, "--webrtc-relay-only conflicts with --no-webrtc or --webrtc-disable-turn");
606 }
607 if ((options->turn_username[0] != '\0') != (options->turn_credential[0] != '\0')) {
608 return SET_ERRNO(ERROR_INVALID_PARAM, "Provide both --turn-username and --turn-credential, or leave both blank");
609 }
610 if (strlen(options->turn_username) > 127 || strlen(options->turn_credential) > 127) {
611 return SET_ERRNO(ERROR_INVALID_PARAM, "TURN username and credential must each fit 127 bytes");
612 }
613 (void)config;
614 return options_validate_libflags((options_t *)options);
615}
616
617// ============================================================================
618// Print Project Links
619// ============================================================================
620
621void print_project_links(FILE *desc) {
622 if (!desc) {
623 return;
624 }
625
626 (void)fprintf(desc, "🔗 %s\n", colored_string(LOG_COLOR_GREY, "https://ascii-chat.com"));
627 (void)fprintf(desc, "🔗 %s\n", colored_string(LOG_COLOR_GREY, "https://github.com/zfogg/ascii-chat"));
628}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
option_type_t
Custom help section descriptor.
Definition builder.h:170
@ OPTION_TYPE_INT
Integer value (–count 42).
Definition builder.h:172
@ OPTION_TYPE_DOUBLE
Floating point (–ratio 1.5).
Definition builder.h:174
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:173
@ OPTION_TYPE_CALLBACK
Custom parser function.
Definition builder.h:175
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
Option enum value definitions - single source of truth.
#define OPT_PALETTE_CUSTOM
Definition enums.h:125
#define OPT_VALUE_FALSE
Definition enums.h:79
#define OPT_PALETTE_COOL
Definition enums.h:124
#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_COLOR_MODE_256
Definition enums.h:101
#define OPT_RENDER_MODE_FG
Definition enums.h:129
#define OPT_VALUE_TRUE
Definition enums.h:78
#define OPT_COLOR_MODE_NONE
Definition enums.h:98
#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_VALUE_AUTO
Definition enums.h:77
#define OPT_COLOR_MODE_TRUECOLOR
Definition enums.h:102
#define OPT_PALETTE_DIGITAL
Definition enums.h:122
#define OPT_RENDER_MODE_BACKGROUND
Definition enums.h:130
#define OPT_COLOR_MODE_MONO
Definition enums.h:99
#define OPT_COLOR_MODE_16
Definition enums.h:100
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
int buffer_size
Size of circular buffer.
Definition grep.c:68
#define BUFFER_SIZE_SMALL
Small buffer size (256 bytes).
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_CRYPTO_KEY
Definition error_codes.h:98
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
log_level_t
Logging levels enumeration.
Definition types.h:33
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
@ LOG_COLOR_GREY
Definition log/log.h:153
unsigned short int GRAY[256]
Grayscale lookup table.
#define COLOR_MODE_16_COLOR
16-color mode (full name)
Definition options.h:226
unsigned short int BLUE[256]
Blue channel lookup table.
#define COLOR_MODE_256_COLOR
256-color mode (full name)
Definition options.h:228
#define COLOR_MODE_TRUECOLOR
24-bit truecolor mode
Definition options.h:229
asciichat_mode_t
Mode type for options parsing.
Definition options.h:916
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:223
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
int strtoint_safe(const char *str)
Safely parse string to integer with validation.
void update_dimensions_to_terminal_size(options_t *opts)
Update dimensions to match current terminal size.
const float weight_blue
Blue weight for luminance calculation.
const float weight_red
Red weight for luminance calculation.
option_mode_bitmask_t
Option mode bitmask.
Definition options.h:933
size_t levenshtein(const char *a, const char *b)
Calculate Levenshtein distance between two strings.
Definition levenshtein.c:73
#define COLOR_MODE_NONE
Monochrome mode.
Definition options.h:224
#define LEVENSHTEIN_SUGGESTION_THRESHOLD
Maximum edit distance to suggest an option.
Definition levenshtein.h:32
void usage(FILE *desc, asciichat_mode_t mode)
Print usage information for a specific mode.
unsigned short int GREEN[256]
Green channel lookup table.
const float weight_green
Green weight for luminance calculation.
unsigned short int RED[256]
Red channel lookup table.
int validate_opt_log_level(const char *value_str, char *error_msg, size_t error_msg_size)
Validate log level string.
Definition validation.c:239
void update_dimensions_for_full_height(options_t *opts)
Update dimensions to use full terminal height while maintaining aspect ratio.
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:918
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_API_SERVER
Image-to-ASCII HTTP API server.
Definition options.h:923
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:919
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
@ OPTION_MODE_ALL
All modes + binary.
Definition options.h:943
@ OPTION_MODE_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:942
@ OPTION_MODE_CLIENT
Client mode (bit 1).
Definition options.h:936
@ OPTION_MODE_API_SERVER
Definition options.h:941
@ OPTION_MODE_DISCOVERY
Discovery mode (bit 4).
Definition options.h:939
@ OPTION_MODE_SERVER
Server mode (bit 0).
Definition options.h:935
@ OPTION_MODE_MIRROR
Mirror mode (bit 2).
Definition options.h:937
@ OPTION_MODE_MCP_SERVER
Definition options.h:940
@ OPTION_MODE_DISCOVERY_SVC
Discovery server mode (bit 3).
Definition options.h:938
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:102
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:108
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:100
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:104
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:106
#define ASCIICHAT_API
Export symbols on Unix platforms (Linux, macOS).
Definition api.h:86
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
char * expand_path(const char *path)
Expand path with tilde (~) support.
Definition path.c:257
#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
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Common utilities and helpers for option parsing across all modes.
Levenshtein distance algorithm for fuzzy string matching.
Central registry of all command-line options with mode applicability.
struct flags_schema_builder flags_schema_builder_t
📝 Logging API with multiple log levels and terminal output control
#define OPTIONS_COMMON_SUGGESTION_BUFFER_SIZE
bool auto_height
char * strip_equals_prefix(const char *opt_value, char *buffer, size_t buffer_size)
Strip equals sign prefix from option argument.
int options_format_default_value(option_type_t type, const void *default_value, char *buf, size_t bufsize)
Format option default value to string.
void print_project_links(FILE *desc)
Update dimensions for full-height mode.
const char * format_available_modes(option_mode_bitmask_t mode_bitmask)
Format all available modes for an option as comma-separated list.
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.
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_width_option(const char *value_str, options_t *opts)
Parse –width option and set opts->width.
asciichat_error_t detect_default_ssh_key(char *key_path, size_t path_size)
Detect default SSH key path for the current user.
char * read_password_from_stdin(const char *prompt)
Read password from stdin with prompt.
bool validate_port_opt(const char *value_str, uint16_t *out_port)
Validate port number (1-65535).
asciichat_error_t parse_height_option(const char *value_str, options_t *opts)
Parse –height option and set opts->height.
bool validate_fps_opt(const char *value_str, int *out_fps)
Validate FPS value (0 for auto, otherwise 1-144).
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_palette_option(const char *value_str, options_t *opts)
Parse –palette option and set opt_palette_type.
bool auto_width
asciichat_error_t parse_log_level_option(const char *value_str, options_t *opts)
Parse –log-level option and set opt_log_level.
const char * find_similar_option_with_mode(const char *unknown_opt, const options_config_t *config, option_mode_bitmask_t current_mode_bitmask)
Find similar option across all modes and suggest with mode information.
asciichat_error_t validate_options_and_report(const void *config, const void *opts)
Validate options and report errors to stderr.
const char * options_get_type_placeholder(option_type_t type)
Get placeholder string for option type.
asciichat_error_t parse_color_mode_option(const char *value_str, options_t *opts)
Parse –color-mode option and set opts->color_mode.
#define OPTIONS_COMMON_BUFFER_BUFFER_SIZE
asciichat_error_t parse_snapshot_delay_option(const char *value_str, options_t *opts)
Parse –snapshot-delay option and set opts->snapshot_delay.
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.
asciichat_error_t parse_webcam_index_option(const char *value_str, options_t *opts)
Parse –webcam-index option and set opts->webcam_index.
Validation functions for options parsing.
asciichat_error_t options_validate_libflags(options_t *options)
Definition options.c:1617
Password prompting utilities with secure input and formatting.
📂 Path Manipulation Utilities
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
int get_terminal_size(unsigned short int *width, unsigned short int *height)
🔤 String Manipulation and Shell Escaping Utilities
Mode metadata for usage display.
const char * program_name
Program name shown in usage.
asciichat_mode_t mode
Application mode.
const char * description
One-line description of mode.
The complete registry entry for one command-line/configuration option.
Definition builder.h:251
const char * long_name
Long option name (e.g., "port").
Definition builder.h:253
Immutable parsing tables produced from an options builder.
Definition builder.h:415
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:416
size_t num_descriptors
Number of descriptors.
Definition builder.h:417
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor).
Definition options.h:1084
bool discovery
Enable discovery session registration (default: false).
Definition options.h:1027
bool webrtc_relay_only
–webrtc-relay-only: Require TURN relay candidates
Definition options.h:1202
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
Definition options.h:978
int height
Terminal height in characters (int for OPTION_TYPE_INT compat).
Definition options.h:992
bool no_webrtc
–no-webrtc: Disable WebRTC, use Direct TCP only
Definition options.h:1199
bool webrtc_disable_turn
–webrtc-disable-turn: Disable Stage 3 (TURN), use STUN only
Definition options.h:1203
bool auto_height
Auto-detect height from terminal.
Definition options.h:994
log_level_t log_level
Log level threshold.
Definition options.h:1160
char turn_credential[256]
ACDS: Credential/password for TURN authentication.
Definition options.h:1214
char palette_custom[256]
Custom palette characters.
Definition options.h:1221
bool palette_custom_set
True if custom palette was set.
Definition options.h:1222
terminal_render_mode_t render_mode
Render mode (foreground/background/half-block).
Definition options.h:1087
int webcam_index
Webcam device index (0 = first).
Definition options.h:1062
double snapshot_delay
Snapshot delay in seconds.
Definition options.h:1143
int width
Terminal width in characters (int for OPTION_TYPE_INT compat).
Definition options.h:991
palette_type_t palette_type
Selected palette type.
Definition options.h:1220
char turn_username[256]
ACDS: Username for TURN authentication.
Definition options.h:1213
bool auto_width
Auto-detect width from terminal.
Definition options.h:993
🔍 Safe Parsing Utilities