ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
manpage.c
Go to the documentation of this file.
1
13
14/* Storage capacities in elements; text buffers include the terminating NUL. */
15#define OPTIONS_MANPAGE_MODE_BUFFER_SIZE 256
16#define OPTIONS_MANPAGE_CURRENT_AUTO_SECTION_BUFFER_SIZE 128
17#define OPTIONS_MANPAGE_CURRENT_MERGE_SECTION_BUFFER_SIZE 64
18#define OPTIONS_MANPAGE_QUESTION_BUFFER_SIZE 4096
19
21
22#include <ascii-chat/common.h>
23#include <ascii-chat/log/log.h>
35#include <ctype.h>
36#include <libplatform/question.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/stat.h>
41
42// =============================================================================
43// Helper Functions (exported for content generators)
44// =============================================================================
45
46const char *escape_groff_special(const char *str) { return str ? str : ""; }
47
48const char *format_mode_names(option_mode_bitmask_t mode_bitmask) {
49 if (mode_bitmask == 0) {
50 return NULL;
51 }
52
53 option_mode_bitmask_t user_modes_mask =
54 (1 << MODE_SERVER) | (1 << MODE_CLIENT) | (1 << MODE_MIRROR) |
56 (1 << MODE_MCP_SERVER);
57 if ((mode_bitmask & OPTION_MODE_BINARY) &&
58 !(mode_bitmask & user_modes_mask)) {
59 return "global";
60 }
61 if ((mode_bitmask & user_modes_mask) == user_modes_mask) {
62 // If all user modes are set, check if binary-level is also set
63 if (mode_bitmask & OPTION_MODE_BINARY) {
64 return "global, client, server, mirror, discovery-service, mcp-server";
65 }
66 return "all modes";
67 }
68
69 static char mode_str[OPTIONS_MANPAGE_MODE_BUFFER_SIZE];
70 mode_str[0] = '\0';
71 int pos = 0;
72
73 // Add global if binary-level is set
74 if (mode_bitmask & OPTION_MODE_BINARY) {
75 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "global");
76 }
77
78 // List modes in order: default, client, server, mirror, discovery-service
79 if (mode_bitmask & (1 << MODE_DISCOVERY)) {
80 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "%sdefault",
81 pos > 0 ? ", " : "");
82 }
83 if (mode_bitmask & (1 << MODE_CLIENT)) {
84 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "%sclient",
85 pos > 0 ? ", " : "");
86 }
87 if (mode_bitmask & (1 << MODE_SERVER)) {
88 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "%sserver",
89 pos > 0 ? ", " : "");
90 }
91 if (mode_bitmask & (1 << MODE_MIRROR)) {
92 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "%smirror",
93 pos > 0 ? ", " : "");
94 }
95 if (mode_bitmask & (1 << MODE_DISCOVERY_SERVICE)) {
96 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos,
97 "%sdiscovery-service", pos > 0 ? ", " : "");
98 }
99
100 if (mode_bitmask & (1 << MODE_MCP_SERVER)) {
101 pos += safe_snprintf(mode_str + pos, sizeof(mode_str) - pos, "%smcp-server",
102 pos > 0 ? ", " : "");
103 }
104
105 return pos > 0 ? mode_str : NULL;
106}
107
108// =============================================================================
109// Main Public API Functions
110// =============================================================================
111
113 const options_config_t *config, const char *program_name,
114 const char *mode_name, const char *output_path,
115 const char *brief_description) {
116 if (!config || !program_name || !brief_description) {
118 "Missing required parameters for man page generation");
119 }
120
121 FILE *f = NULL;
122 bool should_close = false;
123
124 if (output_path) {
125 f = platform_fopen("file_stream", output_path, "w");
126 if (!f) {
127 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open output file: %s",
128 output_path);
129 }
130 should_close = true;
131 } else {
132 f = stdout;
133 }
134
135 // Write title/header
136 manpage_fmt_write_title(f, program_name, mode_name, brief_description);
137
138 // Write SYNOPSIS
139 char *synopsis_content = NULL;
140 size_t synopsis_len = 0;
142 mode_name, &synopsis_content, &synopsis_len);
143 if (err == ASCIICHAT_OK && synopsis_content && synopsis_len > 0) {
144 fprintf(f, "%s", synopsis_content);
145 manpage_merger_free_content(synopsis_content);
146 }
147
148 // Write POSITIONAL ARGUMENTS if present
149 if (config->num_positional_args > 0) {
150 char *pos_content = manpage_content_generate_positional(config);
151 if (pos_content && *pos_content != '\0') {
152 fprintf(f, "%s", pos_content);
153 }
155 }
156
157 // Write DESCRIPTION
158 manpage_fmt_write_section(f, "DESCRIPTION");
159 fprintf(f, ".B ascii-chat\nis a terminal-based video chat application that "
160 "converts webcam video to ASCII\n");
161 fprintf(f, "art in real-time. It enables video chat directly in your "
162 "terminal, whether you're\n");
163 fprintf(f, "using a local console, a remote SSH session, or any terminal "
164 "emulator.\n");
166
167 // Write USAGE
168 char *usage_content = NULL;
169 size_t usage_len = 0;
170 err = manpage_merger_generate_usage(config, &usage_content, &usage_len);
171 if (err == ASCIICHAT_OK && usage_content && usage_len > 0) {
172 fprintf(f, ".SH USAGE\n%s", usage_content);
173 manpage_merger_free_content(usage_content);
174 }
175
176 // Write OPTIONS
177 if (config->num_descriptors > 0) {
178 char *options_content = manpage_content_generate_options(config);
179 if (options_content && *options_content != '\0') {
180 fprintf(f, "%s", options_content);
181 }
182 manpage_content_free_options(options_content);
183 }
184
185 // Write EXAMPLES if present
186 if (config->num_examples > 0) {
187 char *examples_content = manpage_content_generate_examples(config);
188 if (examples_content && *examples_content != '\0') {
189 fprintf(f, "%s", examples_content);
190 }
191 manpage_content_free_examples(examples_content);
192 }
193
194 // Write ENVIRONMENT if present
195 bool has_env_vars = false;
196 for (size_t i = 0; i < config->num_descriptors; i++) {
197 if (config->descriptors[i].env_var_name) {
198 has_env_vars = true;
199 break;
200 }
201 }
202
203 if (has_env_vars) {
204 char *env_content = manpage_content_generate_environment(config);
205 if (env_content && *env_content != '\0') {
206 fprintf(f, "%s", env_content);
207 }
209 }
210
211 // Write placeholder manual sections
212 manpage_fmt_write_section(f, "FILES");
213 fprintf(f, ".I ~/.ascii-chat/config.toml\n");
214 fprintf(f, "User configuration file\n");
216
217 manpage_fmt_write_section(f, "NOTES");
218 fprintf(f,
219 "For more information and examples, visit the project repository.\n");
221
222 manpage_fmt_write_section(f, "BUGS");
223 fprintf(f, "Report bugs at the project issue tracker.\n");
225
226 manpage_fmt_write_section(f, "AUTHOR");
227 fprintf(f, "Contributed by the ascii-chat community.\n");
229
230 manpage_fmt_write_section(f, "SEE ALSO");
231 fprintf(f, ".B man(1),\n");
232 fprintf(f, ".B groff_man(7)\n");
234
235 if (should_close) {
236 fclose(f);
237 }
238
239 log_debug("Generated man page template to %s",
240 output_path ? output_path : "stdout");
241 return ASCIICHAT_OK;
242}
243
245 options_builder_t *builder, const char *program_name, const char *mode_name,
246 const char *output_path, const char *brief_description) {
247 if (!builder || !program_name || !brief_description) {
248 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing required parameters");
249 }
250
251 const options_config_t *config = options_builder_build(builder);
252 if (!config) {
253 return SET_ERRNO(ERROR_CONFIG, "Failed to build options configuration");
254 }
255
257 config, program_name, mode_name, output_path, brief_description);
258}
259
279
280static asciichat_error_t manpage_open_merged_output(const char *output_path,
281 FILE **output,
282 bool *close_output) {
283 FILE *f = NULL;
284 bool should_close = false;
285
286 if (output_path && strlen(output_path) > 0 && strcmp(output_path, "-") != 0) {
287 // Check if file already exists and prompt for confirmation
288 struct stat st;
289 if (stat(output_path, &st) == 0) {
290 // File exists - ask user if they want to overwrite
291 log_plain("Man page file already exists: %s", output_path);
292
294 safe_snprintf(question, sizeof(question),
295 "File already exists: %s\n\nOverwrite", output_path);
296 bool overwrite =
297 ui_notice_confirm_kind(UI_PROMPT_MANPAGE_OVERWRITE, UI_NOTICE_WARNING,
298 question, 30); // Default to No
299 if (!overwrite) {
300 log_plain("Man page generation cancelled.");
301 return SET_ERRNO(ERROR_FILE_OPERATION, "User cancelled overwrite");
302 }
303
304 log_plain("Overwriting existing man page file...");
305 }
306
307 f = platform_fopen("file_stream", output_path, "w");
308 if (!f) {
309 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open output file: %s",
310 output_path);
311 }
312 should_close = true;
313 } else {
314 f = stdout;
315 }
316
317 *output = f;
318 *close_output = should_close;
319 return ASCIICHAT_OK;
320}
321
322static const char *manpage_begin_merge(FILE *f, manpage_merge_state_t *state,
323 const char *p, const char *line_end) {
324 state->in_merge_section = true;
325 state->merge_content_generated = false;
326 state->manual_env_count = 0; // Reset manual variable collection
327 state->manual_env_capacity = 0;
328
329 // Extract section name (e.g., "ENVIRONMENT" from "MERGE-START: ENVIRONMENT")
330 const char *section_start = strstr(p, "MERGE-START:");
331 if (section_start && section_start < line_end) {
332 section_start += strlen("MERGE-START:");
333 while (*section_start && section_start < line_end &&
334 isspace(*section_start))
335 section_start++;
336
337 const char *section_name_end = section_start;
338 // Advance until we reach line_end (the newline position)
339 while (section_name_end < line_end) {
340 section_name_end++;
341 }
342
343 // Trim trailing whitespace
344 while (section_name_end > section_start &&
345 isspace(*(section_name_end - 1))) {
346 section_name_end--;
347 }
348
349 size_t section_name_len = (size_t)(section_name_end - section_start);
350 if (section_name_len > 0 &&
351 section_name_len < sizeof(state->current_merge_section)) {
352 // Use memcpy instead of SAFE_STRNCPY because source is not
353 // null-terminated
354 memcpy(state->current_merge_section, section_start, section_name_len);
355 state->current_merge_section[section_name_len] = '\0';
356 }
357 }
358
359 // Do NOT write the MERGE-START marker line - it's an internal control marker
360 // The content will be generated when MERGE-END is encountered
361
362 // For ENVIRONMENT MERGE sections, find and preserve the .SH header
363 // (skipping over any marker comment lines that might be in between)
364 if (strcmp(state->current_merge_section, "ENVIRONMENT") == 0) {
365 const char *search_line = line_end + 1;
366 bool found_header = false;
367 while (*search_line && !found_header) {
368 const char *search_line_end = strchr(search_line, '\n');
369 if (!search_line_end)
370 break;
371
372 // Check if this is the .SH header
373 if (strncmp(search_line, ".SH ", sizeof(".SH ") - 1) == 0) {
374 // Write the .SH header line
375 size_t header_len = (size_t)(search_line_end - search_line);
376 fwrite(search_line, 1, header_len + 1, f);
377 p = search_line_end + 1;
378 found_header = true;
379 break;
380 }
381
382 // Skip marker comment lines (.\" MANUAL-*, .\" MERGE-*, etc)
383 if (strncmp(search_line, ".\\\" ", sizeof(".\\\" ") - 1) == 0) {
384 search_line = search_line_end + 1;
385 continue; // Keep searching
386 }
387
388 // If we hit a non-marker, non-.SH line, stop searching
389 break;
390 }
391 if (found_header) {
392 return p;
393 }
394 }
395
396 // Skip to next line
397 p = line_end + 1;
398 return p;
399}
400
401static void manpage_clear_manual_environment(manpage_merge_state_t *state) {
402 // Free collected manual variables (strings first, then arrays)
403 for (size_t i = 0; i < state->manual_env_count; i++) {
404 if (state->manual_env_vars && state->manual_env_vars[i]) {
405 char *var_name = (char *)state->manual_env_vars[i];
406 SAFE_FREE(var_name);
407 }
408 if (state->manual_env_descs && state->manual_env_descs[i]) {
409 char *var_desc = (char *)state->manual_env_descs[i];
410 SAFE_FREE(var_desc);
411 }
412 }
413 if (state->manual_env_vars) {
415 state->manual_env_vars = NULL;
416 }
417 if (state->manual_env_descs) {
419 state->manual_env_descs = NULL;
420 }
421 state->manual_env_count = 0;
422 state->manual_env_capacity = 0;
423}
424
425static void manpage_end_merge(FILE *f, const options_config_t *config,
426 manpage_merge_state_t *state) {
427 // Before writing MERGE-END, generate content if not already done
428 if (!state->merge_content_generated) {
429 if (strcmp(state->current_merge_section, "ENVIRONMENT") == 0) {
430 log_debug("[MANPAGE] Generating ENVIRONMENT with %zu manual + %zu auto "
431 "variables",
432 state->manual_env_count, config->num_descriptors);
434 config, state->manual_env_vars, state->manual_env_count,
435 state->manual_env_descs);
436 if (env_content && *env_content != '\0') {
437 log_debug("[MANPAGE] Writing ENVIRONMENT content: %zu bytes",
438 strlen(env_content));
439 fprintf(f, "%s", env_content);
440 } else {
441 log_warn("[MANPAGE] ENVIRONMENT content is empty!");
442 }
444 }
445 state->merge_content_generated = true;
446 }
447
448 state->in_merge_section = false;
449 // Do NOT write the MERGE-END marker line - it's an internal control marker
450 memset(state->current_merge_section, 0, sizeof(state->current_merge_section));
451
452 manpage_clear_manual_environment(state);
453}
454
455static void manpage_collect_environment(manpage_merge_state_t *state,
456 const char *p, const char *line_end,
457 size_t line_len) {
458 // Check if line starts with ".TP" or ".B " (groff markers)
459 bool is_tp_marker =
460 (line_len >= 3 && strncmp(p, ".TP", sizeof(".TP") - 1) == 0 &&
461 (line_len == 3 || isspace(p[3])));
462 bool is_b_marker =
463 (line_len >= 3 && strncmp(p, ".B ", sizeof(".B ") - 1) == 0);
464
465 if (is_b_marker) {
466 // Extract variable name after ".B "
467 const char *var_start = p + 3;
468 while (*var_start && var_start < line_end && isspace(*var_start))
469 var_start++;
470
471 const char *var_end = var_start;
472 while (var_end < line_end && *var_end && *var_end != '\n') {
473 var_end++;
474 }
475
476 size_t var_len = (size_t)(var_end - var_start);
477 if (var_len > 0) {
478 char *var_name = SAFE_MALLOC(var_len + 1, char *);
479 memcpy(var_name, var_start, var_len);
480 var_name[var_len] = '\0';
481
482 // Trim trailing whitespace
483 while (var_len > 0 && isspace(var_name[var_len - 1])) {
484 var_name[--var_len] = '\0';
485 }
486
487 // Store the variable name
488 if (state->manual_env_count >= state->manual_env_capacity) {
489 state->manual_env_capacity = state->manual_env_capacity == 0
490 ? 16
491 : state->manual_env_capacity * 2;
493 state->manual_env_vars,
494 state->manual_env_capacity * sizeof(const char *), const char **);
496 state->manual_env_descs,
497 state->manual_env_capacity * sizeof(const char *), const char **);
498 }
499
500 state->manual_env_vars[state->manual_env_count] = var_name;
501 state->manual_env_descs[state->manual_env_count] =
502 NULL; // Will be set from next line(s)
503 state->manual_env_count++;
504 }
505 } else if (state->manual_env_count > 0 && !is_tp_marker && !is_b_marker) {
506 // This is likely a description line following a ".B var_name" line
507 // Set description for the last collected variable
508 const char *desc_start = p;
509 while (*desc_start && desc_start < line_end && isspace(*desc_start))
510 desc_start++;
511
512 size_t desc_len = (size_t)(line_end - desc_start);
513 if (desc_len > 0 &&
514 state->manual_env_descs[state->manual_env_count - 1] == NULL) {
515 char *desc = SAFE_MALLOC(desc_len + 1, char *);
516 memcpy(desc, desc_start, desc_len);
517 desc[desc_len] = '\0';
518
519 // Trim trailing whitespace
520 while (desc_len > 0 && isspace(desc[desc_len - 1])) {
521 desc[--desc_len] = '\0';
522 }
523
524 if (desc_len > 0) {
525 state->manual_env_descs[state->manual_env_count - 1] = desc;
526 } else {
527 SAFE_FREE(desc);
528 }
529 }
530 }
531}
532
533static void manpage_begin_auto(manpage_merge_state_t *state, const char *p,
534 const char *line_end) {
535 state->in_auto_section = true;
536 state->found_section_header = false;
537
538 // Extract section name (e.g., "SYNOPSIS" from "AUTO-START: SYNOPSIS")
539 const char *section_start = strstr(p, "AUTO-START:");
540 if (section_start && section_start < line_end) {
541 section_start += strlen("AUTO-START:");
542 while (*section_start && section_start < line_end &&
543 isspace(*section_start))
544 section_start++;
545
546 const char *section_name_end = section_start;
547 // Extract section name: everything from section_start until line_end
548 // Do NOT use a condition based on '\n' - just advance until we reach
549 // line_end
550 while (section_name_end < line_end) {
551 section_name_end++;
552 }
553
554 // Now trim trailing whitespace from the section name
555 while (section_name_end > section_start &&
556 isspace(*(section_name_end - 1))) {
557 section_name_end--;
558 }
559
560 size_t section_name_len = (size_t)(section_name_end - section_start);
561 if (section_name_len > 0 &&
562 section_name_len < sizeof(state->current_auto_section)) {
563 // Use memcpy instead of SAFE_STRNCPY because source is not
564 // null-terminated SAFE_STRNCPY uses strlcpy which only copies size-1
565 // bytes
566 memcpy(state->current_auto_section, section_start, section_name_len);
567 state->current_auto_section[section_name_len] = '\0';
568 log_debug("[MANPAGE] Found AUTO-START section: '%s' (%zu bytes)",
569 state->current_auto_section, section_name_len);
570 }
571 }
572
573 // Don't write the AUTO-START marker line - it's an internal control comment
574}
575
576static void manpage_write_auto_content(FILE *f, const options_config_t *config,
577 const manpage_merge_state_t *state) {
578 // Generate content for this AUTO section
579 if (strcmp(state->current_auto_section, "SYNOPSIS") == 0) {
580 log_debug("[MANPAGE] Generating SYNOPSIS section");
581 char *synopsis_content = NULL;
582 size_t synopsis_len = 0;
584 NULL, &synopsis_content, &synopsis_len);
585 log_debug("[MANPAGE] SYNOPSIS: err=%d, len=%zu", gen_err, synopsis_len);
586 if (gen_err == ASCIICHAT_OK && synopsis_content && synopsis_len > 0) {
587 fprintf(f, "%s", synopsis_content);
588 manpage_merger_free_content(synopsis_content);
589 }
590 } else if (strcmp(state->current_auto_section, "POSITIONAL ARGUMENTS") == 0) {
591 log_debug("[MANPAGE] Generating POSITIONAL ARGUMENTS (config has %zu args)",
592 config->num_positional_args);
593 char *pos_content = manpage_content_generate_positional(config);
594 if (pos_content) {
595 size_t pos_len = strlen(pos_content);
596 log_debug("[MANPAGE] POSITIONAL ARGUMENTS: %zu bytes", pos_len);
597 if (*pos_content != '\0') {
598 fprintf(f, "%s", pos_content);
599 }
600 }
602 } else if (strcmp(state->current_auto_section, "USAGE") == 0) {
603 log_debug("[MANPAGE] Generating USAGE (config has %zu usage lines)",
604 config->num_usage_lines);
605 char *usage_content = NULL;
606 size_t usage_len = 0;
607 asciichat_error_t usage_err =
608 manpage_merger_generate_usage(config, &usage_content, &usage_len);
609 log_debug("[MANPAGE] USAGE: err=%d, len=%zu", usage_err, usage_len);
610 if (usage_err == ASCIICHAT_OK && usage_content && usage_len > 0) {
611 fprintf(f, "%s", usage_content);
612 manpage_merger_free_content(usage_content);
613 }
614 } else if (strcmp(state->current_auto_section, "EXAMPLES") == 0) {
615 log_debug("[MANPAGE] Generating EXAMPLES (config has %zu examples)",
616 config->num_examples);
617 char *examples_content = manpage_content_generate_examples(config);
618 if (examples_content) {
619 size_t ex_len = strlen(examples_content);
620 log_debug("[MANPAGE] EXAMPLES: %zu bytes", ex_len);
621 if (*examples_content != '\0') {
622 fprintf(f, "%s", examples_content);
623 }
624 }
625 manpage_content_free_examples(examples_content);
626 } else if (strcmp(state->current_auto_section, "OPTIONS") == 0) {
627 log_debug("[MANPAGE] Generating OPTIONS (config has %zu descriptors)",
628 config->num_descriptors);
629 char *options_content = manpage_content_generate_options(config);
630 if (options_content) {
631 size_t opt_len = strlen(options_content);
632 log_debug("[MANPAGE] OPTIONS: %zu bytes", opt_len);
633 if (*options_content != '\0') {
634 fprintf(f, "%s", options_content);
635 }
636 }
637 manpage_content_free_options(options_content);
638 }
639}
640
641static void manpage_write_auto_line(FILE *f, const options_config_t *config,
642 manpage_merge_state_t *state, const char *p,
643 const char *line_end, size_t line_len) {
644 // Preserve comment lines that come before the section header
645 if (!state->found_section_header && strstr(p, ".\\\"") != NULL &&
646 strstr(p, ".\\\"") < line_end) {
647 // Skip comment lines in AUTO sections that contain "auto-generated" text
648 if (strstr(p, "auto-generated") == NULL) {
649 // Non-marker comment lines can be preserved
650 fwrite(p, 1, line_len + 1, f);
651 }
652 // Skip comment lines with "auto-generated" marker text
653 } else if (!state->found_section_header && strstr(p, ".SH ") != NULL &&
654 strstr(p, ".SH ") < line_end) {
655 // If this is the section header line (.SH ...), write it and generate
656 // content
657 fwrite(p, 1, line_len + 1, f);
658 state->found_section_header = true;
659
660 manpage_write_auto_content(f, config, state);
661 }
662 // Otherwise skip old content between section header and AUTO-END
663}
664
665static void manpage_merge_template(FILE *f, const options_config_t *config,
666 const char *p) {
667 manpage_merge_state_t storage = {0};
668 manpage_merge_state_t *state = &storage;
669 while (*p) {
670 // Find next line
671 const char *line_end = strchr(p, '\n');
672 if (!line_end) {
673 // Last line without newline
674 if (!state->in_auto_section && !state->in_merge_section) {
675 fputs(p, f);
676 }
677 break;
678 }
679
680 size_t line_len = (size_t)(line_end - p);
681
682 // Check for MERGE-START marker (only in current line)
683 bool has_merge_start = false;
684 if (strstr(p, "MERGE-START:") != NULL &&
685 strstr(p, "MERGE-START:") < line_end) {
686 has_merge_start = true;
687 }
688
689 if (has_merge_start) {
690 p = manpage_begin_merge(f, state, p, line_end);
691 continue;
692 } else if (state->in_merge_section && strstr(p, "MERGE-END:") != NULL &&
693 strstr(p, "MERGE-END:") < line_end) {
694 manpage_end_merge(f, config, state);
695 p = line_end + 1;
696 continue;
697 } else if (state->in_merge_section) {
698 if (strcmp(state->current_merge_section, "ENVIRONMENT") == 0) {
699 manpage_collect_environment(state, p, line_end, line_len);
700 } else {
701 fwrite(p, 1, line_len + 1, f);
702 }
703 p = line_end + 1;
704 continue;
705 }
706
707 // Check for AUTO-START marker (only in current line)
708 bool has_auto_start = false;
709 const char *temp = p;
710 while (temp < line_end) {
711 if (strstr(temp, "AUTO-START:") != NULL) {
712 const char *found = strstr(temp, "AUTO-START:");
713 if (found < line_end) {
714 has_auto_start = true;
715 break;
716 }
717 // strstr() found it but it's beyond this line, so keep looking
718 temp = found + 1;
719 } else {
720 break;
721 }
722 }
723
724 if (has_auto_start) {
725 manpage_begin_auto(state, p, line_end);
726 } else if (strstr(p, "AUTO-END:") != NULL &&
727 strstr(p, "AUTO-END:") < line_end) {
728 state->in_auto_section = false;
729 // Don't write the AUTO-END marker line - it's an internal control comment
730 memset(state->current_auto_section, 0,
731 sizeof(state->current_auto_section));
732 state->found_section_header = false;
733
734 } else if (state->in_auto_section) {
735 manpage_write_auto_line(f, config, state, p, line_end, line_len);
736 } else {
737 // Write all manual content (not in AUTO section)
738 // Skip marker comment lines: .\" AUTO-*, .\" MANUAL-*, .\" MERGE-*
739 // These are internal build-time control comments
740 bool is_marker_comment = (strstr(p, ".\\\" AUTO-") != NULL &&
741 strstr(p, ".\\\" AUTO-") < line_end) ||
742 (strstr(p, ".\\\" MANUAL-") != NULL &&
743 strstr(p, ".\\\" MANUAL-") < line_end) ||
744 (strstr(p, ".\\\" MERGE-") != NULL &&
745 strstr(p, ".\\\" MERGE-") < line_end);
746 if (!is_marker_comment) {
747 fwrite(p, 1, line_len + 1, f);
748 }
749 }
750
751 // Move to next line
752 p = line_end + 1;
753 }
754}
755
757 const options_config_t *config, const char *program_name,
758 const char *mode_name, const char *output_path,
759 const char *brief_description) {
760 (void)mode_name; // Not used
761 (void)program_name; // Not used
762 (void)brief_description; // Not used
763
764 if (!config) {
766 "config is required for man page generation");
767 }
768
769 FILE *f = NULL;
770 bool should_close = false;
772 manpage_open_merged_output(output_path, &f, &should_close);
773 if (err != ASCIICHAT_OK) {
774 return err;
775 }
776
777 // Load template resources
778 manpage_resources_t resources;
779 memset(&resources, 0, sizeof(resources));
780 err = manpage_resources_load(&resources);
781 if (err != ASCIICHAT_OK) {
782 if (should_close)
783 fclose(f);
784 return err;
785 }
786
787 if (!manpage_resources_is_valid(&resources)) {
788 if (should_close)
789 fclose(f);
790 manpage_resources_destroy(&resources);
791 return SET_ERRNO(ERROR_CONFIG, "Man page resources are not valid");
792 }
793
794 manpage_merge_template(f, config, resources.template_content);
795
796 manpage_resources_destroy(&resources);
797
798 fflush(f);
799 if (should_close) {
800 fclose(f);
801 }
802
803 log_debug("Generated merged man page to %s",
804 output_path ? output_path : "stdout");
805 return ASCIICHAT_OK;
806}
807
809 size_t *num_sections) {
810 if (!filepath || !num_sections) {
811 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
812 return NULL;
813 }
814
815 FILE *f = platform_fopen("file_stream", filepath, "r");
816 if (!f) {
817 SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open file: %s", filepath);
818 return NULL;
819 }
820
821 parsed_section_t *sections = NULL;
822 asciichat_error_t err = manpage_parser_parse_file(f, &sections, num_sections);
823 fclose(f);
824
825 if (err != ASCIICHAT_OK) {
826 return NULL;
827 }
828
829 return sections;
830}
831
832void free_parsed_sections(parsed_section_t *sections, size_t num_sections) {
833 manpage_parser_free_sections(sections, num_sections);
834}
835
837 size_t num_sections,
838 const char *section_name) {
839 return manpage_parser_find_section(sections, num_sections, section_name);
840}
841
842#ifndef NDEBUG
844 const char *template_path, const char *output_path,
845 const char *version_string, const char *content_file_path) {
846 (void)template_path;
847 (void)output_path;
848 (void)version_string;
849 (void)content_file_path;
850 return SET_ERRNO(ERROR_CONFIG, "Not implemented in refactored version");
851}
852#endif
853
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
Options builder API for flexible command-line option configuration.
options_config_t * options_builder_build(options_builder_t *builder)
Build immutable options config.
char * manpage_content_generate_environment_with_manual(const options_config_t *config, const char **manual_vars, size_t manual_count, const char **manual_descs)
Generate ENVIRONMENT section with manual and auto variables.
Definition environment.c:74
void manpage_content_free_environment(char *content)
Free generated environment content.
char * manpage_content_generate_environment(const options_config_t *config)
Generate ENVIRONMENT VARIABLES section content.
Definition environment.c:14
ENVIRONMENT section generator for man pages.
char * manpage_content_generate_examples(const options_config_t *config)
Generate EXAMPLES section content.
Definition examples.c:81
void manpage_content_free_examples(char *content)
Free generated examples content.
Definition examples.c:148
EXAMPLES section generator for man pages.
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Definition filesystem.c:18
void manpage_fmt_write_title(FILE *f, const char *program_name, const char *mode_name, const char *brief_description)
Write groff title/header (.TH directive).
Definition formatter.c:79
void manpage_fmt_write_section(FILE *f, const char *section_name)
Write a section header directive.
Definition formatter.c:32
void manpage_fmt_write_blank_line(FILE *f)
Write a blank line (for spacing between sections).
Definition formatter.c:39
Groff/troff formatting utilities for man page generation.
#define SAFE_REALLOC(ptr, size, cast)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
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_CONFIG
Definition error_codes.h:56
@ ERROR_FILE_OPERATION
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define log_plain(...)
Plain logging - writes to both log file and stderr without timestamps or log levels.
Definition log/log.h:635
const parsed_section_t * find_section(const parsed_section_t *sections, size_t num_sections, const char *section_name)
Find section by name.
Definition manpage.c:836
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
Definition manpage.c:46
asciichat_error_t options_config_generate_final_manpage(const char *template_path, const char *output_path, const char *version_string, const char *content_file_path)
Generate final man page (.1) from template (.1.in) with version substitution and optional content fil...
Definition manpage.c:843
asciichat_error_t options_builder_generate_manpage_template(options_builder_t *builder, const char *program_name, const char *mode_name, const char *output_path, const char *brief_description)
Generate man page template from builder (before build()).
Definition manpage.c:244
asciichat_error_t options_config_generate_manpage_template(const options_config_t *config, const char *program_name, const char *mode_name, const char *output_path, const char *brief_description)
Generate a man page template from options builder configuration.
Definition manpage.c:112
void free_parsed_sections(parsed_section_t *sections, size_t num_sections)
Free parsed sections array.
Definition manpage.c:832
parsed_section_t * parse_manpage_sections(const char *filepath, size_t *num_sections)
Parse existing man page template to extract sections.
Definition manpage.c:808
const char * format_mode_names(option_mode_bitmask_t mode_bitmask)
Format option mode names as human-readable string.
Definition manpage.c:48
option_mode_bitmask_t
Option mode bitmask.
Definition options.h:933
asciichat_error_t options_config_generate_manpage_merged(const options_config_t *config, const char *program_name, const char *mode_name, const char *output_path, const char *brief_description)
Generate merged man page template preserving manual content.
Definition manpage.c:756
@ 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
@ OPTION_MODE_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:942
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
char * manpage_content_generate_options(const options_config_t *config)
Generate OPTIONS section content.
void manpage_content_free_options(char *content)
Free generated options content.
OPTIONS section generator for man pages.
#define OPTIONS_MANPAGE_CURRENT_AUTO_SECTION_BUFFER_SIZE
Definition manpage.c:16
#define OPTIONS_MANPAGE_MODE_BUFFER_SIZE
Definition manpage.c:15
#define OPTIONS_MANPAGE_QUESTION_BUFFER_SIZE
Definition manpage.c:18
#define OPTIONS_MANPAGE_CURRENT_MERGE_SECTION_BUFFER_SIZE
Definition manpage.c:17
Man page template generation from options builder.
asciichat_error_t manpage_merger_generate_usage(const options_config_t *config, char **out_content, size_t *out_len)
Get auto-generated usage content.
Definition merger.c:82
void manpage_merger_free_content(char *content)
Free merged section content.
Definition merger.c:182
asciichat_error_t manpage_merger_generate_synopsis(const char *mode_name, char **out_content, size_t *out_len)
Get auto-generated synopsis content.
Definition merger.c:140
Man page content merging and orchestration.
bool ui_notice_confirm_kind(ui_prompt_kind_t kind, ui_notice_severity_t severity, const char *text, unsigned timeout_seconds)
Definition notice.c:548
@ UI_NOTICE_WARNING
Definition notice.h:15
asciichat_error_t manpage_parser_parse_file(FILE *f, parsed_section_t **out_sections, size_t *out_count)
Parse man page sections from a FILE handle.
Definition parser.c:345
const parsed_section_t * manpage_parser_find_section(const parsed_section_t *sections, size_t count, const char *section_name)
Find section by name.
Definition parser.c:404
void manpage_parser_free_sections(parsed_section_t *sections, size_t count)
Free parsed sections array.
Definition parser.c:387
Man page template parsing and section extraction.
void manpage_content_free_positional(char *content)
Free generated positional content.
Definition positional.c:72
char * manpage_content_generate_positional(const options_config_t *config)
Generate POSITIONAL ARGUMENTS section content.
Definition positional.c:15
POSITIONAL ARGUMENTS section generator for man pages.
void manpage_resources_destroy(manpage_resources_t *resources)
Cleanup allocated man page resources.
Definition resources.c:143
asciichat_error_t manpage_resources_load(manpage_resources_t *resources)
Load man page resources from embedded or filesystem sources.
Definition resources.c:100
bool manpage_resources_is_valid(const manpage_resources_t *resources)
Check if resources are available.
Definition resources.c:172
Resource management for man page generation.
🔤 String Manipulation and Shell Escaping Utilities
const char ** manual_env_descs
Definition manpage.c:268
char current_auto_section[128]
Definition manpage.c:262
const char ** manual_env_vars
Definition manpage.c:267
char current_merge_section[64]
Definition manpage.c:266
size_t manual_env_capacity
Definition manpage.c:270
Man page resource container.
Definition resources.h:27
const char * template_content
Template file content (.1.in).
Definition resources.h:28
const char * env_var_name
Environment variable fallback (or NULL).
Definition builder.h:293
Immutable parsing tables produced from an options builder.
Definition builder.h:502
Immutable parsing tables produced from an options builder.
Definition builder.h:456
size_t num_usage_lines
Number of usage lines.
Definition builder.h:473
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:457
size_t num_positional_args
Number of positional arguments.
Definition builder.h:465
size_t num_descriptors
Number of descriptors.
Definition builder.h:458
size_t num_examples
Number of examples.
Definition builder.h:476
Owned slice of a parsed roff man-page section.
Definition manpage.h:49
USAGE section generator for man pages.