ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
examples.h File Reference

EXAMPLES section generator for man pages. More...

Go to the source code of this file.

Functions

char * manpage_content_generate_examples (const options_config_t *config)
 Generate EXAMPLES section content.
 
void manpage_content_free_examples (char *content)
 Free generated examples content.
 

Detailed Description

EXAMPLES section generator for man pages.

Generates the EXAMPLES section from example descriptors showing common usage patterns.

Definition in file examples.h.

Function Documentation

◆ manpage_content_free_examples()

void manpage_content_free_examples ( char *  content)

Free generated examples content.

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

Definition at line 147 of file examples.c.

147 {
148 if (content) {
149 SAFE_FREE(content);
150 }
151}
#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_content_generate_examples()

char * manpage_content_generate_examples ( const options_config_t *  config)

Generate EXAMPLES section content.

Creates EXAMPLES section from example descriptors with proper code block and description formatting.

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

Definition at line 79 of file examples.c.

79 {
80 if (!config) {
81 char *buffer = SAFE_MALLOC(1, char *);
82 buffer[0] = '\0';
83 return buffer;
84 }
85
86 if (config->num_examples == 0) {
87 // Generate fallback content when no examples are defined
88 size_t buffer_capacity = 256;
89 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
90 size_t offset = 0;
91 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\nNo examples available for this mode.\n");
92 log_debug("Generated EXAMPLES section (fallback - no examples found)");
93 return buffer;
94 }
95
96 // Allocate growing buffer for examples section
97 size_t buffer_capacity = 8192;
98 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
99 size_t offset = 0;
100
101 for (size_t i = 0; i < config->num_examples; i++) {
102 const example_descriptor_t *example = &config->examples[i];
103
104 // Get all mode names from bitmask (examples can apply to multiple modes)
105 // For man pages, we only show each example once, randomly selecting one mode
106 size_t num_modes = 0;
107 const char **mode_names = get_all_mode_names_from_bitmask(example->mode_bitmask, &num_modes);
108
109 // Ensure buffer is large enough
110 if (offset + 512 >= buffer_capacity) {
111 buffer_capacity *= 2;
112 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
113 }
114
115 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
116 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
117
118 // Add mode name if applicable (randomly select one mode for multi-mode examples)
119 if (num_modes > 0) {
120 size_t selected_mode = randombytes_uniform((uint32_t)num_modes);
121 if (mode_names[selected_mode]) {
122 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", mode_names[selected_mode]);
123 }
124 }
125
126 if (example->args) {
127 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", example->args);
128 }
129 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
130
131 if (example->description) {
132 offset +=
133 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(example->description));
134 }
135
136 // Free mode names array
137 if (mode_names) {
138 SAFE_FREE(mode_names);
139 }
140 }
141
142 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
143
144 return buffer;
145}
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:284
unsigned int uint32_t
Definition common.h:58
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
Example descriptor for programmatic EXAMPLES generation.
Definition builder.h:310
uint32_t mode_bitmask
Bitmask of modes (OPTION_MODE_SERVER, OPTION_MODE_CLIENT, etc.)
Definition builder.h:311
const char * description
Definition builder.h:313
const char * args
Command-line arguments part of the example.
Definition builder.h:312
size_t num_examples
Number of examples.
Definition builder.h:420
example_descriptor_t * examples
Array of example descriptors.
Definition builder.h:419

References example_descriptor_t::args, example_descriptor_t::description, escape_groff_special(), options_config_t::examples, log_debug, example_descriptor_t::mode_bitmask, options_config_t::num_examples, SAFE_FREE, SAFE_MALLOC, SAFE_REALLOC, and safe_snprintf().

Referenced by options_config_generate_manpage_merged(), and options_config_generate_manpage_template().