ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
options.h File Reference

OPTIONS section generator for man pages. More...

Go to the source code of this file.

Functions

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.

Detailed Description

OPTIONS section generator for man pages.

Generates the OPTIONS section from option descriptors in the config.

Definition in file manpage/content/options.h.

Function Documentation

◆ manpage_content_free_options()

void manpage_content_free_options ( char * content)

Free generated options content.

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

Definition at line 199 of file manpage/content/options.c.

199 {
200 if (content) {
201 SAFE_FREE(content);
202 }
203}
#define SAFE_FREE(ptr)

References SAFE_FREE.

Referenced by options_config_generate_manpage_template().

◆ manpage_content_generate_options()

char * manpage_content_generate_options ( const options_config_t * config)

Generate OPTIONS section content.

Creates formatted OPTIONS section from option descriptors, grouped by category with proper groff formatting.

Parameters
[in]configOptions configuration with descriptors
Returns
Newly allocated content string (caller must free)

Definition at line 20 of file manpage/content/options.c.

20 {
21 if (!config) {
22 char *buffer = SAFE_MALLOC(1, char *);
23 buffer[0] = '\0';
24 return buffer;
25 }
26
27 if (config->num_descriptors == 0) {
28 // Generate fallback content when no options are defined
29 size_t buffer_capacity = 512;
30 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
31 size_t offset = 0;
32 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\nNo command-line options are available for this mode.\n");
33 log_debug("Generated OPTIONS section (fallback - no options found)");
34 return buffer;
35 }
36
37 // Use a growing buffer for dynamic content
38 size_t buffer_capacity = 8192;
39 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
40 size_t offset = 0;
41
42 // Add introductory paragraph explaining mode flags, defaults, and env vars
43 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "Each option shows which modes it applies to with\n");
44 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B (modes: ...)\n");
45 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "notation. Default values are shown with\n");
46 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B (default: ...).\n");
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "See the\n");
48 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ENVIRONMENT\n");
49 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "section for environment variable equivalents.\n");
50 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\n");
51 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "Configuration precedence (lowest to highest):\n");
52 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B config.toml\n");
53 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "(see\n");
54 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".BR ascii-chat (5))\n");
55 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "< environment variables <\n");
56 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B command-line flags.\n");
57 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "Later values override earlier ones.\n");
58 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\n");
59 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "Options marked\n");
60 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B global\n");
61 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "must be passed before any mode and apply to all modes.\n");
62 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "Mode-specific options are passed after the mode name.\n");
63 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\n");
64 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "The default mode (running\n");
65 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat\n");
66 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "with no mode) accepts both global and default mode options with no mode specified.\n");
67 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\n");
68
69 // Build list of unique groups in order of first appearance
70 const char **unique_groups = SAFE_MALLOC(config->num_descriptors * sizeof(const char *), const char **);
71 size_t num_unique_groups = 0;
72
73 for (size_t i = 0; i < config->num_descriptors; i++) {
74 const option_descriptor_t *desc = &config->descriptors[i];
75
76 // Skip hidden options
77 if (desc->hide_from_mode_help || desc->hide_from_binary_help || !desc->group) {
78 continue;
79 }
80
81 // Check if this group is already in the list
82 bool group_exists = false;
83 for (size_t j = 0; j < num_unique_groups; j++) {
84 if (unique_groups[j] && strcmp(unique_groups[j], desc->group) == 0) {
85 group_exists = true;
86 break;
87 }
88 }
89
90 // Add new group to list
91 if (!group_exists && num_unique_groups < config->num_descriptors) {
92 unique_groups[num_unique_groups++] = desc->group;
93 }
94 }
95
96 // Print options grouped by category
97 for (size_t g = 0; g < num_unique_groups; g++) {
98 const char *current_group = unique_groups[g];
99
100 // Add section heading for each group
101 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".SS %s\n", current_group);
102
103 // Print all options in this group
104 for (size_t i = 0; i < config->num_descriptors; i++) {
105 const option_descriptor_t *desc = &config->descriptors[i];
106
107 // Skip if not in current group or if hidden
108 bool is_binary_option = (desc->mode_bitmask & OPTION_MODE_BINARY) != 0;
109 bool applies_to_mode = false;
110 if (is_binary_option) {
111 applies_to_mode = !desc->hide_from_binary_help;
112 } else {
113 applies_to_mode = !desc->hide_from_mode_help;
114 }
115 if (!applies_to_mode || !desc->group || strcmp(desc->group, current_group) != 0) {
116 continue;
117 }
118
119 // Ensure buffer is large enough
120 if (offset + 512 >= buffer_capacity) {
121 buffer_capacity *= 2;
122 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
123 }
124
125 // Start option item
126 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
127
128 // Write option flags
129 if (desc->short_name != '\0') {
130 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B \\-%c, \\-\\-%s", desc->short_name, desc->long_name);
131 } else {
132 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B \\-\\-%s", desc->long_name);
133 }
134
135 // Add argument placeholder for value-taking options
136 if (desc->type != OPTION_TYPE_ACTION) {
137 // Check for custom placeholder first, fall back to type-based placeholder
138 const char *placeholder = desc->arg_placeholder ? desc->arg_placeholder : options_get_type_placeholder(desc->type);
139 if (placeholder && *placeholder) {
140 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " \\fI%s\\fR", placeholder);
141 }
142 }
143
144 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
145
146 // Write help text
147 if (desc->help_text) {
148 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s", escape_groff_special(desc->help_text));
149 if (!desc->default_value) {
150 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
151 } else {
152 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " ");
153 }
154 } else if (desc->default_value) {
155 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " ");
156 }
157
158 // Add default value if present
159 if (desc->default_value) {
161 memset(default_buf, 0, sizeof(default_buf)); // Initialize buffer to prevent garbage
162 int n = options_format_default_value(desc->type, desc->default_value, default_buf, sizeof(default_buf));
163 // Skip empty strings (like --grep's default of "")
164 if (n > 0 && default_buf[0] != '\0') {
165 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "(default: ");
166 if (desc->type == OPTION_TYPE_STRING) {
167 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s", escape_groff_special(default_buf));
168 } else {
169 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s", default_buf);
170 }
171 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ")\n");
172 }
173 }
174
175 // Add mode information if applicable
176 const char *mode_str = format_mode_names(desc->mode_bitmask);
177 if (mode_str && strcmp(mode_str, "all modes") != 0) {
178 if (strcmp(mode_str, "global") == 0) {
179 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "(mode: %s)\n", mode_str);
180 } else {
181 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "(modes: %s)\n", mode_str);
182 }
183 }
184
185 // Add REQUIRED note if applicable
186 if (desc->required) {
187 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "[REQUIRED]\n");
188 }
189 }
190 }
191
192 SAFE_FREE(unique_groups);
193 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
194
195 log_debug("Generated OPTIONS section (%zu bytes)", offset);
196 return buffer;
197}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
@ OPTION_TYPE_STRING
String value (–name foo).
Definition builder.h:183
@ OPTION_TYPE_ACTION
Definition builder.h:186
#define SAFE_REALLOC(ptr, size, cast)
#define SAFE_MALLOC(size, cast)
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
Definition manpage.c:46
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_BINARY
Binary-level options (parsed before mode detection).
Definition options.h:942
#define OPTIONS_MANPAGE_CONTENT_OPTIONS_DEFAULT_BUFFER_SIZE
int options_format_default_value(option_type_t type, const void *default_value, char *buf, size_t bufsize)
Format option default value to string.
const char * options_get_type_placeholder(option_type_t type)
Get placeholder string for option type.
The complete registry entry for one command-line/configuration option.
Definition builder.h:271
option_mode_bitmask_t mode_bitmask
Which modes this option applies to.
Definition builder.h:313
const char * help_text
Description for –help.
Definition builder.h:281
bool hide_from_binary_help
Definition builder.h:287
const char * arg_placeholder
Definition builder.h:283
const char * group
Group name for help sections (e.g., "NETWORK OPTIONS").
Definition builder.h:282
const void * default_value
Pointer to default value (or NULL if required).
Definition builder.h:291
bool required
If true, option must be provided.
Definition builder.h:292
const char * long_name
Long option name (e.g., "port").
Definition builder.h:273
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:274
option_type_t type
Value type.
Definition builder.h:277
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:457
size_t num_descriptors
Number of descriptors.
Definition builder.h:458

References option_descriptor_t::arg_placeholder, buffer_capacity(), option_descriptor_t::default_value, options_config_t::descriptors, escape_groff_special(), format_mode_names(), option_descriptor_t::group, option_descriptor_t::help_text, option_descriptor_t::hide_from_binary_help, option_descriptor_t::hide_from_mode_help, log_debug, option_descriptor_t::long_name, option_descriptor_t::mode_bitmask, options_config_t::num_descriptors, OPTION_MODE_BINARY, OPTION_TYPE_ACTION, OPTION_TYPE_STRING, options_format_default_value(), options_get_type_placeholder(), OPTIONS_MANPAGE_CONTENT_OPTIONS_DEFAULT_BUFFER_SIZE, option_descriptor_t::required, SAFE_FREE, SAFE_MALLOC, SAFE_REALLOC, option_descriptor_t::short_name, and option_descriptor_t::type.

Referenced by options_config_generate_manpage_template().