ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
merger.c
Go to the documentation of this file.
1
9
10/* Existing buffer budgets in bytes, including NUL for text storage. */
11#define MANPAGE_INITIAL_BUFFER_SIZE 512
12
15#include <ascii-chat/log/log.h>
16#include <ascii-chat/common.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21// ============================================================================
22// Merger Functions
23// ============================================================================
24
25char *manpage_merger_merge_options(const options_config_t *config, const parsed_section_t *existing_section) {
26 (void)existing_section; // Unused for now
27
28 if (!config) {
29 return NULL;
30 }
31
32 // For now, return empty content. In a full implementation, this would:
33 // 1. Generate options from config
34 // 2. Parse existing section for manual options
35 // 3. Merge them by option name
36 // 4. Return merged content
37
38 log_debug("Merging options section (currently unimplemented - returns empty)");
39 char *content = SAFE_MALLOC(1, char *);
40 content[0] = '\0';
41 return content;
42}
43
44char *manpage_merger_merge_environment(const options_config_t *config, const parsed_section_t *existing_section) {
45 (void)existing_section; // Unused for now
46
47 if (!config) {
48 return NULL;
49 }
50
51 // For now, return empty content. In a full implementation, this would:
52 // 1. Extract environment variables from config options
53 // 2. Parse existing section for manual env var documentation
54 // 3. Merge and deduplicate
55 // 4. Return sorted, merged content
56
57 log_debug("Merging environment section (currently unimplemented - returns empty)");
58 char *content = SAFE_MALLOC(1, char *);
59 content[0] = '\0';
60 return content;
61}
62
63char *manpage_merger_merge_positional(const options_config_t *config, const parsed_section_t *existing_section) {
64 (void)existing_section; // Unused for now
65
66 if (!config) {
67 return NULL;
68 }
69
70 // For now, return empty content. In a full implementation, this would:
71 // 1. Generate positional arguments from config
72 // 2. Parse existing section for manual content
73 // 3. Merge intelligently
74 // 4. Return merged content
75
76 log_debug("Merging positional section (currently unimplemented - returns empty)");
77 char *content = SAFE_MALLOC(1, char *);
78 content[0] = '\0';
79 return content;
80}
81
82asciichat_error_t manpage_merger_generate_usage(const options_config_t *config, char **out_content, size_t *out_len) {
83 if (!out_content || !out_len) {
84 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid output parameters");
85 }
86
87 if (!config || config->num_usage_lines == 0) {
88 *out_content = SAFE_MALLOC(1, char *);
89 (*out_content)[0] = '\0';
90 *out_len = 0;
91 return ASCIICHAT_OK;
92 }
93
94 // Allocate growing buffer for usage section
95 size_t buffer_capacity = 4096;
96 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
97 size_t offset = 0;
98
99 for (size_t i = 0; i < config->num_usage_lines; i++) {
100 const usage_descriptor_t *usage = &config->usage_lines[i];
101
102 // Ensure buffer is large enough
103 if (offset + 512 >= buffer_capacity) {
104 buffer_capacity *= 2;
105 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
106 }
107
108 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
109 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
110 if (usage->mode) {
111 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->mode);
112 }
113 if (usage->positional) {
114 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->positional);
115 }
116 if (usage->show_options) {
117 // Use mode-specific options suffix for mode commands, regular options for binary-level
118 if (usage->mode && strcmp(usage->mode, "<mode>") != 0) {
119 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [mode-options...]");
120 } else {
121 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [options...]");
122 }
123 }
124 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
125
126 if (usage->description) {
127 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", usage->description);
128 }
129 }
130
131 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
132
133 *out_content = buffer;
134 *out_len = offset;
135
136 log_debug("Generated usage section (%zu bytes)", offset);
137 return ASCIICHAT_OK;
138}
139
140asciichat_error_t manpage_merger_generate_synopsis(const char *mode_name, char **out_content, size_t *out_len) {
141 if (!out_content || !out_len) {
142 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid output parameters");
143 }
144
145 // Allocate buffer for synopsis
147 char *buffer = SAFE_MALLOC(buffer_size, char *);
148 size_t offset = 0;
149
150 // Note: Section header is already in template, don't duplicate it
151
152 if (mode_name) {
153 // Mode-specific synopsis
154 offset += safe_snprintf(buffer + offset, buffer_size - offset,
155 ".B ascii-chat\n"
156 ".I %s\n"
157 "[\\fIoptions\\fR]\n",
158 mode_name);
159 } else {
160 // Binary-level synopsis - show main modes
161 offset += safe_snprintf(buffer + offset, buffer_size - offset,
162 ".B ascii-chat\n"
163 "[\\fIoptions\\fR]\n"
164 "\n"
165 ".B ascii-chat\n"
166 "[\\fIoptions\\fR] \\fI<session-string>\\fR\n"
167 "\n"
168 ".B ascii-chat\n"
169 "[\\fIoptions\\fR] [\\fBserver\\fR | \\fBclient\\fR | \\fBmirror\\fR | "
170 "\\fBdiscovery-service\\fR] [\\fImode-options\\fR]\n");
171 }
172
173 offset += safe_snprintf(buffer + offset, buffer_size - offset, "\n");
174
175 *out_content = buffer;
176 *out_len = offset;
177
178 log_debug("Generated synopsis section (%zu bytes)", *out_len);
179 return ASCIICHAT_OK;
180}
181
182void manpage_merger_free_content(char *content) {
183 if (content) {
184 SAFE_FREE(content);
185 }
186}
#define SET_ERRNO(code,...)
size_t buffer_capacity(const void *data)
Definition buffer.c:278
Groff/troff formatting utilities for man page generation.
int buffer_size
Size of circular buffer.
Definition grep.c:68
#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_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
void usage(FILE *desc, asciichat_mode_t mode)
Print usage information for a specific mode.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
char * manpage_merger_merge_options(const options_config_t *config, const parsed_section_t *existing_section)
Merge auto-generated options into section content.
Definition merger.c:25
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
char * manpage_merger_merge_positional(const options_config_t *config, const parsed_section_t *existing_section)
Merge positional arguments and examples.
Definition merger.c:63
void manpage_merger_free_content(char *content)
Free merged section content.
Definition merger.c:182
#define MANPAGE_INITIAL_BUFFER_SIZE
Definition merger.c:11
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
char * manpage_merger_merge_environment(const options_config_t *config, const parsed_section_t *existing_section)
Merge environment variables from auto-generated and manual sources.
Definition merger.c:44
Man page content merging and orchestration.
🔤 String Manipulation and Shell Escaping Utilities
Immutable parsing tables produced from an options builder.
Definition builder.h:415
size_t num_usage_lines
Number of usage lines.
Definition builder.h:431
usage_descriptor_t * usage_lines
Array of usage line descriptors.
Definition builder.h:430
Owned slice of a parsed roff man-page section.
Definition manpage.h:48
The complete registry entry for one command-line/configuration option.
Definition builder.h:308