ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
merger.h File Reference

Man page content merging and orchestration. More...

Go to the source code of this file.

Functions

char * manpage_merger_merge_options (const options_config_t *config, const parsed_section_t *existing_section)
 Merge auto-generated options into section content.
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.
char * manpage_merger_merge_positional (const options_config_t *config, const parsed_section_t *existing_section)
 Merge positional arguments and examples.
asciichat_error_t manpage_merger_generate_usage (const options_config_t *config, char **out_content, size_t *out_len)
 Get auto-generated usage content.
asciichat_error_t manpage_merger_generate_synopsis (const char *mode_name, char **out_content, size_t *out_len)
 Get auto-generated synopsis content.
void manpage_merger_free_content (char *content)
 Free merged section content.

Detailed Description

Man page content merging and orchestration.

This module orchestrates the complete man page generation pipeline:

  1. Load resources (template and content files)
  2. Parse existing sections to preserve manual content
  3. Generate or merge new sections
  4. Write final output with proper formatting

Handles three section types:

  • AUTO: Fully auto-generated, regenerated each time
  • MANUAL: User-maintained, never modified by generator
  • MERGE: Intelligent merge of auto-generated and manual content

Definition in file merger.h.

Function Documentation

◆ manpage_merger_free_content()

void manpage_merger_free_content ( char * content)

Free merged section content.

Frees memory allocated by merger functions. Safe to call with NULL.

Parameters
[in]contentContent to free (can be NULL)

Definition at line 182 of file merger.c.

182 {
183 if (content) {
184 SAFE_FREE(content);
185 }
186}
#define SAFE_FREE(ptr)
Definition common.h:377

References SAFE_FREE.

Referenced by options_config_generate_manpage_template().

◆ manpage_merger_generate_synopsis()

asciichat_error_t manpage_merger_generate_synopsis ( const char * mode_name,
char ** out_content,
size_t * out_len )

Get auto-generated synopsis content.

Generates the SYNOPSIS section for the specified mode.

Parameters
[in]mode_nameMode name or NULL for binary-level
[out]out_contentPointer to store generated content
[out]out_lenPointer to store content length
Returns
ASCIICHAT_OK on success, error code on failure
Note
Caller must free out_content using SAFE_FREE

Definition at line 140 of file merger.c.

140 {
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}
#define SET_ERRNO(code,...)
int buffer_size
Size of circular buffer.
Definition grep.c:68
#define SAFE_MALLOC(size, cast)
Definition common.h:265
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
#define MANPAGE_INITIAL_BUFFER_SIZE
Definition merger.c:11

References ASCIICHAT_OK, buffer_size, ERROR_INVALID_PARAM, log_debug, MANPAGE_INITIAL_BUFFER_SIZE, SAFE_MALLOC, safe_snprintf(), and SET_ERRNO.

Referenced by options_config_generate_manpage_template().

◆ manpage_merger_generate_usage()

asciichat_error_t manpage_merger_generate_usage ( const options_config_t * config,
char ** out_content,
size_t * out_len )

Get auto-generated usage content.

Generates the USAGE section from options config usage descriptors.

Parameters
[in]configOptions config (can be NULL to generate all modes)
[out]out_contentPointer to store generated content
[out]out_lenPointer to store content length
Returns
ASCIICHAT_OK on success, error code on failure
Note
Caller must free out_content using SAFE_FREE

Definition at line 82 of file merger.c.

82 {
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}
size_t buffer_capacity(const void *data)
Definition buffer.c:280
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:285
void usage(FILE *desc, asciichat_mode_t mode)
Print usage information for a specific mode.
size_t num_usage_lines
Number of usage lines.
Definition builder.h:430
usage_descriptor_t * usage_lines
Array of usage line descriptors.
Definition builder.h:429
The complete registry entry for one command-line/configuration option.
Definition builder.h:307

References ASCIICHAT_OK, buffer_capacity(), ERROR_INVALID_PARAM, log_debug, options_config_t::num_usage_lines, SAFE_MALLOC, SAFE_REALLOC, safe_snprintf(), SET_ERRNO, usage(), and options_config_t::usage_lines.

Referenced by options_config_generate_manpage_template().

◆ manpage_merger_merge_environment()

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.

Takes environment variables from the options config and intelligently merges them with existing manual environment documentation (if any).

Handles deduplication and sorting of environment variable names while preserving manual descriptions where they exist.

Parameters
[in]configOptions config with auto-generated environment variables
[in]existing_sectionExisting section with manual documentation (can be NULL)
Returns
Newly allocated merged content (caller must free)
Note
Caller must free the returned string

Definition at line 44 of file merger.c.

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

References log_debug, and SAFE_MALLOC.

◆ manpage_merger_merge_options()

char * manpage_merger_merge_options ( const options_config_t * config,
const parsed_section_t * existing_section )

Merge auto-generated options into section content.

Takes auto-generated options content from config and intelligently merges it with existing manual options (if any). Removes duplicates by option name while preserving manual descriptions.

Currently returns the auto-generated content as-is. In a full implementation, this would parse both auto and manual option lists and merge them.

Parameters
[in]configOptions config with auto-generated options
[in]existing_sectionExisting section with manual options (can be NULL)
Returns
Newly allocated merged content (caller must free)
Note
Caller must free the returned string

Definition at line 25 of file merger.c.

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

References log_debug, and SAFE_MALLOC.

◆ manpage_merger_merge_positional()

char * manpage_merger_merge_positional ( const options_config_t * config,
const parsed_section_t * existing_section )

Merge positional arguments and examples.

Takes auto-generated positional arguments and examples and merges with existing manual content.

Parameters
[in]configOptions config with auto-generated positional args
[in]existing_sectionExisting section (can be NULL)
Returns
Newly allocated merged content (caller must free)
Note
Caller must free the returned string

Definition at line 63 of file merger.c.

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

References log_debug, and SAFE_MALLOC.