ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
bash.c
Go to the documentation of this file.
1
8
9#include <string.h>
10#include <stdio.h>
11#include <stdbool.h>
16#include <ascii-chat/common.h>
17
21static void bash_escape_help(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 fprintf(output, "'\\''");
30 break;
31 case '\n':
32 fprintf(output, "\\n");
33 break;
34 case '\t':
35 fprintf(output, "\\t");
36 break;
37 default:
38 fputc(*p, output);
39 }
40 }
41}
42
46static void bash_write_option(FILE *output, const option_descriptor_t *opt) {
47 if (!opt) {
48 return;
49 }
50
51 if (opt->short_name != '\0') {
52 fprintf(output, " '-%c' '", opt->short_name);
53 bash_escape_help(output, opt->help_text);
54 fprintf(output, "'\n");
55 }
56
57 fprintf(output, " '--%s' '", opt->long_name);
58 bash_escape_help(output, opt->help_text);
59 fprintf(output, "'\n");
60}
61
65static void bash_write_header(FILE *output) {
66 fprintf(output, "# Bash completion script for ascii-chat\n"
67 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
68 "# Usage: eval \"$(ascii-chat --completions bash)\"\n"
69 "\n"
70 "_ascii_chat() {\n"
71 " local cur prev words cword\n"
72 " _init_completion || return\n"
73 "\n");
74}
75
79static void bash_write_footer(FILE *output) {
80 fprintf(output, "}\n"
81 "\n"
82 "complete -F _ascii_chat ascii-chat\n");
83}
84
91static asciichat_error_t bash_write_all_options(FILE *output) {
92 /* Binary-level options - use MODE_DISCOVERY (the default mode) with for_binary_help=true
93 * This matches how options_print_help_for_mode() filters options for '--help' display */
94 size_t binary_count = 0;
95 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
96
97 fprintf(output, " # Binary-level options (same as 'ascii-chat --help')\n local -a binary_opts=(\n");
98 if (binary_opts) {
99 for (size_t i = 0; i < binary_count; i++) {
100 bash_write_option(output, &binary_opts[i]);
101 }
102 SAFE_FREE(binary_opts);
103 }
104 fprintf(output, " )\n\n");
105
106 /* Server options - mode-specific with for_binary_help=false
107 * This matches how options_print_help_for_mode() filters for 'ascii-chat server --help' */
108 size_t server_count = 0;
109 const option_descriptor_t *server_opts = options_registry_get_for_display(MODE_SERVER, false, &server_count);
110
111 fprintf(output, " # Server-mode options (same as 'ascii-chat server --help')\n local -a server_opts=(\n");
112 if (server_opts) {
113 for (size_t i = 0; i < server_count; i++) {
114 bash_write_option(output, &server_opts[i]);
115 }
116 SAFE_FREE(server_opts);
117 }
118 fprintf(output, " )\n\n");
119
120 /* Client options - mode-specific with for_binary_help=false */
121 size_t client_count = 0;
122 const option_descriptor_t *client_opts = options_registry_get_for_display(MODE_CLIENT, false, &client_count);
123
124 fprintf(output, " # Client-mode options (same as 'ascii-chat client --help')\n local -a client_opts=(\n");
125 if (client_opts) {
126 for (size_t i = 0; i < client_count; i++) {
127 bash_write_option(output, &client_opts[i]);
128 }
129 SAFE_FREE(client_opts);
130 }
131 fprintf(output, " )\n\n");
132
133 /* Mirror options - mode-specific with for_binary_help=false */
134 size_t mirror_count = 0;
135 const option_descriptor_t *mirror_opts = options_registry_get_for_display(MODE_MIRROR, false, &mirror_count);
136
137 fprintf(output, " # Mirror-mode options (same as 'ascii-chat mirror --help')\n local -a mirror_opts=(\n");
138 if (mirror_opts) {
139 for (size_t i = 0; i < mirror_count; i++) {
140 bash_write_option(output, &mirror_opts[i]);
141 }
142 SAFE_FREE(mirror_opts);
143 }
144 fprintf(output, " )\n\n");
145
146 /* Discovery-service options */
147 size_t discovery_svc_count = 0;
148 const option_descriptor_t *discovery_svc_opts = options_registry_get_for_display(MODE_DISCOVERY_SERVICE, false, &discovery_svc_count);
149
150 fprintf(output, " # Discovery-service options (same as 'ascii-chat discovery-service --help')\n local -a "
151 "discovery_svc_opts=(\n");
152 if (discovery_svc_opts) {
153 for (size_t i = 0; i < discovery_svc_count; i++) {
154 bash_write_option(output, &discovery_svc_opts[i]);
155 }
156 SAFE_FREE(discovery_svc_opts);
157 }
158 fprintf(output, " )\n\n");
159
160 size_t mcp_count = 0;
161 const option_descriptor_t *mcp_opts = options_registry_get_for_display(MODE_MCP_SERVER, false, &mcp_count);
162 fprintf(output, " local -a mcp_opts=(\n");
163 for (size_t i = 0; i < mcp_count; i++) {
164 bash_write_option(output, &mcp_opts[i]);
165 }
166 SAFE_FREE(mcp_opts);
167 fprintf(output, " )\n\n");
168
169 return ASCIICHAT_OK;
170}
171
175static void bash_write_enum_cases(FILE *output) {
176 size_t combined_count = 0;
177 option_descriptor_t *combined_opts = completions_collect_all_modes_unique(&combined_count);
178
179 // Also add binary-level options (--log-level, --log-file, etc.)
180 size_t binary_count = 0;
181 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
182
183 if (binary_opts) {
184 for (size_t i = 0; i < binary_count; i++) {
185 // Check if already in combined_opts
186 bool already_has = false;
187 for (size_t j = 0; j < combined_count; j++) {
188 if (strcmp(combined_opts[j].long_name, binary_opts[i].long_name) == 0) {
189 already_has = true;
190 break;
191 }
192 }
193 if (!already_has) {
194 combined_count++;
195 option_descriptor_t *temp = (option_descriptor_t *)SAFE_REALLOC(combined_opts, combined_count * sizeof(option_descriptor_t), option_descriptor_t *);
196 if (temp) {
197 combined_opts = temp;
198 combined_opts[combined_count - 1] = binary_opts[i];
199 }
200 }
201 }
202 SAFE_FREE(binary_opts);
203 }
204
205 if (!combined_opts) {
206 return;
207 }
208
209 for (size_t i = 0; i < combined_count; i++) {
210 const option_descriptor_t *opt = &combined_opts[i];
211 const option_metadata_t *meta = &opt->metadata;
212
213 // Skip options with no enum or examples
214 bool has_examples = meta->examples && meta->examples[0] != NULL;
215 if (meta->input_type != OPTION_INPUT_ENUM && !has_examples && meta->numeric_range.max == 0) {
216 continue;
217 }
218
219 // Write case statement
220 if (opt->short_name != '\0') {
221 fprintf(output, " -%c | ", opt->short_name);
222 }
223 fprintf(output, "--%-25s)\n", opt->long_name);
224 fprintf(output, " COMPREPLY=($(compgen -W \"");
225
226 // Write enum values
227 if (meta->input_type == OPTION_INPUT_ENUM && meta->enum_values && meta->enum_values[0] != NULL) {
228 for (size_t j = 0; meta->enum_values[j] != NULL; j++) {
229 fprintf(output, "%s", meta->enum_values[j]);
230 if (meta->enum_values[j + 1] != NULL) {
231 fprintf(output, " ");
232 }
233 }
234 }
235 // Check for examples first (more practical than calculated ranges)
236 else if (has_examples) {
237 for (size_t j = 0; meta->examples[j] != NULL; j++) {
238 fprintf(output, "%s", meta->examples[j]);
239 if (meta->examples[j + 1] != NULL) {
240 fprintf(output, " ");
241 }
242 }
243 }
244 // Fallback to numeric range if no examples
245 else if (meta->input_type == OPTION_INPUT_NUMERIC) {
246 if (meta->numeric_range.min == 1 && meta->numeric_range.max == 9) {
247 fprintf(output, "1 2 3 4 5 6 7 8 9");
248 } else if (meta->numeric_range.max > 0) {
249 fprintf(output, "%d %d %d", meta->numeric_range.min, (meta->numeric_range.min + meta->numeric_range.max) / 2, meta->numeric_range.max);
250 }
251 }
252
253 fprintf(output, "\" -- \"$cur\"))\n");
254 fprintf(output, " return\n");
255 fprintf(output, " ;;\n");
256 }
257
258 SAFE_FREE(combined_opts);
259}
260
264static void bash_write_completion_logic(FILE *output) {
265 fprintf(output, " # Modes\n"
266 " local modes=\"server client mirror discovery-service mcp-server\"\n"
267 "\n"
268 " # Detect which mode we're in\n"
269 " local mode=\"\"\n"
270 " local i\n"
271 " for ((i = 1; i < cword; i++)); do\n"
272 " case \"${words[i]}\" in\n"
273 " server | client | mirror | discovery-service | mcp-server)\n"
274 " mode=\"${words[i]}\"\n"
275 " break\n"
276 " ;;\n"
277 " esac\n"
278 " done\n"
279 "\n"
280 " case \"$prev\" in\n"
281 " # Options that take file paths\n");
282
283 // Generate file path options dynamically from registry (collect from all modes)
284 size_t combined_count = 0;
285 option_descriptor_t *combined_opts = completions_collect_all_modes_unique(&combined_count);
286
287 if (combined_opts) {
288 bool first = true;
289 for (size_t i = 0; i < combined_count; i++) {
290 const option_descriptor_t *opt = &combined_opts[i];
292
293 if (meta && meta->input_type == OPTION_INPUT_FILEPATH) {
294 if (!first) {
295 fprintf(output, " | ");
296 }
297 first = false;
298
299 if (opt->short_name != '\0') {
300 fprintf(output, "-%c | ", opt->short_name);
301 }
302 fprintf(output, "--%s", opt->long_name);
303 }
304 }
305 SAFE_FREE(combined_opts);
306
307 if (!first) {
308 fprintf(output, ")\n");
309 fprintf(output, " _filedir\n");
310 fprintf(output, " return\n");
311 fprintf(output, " ;;\n");
312 }
313 }
314
315 /* Write enum cases */
316 bash_write_enum_cases(output);
317
318 fprintf(output, " esac\n"
319 "\n"
320 " # If current word starts with -, complete options\n"
321 " if [[ \"$cur\" == -* ]]; then\n"
322 " local -a opts_to_complete\n"
323 "\n"
324 " case \"$mode\" in\n"
325 " client)\n"
326 " opts_to_complete=(\"${binary_opts[@]}\" \"${client_opts[@]}\")\n"
327 " ;;\n"
328 " server)\n"
329 " opts_to_complete=(\"${binary_opts[@]}\" \"${server_opts[@]}\")\n"
330 " ;;\n"
331 " mirror)\n"
332 " opts_to_complete=(\"${binary_opts[@]}\" \"${mirror_opts[@]}\")\n"
333 " ;;\n"
334 " mcp-server)\n"
335 " opts_to_complete=(\"${binary_opts[@]}\" \"${mcp_opts[@]}\")\n"
336 " ;;\n"
337 " *)\n"
338 " opts_to_complete=(\"${binary_opts[@]}\")\n"
339 " ;;\n"
340 " esac\n"
341 "\n"
342 " # Generate completions with help text\n"
343 " local -a completions\n"
344 " for ((i = 0; i < ${#opts_to_complete[@]}; i += 2)); do\n"
345 " if [[ \"${opts_to_complete[i]}\" == \"$cur\"* ]]; then\n"
346 " completions+=(\"${opts_to_complete[i]}\")\n"
347 " fi\n"
348 " done\n"
349 "\n"
350 " if [[ ${#completions[@]} -gt 0 ]]; then\n"
351 " if compopt &>/dev/null 2>&1; then\n"
352 " compopt -o nosort 2>/dev/null || true\n"
353 " COMPREPLY=()\n"
354 " for opt in \"${completions[@]}\"; do\n"
355 " for ((i = 0; i < ${#opts_to_complete[@]}; i += 2)); do\n"
356 " if [[ \"${opts_to_complete[i]}\" == \"$opt\" ]]; then\n"
357 " COMPREPLY+=(\"$opt ${opts_to_complete[i+1]}\")\n"
358 " break\n"
359 " fi\n"
360 " done\n"
361 " done\n"
362 " else\n"
363 " COMPREPLY=($(compgen -W \"${completions[*]}\" -- \"$cur\"))\n"
364 " fi\n"
365 " fi\n"
366 " return\n"
367 " fi\n"
368 "\n"
369 " # If no mode specified yet, suggest modes\n"
370 " if [[ -z \"$mode\" ]]; then\n"
371 " COMPREPLY=($(compgen -W \"$modes\" -- \"$cur\"))\n"
372 " return\n"
373 " fi\n");
374}
375
377 if (!output) {
378 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
379 }
380
381 bash_write_header(output);
382 asciichat_error_t err = bash_write_all_options(output);
383 if (err != ASCIICHAT_OK) {
384 return err;
385 }
386 bash_write_completion_logic(output);
387 bash_write_footer(output);
388
389 return ASCIICHAT_OK;
390}
#define SET_ERRNO(code,...)
asciichat_error_t completions_generate_bash(FILE *output)
Generate bash completion script.
Definition bash.c:376
Bash shell completion generator.
@ OPTION_INPUT_NUMERIC
Numeric value with optional min/max/step.
Definition builder.h:188
@ 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_descriptor_t * completions_collect_all_modes_unique(size_t *count)
Collect options from all modes with deduplication.
Auto-generated shell completions from options registry.
Option enum value definitions - single source of truth.
#define SAFE_REALLOC(ptr, size, cast)
#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
@ 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
Central registry of all command-line options with mode applicability.
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
const option_metadata_t * options_registry_get_metadata(const char *long_name)
Get complete metadata for an option.
Definition public_api.c:251
🔤 String Manipulation and Shell Escaping Utilities
The complete registry entry for one command-line/configuration option.
Definition builder.h:251
const char * help_text
Description for –help.
Definition builder.h:261
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
Completion metadata attached to one option descriptor.
Definition builder.h:201
int max
Maximum value (or 0 if no limit).
Definition builder.h:210
const char ** enum_values
Enum value strings (null-terminated, e.g., {"auto", "none", "16", "256", "truecolor",...
Definition builder.h:203
int min
Minimum value (or 0 if no limit).
Definition builder.h:209
option_input_type_t input_type
What kind of input this option expects.
Definition builder.h:221
const char ** examples
Example values or command invocations (null-terminated array).
Definition builder.h:215
struct option_metadata_t::@061372117173071240020206164036331043262170062306 numeric_range