ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
positional.h File Reference

POSITIONAL ARGUMENTS section generator for man pages. More...

Go to the source code of this file.

Functions

char * manpage_content_generate_positional (const options_config_t *config)
 Generate POSITIONAL ARGUMENTS section content.
void manpage_content_free_positional (char *content)
 Free generated positional content.

Detailed Description

POSITIONAL ARGUMENTS section generator for man pages.

Generates the POSITIONAL ARGUMENTS section from positional argument descriptors showing what arguments a program accepts.

Definition in file positional.h.

Function Documentation

◆ manpage_content_free_positional()

void manpage_content_free_positional ( char * content)

Free generated positional content.

Parameters
[in]contentContent to free (can be NULL)

Definition at line 74 of file positional.c.

74 {
75 if (content) {
76 SAFE_FREE(content);
77 }
78}
#define SAFE_FREE(ptr)
Definition common.h:377

References SAFE_FREE.

Referenced by options_config_generate_manpage_template().

◆ manpage_content_generate_positional()

char * manpage_content_generate_positional ( const options_config_t * config)

Generate POSITIONAL ARGUMENTS section content.

Creates POSITIONAL ARGUMENTS section from positional argument descriptors with proper groff formatting and examples.

Parameters
[in]configOptions configuration with positional descriptors
Returns
Newly allocated content string (caller must free)

Definition at line 15 of file positional.c.

15 {
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}
size_t buffer_capacity(const void *data)
Definition buffer.c:280
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:285
#define SAFE_MALLOC(size, cast)
Definition common.h:265
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
const char * escape_groff_special(const char *str)
Escape special groff/troff characters in text.
Definition manpage.c:46
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
positional_arg_descriptor_t * positional_args
Array of positional argument descriptors.
Definition builder.h:421
size_t num_positional_args
Number of positional arguments.
Definition builder.h:422
Describes one non-option command-line argument and how the parser consumes it.
Definition builder.h:369
const char * help_text
Description for usage message.
Definition builder.h:371
const char * name
Name for help text (e.g., "address", "bind-addr").
Definition builder.h:370
const char ** examples
Array of example strings with descriptions.
Definition builder.h:376
size_t num_examples
Number of examples.
Definition builder.h:377

References buffer_capacity(), escape_groff_special(), positional_arg_descriptor_t::examples, positional_arg_descriptor_t::help_text, log_debug, positional_arg_descriptor_t::name, positional_arg_descriptor_t::num_examples, options_config_t::num_positional_args, options_config_t::positional_args, SAFE_MALLOC, SAFE_REALLOC, and safe_snprintf().

Referenced by options_config_generate_manpage_template().