ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
positional.c
Go to the documentation of this file.
1
9#include <ascii-chat/common.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
16 if (!config) {
17 char *buffer = SAFE_MALLOC(1, char *);
18 buffer[0] = '\0';
19 return buffer;
20 }
21
22 if (config->num_positional_args == 0) {
23 // Generate fallback content when no positional arguments are defined
24 size_t buffer_capacity = 256;
25 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
26 size_t offset = 0;
27 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".PP\nNo positional arguments for this mode.\n");
28 log_debug("Generated POSITIONAL ARGUMENTS section (fallback - no args found)");
29 return buffer;
30 }
31
32 // Allocate growing buffer for positional section
33 size_t buffer_capacity = 4096;
34 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
35 size_t offset = 0;
36
37 for (size_t i = 0; i < config->num_positional_args; i++) {
38 const positional_arg_descriptor_t *pos_arg = &config->positional_args[i];
39
40 // Ensure buffer is large enough
41 if (offset + 512 >= buffer_capacity) {
42 buffer_capacity *= 2;
43 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
44 }
45
46 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B %s\n", pos_arg->name);
48
49 if (pos_arg->help_text) {
50 offset +=
51 safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(pos_arg->help_text));
52 }
53
54 // Add examples if present
55 if (pos_arg->num_examples > 0) {
56 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
57 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B Examples:\n");
58 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
59 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".nf\n");
60 for (size_t j = 0; j < pos_arg->num_examples; j++) {
61 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n",
62 escape_groff_special(pos_arg->examples[j]));
63 }
64 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".fi\n");
65 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
66 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
67 }
68 }
69
70 log_debug("Generated POSITIONAL ARGUMENTS section (%zu bytes)", offset);
71 return buffer;
72}
73
75 if (content) {
76 SAFE_FREE(content);
77 }
78}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:284
#define SAFE_FREE(ptr)
Definition common.h:376
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
📝 Logging API with multiple log levels and terminal output control
Man page template generation from options builder.
void manpage_content_free_positional(char *content)
Free generated positional content.
Definition positional.c:74
char * manpage_content_generate_positional(const options_config_t *config)
Generate POSITIONAL ARGUMENTS section content.
Definition positional.c:15
POSITIONAL ARGUMENTS section generator for man pages.
Options configuration.
Definition builder.h:401
positional_arg_descriptor_t * positional_args
Array of positional argument descriptors.
Definition builder.h:408
size_t num_positional_args
Number of positional arguments.
Definition builder.h:409
Positional argument descriptor.
Definition builder.h:357
const char * help_text
Description for usage message.
Definition builder.h:359
const char * name
Name for help text (e.g., "address", "bind-addr")
Definition builder.h:358
const char ** examples
Array of example strings with descriptions.
Definition builder.h:364
size_t num_examples
Number of examples.
Definition builder.h:365