ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
zsh.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define OPTIONS_COMPLETIONS_ZSH_NAME_BUFFER_SIZE 256
9
10#include <string.h>
11#include <stdio.h>
16#include <ascii-chat/common.h>
17
21static void zsh_escape_desc(FILE *output, const char *text) {
22 if (!text) {
23 return;
24 }
25
26 for (const char *p = text; *p; p++) {
27 switch (*p) {
28 case '\'':
29 // Escape single quotes: end quote, escaped quote, start quote
30 fprintf(output, "'\\''");
31 break;
32 case ':':
33 case '|':
34 case '[':
35 case ']':
36 // Escape special zsh characters that conflict with completion syntax
37 fprintf(output, "\\%c", *p);
38 break;
39 case '\\':
40 // Escape backslashes
41 fprintf(output, "\\\\");
42 break;
43 case '\n':
44 case '\t':
45 fprintf(output, " ");
46 break;
47 default:
48 fputc(*p, output);
49 }
50 }
51}
52
54static void zsh_write_combined_args(FILE *output, const option_descriptor_t *opts, size_t count, bool modes) {
55 if (!opts || count == 0)
56 return;
57
58 fprintf(output, " local -a args=(\n");
59
60 for (size_t i = 0; i < count; i++) {
61 for (int spelling = 0; spelling < (opts[i].short_name ? 2 : 1); ++spelling) {
62 fprintf(output, " '");
64 if (spelling == 0)
65 SAFE_SNPRINTF(name, sizeof(name), "--%s", opts[i].long_name);
66 else
67 SAFE_SNPRINTF(name, sizeof(name), "-%c", opts[i].short_name);
68
69 // Device index options: call helper functions
70 if (strcmp(opts[i].long_name, "webcam-index") == 0) {
71 fprintf(output, "%s=[", name);
72 zsh_escape_desc(output, opts[i].help_text);
73 fprintf(output, "]:device index:_ascii_chat_webcam_indices'\n");
74 } else if (strcmp(opts[i].long_name, "microphone-index") == 0) {
75 fprintf(output, "%s=[", name);
76 zsh_escape_desc(output, opts[i].help_text);
77 fprintf(output, "]:device index:_ascii_chat_microphone_indices'\n");
78 } else if (strcmp(opts[i].long_name, "speakers-index") == 0) {
79 fprintf(output, "%s=[", name);
80 zsh_escape_desc(output, opts[i].help_text);
81 fprintf(output, "]:device index:_ascii_chat_speakers_indices'\n");
82 }
83 // Enum options: full spec with values and descriptions
84 else if (opts[i].metadata.input_type == OPTION_INPUT_ENUM && opts[i].metadata.enum_values) {
85 fprintf(output, "%s=[", name);
86 zsh_escape_desc(output, opts[i].help_text);
87 fprintf(output, "]%s%s:((\\\n", opts[i].optional_arg ? "::" : ":", opts[i].long_name);
88
89 for (size_t j = 0; opts[i].metadata.enum_values[j] != NULL; j++) {
90 fprintf(output, " %s", opts[i].metadata.enum_values[j]);
91 if (opts[i].metadata.enum_descriptions && opts[i].metadata.enum_descriptions[j]) {
92 fprintf(output, "\\:\"");
93 zsh_escape_desc(output, opts[i].metadata.enum_descriptions[j]);
94 fprintf(output, "\"");
95 }
96 fprintf(output, "\\\n");
97 }
98 fprintf(output, " ))'\n");
99 }
100 // Boolean options with values
101 else if (opts[i].type == OPTION_TYPE_BOOL) {
102 fprintf(output, "%s=[", name);
103 zsh_escape_desc(output, opts[i].help_text);
104 fprintf(output, "]::value:((" OPT_VALUE_TRUE "\\:\"enable\" " OPT_VALUE_FALSE "\\:\"disable\"))'\n");
105 }
106 // Action options (no value)
107 else if (opts[i].type == OPTION_TYPE_ACTION) {
108 fprintf(output, "%s[", name);
109 zsh_escape_desc(output, opts[i].help_text);
110 fprintf(output, "]'\n");
111 } else if (opts[i].metadata.input_type == OPTION_INPUT_FILEPATH || (opts[i].arg_placeholder && strstr(opts[i].arg_placeholder, "FILE"))) {
112 fprintf(output, "%s=[", name);
113 zsh_escape_desc(output, opts[i].help_text);
114 fprintf(output, "]:file:_files'\n");
115 } else if (opts[i].metadata.examples) {
116 fprintf(output, "%s=[", name);
117 zsh_escape_desc(output, opts[i].help_text);
118 fprintf(output, "]%svalue:(", opts[i].optional_arg ? "::" : ":");
119 for (size_t j = 0; opts[i].metadata.examples[j]; ++j) {
120 if (j)
121 fputc(' ', output);
122 zsh_escape_desc(output, opts[i].metadata.examples[j]);
123 }
124 fprintf(output, ")'\n");
125 }
126 // Other options
127 else {
128 fprintf(output, "%s=[", name);
129 zsh_escape_desc(output, opts[i].help_text);
130 fprintf(output, "]%svalue:'\n", opts[i].optional_arg ? "::" : ":");
131 }
132 }
133 }
134 fprintf(output, " )\n\n");
135 fprintf(output, " _arguments -C -s -S \"${args[@]}\" ");
136 if (modes)
137 fprintf(output, "'1:mode:_ascii_chat_modes' ");
138 fprintf(output, "'*:argument:'\n\n");
139}
140
142 if (!output) {
143 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
144 }
145
146 // Before a mode, expose the complete registry; after a mode, use its own options.
147 size_t binary_count = 0;
148 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
149
150 fprintf(output, "# Zsh completion script for ascii-chat\n"
151 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
152 "\n"
153 "# Configure completion for ascii-chat\n"
154 "# Use only _complete (no corrections or approximate matching)\n"
155 "zstyle ':completion:*:ascii-chat:*' completer _complete\n"
156 "\n"
157 "# Device index completion helpers\n"
158 "_ascii_chat_webcam_indices() {\n"
159 " local -a indices\n"
160 " local output\n"
161 " output=$(\"${words[1]}\" --list-webcams 2>/dev/null) || return\n"
162 " while IFS= read -r line; do\n"
163 " [[ \"$line\" =~ ^[[:space:]]*([0-9]+)[[:space:]]+(.+)$ ]] && \\\n"
164 " indices+=(\"${match[1]}:${match[2]}\")\n"
165 " done <<< \"$output\"\n"
166 " _describe 'webcam' indices\n"
167 "}\n"
168 "\n"
169 "_ascii_chat_microphone_indices() {\n"
170 " local -a indices\n"
171 " local output\n"
172 " output=$(\"${words[1]}\" --list-microphones 2>/dev/null) || return\n"
173 " while IFS= read -r line; do\n"
174 " [[ \"$line\" =~ ^[[:space:]]*([0-9-]+)[[:space:]]+(.+)$ ]] && \\\n"
175 " indices+=(\"${match[1]}:${match[2]}\")\n"
176 " done <<< \"$output\"\n"
177 " _describe 'microphone' indices\n"
178 "}\n"
179 "\n"
180 "_ascii_chat_speakers_indices() {\n"
181 " local -a indices\n"
182 " local output\n"
183 " output=$(\"${words[1]}\" --list-speakers 2>/dev/null) || return\n"
184 " while IFS= read -r line; do\n"
185 " [[ \"$line\" =~ ^[[:space:]]*([0-9-]+)[[:space:]]+(.+)$ ]] && \\\n"
186 " indices+=(\"${match[1]}:${match[2]}\")\n"
187 " done <<< \"$output\"\n"
188 " _describe 'speakers' indices\n"
189 "}\n"
190 "\n"
191 "_ascii_chat_binary_grouped() {\n"
192 " local curcontext=$curcontext\n"
193 " local -a context line state_descr args\n"
194 " local state\n"
195 " local -A opt_args\n\n");
196
197 /* Write binary-level options in grouped format */
198 if (binary_opts) {
199 zsh_write_combined_args(output, binary_opts, binary_count, true);
200 }
201
202 fprintf(output, "}\n\n_ascii_chat() {\n"
203 " local i mode=''\n"
204 " for (( i=2; i<CURRENT; ++i )); do\n"
205 " case $words[i] in\n"
206 " --) break ;;\n"
207 " --*=*) continue ;;\n");
208 // Skip required option values so a file called 'mirror' is not a mode.
209 for (size_t i = 0; i < binary_count; ++i) {
210 const option_descriptor_t *opt = &binary_opts[i];
211 if (opt->type == OPTION_TYPE_ACTION || opt->type == OPTION_TYPE_BOOL || opt->optional_arg)
212 continue;
213 fprintf(output, " --%s", opt->long_name);
214 if (opt->short_name)
215 fprintf(output, "|-%c", opt->short_name);
216 fprintf(output, ") (( ++i )) ;;\n");
217 }
218 fprintf(output, " server|client|mirror|discovery-service|acds|mcp-server)\n"
219 " mode=$words[i]\n"
220 " words=(\"$words[1]\" \"${(@)words[i+1,-1]}\")\n"
221 " (( CURRENT -= i - 1 ))\n"
222 " break ;;\n"
223 " esac\n"
224 " done\n"
225 " case $mode in\n"
226 " # Server-like modes\n"
227 " server)\n"
228 " _ascii_chat_server\n"
229 " return\n"
230 " ;;\n"
231 " discovery-service|acds)\n"
232 " _ascii_chat_discovery_service\n"
233 " return\n"
234 " ;;\n"
235 "\n"
236 " mcp-server)\n"
237 " _ascii_chat_mcp_server\n"
238 " return\n"
239 " ;;\n"
240 " # Client-like modes\n"
241 " client)\n"
242 " _ascii_chat_client\n"
243 " return\n"
244 " ;;\n"
245 " mirror)\n"
246 " _ascii_chat_mirror\n"
247 " return\n"
248 " ;;\n"
249 " *) _ascii_chat_binary_grouped ;;\n"
250 " esac\n"
251 "}\n\n"
252 "_ascii_chat_modes() {\n"
253 " local -a server_modes client_modes\n"
254 " server_modes=(\n");
255
256 /* Generate server-like modes from registry */
257 size_t mode_server_count = 0;
258 const mode_descriptor_t *mode_server_descs = get_modes_by_group("server-like", &mode_server_count);
259 if (mode_server_descs) {
260 for (size_t i = 0; i < mode_server_count; i++) {
261 fprintf(output, " '%s:%s'\n", mode_server_descs[i].name, mode_server_descs[i].description);
262 }
263 SAFE_FREE(mode_server_descs);
264 }
265
266 fprintf(output, " )\n"
267 " client_modes=(\n");
268
269 /* Generate client-like modes from registry (exclude discovery since it's the default mode) */
270 size_t mode_client_count = 0;
271 const mode_descriptor_t *mode_client_descs = get_modes_by_group("client-like", &mode_client_count);
272 if (mode_client_descs) {
273 for (size_t i = 0; i < mode_client_count; i++) {
274 /* Skip discovery mode - it's the default when no mode is specified */
275 if (strcmp(mode_client_descs[i].name, "discovery") == 0) {
276 continue;
277 }
278 fprintf(output, " '%s:%s'\n", mode_client_descs[i].name, mode_client_descs[i].description);
279 }
280 SAFE_FREE(mode_client_descs);
281 }
282
283 fprintf(output, " )\n"
284 " _describe -t server-modes 'server-like modes' server_modes\n"
285 " _describe -t client-modes 'client-like modes' client_modes\n"
286 "}\n\n"
287 "# Server-like modes: handle incoming connections and stream management\n"
288 "_ascii_chat_server() {\n"
289 " local curcontext=$curcontext\n"
290 " local -a context line state_descr args\n"
291 " local state\n"
292 " local -A opt_args\n\n");
293
294 /* Server options - grouped by category */
295 size_t server_count = 0;
296 const option_descriptor_t *server_opts = options_registry_get_for_display(MODE_SERVER, false, &server_count);
297
298 if (server_opts) {
299 zsh_write_combined_args(output, server_opts, server_count, false);
300 SAFE_FREE(server_opts);
301 }
302
303 fprintf(output, "}\n"
304 "\n"
305 "_ascii_chat_discovery_service() {\n"
306 " local curcontext=$curcontext\n"
307 " local -a context line state_descr args\n"
308 " local state\n"
309 " local -A opt_args\n\n");
310
311 /* Discovery-service options - grouped by category */
312 size_t discovery_svc_count = 0;
313 const option_descriptor_t *discovery_svc_opts = options_registry_get_for_display(MODE_DISCOVERY_SERVICE, false, &discovery_svc_count);
314
315 if (discovery_svc_opts) {
316 zsh_write_combined_args(output, discovery_svc_opts, discovery_svc_count, false);
317 SAFE_FREE(discovery_svc_opts);
318 }
319
320 fprintf(output, "}\n"
321 "\n"
322 "# Client-like modes: connect to servers or render local media\n"
323 "_ascii_chat_client() {\n"
324 " local curcontext=$curcontext\n"
325 " local -a context line state_descr args\n"
326 " local state\n"
327 " local -A opt_args\n\n");
328
329 /* Client options - grouped by category */
330 size_t client_count = 0;
331 const option_descriptor_t *client_opts = options_registry_get_for_display(MODE_CLIENT, false, &client_count);
332
333 if (client_opts) {
334 zsh_write_combined_args(output, client_opts, client_count, false);
335 SAFE_FREE(client_opts);
336 }
337
338 fprintf(output, "}\n"
339 "\n"
340 "_ascii_chat_mirror() {\n"
341 " local curcontext=$curcontext\n"
342 " local -a context line state_descr args\n"
343 " local state\n"
344 " local -A opt_args\n\n");
345
346 /* Mirror options - grouped by category */
347 size_t mirror_count = 0;
348 const option_descriptor_t *mirror_opts = options_registry_get_for_display(MODE_MIRROR, false, &mirror_count);
349
350 if (mirror_opts) {
351 zsh_write_combined_args(output, mirror_opts, mirror_count, false);
352 SAFE_FREE(mirror_opts);
353 }
354
355 fprintf(output, "}\n"
356 "\n"
357 "# Register completion functions (must come after definitions)\n"
358 "compdef _ascii_chat ascii-chat\n"
359 "compdef _ascii_chat 'build/bin/ascii-chat'\n");
360
361 fprintf(output, "_ascii_chat_mcp_server() {\n"
362 " local curcontext=$curcontext\n"
363 " local -a context line state_descr args\n"
364 " local state\n"
365 " local -A opt_args\n\n");
366 size_t mcp_count = 0;
367 const option_descriptor_t *mcp_opts = options_registry_get_for_display(MODE_MCP_SERVER, false, &mcp_count);
368 if (mcp_opts) {
369 zsh_write_combined_args(output, mcp_opts, mcp_count, false);
370 SAFE_FREE(mcp_opts);
371 }
372 fprintf(output, "}\n\n");
373
374 SAFE_FREE(binary_opts);
375
376 return ASCIICHAT_OK;
377}
#define SET_ERRNO(code,...)
@ OPTION_INPUT_FILEPATH
File path completion.
Definition builder.h:190
@ OPTION_INPUT_ENUM
Choose from fixed set of enum values.
Definition builder.h:187
@ OPTION_TYPE_ACTION
Action that executes and may exit (–list-webcams, etc.).
Definition builder.h:176
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
Option enum value definitions - single source of truth.
#define OPT_VALUE_FALSE
Definition enums.h:79
#define OPT_VALUE_TRUE
Definition enums.h:78
#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
@ ERROR_INVALID_PARAM
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:918
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:919
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Mode-aware default value getters and mode metadata.
Central registry of all command-line options with mode applicability.
const mode_descriptor_t * get_modes_by_group(const char *group, size_t *out_count)
Get all mode descriptors grouped by category.
const option_descriptor_t * options_registry_get_for_display(asciichat_mode_t mode, bool for_binary_help, size_t *num_options)
Get options for help/completions display with unified filtering.
Definition public_api.c:204
🔤 String Manipulation and Shell Escaping Utilities
The complete registry entry for one command-line/configuration option.
Definition builder.h:251
const char * long_name
Long option name (e.g., "port").
Definition builder.h:253
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:254
option_metadata_t metadata
Metadata for shell completions (enums, ranges, examples, etc.).
Definition builder.h:291
option_type_t type
Value type.
Definition builder.h:257
bool optional_arg
If true, argument is optional (for callbacks like –verbose).
Definition builder.h:285
const char ** enum_values
Enum value strings (null-terminated, e.g., {"auto", "none", "16", "256", "truecolor",...
Definition builder.h:203
const char ** examples
Example values or command invocations (null-terminated array).
Definition builder.h:215
#define OPTIONS_COMPLETIONS_ZSH_NAME_BUFFER_SIZE
Definition zsh.c:8
asciichat_error_t completions_generate_zsh(FILE *output)
Generate zsh completion script.
Definition zsh.c:141
Zsh shell completion generator.