ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
positional.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
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 += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(pos_arg->help_text));
51 }
52
53 // Add examples if present
54 if (pos_arg->num_examples > 0) {
55 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
56 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B Examples:\n");
57 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RS\n");
58 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".nf\n");
59 for (size_t j = 0; j < pos_arg->num_examples; j++) {
60 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(pos_arg->examples[j]));
61 }
62 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".fi\n");
63 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
64 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".RE\n");
65 }
66 }
67
68 log_debug("Generated POSITIONAL ARGUMENTS section (%zu bytes)", offset);
69 return buffer;
70}
71
73 if (content) {
74 SAFE_FREE(content);
75 }
76}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
#define SAFE_REALLOC(ptr, size, cast)
#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
⚙️ 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.
void manpage_content_free_positional(char *content)
Free generated positional content.
Definition positional.c:72
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.
🔤 String Manipulation and Shell Escaping Utilities
Immutable parsing tables produced from an options builder.
Definition builder.h:456
positional_arg_descriptor_t * positional_args
Array of positional argument descriptors.
Definition builder.h:464
size_t num_positional_args
Number of positional arguments.
Definition builder.h:465
Describes one non-option command-line argument and how the parser consumes it.
Definition builder.h:407
const char * help_text
Description for usage message.
Definition builder.h:409
const char * name
Name for help text (e.g., "address", "bind-addr").
Definition builder.h:408
const char ** examples
Array of example strings with descriptions.
Definition builder.h:415
size_t num_examples
Number of examples.
Definition builder.h:416