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
Go to the documentation of this file.
1
6
9#include <ascii-chat/common.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sodium.h>
15
20typedef struct {
22 const char *name;
24
25static const mode_bit_name_t mode_bit_names[] = {
26 {(1 << 0), "server"}, // MODE_SERVER
27 {(1 << 1), "client"}, // MODE_CLIENT
28 {(1 << 2), "mirror"}, // MODE_MIRROR
29 {(1 << 3), "discovery-service"}, // MODE_DISCOVERY_SERVICE
30 {OPTION_MODE_MCP_SERVER, "mcp-server"},
31 {(1 << 4), NULL}, // MODE_DISCOVERY (no mode prefix)
32 {0, NULL} // Terminator
33};
34
45static const char **get_all_mode_names_from_bitmask(uint32_t mode_bitmask, size_t *count) {
46 // Skip binary-only examples (OPTION_MODE_BINARY = 0x100)
47 if (mode_bitmask & 0x100 && !(mode_bitmask & 0x1F)) {
48 *count = 0;
49 return NULL; // Binary-level example, no mode name
50 }
51
52 // Count how many mode bits are set
53 size_t num_modes = 0;
54 for (size_t i = 0; mode_bit_names[i].bit != 0; i++) {
55 if ((mode_bitmask & mode_bit_names[i].bit) && mode_bit_names[i].name) {
56 num_modes++;
57 }
58 }
59
60 if (num_modes == 0) {
61 *count = 0;
62 return NULL;
63 }
64
65 // Allocate array for mode names (plus NULL terminator)
66 const char **names = SAFE_MALLOC((num_modes + 1) * sizeof(char *), const char **);
67
68 // Fill in mode names
69 size_t idx = 0;
70 for (size_t i = 0; mode_bit_names[i].bit != 0; i++) {
71 if ((mode_bitmask & mode_bit_names[i].bit) && mode_bit_names[i].name) {
72 names[idx++] = mode_bit_names[i].name;
73 }
74 }
75 names[idx] = NULL;
76
77 *count = num_modes;
78 return names;
79}
80
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}
147
148void manpage_content_free_examples(char *content) {
149 if (content) {
150 SAFE_FREE(content);
151 }
152}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
char * manpage_content_generate_examples(const options_config_t *config)
Generate EXAMPLES section content.
Definition examples.c:81
void manpage_content_free_examples(char *content)
Free generated examples content.
Definition examples.c:148
EXAMPLES section generator for man pages.
#define SAFE_REALLOC(ptr, size, cast)
unsigned int uint32_t
#define SAFE_FREE(ptr)
#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
@ OPTION_MODE_MCP_SERVER
Definition options.h:940
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
Man page template generation from options builder.
🔤 String Manipulation and Shell Escaping Utilities
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
Mode name mapping for bitmask extraction.
Definition examples.c:20
const char * name
Definition examples.c:22
uint32_t bit
Definition examples.c:21
Immutable parsing tables produced from an options builder.
Definition builder.h:456
size_t num_examples
Number of examples.
Definition builder.h:476
example_descriptor_t * examples
Array of example descriptors.
Definition builder.h:475