ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
usage.c File Reference

USAGE section generator for man pages. More...

Go to the source code of this file.

Functions

char * manpage_content_generate_usage (const options_config_t *config)
 Generate USAGE section content.
void manpage_content_free_usage (char *content)
 Free generated usage content.

Detailed Description

USAGE section generator for man pages.

Definition in file usage.c.

Function Documentation

◆ manpage_content_free_usage()

void manpage_content_free_usage ( char * content)

Free generated usage content.

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

Definition at line 60 of file usage.c.

60 {
61 if (content) {
62 SAFE_FREE(content);
63 }
64}
#define SAFE_FREE(ptr)

References SAFE_FREE.

◆ manpage_content_generate_usage()

char * manpage_content_generate_usage ( const options_config_t * config)

Generate USAGE section content.

Creates USAGE section from usage descriptors with proper groff tagged paragraph formatting.

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

Definition at line 15 of file usage.c.

15 {
16 if (!config || config->num_usage_lines == 0) {
17 char *buffer = SAFE_MALLOC(1, char *);
18 buffer[0] = '\0';
19 return buffer;
20 }
21
22 // Allocate growing buffer for usage 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_usage_lines; i++) {
28 const usage_descriptor_t *usage = &config->usage_lines[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 ascii-chat");
38 if (usage->mode) {
39 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->mode);
40 }
41 if (usage->positional) {
42 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " %s", usage->positional);
43 }
44 if (usage->show_options) {
45 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, " [options...]");
46 }
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
48
49 if (usage->description) {
50 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", escape_groff_special(usage->description));
51 }
52 }
53
54 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "\n");
55
56 log_debug("Generated USAGE section (%zu bytes)", offset);
57 return buffer;
58}
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
void usage(FILE *desc, asciichat_mode_t mode)
Print usage information for a specific mode.
size_t num_usage_lines
Number of usage lines.
Definition builder.h:473
usage_descriptor_t * usage_lines
Array of usage line descriptors.
Definition builder.h:472
The complete registry entry for one command-line/configuration option.
Definition builder.h:336

References buffer_capacity(), escape_groff_special(), log_debug, options_config_t::num_usage_lines, SAFE_MALLOC, SAFE_REALLOC, usage(), and options_config_t::usage_lines.