ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
usage.c
Go to the documentation of this file.
1
7#include <ascii-chat/options/manpage/content/usage.h>
8#include <ascii-chat/log/logging.h>
9#include <ascii-chat/common.h>
10#include <ascii-chat/options/manpage.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15char *manpage_content_generate_usage(const options_config_t *config) {
16 if (!config || config->num_usage_lines == 0) {
17 char *buffer = SAFE_MALLOC(1, char *);
18 buffer[0] = '\0';
19 return buffer;
20 }
21
22 // Allocate growing buffer for usage section
23 size_t buffer_capacity = 4096;
24 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
25 size_t offset = 0;
26
27 for (size_t i = 0; i < config->num_usage_lines; i++) {
28 const usage_descriptor_t *usage = &config->usage_lines[i];
29
30 // Ensure buffer is large enough
31 if (offset + 512 >= buffer_capacity) {
32 buffer_capacity *= 2;
33 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
34 }
35
36 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
37 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B ascii-chat");
38 if (usage->mode) {
39 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->mode);
40 }
41 if (usage->positional) {
42 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->positional);
43 }
44 if (usage->show_options) {
45 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [options...]");
46 }
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
48
49 if (usage->description) {
50 offset +=
51 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(usage->description));
52 }
53 }
54
55 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
56
57 log_debug("Generated USAGE section (%zu bytes)", offset);
58 return buffer;
59}
60
61void manpage_content_free_usage(char *content) {
62 if (content) {
63 SAFE_FREE(content);
64 }
65}
void usage(FILE *desc, asciichat_mode_t mode)
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
void manpage_content_free_usage(char *content)
Definition usage.c:61
char * manpage_content_generate_usage(const options_config_t *config)
Definition usage.c:15