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

EXAMPLES section generator for man pages. More...

Go to the source code of this file.

Data Structures

struct  mode_bit_name_t
 Mode name mapping for bitmask extraction. More...

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.

Definition in file examples.c.

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 148 of file examples.c.

148 {
149 if (content) {
150 SAFE_FREE(content);
151 }
152}
#define SAFE_FREE(ptr)

References SAFE_FREE.

Referenced by 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 81 of file examples.c.

81 {
82 if (!config) {
83 char *buffer = SAFE_MALLOC(1, char *);
84 buffer[0] = '\0';
85 return buffer;
86 }
87
88 if (config->num_examples == 0) {
89 // Generate fallback content when no examples are defined
90 size_t buffer_capacity = 256;
91 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
92 size_t offset = 0;
93 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\nNo examples available for this mode.\n");
94 log_debug("Generated EXAMPLES section (fallback - no examples found)");
95 return buffer;
96 }
97
98 // Allocate growing buffer for examples section
99 size_t buffer_capacity = 8192;
100 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
101 size_t offset = 0;
102
103 for (size_t i = 0; i < config->num_examples; i++) {
104 const example_descriptor_t *example = &config->examples[i];
105
106 // Get all mode names from bitmask (examples can apply to multiple modes)
107 // For man pages, we only show each example once, randomly selecting one mode
108 size_t num_modes = 0;
109 const char **mode_names = get_all_mode_names_from_bitmask(example->mode_bitmask, &num_modes);
110
111 // Ensure buffer is large enough
112 if (offset + 512 >= buffer_capacity) {
113 buffer_capacity *= 2;
114 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
115 }
116
117 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
118 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
119
120 // Add mode name if applicable (randomly select one mode for multi-mode examples)
121 if (num_modes > 0) {
122 size_t selected_mode = randombytes_uniform((uint32_t)num_modes);
123 if (mode_names[selected_mode]) {
124 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", mode_names[selected_mode]);
125 }
126 }
127
128 if (example->args) {
129 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", example->args);
130 }
131 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
132
133 if (example->description) {
134 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(example->description));
135 }
136
137 // Free mode names array
138 if (mode_names) {
139 SAFE_FREE(mode_names);
140 }
141 }
142
143 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
144
145 return buffer;
146}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
#define SAFE_REALLOC(ptr, size, cast)
unsigned int uint32_t
#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
Example descriptor for programmatic EXAMPLES generation.
Definition builder.h:352
uint32_t mode_bitmask
Definition builder.h:353
const char * description
Definition builder.h:356
const char * args
Command-line arguments part of the example.
Definition builder.h:355
size_t num_examples
Number of examples.
Definition builder.h:476
example_descriptor_t * examples
Array of example descriptors.
Definition builder.h:475

References example_descriptor_t::args, buffer_capacity(), 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, and SAFE_REALLOC.

Referenced by options_config_generate_manpage_template().