ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
parsers.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE 32
9#define OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE 256
10#define OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE 512
11#define OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE 256
12
15#include <string.h>
16#include <stdlib.h>
17#include <ctype.h>
18#include <stddef.h>
19#include <ascii-chat/common.h>
22#include <ascii-chat/options/enums.h> // For OPT_* enum constants
23#include <ascii-chat/options/strings.h> // For fuzzy matching suggestions
24#include <ascii-chat/discovery/strings.h> // For is_session_string() validation
25#include <ascii-chat/util/parsing.h> // For parse_port() validation
26#include <ascii-chat/util/path.h> // For path_validate_user_path()
27#include <ascii-chat/util/time.h> // For SEC_PER_HOUR, SEC_PER_MIN
28#include <ascii-chat/video/rgba/color_filter.h> // For color_filter_from_cli_name()
29
30// Helper function to convert string to lowercase in-place (non-destructive)
31static void to_lower(const char *src, char *dst, size_t max_len) {
32 size_t i = 0;
33 while (i < max_len - 1 && src[i]) {
34 dst[i] = (char)tolower((unsigned char)src[i]);
35 i++;
36 }
37 dst[i] = '\0';
38}
39
44typedef struct {
45 const char *match;
48
60static bool parse_setting_generic(const char *arg, void *dest, const setting_map_entry_t *lookup_table, char **error_msg) {
61 if (!dest || !lookup_table) {
62 if (error_msg) {
63 *error_msg = platform_strdup("Internal error: NULL destination or lookup table");
64 }
65 return false;
66 }
67
68 int *result = (int *)dest;
69
70 // Handle optional argument - default to first entry in table (usually "auto")
71 if (!arg || arg[0] == '\0') {
72 *result = lookup_table[0].enum_value;
73 return true;
74 }
75
76 // Convert to lowercase
78 to_lower(arg, lower, sizeof(lower));
79
80 // Linear search through lookup table to get enum value
81 for (int i = 0; lookup_table[i].match != NULL; i++) {
82 if (strcmp(lower, lookup_table[i].match) == 0) {
83 *result = lookup_table[i].enum_value;
84 return true;
85 }
86 }
87
88 if (error_msg) {
89 *error_msg = platform_strdup("Invalid setting value");
90 }
91 return false;
92}
93// NOTE: is_session_string() is now imported from lib/discovery/strings.h
94// and provides enhanced validation against actual wordlists via hashtable lookup.
95// See that module for the full implementation.
96
97// Lookup table for color setting strings
98static const setting_map_entry_t g_color_setting_map[] = {
100 {"yes", COLOR_SETTING_TRUE}, {"1", COLOR_SETTING_TRUE}, {"on", COLOR_SETTING_TRUE}, {"enabled", COLOR_SETTING_TRUE},
101 {"enable", COLOR_SETTING_TRUE}, {"false", COLOR_SETTING_FALSE}, {"no", COLOR_SETTING_FALSE}, {"-1", COLOR_SETTING_FALSE},
102 {"off", COLOR_SETTING_FALSE}, {"disabled", COLOR_SETTING_FALSE}, {"disable", COLOR_SETTING_FALSE}, {NULL, 0} // Sentinel
103};
104
105bool parse_color_setting(const char *arg, void *dest, char **error_msg) {
106 if (!dest) {
107 if (error_msg) {
108 *error_msg = platform_strdup("Internal error: NULL destination");
109 }
110 return false;
111 }
112
113 // Use generic parser with color setting lookup table
114 // Default to TRUE if no arg provided
115 if (!arg || arg[0] == '\0') {
116 int *color_setting = (int *)dest;
117 *color_setting = COLOR_SETTING_TRUE;
118 return true;
119 }
120
121 return parse_setting_generic(arg, dest, g_color_setting_map, error_msg);
122}
123
124// Lookup table for UTF-8 setting strings (identical to color setting for boolean patterns)
125static const setting_map_entry_t g_utf8_setting_map[] = {
126 {"auto", UTF8_SETTING_AUTO}, {"a", UTF8_SETTING_AUTO}, {"0", UTF8_SETTING_AUTO}, {"true", UTF8_SETTING_TRUE},
127 {"yes", UTF8_SETTING_TRUE}, {"1", UTF8_SETTING_TRUE}, {"on", UTF8_SETTING_TRUE}, {"enabled", UTF8_SETTING_TRUE},
128 {"enable", UTF8_SETTING_TRUE}, {"false", UTF8_SETTING_FALSE}, {"no", UTF8_SETTING_FALSE}, {"-1", UTF8_SETTING_FALSE},
129 {"off", UTF8_SETTING_FALSE}, {"disabled", UTF8_SETTING_FALSE}, {"disable", UTF8_SETTING_FALSE}, {NULL, 0} // Sentinel
130};
131
132bool parse_utf8_setting(const char *arg, void *dest, char **error_msg) {
133 if (!dest) {
134 if (error_msg) {
135 *error_msg = platform_strdup("Internal error: NULL destination");
136 }
137 return false;
138 }
139
140 // Use generic parser with UTF-8 setting lookup table
141 // Default to TRUE if no arg provided
142 if (!arg || arg[0] == '\0') {
143 int *utf8_setting = (int *)dest;
144 *utf8_setting = UTF8_SETTING_TRUE;
145 return true;
146 }
147
148 return parse_setting_generic(arg, dest, g_utf8_setting_map, error_msg);
149}
150
151bool parse_color_mode(const char *arg, void *dest, char **error_msg) {
152 if (!arg || !dest) {
153 if (error_msg) {
154 *error_msg = platform_strdup("Internal error: NULL argument or destination");
155 }
156 return false;
157 }
158
159 terminal_color_mode_t *color_mode = (terminal_color_mode_t *)dest;
161 to_lower(arg, lower, sizeof(lower));
162
163 // Auto-detect
164 if (strcmp(lower, OPT_COLOR_MODE_AUTO) == 0 || strcmp(lower, "a") == 0) {
165 *color_mode = TERM_COLOR_AUTO;
166 return true;
167 }
168
169 // Monochrome/None
170 if (strcmp(lower, OPT_COLOR_MODE_NONE) == 0 || strcmp(lower, OPT_COLOR_MODE_MONO) == 0 || strcmp(lower, "monochrome") == 0 || strcmp(lower, "0") == 0) {
171 *color_mode = TERM_COLOR_NONE;
172 return true;
173 }
174
175 // 16-color
176 if (strcmp(lower, OPT_COLOR_MODE_16) == 0 || strcmp(lower, "16color") == 0 || strcmp(lower, "ansi") == 0 || strcmp(lower, "1") == 0) {
177 *color_mode = TERM_COLOR_16;
178 return true;
179 }
180
181 // 256-color
182 if (strcmp(lower, OPT_COLOR_MODE_256) == 0 || strcmp(lower, "256color") == 0 || strcmp(lower, "2") == 0) {
183 *color_mode = TERM_COLOR_256;
184 return true;
185 }
186
187 // Truecolor
188 if (strcmp(lower, OPT_COLOR_MODE_TRUECOLOR) == 0 || strcmp(lower, "true") == 0 || strcmp(lower, "tc") == 0 || strcmp(lower, "rgb") == 0 || strcmp(lower, "24bit") == 0 || strcmp(lower, "3") == 0) {
189 *color_mode = TERM_COLOR_TRUECOLOR;
190 return true;
191 }
192
193 // Invalid value - suggest closest match
194 if (error_msg) {
196 const char *suggestion = asciichat_suggest_enum_value("color-mode", arg);
197 if (suggestion) {
198 safe_snprintf(msg, sizeof(msg), "Invalid color mode '%s'. Did you mean '%s'?", arg, suggestion);
199 } else {
200 safe_snprintf(msg, sizeof(msg), "Invalid color mode '%s'. Valid values: auto, none, 16, 256, truecolor", arg);
201 }
202 *error_msg = platform_strdup(msg);
203 }
204 return false;
205}
206
207bool parse_color_filter(const char *arg, void *dest, char **error_msg) {
208 if (!arg || !dest) {
209 if (error_msg) {
210 *error_msg = platform_strdup("Internal error: NULL argument or destination");
211 }
212 return false;
213 }
214
215 color_filter_t *color_filter = (color_filter_t *)dest;
217 to_lower(arg, lower, sizeof(lower));
218
219 // Try to match against all known color filters
220 color_filter_t parsed_filter = color_filter_from_cli_name(lower);
221 if (parsed_filter != COLOR_FILTER_NONE || strcmp(lower, OPT_COLOR_FILTER_NONE) == 0) {
222 *color_filter = parsed_filter;
223 return true;
224 }
225
226 // Invalid value
227 if (error_msg) {
229 safe_snprintf(msg, sizeof(msg),
230 "Invalid color filter '%s'. Valid values: none, black, white, green, magenta, fuchsia, "
231 "orange, teal, cyan, pink, red, yellow",
232 arg);
233 *error_msg = platform_strdup(msg);
234 }
235 return false;
236}
237
238bool parse_render_mode(const char *arg, void *dest, char **error_msg) {
239 if (!arg || !dest) {
240 if (error_msg) {
241 *error_msg = platform_strdup("Internal error: NULL argument or destination");
242 }
243 return false;
244 }
245
246 terminal_render_mode_t *render_mode = (terminal_render_mode_t *)dest;
248 to_lower(arg, lower, sizeof(lower));
249
250 // Foreground mode
251 if (strcmp(lower, OPT_RENDER_MODE_FOREGROUND) == 0 || strcmp(lower, OPT_RENDER_MODE_FG) == 0 || strcmp(lower, "0") == 0) {
252 *render_mode = RENDER_MODE_FOREGROUND;
253 return true;
254 }
255
256 // Background mode
257 if (strcmp(lower, OPT_RENDER_MODE_BACKGROUND) == 0 || strcmp(lower, OPT_RENDER_MODE_BG) == 0 || strcmp(lower, "1") == 0) {
258 *render_mode = RENDER_MODE_BACKGROUND;
259 return true;
260 }
261
262 // Half-block mode
263 if (strcmp(lower, OPT_RENDER_MODE_HALF_BLOCK) == 0 || strcmp(lower, "hb") == 0 || strcmp(lower, "2") == 0) {
264 *render_mode = RENDER_MODE_HALF_BLOCK;
265 return true;
266 }
267
268 // Invalid value - suggest closest match
269 if (error_msg) {
271 const char *suggestion = asciichat_suggest_enum_value("render-mode", arg);
272 if (suggestion) {
273 safe_snprintf(msg, sizeof(msg), "Invalid render mode '%s'. Did you mean '%s'?", arg, suggestion);
274 } else {
275 safe_snprintf(msg, sizeof(msg), "Invalid render mode '%s'. Valid values: foreground, background, half-block", arg);
276 }
277 *error_msg = platform_strdup(msg);
278 }
279 return false;
280}
281
282bool parse_render_theme(const char *arg, void *dest, char **error_msg) {
283 if (!arg || !dest) {
284 if (error_msg) {
285 *error_msg = platform_strdup("Internal error: NULL argument or destination");
286 }
287 return false;
288 }
289
290 int *theme = (int *)dest;
292 to_lower(arg, lower, sizeof(lower));
293
294 // Dark theme
295 if (strcmp(lower, OPT_RENDER_THEME_DARK) == 0 || strcmp(lower, "0") == 0) {
296 *theme = 0; // TERM_RENDERER_THEME_DARK
297 return true;
298 }
299
300 // Light theme
301 if (strcmp(lower, OPT_RENDER_THEME_LIGHT) == 0 || strcmp(lower, "1") == 0) {
302 *theme = 1; // TERM_RENDERER_THEME_LIGHT
303 return true;
304 }
305
306 // Auto theme
307 if (strcmp(lower, OPT_VALUE_AUTO) == 0 || strcmp(lower, "2") == 0) {
308 *theme = 2; // TERM_RENDERER_THEME_AUTO
309 return true;
310 }
311
312 // Invalid value
313 if (error_msg) {
315 safe_snprintf(msg, sizeof(msg), "Invalid render theme '%s'. Valid values: dark, light, auto", arg);
316 *error_msg = platform_strdup(msg);
317 }
318 return false;
319}
320
321bool parse_palette_type(const char *arg, void *dest, char **error_msg) {
322 if (!arg || !dest) {
323 if (error_msg) {
324 *error_msg = platform_strdup("Internal error: NULL argument or destination");
325 }
326 return false;
327 }
328
329 palette_type_t *palette_type = (palette_type_t *)dest;
331 to_lower(arg, lower, sizeof(lower));
332
333 // Standard palette
334 if (strcmp(lower, OPT_PALETTE_STANDARD) == 0 || strcmp(lower, "std") == 0 || strcmp(lower, "0") == 0) {
335 *palette_type = PALETTE_STANDARD;
336 return true;
337 }
338
339 // Blocks palette
340 if (strcmp(lower, OPT_PALETTE_BLOCKS) == 0 || strcmp(lower, "block") == 0 || strcmp(lower, "1") == 0) {
341 *palette_type = PALETTE_BLOCKS;
342 return true;
343 }
344
345 // Digital palette
346 if (strcmp(lower, OPT_PALETTE_DIGITAL) == 0 || strcmp(lower, "dig") == 0 || strcmp(lower, "2") == 0) {
347 *palette_type = PALETTE_DIGITAL;
348 return true;
349 }
350
351 // Minimal palette
352 if (strcmp(lower, OPT_PALETTE_MINIMAL) == 0 || strcmp(lower, "min") == 0 || strcmp(lower, "3") == 0) {
353 *palette_type = PALETTE_MINIMAL;
354 return true;
355 }
356
357 // Cool palette
358 if (strcmp(lower, OPT_PALETTE_COOL) == 0 || strcmp(lower, "4") == 0) {
359 *palette_type = PALETTE_COOL;
360 return true;
361 }
362
363 // Custom palette
364 if (strcmp(lower, OPT_PALETTE_CUSTOM) == 0 || strcmp(lower, "5") == 0) {
365 *palette_type = PALETTE_CUSTOM;
366 return true;
367 }
368
369 // Invalid value - suggest closest match
370 if (error_msg) {
372 const char *suggestion = asciichat_suggest_enum_value("palette", arg);
373 if (suggestion) {
374 safe_snprintf(msg, sizeof(msg), "Invalid palette type '%s'. Did you mean '%s'?", arg, suggestion);
375 } else {
376 safe_snprintf(msg, sizeof(msg), "Invalid palette type '%s'. Valid values: standard, blocks, digital, minimal, cool, custom", arg);
377 }
378 *error_msg = platform_strdup(msg);
379 }
380 return false;
381}
382
383bool parse_log_level(const char *arg, void *dest, char **error_msg) {
384 if (!dest) {
385 if (error_msg) {
386 *error_msg = platform_strdup("Internal error: NULL destination");
387 }
388 return false;
389 }
390
391 log_level_t *log_level = (log_level_t *)dest;
392
393 // If no argument provided, use the default log level (based on build type)
394 if (!arg || arg[0] == '\0') {
395 *log_level = DEFAULT_LOG_LEVEL;
396 return true;
397 }
398
400 to_lower(arg, lower, sizeof(lower));
401
402 // Development level
403 if (strcmp(lower, OPT_LOG_LEVEL_DEV) == 0 || strcmp(lower, "development") == 0 || strcmp(lower, "0") == 0) {
404 *log_level = LOG_DEV;
405 return true;
406 }
407
408 // Debug level
409 if (strcmp(lower, OPT_LOG_LEVEL_DEBUG) == 0 || strcmp(lower, "dbg") == 0 || strcmp(lower, "1") == 0) {
410 *log_level = LOG_DEBUG;
411 return true;
412 }
413
414 // Info level
415 if (strcmp(lower, OPT_LOG_LEVEL_INFO) == 0 || strcmp(lower, "information") == 0 || strcmp(lower, "2") == 0) {
416 *log_level = LOG_INFO;
417 return true;
418 }
419
420 // Warning level
421 if (strcmp(lower, OPT_LOG_LEVEL_WARN) == 0 || strcmp(lower, "warning") == 0 || strcmp(lower, "3") == 0) {
422 *log_level = LOG_WARN;
423 return true;
424 }
425
426 // Error level
427 if (strcmp(lower, OPT_LOG_LEVEL_ERROR) == 0 || strcmp(lower, "err") == 0 || strcmp(lower, "4") == 0) {
428 *log_level = LOG_ERROR;
429 return true;
430 }
431
432 // Fatal level
433 if (strcmp(lower, OPT_LOG_LEVEL_FATAL) == 0 || strcmp(lower, "5") == 0) {
434 *log_level = LOG_FATAL;
435 return true;
436 }
437
438 // Invalid value
439 if (error_msg) {
441 safe_snprintf(msg, sizeof(msg), "Invalid log level '%s'. Valid values: dev, debug, info, warn, error, fatal", arg);
442 *error_msg = platform_strdup(msg);
443 }
444 return false;
445}
446
447bool parse_port_option(const char *arg, void *dest, char **error_msg) {
448 if (!arg || !dest) {
449 if (error_msg) {
450 *error_msg = platform_strdup("Internal error: NULL argument or destination");
451 }
452 return false;
453 }
454
455 int *port_value = (int *)dest;
456 uint16_t port_num;
457
458 // Use the existing parse_port function for validation
459 asciichat_error_t err = parse_port(arg, &port_num);
460 if (err != ASCIICHAT_OK) {
461 if (error_msg) {
463 safe_snprintf(msg, sizeof(msg), "Invalid port '%s'. Port must be a number between 0 and 65535 (0 = auto-assign).", arg);
464 *error_msg = platform_strdup(msg);
465 }
466 return false;
467 }
468
469 // Port zero is useful to low-level socket APIs for ephemeral binding, but
470 // it is not a valid ascii-chat user-facing port and conflicts with the
471 // option registry's 1-65535 contract.
472 if (port_num == 0) {
473 if (error_msg) {
474 *error_msg = platform_strdup("Port must be between 1 and 65535");
475 }
476 return false;
477 }
478
479 *port_value = (int)port_num;
480 return true;
481}
482
483// ============================================================================
484// Positional Argument Parsers
485// ============================================================================
486
487#include <ascii-chat/util/ip.h>
488#include <string.h>
489
497int parse_server_bind_address(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg) {
498 if (!arg || !config) {
499 if (error_msg) {
500 *error_msg = platform_strdup("Internal error: NULL argument or config");
501 }
502 return -1;
503 }
504
505 // Assume config struct has address and address6 fields (OPTIONS_BUFF_SIZE each)
506 // This is a simplified version that assumes standard options_state layout
507 char *address = (char *)config + offsetof(struct options_state, address);
508 char *address6 = (char *)config + offsetof(struct options_state, address6);
509
510 int consumed = 0;
511
512 // Parse first argument (IPv4 or IPv6)
513 char parsed_addr[OPTIONS_BUFF_SIZE];
514 const char *addr_to_check = arg;
515 if (parse_ipv6_address(arg, parsed_addr, sizeof(parsed_addr)) == 0) {
516 addr_to_check = parsed_addr;
517 }
518
519 // Check if it's IPv4 or IPv6
520 if (is_valid_ipv4(addr_to_check)) {
521 // Check if we already have a non-default IPv4 address
522 // Allow overwriting defaults (localhost, 0.0.0.0)
523 if (address[0] != '\0' && !is_localhost_ipv4(address) && strcmp(address, "localhost") != 0 && strcmp(address, "0.0.0.0") != 0) {
524 if (error_msg) {
526 safe_snprintf(msg, sizeof(msg),
527 "Cannot specify multiple IPv4 addresses.\n"
528 "Already have: %s\n"
529 "Cannot add: %s",
530 address, addr_to_check);
531 *error_msg = platform_strdup(msg);
532 }
533 return -1;
534 }
535 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", addr_to_check);
536 consumed = 1;
537 } else if (is_valid_ipv6(addr_to_check)) {
538 // Check if we already have a non-default IPv6 address
539 // Allow overwriting default (::1)
540 if (address6[0] != '\0' && !is_localhost_ipv6(address6)) {
541 if (error_msg) {
543 safe_snprintf(msg, sizeof(msg),
544 "Cannot specify multiple IPv6 addresses.\n"
545 "Already have: %s\n"
546 "Cannot add: %s",
547 address6, addr_to_check);
548 *error_msg = platform_strdup(msg);
549 }
550 return -1;
551 }
552 SAFE_SNPRINTF(address6, OPTIONS_BUFF_SIZE, "%s", addr_to_check);
553 consumed = 1;
554 } else {
555 if (error_msg) {
557 safe_snprintf(msg, sizeof(msg),
558 "Invalid IP address '%s'.\n"
559 "Server bind addresses must be valid IPv4 or IPv6 addresses.\n"
560 "Examples:\n"
561 " ascii-chat server 0.0.0.0\n"
562 " ascii-chat server ::1\n"
563 " ascii-chat server 0.0.0.0 ::1",
564 arg);
565 *error_msg = platform_strdup(msg);
566 }
567 return -1;
568 }
569
570 // Try to parse second address if available
571 if (remaining && num_remaining > 0 && remaining[0]) {
572 const char *second_arg = remaining[0];
573 memset(parsed_addr, 0, sizeof(parsed_addr));
574 addr_to_check = second_arg;
575 if (parse_ipv6_address(second_arg, parsed_addr, sizeof(parsed_addr)) == 0) {
576 addr_to_check = parsed_addr;
577 }
578
579 if (is_valid_ipv4(addr_to_check)) {
580 // Second is IPv4
581 if (address[0] != '\0' && !is_localhost_ipv4(address) && strcmp(address, "localhost") != 0 && strcmp(address, "0.0.0.0") != 0) {
582 // Already have an IPv4, can't add another
583 return consumed;
584 }
585 if (is_valid_ipv4(arg)) {
586 // First was also IPv4, can't have two IPv4s
587 return consumed;
588 }
589 // First was IPv6, second is IPv4 - accept both
590 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", addr_to_check);
591 consumed = 2;
592 } else if (is_valid_ipv6(addr_to_check)) {
593 // Second is IPv6
594 if (address6[0] != '\0' && !is_localhost_ipv6(address6)) {
595 // Already have an IPv6, can't add another
596 return consumed;
597 }
598 if (is_valid_ipv6(arg)) {
599 // First was also IPv6, can't have two IPv6s
600 return consumed;
601 }
602 // First was IPv4, second is IPv6 - accept both
603 SAFE_SNPRINTF(address6, OPTIONS_BUFF_SIZE, "%s", addr_to_check);
604 consumed = 2;
605 }
606 }
607
608 return consumed;
609}
610
617int parse_client_address(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg) {
618 (void)remaining;
619 (void)num_remaining;
620
621 if (!arg || !config) {
622 if (error_msg) {
623 *error_msg = platform_strdup("Internal error: NULL argument or config");
624 }
625 return -1;
626 }
627
628 log_debug("Processing argument: '%s'", arg);
629
630#ifndef __EMSCRIPTEN__
631 ((options_t *)config)->address_explicit = true;
632#endif
633 // Access address and port fields from options_state struct
634 char *address = (char *)config + offsetof(struct options_state, address);
635 int *port = (int *)((char *)config + offsetof(struct options_state, port));
636
637 // Check for WebSocket URL (ws:// or wss://) FIRST before session string validation
638 // WebSocket URLs are passed through without validation or port extraction
639 if (strncmp(arg, "ws://", 5) == 0 || strncmp(arg, "wss://", 6) == 0) {
640 log_debug("Detected WebSocket URL: %s", arg);
641 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", arg);
642 // Don't set port - WebSocket transport handles URL parsing internally
643 return 1; // Consumed 1 argument
644 }
645
646 // Explicit transport URLs are resolved by the connection layer.
647 if (strncmp(arg, "tcp://", 6) == 0 || strncmp(arg, "quic://", 7) == 0) {
648 log_debug("Detected transport URL: %s", arg);
649 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", arg);
650 // The connection layer extracts the port from the URL.
651 return 1; // Consumed 1 argument
652 }
653
654 // Check if this is a session string (format: adjective-noun-noun)
655 // Session strings have exactly 2 hyphens, only lowercase letters, length 5-47
656 bool is_session = is_session_string(arg);
657 log_debug("is_session_string('%s') = %s", arg, is_session ? "true" : "false");
658
659 if (is_session) {
660 // This is a session string, not a server address
661 char *session_string = (char *)config + offsetof(struct options_state, session_string);
662 SAFE_SNPRINTF(session_string, SESSION_STRING_BUFFER_SIZE, "%s", arg);
663 log_debug("Stored session string: %s", arg);
664 return 1; // Consumed 1 arg
665 }
666
667 // Not a session string, parse as server address
668 log_debug("Parsing as server address (not a session string)");
669
670 // Check for port in address (format: address:port or [ipv6]:port)
671 const char *colon = strrchr(arg, ':');
672
673 if (colon != NULL) {
674 // Check if this is IPv6 with port [::1]:port or plain hostname:port
675 if (arg[0] == '[') {
676 // IPv6 with brackets: [address]:port
677 const char *closing_bracket = strchr(arg, ']');
678 if (closing_bracket && closing_bracket < colon) {
679 // Extract address (remove brackets)
680 size_t addr_len = (size_t)(closing_bracket - arg - 1);
681 if (addr_len >= OPTIONS_BUFF_SIZE) {
682 if (error_msg) {
683 *error_msg = platform_strdup("IPv6 address too long");
684 }
685 return -1;
686 }
687 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%.*s", (int)addr_len, arg + 1);
688
689 // Extract and validate port
690 const char *port_str = colon + 1;
691 char *endptr;
692 long port_num = strtol(port_str, &endptr, 10);
693 if (*endptr != '\0' || port_num < 1 || port_num > 65535) {
694 if (error_msg) {
696 safe_snprintf(msg, sizeof(msg), "Invalid port number '%s'. Must be 1-65535.", port_str);
697 *error_msg = platform_strdup(msg);
698 }
699 return -1;
700 }
701 *port = (int)port_num;
702 }
703 } else {
704 // Check if it's IPv6 without brackets (no port allowed)
705 // or hostname/IPv4:port
706 size_t colon_count = 0;
707 for (const char *p = arg; *p; p++) {
708 if (*p == ':')
709 colon_count++;
710 }
711
712 if (colon_count == 1) {
713 // Likely hostname:port or IPv4:port
714 size_t addr_len = (size_t)(colon - arg);
715 if (addr_len >= OPTIONS_BUFF_SIZE) {
716 if (error_msg) {
717 *error_msg = platform_strdup("Address too long");
718 }
719 return -1;
720 }
721 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%.*s", (int)addr_len, arg);
722
723 // Extract and validate port
724 const char *port_str = colon + 1;
725 char *endptr;
726 long port_num = strtol(port_str, &endptr, 10);
727 if (*endptr != '\0' || port_num < 1 || port_num > 65535) {
728 if (error_msg) {
730 safe_snprintf(msg, sizeof(msg), "Invalid port number '%s'. Must be 1-65535.", port_str);
731 *error_msg = platform_strdup(msg);
732 }
733 return -1;
734 }
735 *port = (int)port_num;
736 } else {
737 // Multiple colons - likely bare IPv6 address
738 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", arg);
739 }
740 }
741 } else {
742 // No colon - just an address
743 SAFE_SNPRINTF(address, OPTIONS_BUFF_SIZE, "%s", arg);
744 }
745
746 // Validate addresses using comprehensive IPv4/IPv6 detection
747 bool has_dot = strchr(address, '.') != NULL;
748 bool has_colon = strchr(address, ':') != NULL;
749 bool starts_with_digit = address[0] >= '0' && address[0] <= '9';
750
751 // Potential IPv6 address (has colons) - validate as IPv6
752 if (has_colon) {
753 if (!is_valid_ipv6(address)) {
754 if (error_msg) {
756 safe_snprintf(msg, sizeof(msg),
757 "Invalid IPv6 address '%s'.\n"
758 "IPv6 addresses must be valid hex notation with colons.\n"
759 "Examples: ::1, 2001:db8::1, fe80::1\n"
760 "Or use hostnames like example.com",
761 address);
762 *error_msg = platform_strdup(msg);
763 }
764 return -1;
765 }
766 } else if (has_dot && starts_with_digit) {
767 // Potential IPv4 address (has dots and starts with digit) - validate strictly
768 if (!is_valid_ipv4(address)) {
769 if (error_msg) {
771 safe_snprintf(msg, sizeof(msg),
772 "Invalid IPv4 address '%s'.\n"
773 "IPv4 addresses must have exactly 4 octets (0-255) separated by dots.\n"
774 "Examples: 127.0.0.1, 192.168.1.1\n"
775 "For hostnames, use letters: example.com, localhost",
776 address);
777 *error_msg = platform_strdup(msg);
778 }
779 return -1;
780 }
781 }
782 // Otherwise treat as valid hostname (no validation needed)
783
784 // Note: Port conflict checking would require additional state
785 // (checking if --port flag was used). For now, this is a simplified version.
786 // Full implementation would need to track whether port was set via flag.
787
788 log_debug("Set address='%s', port=%d", address[0] ? address : "(empty)", *port);
789
790 return 1; // Consumed 1 arg
791}
792
793// ============================================================================
794// Mirror Mode Media Positional Argument Parser
795// ============================================================================
796
806int parse_mirror_media(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg) {
807 (void)remaining;
808 (void)num_remaining;
809
810 if (!arg || !config) {
811 if (error_msg) {
812 *error_msg = platform_strdup("Internal error: NULL argument or config");
813 }
814 return -1;
815 }
816
817 log_debug("Processing argument: '%s'", arg);
818
819 // Access media_file and media_url fields from options_state struct (same as other positional parsers)
820 char *media_file = (char *)config + offsetof(struct options_state, media_file);
821 char *media_url = (char *)config + offsetof(struct options_state, media_url);
822
823 // Enforce mutual exclusion with --file flag
824 if (media_file[0] != '\0') {
825 if (error_msg) {
826 *error_msg = platform_strdup("cannot use both --file and a positional argument");
827 }
828 return -1;
829 }
830
831 // Enforce mutual exclusion with --url flag
832 if (media_url[0] != '\0') {
833 if (error_msg) {
834 *error_msg = platform_strdup("cannot use both --url and a positional argument");
835 }
836 return -1;
837 }
838
839 // Check if it's an accessible file path (priority 1)
840 if (platform_access(arg, PLATFORM_ACCESS_READ) == 0) {
841 log_debug("Argument '%s' is an accessible file", arg);
842 SAFE_SNPRINTF(media_file, OPTIONS_BUFF_SIZE, "%s", arg);
843 return 1; // Consumed 1 argument
844 }
845
846 // Not a file; treat as URL (yt-dlp/ffmpeg will handle any errors)
847 log_debug("Argument '%s' not found as file; treating as URL", arg);
848 SAFE_SNPRINTF(media_url, OPTIONS_BUFF_SIZE, "%s", arg);
849 return 1; // Consumed 1 argument
850}
851
852// ============================================================================
853// Palette Characters Parser
854// ============================================================================
855
856bool parse_palette_chars(const char *arg, void *dest, char **error_msg) {
857 if (!arg || !dest) {
858 if (error_msg) {
859 *error_msg = platform_strdup("Internal error: NULL argument or destination");
860 }
861 return false;
862 }
863
864 // The dest pointer points to the palette_custom field in options_t
865 // We need to get the full options_t struct to call parse_palette_chars_option
866 // Since we only have the field pointer, we need to handle this directly
867
868 char *palette_custom = (char *)dest;
869
870 size_t len = strlen(arg);
871 if (len == 0) {
872 if (error_msg) {
873 *error_msg = platform_strdup("Invalid palette-chars: value cannot be empty");
874 }
875 return false;
876 }
877
879 if (error_msg) {
881 safe_snprintf(msg, sizeof(msg), "Invalid palette-chars: too long (%zu chars, max 255)", len);
882 *error_msg = platform_strdup(msg);
883 }
884 return false;
885 }
886
887 // Copy the palette characters
889 palette_custom[OPTIONS_PALETTE_CUSTOM_BUFFER_SIZE - 1] = '\0';
890
891 // Also set the palette type to custom by calculating back to options_t pointer
892 // dest points to options_t.palette_custom, so we can get options_t* using offset arithmetic
893 options_t *opts = (options_t *)((char *)dest - offsetof(options_t, palette_custom));
895 opts->palette_explicit = true;
896
897 return true;
898}
899
900bool parse_verbose_flag(const char *arg, void *dest, char **error_msg) {
901 (void)error_msg; // Unused but required by function signature
902
903 // If arg is NULL or starts with a flag, just increment
904 // Otherwise try to parse as integer count
905 unsigned short int *verbose_level = (unsigned short int *)dest;
906
907 if (!arg || arg[0] == '\0') {
908 // No argument provided, just increment
909 (*verbose_level)++;
910 return true;
911 }
912
913 // Try to parse as integer count
914 char *endptr;
915 long value = strtol(arg, &endptr, 10);
916 if (*endptr == '\0' && value >= 0 && value <= 100) {
917 *verbose_level = (unsigned short int)value;
918 return true;
919 }
920
921 // If it didn't parse as int, treat as flag increment
922 (*verbose_level)++;
923 return true;
924}
925
938bool parse_timestamp(const char *arg, void *dest, char **error_msg) {
939 if (!arg || arg[0] == '\0') {
940 if (error_msg) {
941 *error_msg = platform_strdup("--seek requires a timestamp argument");
942 }
943 return false;
944 }
945
946 double *timestamp = (double *)dest;
947 char *endptr;
948 long strtol_result;
949
950 // Count colons to determine format
951 int colon_count = 0;
952 for (const char *p = arg; *p; p++) {
953 if (*p == ':')
954 colon_count++;
955 }
956
957 if (colon_count == 0) {
958 // Plain seconds format: "30" or "30.5"
959 *timestamp = strtod(arg, &endptr);
960 if (*endptr != '\0' || *timestamp < 0.0) {
961 if (error_msg) {
962 *error_msg = platform_strdup("Invalid timestamp: expected non-negative seconds");
963 }
964 return false;
965 }
966 return true;
967 } else if (colon_count == 1) {
968 // MM:SS or MM:SS.ms format
969 strtol_result = strtol(arg, &endptr, 10);
970 if (*endptr != ':' || strtol_result < 0) {
971 if (error_msg) {
972 *error_msg = platform_strdup("Invalid timestamp: expected MM:SS or MM:SS.ms format");
973 }
974 return false;
975 }
976 long minutes = strtol_result;
977 double seconds = strtod(endptr + 1, &endptr);
978 if (*endptr != '\0' && *endptr != '.' && *endptr != '\0') {
979 if (error_msg) {
980 *error_msg = platform_strdup("Invalid timestamp: expected MM:SS or MM:SS.ms format");
981 }
982 return false;
983 }
984 *timestamp = minutes * 60.0 + seconds;
985 return true;
986 } else if (colon_count == 2) {
987 // HH:MM:SS or HH:MM:SS.ms format
988 strtol_result = strtol(arg, &endptr, 10);
989 if (*endptr != ':' || strtol_result < 0) {
990 if (error_msg) {
991 *error_msg = platform_strdup("Invalid timestamp: expected HH:MM:SS or HH:MM:SS.ms format");
992 }
993 return false;
994 }
995 long hours = strtol_result;
996
997 strtol_result = strtol(endptr + 1, &endptr, 10);
998 if (*endptr != ':' || strtol_result < 0 || strtol_result >= 60) {
999 if (error_msg) {
1000 *error_msg = platform_strdup("Invalid timestamp: minutes must be 0-59");
1001 }
1002 return false;
1003 }
1004 long minutes = strtol_result;
1005
1006 double seconds = strtod(endptr + 1, &endptr);
1007 if (*endptr != '\0') {
1008 if (error_msg) {
1009 *error_msg = platform_strdup("Invalid timestamp: expected HH:MM:SS or HH:MM:SS.ms format");
1010 }
1011 return false;
1012 }
1013 *timestamp = hours * (double)SEC_PER_HOUR + minutes * (double)SEC_PER_MIN + seconds;
1014 return true;
1015 } else {
1016 if (error_msg) {
1017 *error_msg = platform_strdup("Invalid timestamp format: too many colons");
1018 }
1019 return false;
1020 }
1021}
1022
1023bool parse_volume(const char *arg, void *dest, char **error_msg) {
1024 if (!arg || !dest) {
1025 if (error_msg) {
1026 *error_msg = platform_strdup("Internal error: NULL argument or destination");
1027 }
1028 return false;
1029 }
1030
1031 float *volume = (float *)dest;
1032 char *endptr;
1033 float val = strtof(arg, &endptr);
1034
1035 if (*endptr != '\0' || arg == endptr) {
1036 if (error_msg) {
1037 *error_msg = platform_strdup("Invalid volume value. Must be a number between 0.0 and 1.0");
1038 }
1039 return false;
1040 }
1041
1042 if (val < 0.0f || val > 1.0f) {
1043 if (error_msg) {
1045 SAFE_SNPRINTF(buf, sizeof(buf), "Volume must be between 0.0 and 1.0 (got %.2f)", val);
1046 *error_msg = platform_strdup(buf);
1047 }
1048 return false;
1049 }
1050
1051 *volume = val;
1052 return true;
1053}
1054
1055bool parse_log_file(const char *arg, void *dest, char **error_msg) {
1056 if (!arg || !dest) {
1057 if (error_msg) {
1058 *error_msg = platform_strdup("Internal error: NULL argument or destination");
1059 }
1060 return false;
1061 }
1062
1063 if (strcmp(arg, "/dev/null") == 0) {
1064 SAFE_STRNCPY((char *)dest, arg, 255);
1065 ((char *)dest)[255] = '\0';
1066 return true;
1067 }
1068
1069 // Validate and normalize the log file path
1070 char *normalized = NULL;
1072
1073 if (result != ASCIICHAT_OK) {
1074 if (error_msg) {
1076 if (HAS_ERRNO(&err_ctx)) {
1077 *error_msg = platform_strdup(err_ctx.context_message);
1078 } else {
1079 *error_msg = platform_strdup("Log file path validation failed");
1080 }
1081 }
1082 return false;
1083 }
1084
1085 // Copy validated path to destination
1086 char *log_file_buf = (char *)dest;
1087 const size_t max_size = 256;
1088 SAFE_STRNCPY(log_file_buf, normalized, max_size - 1);
1089 log_file_buf[max_size - 1] = '\0';
1090
1091 SAFE_FREE(normalized);
1092 return true;
1093}
1094
1095bool parse_audio_source(const char *arg, void *dest, char **error_msg) {
1096 if (!arg || !dest) {
1097 if (error_msg) {
1098 *error_msg = platform_strdup("Internal error: NULL argument or destination");
1099 }
1100 return false;
1101 }
1102
1103 audio_source_t *audio_source = (audio_source_t *)dest;
1105 to_lower(arg, lower, sizeof(lower));
1106
1107 if (strcmp(lower, OPT_AUDIO_SOURCE_ALL) == 0 || strcmp(lower, OPT_AUDIO_SOURCE_AUTO) == 0 || strcmp(lower, "both") == 0) {
1108 *audio_source = AUDIO_SOURCE_ALL;
1109 return true;
1110 }
1111 if (strcmp(lower, OPT_AUDIO_SOURCE_CALL) == 0 || strcmp(lower, "remote") == 0) {
1112 *audio_source = AUDIO_SOURCE_CALL;
1113 return true;
1114 }
1115 if (strcmp(lower, OPT_AUDIO_SOURCE_MIC) == 0) {
1116 *audio_source = AUDIO_SOURCE_MIC;
1117 return true;
1118 }
1119 if (strcmp(lower, OPT_AUDIO_SOURCE_MEDIA) == 0) {
1120 *audio_source = AUDIO_SOURCE_MEDIA;
1121 return true;
1122 }
1123
1124 if (error_msg) {
1125 *error_msg = platform_strdup("Audio source must be 'all', 'call', 'mic', or 'media'");
1126 }
1127 return false;
1128}
1129
1130bool parse_audio_capture_source(const char *arg, void *dest, char **error_msg) {
1131 if (!arg || !dest) {
1132 if (error_msg)
1133 *error_msg = platform_strdup("Internal error: NULL argument or destination");
1134 return false;
1135 }
1136
1139 to_lower(arg, lower, sizeof(lower));
1140 if (strcmp(lower, OPT_AUDIO_CAPTURE_SOURCE_AUTO) == 0)
1142 else if (strcmp(lower, OPT_AUDIO_CAPTURE_SOURCE_MIC) == 0)
1144 else if (strcmp(lower, OPT_AUDIO_CAPTURE_SOURCE_MEDIA) == 0)
1146 else if (strcmp(lower, OPT_AUDIO_CAPTURE_SOURCE_BOTH) == 0)
1148 else if (strcmp(lower, OPT_AUDIO_CAPTURE_SOURCE_REMOTE) == 0)
1150 else {
1151 if (error_msg)
1152 *error_msg = platform_strdup("Audio capture source must be 'auto', 'mic', 'media', 'both', or 'remote'");
1153 return false;
1154 }
1155 return true;
1156}
#define HAS_ERRNO(context)
Options builder API for flexible command-line option configuration.
bool is_session_string(const char *str)
Check if a string is a valid session string.
Option enum value definitions - single source of truth.
#define OPT_PALETTE_CUSTOM
Definition enums.h:125
#define OPT_AUDIO_SOURCE_CALL
Definition enums.h:140
#define OPT_LOG_LEVEL_DEV
Definition enums.h:89
#define OPT_AUDIO_SOURCE_AUTO
Definition enums.h:143
#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_AUDIO_CAPTURE_SOURCE_BOTH
Definition enums.h:149
#define OPT_AUDIO_CAPTURE_SOURCE_AUTO
Definition enums.h:146
#define OPT_AUDIO_CAPTURE_SOURCE_MEDIA
Definition enums.h:148
#define OPT_LOG_LEVEL_FATAL
Definition enums.h:94
#define OPT_LOG_LEVEL_ERROR
Definition enums.h:93
#define OPT_RENDER_MODE_HALF_BLOCK
Definition enums.h:132
#define OPT_COLOR_MODE_256
Definition enums.h:101
#define OPT_AUDIO_SOURCE_ALL
Definition enums.h:139
#define OPT_RENDER_MODE_FG
Definition enums.h:129
#define OPT_LOG_LEVEL_INFO
Definition enums.h:91
#define OPT_LOG_LEVEL_DEBUG
Definition enums.h:90
#define OPT_COLOR_MODE_NONE
Definition enums.h:98
#define OPT_PALETTE_MINIMAL
Definition enums.h:123
#define OPT_AUDIO_CAPTURE_SOURCE_REMOTE
Definition enums.h:150
#define OPT_PALETTE_BLOCKS
Definition enums.h:121
#define OPT_COLOR_FILTER_NONE
Definition enums.h:105
#define OPT_AUDIO_CAPTURE_SOURCE_MIC
Definition enums.h:147
#define OPT_RENDER_THEME_LIGHT
Definition enums.h:85
#define OPT_AUDIO_SOURCE_MEDIA
Definition enums.h:142
#define OPT_PALETTE_STANDARD
Definition enums.h:120
#define OPT_RENDER_THEME_DARK
Definition enums.h:84
#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_LOG_LEVEL_WARN
Definition enums.h:92
#define OPT_RENDER_MODE_BACKGROUND
Definition enums.h:130
#define OPT_COLOR_MODE_AUTO
Definition enums.h:97
#define OPT_COLOR_MODE_MONO
Definition enums.h:99
#define OPT_COLOR_MODE_16
Definition enums.h:100
#define OPT_AUDIO_SOURCE_MIC
Definition enums.h:141
int color_mode
Definition fft.c:26
audio_visualization_source_t source
Definition fft.c:25
terminal_render_mode_t render_mode
Definition fft.c:27
int platform_access(const char *pathname, int mode)
Definition filesystem.c:95
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
log_level_t
Logging levels enumeration.
Definition types.h:33
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define DEFAULT_LOG_LEVEL
Default log level for debug builds (DEBUG and above).
Definition log/log.h:68
#define SEC_PER_HOUR
Definition time.h:184
#define SEC_PER_MIN
Definition time.h:183
audio_capture_source_t
Definition options.h:291
#define OPTIONS_PALETTE_CUSTOM_BUFFER_SIZE
Definition options.h:211
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
#define OPTIONS_BUFF_SIZE
Buffer size for option string values.
Definition options.h:305
audio_source_t
Audio source selected for visualization.
Definition options.h:277
@ UTF8_SETTING_AUTO
Smart detection (default): UTF-8 if terminal supports it.
Definition options.h:265
@ UTF8_SETTING_FALSE
Force UTF-8 OFF: disable UTF-8 support.
Definition options.h:269
@ UTF8_SETTING_TRUE
Force UTF-8 ON: always use UTF-8 regardless of terminal capability.
Definition options.h:267
@ AUDIO_CAPTURE_SOURCE_BOTH
Definition options.h:291
@ AUDIO_CAPTURE_SOURCE_REMOTE
Definition options.h:291
@ AUDIO_CAPTURE_SOURCE_MEDIA
Definition options.h:291
@ AUDIO_CAPTURE_SOURCE_MIC
Definition options.h:291
@ AUDIO_CAPTURE_SOURCE_AUTO
Definition options.h:291
@ AUDIO_SOURCE_MIC
Definition options.h:283
@ AUDIO_SOURCE_CALL
Definition options.h:281
@ AUDIO_SOURCE_MEDIA
Definition options.h:285
@ AUDIO_SOURCE_ALL
Definition options.h:279
@ COLOR_SETTING_TRUE
Force colors ON: always colorize regardless of TTY/piping/CLAUDECODE.
Definition options.h:248
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
@ COLOR_SETTING_AUTO
Smart detection (default): colors if TTY and not CLAUDECODE and not piping.
Definition options.h:246
palette_type_t
Built-in palette type enumeration.
Definition palette.h:96
@ 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
int is_localhost_ipv6(const char *ip)
Check if IPv6 address is localhost.
Definition ip.c:1230
int is_valid_ipv4(const char *ip)
Check if a string is a valid IPv4 address.
Definition ip.c:32
int is_valid_ipv6(const char *ip)
Check if a string is a valid IPv6 address.
Definition ip.c:63
int is_localhost_ipv4(const char *ip)
Check if IPv4 address is localhost.
Definition ip.c:1209
asciichat_error_t path_validate_user_path(const char *input, path_role_t role, char **normalized_out)
Validate and canonicalize a user-supplied filesystem path.
Definition path.c:705
int parse_ipv6_address(const char *input, char *output, size_t output_size)
Parse IPv6 address, removing brackets if present.
Definition ip.c:116
@ PATH_ROLE_LOG_FILE
Definition path.h:324
color_filter_t color_filter_from_cli_name(const char *cli_name)
Convert color filter CLI name to enum value.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🌍 IP Address Parsing and Formatting Utilities
const char * asciichat_suggest_enum_value(const char *option_name, const char *input)
Suggest closest matching enum value using fuzzy matching.
Centralized enum and mode string conversion API.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
bool parse_render_mode(const char *arg, void *dest, char **error_msg)
Parse render mode option.
Definition parsers.c:238
int parse_server_bind_address(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg)
Parse server bind address positional argument.
Definition parsers.c:497
bool parse_audio_source(const char *arg, void *dest, char **error_msg)
Parse visualization source option (–audio-source).
Definition parsers.c:1095
bool parse_port_option(const char *arg, void *dest, char **error_msg)
Parse and validate port option for CLI.
Definition parsers.c:447
bool parse_timestamp(const char *arg, void *dest, char **error_msg)
Custom parser for –seek flag.
Definition parsers.c:938
#define OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE
Definition parsers.c:8
bool parse_palette_type(const char *arg, void *dest, char **error_msg)
Parse palette type option.
Definition parsers.c:321
bool parse_verbose_flag(const char *arg, void *dest, char **error_msg)
Custom parser for –verbose flag.
Definition parsers.c:900
bool parse_audio_capture_source(const char *arg, void *dest, char **error_msg)
Definition parsers.c:1130
bool parse_color_filter(const char *arg, void *dest, char **error_msg)
Parse color filter option.
Definition parsers.c:207
bool parse_color_setting(const char *arg, void *dest, char **error_msg)
Parse color setting option (–color flag).
Definition parsers.c:105
int parse_mirror_media(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg)
Parse mirror mode positional argument (file path or URL).
Definition parsers.c:806
bool parse_log_file(const char *arg, void *dest, char **error_msg)
Custom parser for log file paths (–log-file, -L).
Definition parsers.c:1055
bool parse_volume(const char *arg, void *dest, char **error_msg)
Custom parser for volume options (–volume, –speakers-volume, –microphone-volume).
Definition parsers.c:1023
bool parse_log_level(const char *arg, void *dest, char **error_msg)
Parse log level option.
Definition parsers.c:383
#define OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE
Definition parsers.c:10
int parse_client_address(const char *arg, void *config, char **remaining, int num_remaining, char **error_msg)
Parse client address positional argument.
Definition parsers.c:617
bool parse_render_theme(const char *arg, void *dest, char **error_msg)
Parse render theme option for pixel rendering.
Definition parsers.c:282
#define OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE
Definition parsers.c:9
bool parse_color_mode(const char *arg, void *dest, char **error_msg)
Parse terminal color level option.
Definition parsers.c:151
bool parse_utf8_setting(const char *arg, void *dest, char **error_msg)
Parse UTF-8 setting option (–utf8 flag).
Definition parsers.c:132
bool parse_palette_chars(const char *arg, void *dest, char **error_msg)
Parse custom palette characters option.
Definition parsers.c:856
#define OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE
Definition parsers.c:11
Custom option parsers for enum types.
📂 Path Manipulation Utilities
char * platform_strdup(const char *s)
🔤 String Manipulation and Shell Escaping Utilities
#define SESSION_STRING_BUFFER_SIZE
Immutable, RCU-published snapshot of the application's runtime configuration.
Definition options.h:974
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
palette_type_t palette_type
Selected palette type.
Definition options.h:1220
Lookup table for setting string-to-enum mapping.
Definition parsers.c:44
int enum_value
Corresponding enum value.
Definition parsers.c:46
const char * match
Setting string (lowercased).
Definition parsers.c:45
⏱️ High-precision timing utilities using sokol_time.h and uthash
#define LOG_DEV
Definition types.h:42
#define LOG_DEBUG
Definition types.h:43
#define LOG_FATAL
Definition types.h:47
#define LOG_ERROR
Definition types.h:46
#define LOG_WARN
Definition types.h:45
#define LOG_INFO
Definition types.h:44
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.
🔍 Safe Parsing Utilities