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 File Reference

Custom option parsers implementation. More...

Go to the source code of this file.

Data Structures

struct  setting_map_entry_t
 Lookup table for setting string-to-enum mapping. More...

Macros

#define OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE   32
#define OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE   256
#define OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE   512
#define OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE   256

Functions

bool parse_color_setting (const char *arg, void *dest, char **error_msg)
 Parse color setting option (–color flag).
bool parse_utf8_setting (const char *arg, void *dest, char **error_msg)
 Parse UTF-8 setting option (–utf8 flag).
bool parse_color_mode (const char *arg, void *dest, char **error_msg)
 Parse terminal color level option.
bool parse_color_filter (const char *arg, void *dest, char **error_msg)
 Parse color filter option.
bool parse_render_mode (const char *arg, void *dest, char **error_msg)
 Parse render mode option.
bool parse_render_theme (const char *arg, void *dest, char **error_msg)
 Parse render theme option for pixel rendering.
bool parse_palette_type (const char *arg, void *dest, char **error_msg)
 Parse palette type option.
bool parse_log_level (const char *arg, void *dest, char **error_msg)
 Parse log level option.
bool parse_port_option (const char *arg, void *dest, char **error_msg)
 Parse and validate port option for CLI.
int parse_server_bind_address (const char *arg, void *config, char **remaining, int num_remaining, char **error_msg)
 Parse server bind address positional argument.
int parse_client_address (const char *arg, void *config, char **remaining, int num_remaining, char **error_msg)
 Parse client address positional argument.
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).
bool parse_palette_chars (const char *arg, void *dest, char **error_msg)
 Parse custom palette characters option.
bool parse_verbose_flag (const char *arg, void *dest, char **error_msg)
 Custom parser for –verbose flag.
bool parse_timestamp (const char *arg, void *dest, char **error_msg)
 Custom parser for –seek flag.
bool parse_volume (const char *arg, void *dest, char **error_msg)
 Custom parser for volume options (–volume, –speakers-volume, –microphone-volume).
bool parse_log_file (const char *arg, void *dest, char **error_msg)
 Custom parser for log file paths (–log-file, -L).
bool parse_audio_source (const char *arg, void *dest, char **error_msg)
 Parse visualization source option (–audio-source).
bool parse_audio_capture_source (const char *arg, void *dest, char **error_msg)

Detailed Description

Custom option parsers implementation.

Definition in file parsers.c.

Macro Definition Documentation

◆ OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE

#define OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE   256

Definition at line 11 of file parsers.c.

Referenced by parse_volume().

◆ OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE

#define OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE   32

◆ OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE

◆ OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE

#define OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE   512

Definition at line 10 of file parsers.c.

Referenced by parse_client_address(), and parse_server_bind_address().

Function Documentation

◆ parse_audio_capture_source()

bool parse_audio_capture_source ( const char * arg,
void * dest,
char ** error_msg )

Parse local capture policy option (–audio-capture-source).

Definition at line 1130 of file parsers.c.

1130 {
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 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_AUDIO_CAPTURE_SOURCE_REMOTE
Definition enums.h:150
#define OPT_AUDIO_CAPTURE_SOURCE_MIC
Definition enums.h:147
audio_visualization_source_t source
Definition fft.c:25
audio_capture_source_t
Definition options.h:291
@ 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
#define OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE
Definition parsers.c:8
char * platform_strdup(const char *s)

References AUDIO_CAPTURE_SOURCE_AUTO, AUDIO_CAPTURE_SOURCE_BOTH, AUDIO_CAPTURE_SOURCE_MEDIA, AUDIO_CAPTURE_SOURCE_MIC, AUDIO_CAPTURE_SOURCE_REMOTE, OPT_AUDIO_CAPTURE_SOURCE_AUTO, OPT_AUDIO_CAPTURE_SOURCE_BOTH, OPT_AUDIO_CAPTURE_SOURCE_MEDIA, OPT_AUDIO_CAPTURE_SOURCE_MIC, OPT_AUDIO_CAPTURE_SOURCE_REMOTE, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, platform_strdup(), and source.

◆ parse_audio_source()

bool parse_audio_source ( const char * arg,
void * dest,
char ** error_msg )

Parse visualization source option (–audio-source).

Parameters
argString argument (e.g., "all", "call", "mic", "media")
destDestination pointer (audio_source_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values: all (the default), call, mic, or media.

Definition at line 1095 of file parsers.c.

1095 {
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}
#define OPT_AUDIO_SOURCE_CALL
Definition enums.h:140
#define OPT_AUDIO_SOURCE_AUTO
Definition enums.h:143
#define OPT_AUDIO_SOURCE_ALL
Definition enums.h:139
#define OPT_AUDIO_SOURCE_MEDIA
Definition enums.h:142
#define OPT_AUDIO_SOURCE_MIC
Definition enums.h:141
audio_source_t
Audio source selected for visualization.
Definition options.h:277
@ 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

References AUDIO_SOURCE_ALL, AUDIO_SOURCE_CALL, AUDIO_SOURCE_MEDIA, AUDIO_SOURCE_MIC, OPT_AUDIO_SOURCE_ALL, OPT_AUDIO_SOURCE_AUTO, OPT_AUDIO_SOURCE_CALL, OPT_AUDIO_SOURCE_MEDIA, OPT_AUDIO_SOURCE_MIC, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, and platform_strdup().

◆ parse_client_address()

int parse_client_address ( const char * arg,
void * config,
char ** remaining,
int num_remaining,
char ** error_msg )

Parse client address positional argument.

Implements the client address parsing logic from client.c. Parses [address][:port] with complex IPv6 handling.

Definition at line 617 of file parsers.c.

617 {
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}
bool is_session_string(const char *str)
Check if a string is a valid session string.
#define SAFE_SNPRINTF(buffer, buffer_size,...)
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
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
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
#define OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE
Definition parsers.c:10
#define OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE
Definition parsers.c:9
#define SESSION_STRING_BUFFER_SIZE
Immutable, RCU-published snapshot of the application's runtime configuration.
Definition options.h:974

References is_session_string(), is_valid_ipv4(), is_valid_ipv6(), log_debug, OPTIONS_BUFF_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE, platform_strdup(), SAFE_SNPRINTF, and SESSION_STRING_BUFFER_SIZE.

Referenced by options_preset_unified().

◆ parse_color_filter()

bool parse_color_filter ( const char * arg,
void * dest,
char ** error_msg )

Parse color filter option.

Parameters
argString argument (e.g., "green", "cyan", "pink")
destDestination pointer (color_filter_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values: none, black, white, green, magenta, fuchsia, orange, teal, cyan, pink, red, yellow

Definition at line 207 of file parsers.c.

207 {
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}
#define OPT_COLOR_FILTER_NONE
Definition enums.h:105
color_filter_t color_filter_from_cli_name(const char *cli_name)
Convert color filter CLI name to enum value.

References color_filter_from_cli_name(), OPT_COLOR_FILTER_NONE, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, and platform_strdup().

◆ parse_color_mode()

bool parse_color_mode ( const char * arg,
void * dest,
char ** error_msg )

Parse terminal color level option.

Parameters
argString argument (e.g., "auto", "none", "16", "256", "truecolor")
destDestination pointer (terminal_color_mode_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "auto", "a" - Auto-detect from terminal
  • "none", "mono", "monochrome", "0" - No color (TERM_COLOR_NONE)
  • "16", "16color", "ansi" - 16-color mode (TERM_COLOR_16)
  • "256", "256color" - 256-color mode (TERM_COLOR_256)
  • "truecolor", "true", "tc", "rgb", "24bit" - Truecolor mode (TERM_COLOR_TRUECOLOR)

Definition at line 151 of file parsers.c.

151 {
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}
#define OPT_COLOR_MODE_256
Definition enums.h:101
#define OPT_COLOR_MODE_NONE
Definition enums.h:98
#define OPT_COLOR_MODE_TRUECOLOR
Definition enums.h:102
#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
int color_mode
Definition fft.c:26
const char * asciichat_suggest_enum_value(const char *option_name, const char *input)
Suggest closest matching enum value using fuzzy matching.

References asciichat_suggest_enum_value(), color_mode, OPT_COLOR_MODE_16, OPT_COLOR_MODE_256, OPT_COLOR_MODE_AUTO, OPT_COLOR_MODE_MONO, OPT_COLOR_MODE_NONE, OPT_COLOR_MODE_TRUECOLOR, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, and platform_strdup().

◆ parse_color_setting()

bool parse_color_setting ( const char * arg,
void * dest,
char ** error_msg )

Parse color setting option (–color flag).

Parameters
argString argument (e.g., "auto", "true", "false")
destDestination pointer (int*, will store color_setting_t value)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "auto", "a", "0" - Smart detection (COLOR_SETTING_AUTO, default)
  • "true", "yes", "1", "on" - Force colors ON (COLOR_SETTING_TRUE)
  • "false", "no", "-1", "off" - Force colors OFF (COLOR_SETTING_FALSE)

This controls whether colors are enabled ("auto"), always on ("true"), or always off ("false") regardless of TTY detection or environment variables.

Definition at line 105 of file parsers.c.

105 {
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}
@ COLOR_SETTING_TRUE
Force colors ON: always colorize regardless of TTY/piping/CLAUDECODE.
Definition options.h:248

References COLOR_SETTING_TRUE, and platform_strdup().

◆ parse_log_file()

bool parse_log_file ( const char * arg,
void * dest,
char ** error_msg )

Custom parser for log file paths (–log-file, -L).

Validates that the log file path is safe:

  • Rejects attempts to write to protected system directories (/etc, /System, /Windows, etc.)
  • Allows overwriting existing ascii-chat log files
  • Allows paths in safe locations (/tmp, /var/log, home directory, cwd, etc.)
  • Returns error message if validation fails

Definition at line 1055 of file parsers.c.

1055 {
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}
#define HAS_ERRNO(context)
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
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
@ PATH_ROLE_LOG_FILE
Definition path.h:324

References ASCIICHAT_OK, asciichat_error_context_t::context_message, HAS_ERRNO, PATH_ROLE_LOG_FILE, path_validate_user_path(), platform_strdup(), SAFE_FREE, and SAFE_STRNCPY.

◆ parse_log_level()

bool parse_log_level ( const char * arg,
void * dest,
char ** error_msg )

Parse log level option.

Parameters
argString argument (e.g., "debug", "info", "warn")
destDestination pointer (log_level_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "dev", "development", "0" - Development level (LOG_DEV)
  • "debug", "dbg", "1" - Debug level (LOG_DEBUG)
  • "info", "information", "2" - Info level (LOG_INFO)
  • "warn", "warning", "3" - Warning level (LOG_WARN)
  • "error", "err", "4" - Error level (LOG_ERROR)
  • "fatal", "5" - Fatal level (LOG_FATAL)

Definition at line 383 of file parsers.c.

383 {
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}
#define OPT_LOG_LEVEL_DEV
Definition enums.h:89
#define OPT_LOG_LEVEL_FATAL
Definition enums.h:94
#define OPT_LOG_LEVEL_ERROR
Definition enums.h:93
#define OPT_LOG_LEVEL_INFO
Definition enums.h:91
#define OPT_LOG_LEVEL_DEBUG
Definition enums.h:90
#define OPT_LOG_LEVEL_WARN
Definition enums.h:92
log_level_t
Logging levels enumeration.
Definition types.h:33
#define DEFAULT_LOG_LEVEL
Default log level for debug builds (DEBUG and above).
Definition log/log.h:68
#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

References DEFAULT_LOG_LEVEL, LOG_DEBUG, LOG_DEV, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_WARN, OPT_LOG_LEVEL_DEBUG, OPT_LOG_LEVEL_DEV, OPT_LOG_LEVEL_ERROR, OPT_LOG_LEVEL_FATAL, OPT_LOG_LEVEL_INFO, OPT_LOG_LEVEL_WARN, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, and platform_strdup().

◆ parse_mirror_media()

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).

Parse mirror mode media positional argument.

Logic:

  1. Enforce mutual exclusion with –file and –url flags
  2. Check if argument is an accessible file path first
  3. If not a file, treat as URL (yt-dlp/ffmpeg will handle errors)
  4. Set media_file or media_url accordingly

Definition at line 806 of file parsers.c.

806 {
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}
int platform_access(const char *pathname, int mode)
Definition filesystem.c:95

References log_debug, OPTIONS_BUFF_SIZE, platform_access(), platform_strdup(), and SAFE_SNPRINTF.

Referenced by options_preset_unified().

◆ parse_palette_chars()

bool parse_palette_chars ( const char * arg,
void * dest,
char ** error_msg )

Parse custom palette characters option.

Parameters
argString of characters ordered from darkest to brightest
destDestination pointer (char[256])
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

The characters should be ordered from darkest (leftmost) to brightest (rightmost). Maximum length is 255 characters.

Example: " .:-=+*#%@"

Definition at line 856 of file parsers.c.

856 {
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}
#define OPTIONS_PALETTE_CUSTOM_BUFFER_SIZE
Definition options.h:211
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
palette_type_t palette_type
Selected palette type.
Definition options.h:1220

References OPTIONS_PALETTE_CUSTOM_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, PALETTE_CUSTOM, options_state::palette_explicit, options_state::palette_type, platform_strdup(), and SAFE_STRNCPY.

◆ parse_palette_type()

bool parse_palette_type ( const char * arg,
void * dest,
char ** error_msg )

Parse palette type option.

Parameters
argString argument (e.g., "standard", "blocks", "custom")
destDestination pointer (palette_type_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "standard", "std", "0" - Standard palette (PALETTE_STANDARD)
  • "blocks", "block", "1" - Blocks palette (PALETTE_BLOCKS)
  • "digital", "dig", "2" - Digital palette (PALETTE_DIGITAL)
  • "minimal", "min", "3" - Minimal palette (PALETTE_MINIMAL)
  • "cool", "4" - Cool palette (PALETTE_COOL)
  • "custom", "5" - Custom palette (PALETTE_CUSTOM)

Definition at line 321 of file parsers.c.

321 {
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}
#define OPT_PALETTE_CUSTOM
Definition enums.h:125
#define OPT_PALETTE_COOL
Definition enums.h:124
#define OPT_PALETTE_MINIMAL
Definition enums.h:123
#define OPT_PALETTE_BLOCKS
Definition enums.h:121
#define OPT_PALETTE_STANDARD
Definition enums.h:120
#define OPT_PALETTE_DIGITAL
Definition enums.h:122
palette_type_t
Built-in palette type enumeration.
Definition palette.h:96
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:102
@ 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

References asciichat_suggest_enum_value(), OPT_PALETTE_BLOCKS, OPT_PALETTE_COOL, OPT_PALETTE_CUSTOM, OPT_PALETTE_DIGITAL, OPT_PALETTE_MINIMAL, OPT_PALETTE_STANDARD, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, PALETTE_BLOCKS, PALETTE_COOL, PALETTE_CUSTOM, PALETTE_DIGITAL, PALETTE_MINIMAL, PALETTE_STANDARD, and platform_strdup().

◆ parse_port_option()

bool parse_port_option ( const char * arg,
void * dest,
char ** error_msg )

Parse and validate port option for CLI.

Parameters
argPort string to parse
destDestination pointer (int* for port storage)
error_msgError message output (set on failure, caller must free)
Returns
true on success, false on error

Validates port is a number in the range 1-65535.

Definition at line 447 of file parsers.c.

447 {
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}
unsigned short uint16_t
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.

References ASCIICHAT_OK, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, parse_port(), and platform_strdup().

◆ parse_render_mode()

bool parse_render_mode ( const char * arg,
void * dest,
char ** error_msg )

Parse render mode option.

Parameters
argString argument (e.g., "foreground", "background", "half-block")
destDestination pointer (terminal_render_mode_t*)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "foreground", "fg", "0" - Foreground mode (RENDER_MODE_FOREGROUND)
  • "background", "bg", "1" - Background mode (RENDER_MODE_BACKGROUND)
  • "half-block", "half", "hb", "2" - Half-block mode (RENDER_MODE_HALF_BLOCK)

Definition at line 238 of file parsers.c.

238 {
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}
#define OPT_RENDER_MODE_BG
Definition enums.h:131
#define OPT_RENDER_MODE_FOREGROUND
Definition enums.h:128
#define OPT_RENDER_MODE_HALF_BLOCK
Definition enums.h:132
#define OPT_RENDER_MODE_FG
Definition enums.h:129
#define OPT_RENDER_MODE_BACKGROUND
Definition enums.h:130
terminal_render_mode_t render_mode
Definition fft.c:27

References asciichat_suggest_enum_value(), OPT_RENDER_MODE_BACKGROUND, OPT_RENDER_MODE_BG, OPT_RENDER_MODE_FG, OPT_RENDER_MODE_FOREGROUND, OPT_RENDER_MODE_HALF_BLOCK, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, platform_strdup(), and render_mode.

◆ parse_render_theme()

bool parse_render_theme ( const char * arg,
void * dest,
char ** error_msg )

Parse render theme option for pixel rendering.

Parameters
argString argument (e.g., "dark", "light", "auto")
destDestination pointer (int*, will store term_renderer_theme_t value)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Controls the rendering theme when using pixel output. The theme determines color palette for ANSI text rendering.

Valid values:

  • "dark", "0" - Dark theme: dark background, light text (TERM_RENDERER_THEME_DARK)
  • "light", "1" - Light theme: light background, dark text (TERM_RENDERER_THEME_LIGHT)
  • "auto", "2" - Auto-detect theme from terminal (TERM_RENDERER_THEME_AUTO, default)

Definition at line 282 of file parsers.c.

282 {
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}
#define OPT_RENDER_THEME_LIGHT
Definition enums.h:85
#define OPT_RENDER_THEME_DARK
Definition enums.h:84
#define OPT_VALUE_AUTO
Definition enums.h:77

References OPT_RENDER_THEME_DARK, OPT_RENDER_THEME_LIGHT, OPT_VALUE_AUTO, OPTIONS_PARSING_PARSERS_LOWER_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, and platform_strdup().

◆ parse_server_bind_address()

int parse_server_bind_address ( const char * arg,
void * config,
char ** remaining,
int num_remaining,
char ** error_msg )

Parse server bind address positional argument.

Implements the server bind address parsing logic from server.c. Can consume 1 argument per call, handling IPv4 or IPv6 bind addresses. The positional arg system will call this multiple times for multiple args.

Definition at line 497 of file parsers.c.

497 {
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}
int is_localhost_ipv6(const char *ip)
Check if IPv6 address is localhost.
Definition ip.c:1230
int is_localhost_ipv4(const char *ip)
Check if IPv4 address is localhost.
Definition ip.c:1209
int parse_ipv6_address(const char *input, char *output, size_t output_size)
Parse IPv6 address, removing brackets if present.
Definition ip.c:116

References is_localhost_ipv4(), is_localhost_ipv6(), is_valid_ipv4(), is_valid_ipv6(), OPTIONS_BUFF_SIZE, OPTIONS_PARSING_PARSERS_MSG_BUFFER_SIZE, OPTIONS_PARSING_PARSERS_MSG_EXTENDED_BUFFER_SIZE, parse_ipv6_address(), platform_strdup(), and SAFE_SNPRINTF.

Referenced by options_preset_unified().

◆ parse_timestamp()

bool parse_timestamp ( const char * arg,
void * dest,
char ** error_msg )

Custom parser for –seek flag.

Custom parser for –timestamp flag.

Accepts both "hh:mm:ss.ms" format and plain seconds format. Examples:

  • "30" = 30 seconds
  • "30.5" = 30.5 seconds
  • "1:30" = 1 minute 30 seconds (90 seconds)
  • "1:30.5" = 1 minute 30.5 seconds (90.5 seconds)
  • "0:1:30.5" = 1 minute 30.5 seconds (90.5 seconds)
  • "1:2:30.5" = 1 hour 2 minutes 30.5 seconds (3750.5 seconds)

Definition at line 938 of file parsers.c.

938 {
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}
#define SEC_PER_HOUR
Definition time.h:184
#define SEC_PER_MIN
Definition time.h:183

References platform_strdup(), SEC_PER_HOUR, and SEC_PER_MIN.

◆ parse_utf8_setting()

bool parse_utf8_setting ( const char * arg,
void * dest,
char ** error_msg )

Parse UTF-8 setting option (–utf8 flag).

Parameters
argString argument (e.g., "auto", "true", "false"), or NULL/empty for default
destDestination pointer (int*, will store utf8_setting_t value)
error_msgOptional error message output (set on failure)
Returns
true on success, false on error

Valid values:

  • "auto", "a", "0" - Smart detection (UTF8_SETTING_AUTO, default)
  • "true", "yes", "1", "on" - Force UTF-8 ON (UTF8_SETTING_TRUE)
  • "false", "no", "-1", "off" - Force UTF-8 OFF (UTF8_SETTING_FALSE)

When no argument is provided (–utf8 with no value), defaults to UTF8_SETTING_TRUE. This controls whether UTF-8 is auto-detected ("auto"), always on ("true"), or always off ("false") regardless of terminal capability detection.

Definition at line 132 of file parsers.c.

132 {
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}
@ UTF8_SETTING_TRUE
Force UTF-8 ON: always use UTF-8 regardless of terminal capability.
Definition options.h:267

References platform_strdup(), and UTF8_SETTING_TRUE.

◆ parse_verbose_flag()

bool parse_verbose_flag ( const char * arg,
void * dest,
char ** error_msg )

Custom parser for –verbose flag.

Allows –verbose to work both as a flag (without argument) and with an optional count argument. Increments verbose_level each time called.

Definition at line 900 of file parsers.c.

900 {
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}

◆ parse_volume()

bool parse_volume ( const char * arg,
void * dest,
char ** error_msg )

Custom parser for volume options (–volume, –speakers-volume, –microphone-volume).

Validates that the volume is a float value between 0.0 and 1.0. Sets the destination float to the parsed volume value.

Definition at line 1023 of file parsers.c.

1023 {
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}
#define OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE
Definition parsers.c:11

References OPTIONS_PARSING_PARSERS_BUF_BUFFER_SIZE, platform_strdup(), and SAFE_SNPRINTF.