ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
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)
 
void manpage_content_free_examples (char *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)

Definition at line 141 of file examples.c.

141 {
142 if (content) {
143 SAFE_FREE(content);
144 }
145}

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)

Definition at line 79 of file examples.c.

79 {
80 if (!config || config->num_examples == 0) {
81 char *buffer = SAFE_MALLOC(1, char *);
82 buffer[0] = '\0';
83 return buffer;
84 }
85
86 // Seed random number generator for mode selection
87 // Use config pointer address for deterministic-but-varied seed
88 srand((unsigned int)((uintptr_t)config));
89
90 // Allocate growing buffer for examples section
91 size_t buffer_capacity = 8192;
92 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
93 size_t offset = 0;
94
95 for (size_t i = 0; i < config->num_examples; i++) {
96 const example_descriptor_t *example = &config->examples[i];
97
98 // Get all mode names from bitmask (examples can apply to multiple modes)
99 // For man pages, we only show each example once, randomly selecting one mode
100 size_t num_modes = 0;
101 const char **mode_names = get_all_mode_names_from_bitmask(example->mode_bitmask, &num_modes);
102
103 // Ensure buffer is large enough
104 if (offset + 512 >= buffer_capacity) {
105 buffer_capacity *= 2;
106 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
107 }
108
109 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
110 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
111
112 // Add mode name if applicable (randomly select one mode for multi-mode examples)
113 if (num_modes > 0) {
114 size_t selected_mode = (size_t)rand() % num_modes;
115 if (mode_names[selected_mode]) {
116 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", mode_names[selected_mode]);
117 }
118 }
119
120 if (example->args) {
121 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", example->args);
122 }
123 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
124
125 if (example->description) {
126 offset +=
127 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(example->description));
128 }
129
130 // Free mode names array
131 if (mode_names) {
132 SAFE_FREE(mode_names);
133 }
134 }
135
136 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
137
138 return buffer;
139}
const char * escape_groff_special(const char *str)
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:456

References escape_groff_special(), and safe_snprintf().

Referenced by options_config_generate_manpage_merged(), and options_config_generate_manpage_template().