ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 149 of file examples.c.

149 {
150 if (content) {
151 SAFE_FREE(content);
152 }
153}
#define SAFE_FREE(ptr)
Definition common.h:377

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 +=
135 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(example->description));
136 }
137
138 // Free mode names array
139 if (mode_names) {
140 SAFE_FREE(mode_names);
141 }
142 }
143
144 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
145
146 return buffer;
147}
size_t buffer_capacity(const void *data)
Definition buffer.c:280
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:285
unsigned int uint32_t
Definition common.h:62
#define SAFE_MALLOC(size, cast)
Definition common.h:265
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
Definition manpage.c:46
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
Example descriptor for programmatic EXAMPLES generation.
Definition builder.h:321
uint32_t mode_bitmask
Bitmask of modes (OPTION_MODE_SERVER, OPTION_MODE_CLIENT, etc.).
Definition builder.h:322
const char * description
Definition builder.h:324
const char * args
Command-line arguments part of the example.
Definition builder.h:323
size_t num_examples
Number of examples.
Definition builder.h:433
example_descriptor_t * examples
Array of example descriptors.
Definition builder.h:432

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, SAFE_REALLOC, and safe_snprintf().

Referenced by options_config_generate_manpage_template().