ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
positional.c
Go to the documentation of this file.
1
7#include <ascii-chat/options/manpage/content/positional.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_positional(const options_config_t *config) {
16 if (!config || config->num_positional_args == 0) {
17 char *buffer = SAFE_MALLOC(1, char *);
18 buffer[0] = '\0';
19 return buffer;
20 }
21
22 // Allocate growing buffer for positional 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_positional_args; i++) {
28 const positional_arg_descriptor_t *pos_arg = &config->positional_args[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 %s\n", pos_arg->name);
38
39 if (pos_arg->help_text) {
40 offset +=
41 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(pos_arg->help_text));
42 }
43
44 // Add examples if present
45 if (pos_arg->num_examples > 0) {
46 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B Examples:\n");
48 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
49 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".nf\n");
50 for (size_t j = 0; j < pos_arg->num_examples; j++) {
51 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n",
52 escape_groff_special(pos_arg->examples[j]));
53 }
54 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".fi\n");
55 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
56 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
57 }
58 }
59
60 log_debug("Generated POSITIONAL ARGUMENTS section (%zu bytes)", offset);
61 return buffer;
62}
63
65 if (content) {
66 SAFE_FREE(content);
67 }
68}
const char * escape_groff_special(const char *str)
void manpage_content_free_positional(char *content)
Definition positional.c:64
char * manpage_content_generate_positional(const options_config_t *config)
Definition positional.c:15
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:456