ascii-chat 0.11.33
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 179 of file merger.c.

179 {
180 if (content) {
181 SAFE_FREE(content);
182 }
183}
#define SAFE_FREE(ptr)
Definition common.h:376

References SAFE_FREE.

Referenced by options_config_generate_manpage_merged(), and 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 137 of file merger.c.

137 {
138 if (!out_content || !out_len) {
139 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid output parameters for manpage_merger_generate_synopsis");
140 }
141
142 // Allocate buffer for synopsis
143 size_t buffer_size = 512;
144 char *buffer = SAFE_MALLOC(buffer_size, char *);
145 size_t offset = 0;
146
147 // Note: Section header is already in template, don't duplicate it
148
149 if (mode_name) {
150 // Mode-specific synopsis
151 offset += safe_snprintf(buffer + offset, buffer_size - offset,
152 ".B ascii-chat\n"
153 ".I %s\n"
154 "[\\fIoptions\\fR]\n",
155 mode_name);
156 } else {
157 // Binary-level synopsis - show main modes
158 offset += safe_snprintf(buffer + offset, buffer_size - offset,
159 ".B ascii-chat\n"
160 "[\\fIoptions\\fR]\n"
161 "\n"
162 ".B ascii-chat\n"
163 "[\\fIoptions\\fR] \\fI<session-string>\\fR\n"
164 "\n"
165 ".B ascii-chat\n"
166 "[\\fIoptions\\fR] [\\fBserver\\fR | \\fBclient\\fR | \\fBmirror\\fR | "
167 "\\fBdiscovery-service\\fR] [\\fImode-options\\fR]\n");
168 }
169
170 offset += safe_snprintf(buffer + offset, buffer_size - offset, "\n");
171
172 *out_content = buffer;
173 *out_len = offset;
174
175 log_debug("Generated synopsis section (%zu bytes)", *out_len);
176 return ASCIICHAT_OK;
177}
int buffer_size
Size of circular buffer.
Definition grep.c:90
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148

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

Referenced by options_config_generate_manpage_merged(), and 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 79 of file merger.c.

79 {
80 if (!out_content || !out_len) {
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid output parameters for manpage_merger_generate_usage");
82 }
83
84 if (!config || config->num_usage_lines == 0) {
85 *out_content = SAFE_MALLOC(1, char *);
86 (*out_content)[0] = '\0';
87 *out_len = 0;
88 return ASCIICHAT_OK;
89 }
90
91 // Allocate growing buffer for usage section
92 size_t buffer_capacity = 4096;
93 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
94 size_t offset = 0;
95
96 for (size_t i = 0; i < config->num_usage_lines; i++) {
97 const usage_descriptor_t *usage = &config->usage_lines[i];
98
99 // Ensure buffer is large enough
100 if (offset + 512 >= buffer_capacity) {
101 buffer_capacity *= 2;
102 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
103 }
104
105 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
106 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
107 if (usage->mode) {
108 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->mode);
109 }
110 if (usage->positional) {
111 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->positional);
112 }
113 if (usage->show_options) {
114 // Use mode-specific options suffix for mode commands, regular options for binary-level
115 if (usage->mode && strcmp(usage->mode, "<mode>") != 0) {
116 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [mode-options...]");
117 } else {
118 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [options...]");
119 }
120 }
121 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
122
123 if (usage->description) {
124 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", usage->description);
125 }
126 }
127
128 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
129
130 *out_content = buffer;
131 *out_len = offset;
132
133 log_debug("Generated usage section (%zu bytes)", offset);
134 return ASCIICHAT_OK;
135}
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:284
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:417
usage_descriptor_t * usage_lines
Array of usage line descriptors.
Definition builder.h:416
Usage line descriptor for programmatic USAGE generation.
Definition builder.h:296

References ASCIICHAT_OK, 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_merged(), and 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 41 of file merger.c.

41 {
42 (void)existing_section; // Unused for now
43
44 if (!config) {
45 return NULL;
46 }
47
48 // For now, return empty content. In a full implementation, this would:
49 // 1. Extract environment variables from config options
50 // 2. Parse existing section for manual env var documentation
51 // 3. Merge and deduplicate
52 // 4. Return sorted, merged content
53
54 log_debug("Merging environment section (currently unimplemented - returns empty)");
55 char *content = SAFE_MALLOC(1, char *);
56 content[0] = '\0';
57 return content;
58}

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 22 of file merger.c.

22 {
23 (void)existing_section; // Unused for now
24
25 if (!config) {
26 return NULL;
27 }
28
29 // For now, return empty content. In a full implementation, this would:
30 // 1. Generate options from config
31 // 2. Parse existing section for manual options
32 // 3. Merge them by option name
33 // 4. Return merged content
34
35 log_debug("Merging options section (currently unimplemented - returns empty)");
36 char *content = SAFE_MALLOC(1, char *);
37 content[0] = '\0';
38 return content;
39}

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 60 of file merger.c.

60 {
61 (void)existing_section; // Unused for now
62
63 if (!config) {
64 return NULL;
65 }
66
67 // For now, return empty content. In a full implementation, this would:
68 // 1. Generate positional arguments from config
69 // 2. Parse existing section for manual content
70 // 3. Merge intelligently
71 // 4. Return merged content
72
73 log_debug("Merging positional section (currently unimplemented - returns empty)");
74 char *content = SAFE_MALLOC(1, char *);
75 content[0] = '\0';
76 return content;
77}

References log_debug, and SAFE_MALLOC.