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 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.

Definition in file positional.c.

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 72 of file positional.c.

72 {
73 if (content) {
74 SAFE_FREE(content);
75 }
76}
#define SAFE_FREE(ptr)

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 += 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}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
#define SAFE_REALLOC(ptr, size, cast)
#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
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

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, and SAFE_REALLOC.

Referenced by options_config_generate_manpage_template().