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

ENVIRONMENT section generator for man pages. More...

Go to the source code of this file.

Functions

char * manpage_content_generate_environment (const options_config_t *config)
 Generate ENVIRONMENT VARIABLES section content.
char * manpage_content_generate_environment_with_manual (const options_config_t *config, const char **manual_vars, size_t manual_count, const char **manual_descs)
 Generate ENVIRONMENT section with manual and auto variables.
void manpage_content_free_environment (char *content)
 Free generated environment content.

Detailed Description

ENVIRONMENT section generator for man pages.

Generates the ENVIRONMENT section from environment variable metadata in option descriptors.

Definition in file environment.h.

Function Documentation

◆ manpage_content_free_environment()

void manpage_content_free_environment ( char * content)

Free generated environment content.

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

Definition at line 148 of file environment.c.

148 {
149 if (content) {
150 SAFE_FREE(content);
151 }
152}
#define SAFE_FREE(ptr)

References SAFE_FREE.

Referenced by options_config_generate_manpage_template().

◆ manpage_content_generate_environment()

char * manpage_content_generate_environment ( const options_config_t * config)

Generate ENVIRONMENT VARIABLES section content.

Extracts environment variables from option descriptors and generates formatted ENVIRONMENT section with sorted variable names.

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

Definition at line 14 of file environment.c.

14 {
15 if (!config || config->num_descriptors == 0) {
16 char *buffer = SAFE_MALLOC(1, char *);
17 buffer[0] = '\0';
18 return buffer;
19 }
20
21 // Generate only ASCII_CHAT_* environment variables from config
22 // Manual variables are preserved from the template
23 size_t buffer_capacity = 32768;
24 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
25 size_t offset = 0;
26
27 // Iterate through all option descriptors
28 for (size_t i = 0; i < config->num_descriptors; i++) {
29 const option_descriptor_t *desc = &config->descriptors[i];
30
31 // Only include descriptors that have environment variable names
32 if (!desc->env_var_name) {
33 continue;
34 }
35
36 // Ensure buffer is large enough
37 if (offset + 512 >= buffer_capacity) {
38 buffer_capacity *= 2;
39 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
40 }
41
42 log_debug("[ENVIRONMENT] env=%s, desc=%s", desc->env_var_name ? desc->env_var_name : "NULL", desc->help_text ? desc->help_text : "NULL");
43
44 // Generate .TP tagged paragraph for this environment variable
45 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
46 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B %s\n", desc->env_var_name);
47
48 if (desc->help_text) {
49 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", desc->help_text);
50 }
51 }
52
53 log_debug("Generated ENVIRONMENT ASCII_CHAT_* section (%zu bytes)", offset);
54 return buffer;
55}
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
The complete registry entry for one command-line/configuration option.
Definition builder.h:271
const char * help_text
Description for –help.
Definition builder.h:281
const char * env_var_name
Environment variable fallback (or NULL).
Definition builder.h:293
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:457
size_t num_descriptors
Number of descriptors.
Definition builder.h:458

References buffer_capacity(), options_config_t::descriptors, option_descriptor_t::env_var_name, option_descriptor_t::help_text, log_debug, options_config_t::num_descriptors, SAFE_MALLOC, and SAFE_REALLOC.

Referenced by options_config_generate_manpage_template().

◆ manpage_content_generate_environment_with_manual()

char * manpage_content_generate_environment_with_manual ( const options_config_t * config,
const char ** manual_vars,
size_t manual_count,
const char ** manual_descs )

Generate ENVIRONMENT section with manual and auto variables.

Combines manual environment variables (from template) with ASCII_CHAT_* variables from config, sorts them all alphabetically, and generates formatted ENVIRONMENT section content.

Parameters
[in]configOptions configuration with descriptors
[in]manual_varsArray of manual variable names (can be NULL)
[in]manual_countNumber of manual variables
[in]manual_descsArray of manual variable descriptions (can be NULL)
Returns
Newly allocated content string (caller must free)

Definition at line 74 of file environment.c.

74 {
75
76 if (!config) {
77 char *buffer = SAFE_MALLOC(1, char *);
78 buffer[0] = '\0';
79 return buffer;
80 }
81
82 // Count how many ASCII_CHAT_* variables we have
83 size_t num_auto_vars = 0;
84 for (size_t i = 0; i < config->num_descriptors; i++) {
85 if (config->descriptors[i].env_var_name) {
86 num_auto_vars++;
87 }
88 }
89
90 // Allocate array for all variables
91 size_t total_vars = num_auto_vars + manual_count;
92 env_var_entry_t *all_vars = SAFE_MALLOC(total_vars * sizeof(env_var_entry_t), env_var_entry_t *);
93 size_t var_idx = 0;
94
95 // Copy auto-generated variables from config
96 for (size_t i = 0; i < config->num_descriptors; i++) {
97 const option_descriptor_t *desc = &config->descriptors[i];
98 if (desc->env_var_name) {
99 all_vars[var_idx].name = desc->env_var_name;
100 all_vars[var_idx].description = desc->help_text ? desc->help_text : "";
101 all_vars[var_idx].option_long_name = desc->long_name; // Store for reference
102 var_idx++;
103 }
104 }
105
106 // Add manual variables
107 for (size_t i = 0; i < manual_count && var_idx < total_vars; i++) {
108 all_vars[var_idx].name = manual_vars[i];
109 all_vars[var_idx].description = (manual_descs && manual_descs[i]) ? manual_descs[i] : "";
110 all_vars[var_idx].option_long_name = NULL; // Manual variables have no option reference
111 var_idx++;
112 }
113
114 // Sort all variables alphabetically
115 qsort(all_vars, total_vars, sizeof(env_var_entry_t), env_var_compare);
116
117 // Generate output buffer with all environment variables
118 size_t buffer_capacity = 32768;
119 char *buffer = SAFE_MALLOC(buffer_capacity, char *);
120 size_t offset = 0;
121
122 // Write all environment variables in sorted order
123 for (size_t i = 0; i < total_vars; i++) {
124 if (offset + 512 >= buffer_capacity) {
125 buffer_capacity *= 2;
126 buffer = SAFE_REALLOC(buffer, buffer_capacity, char *);
127 }
128
129 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
130 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B %s\n", all_vars[i].name);
131 if (*all_vars[i].description) {
132 if (all_vars[i].option_long_name) {
133 // For auto-generated variables, just reference the flag
134 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "See: \\fB\\-\\-%s\\fR\n", all_vars[i].option_long_name);
135 } else {
136 // For manual variables, show full description
137 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", all_vars[i].description);
138 }
139 }
140 }
141
142 SAFE_FREE(all_vars);
143
144 log_debug("Generated ENVIRONMENT section (%zu manual + %zu auto = %zu total, %zu bytes)", manual_count, num_auto_vars, total_vars, offset);
145 return buffer;
146}
Environment variable entry for manpage generation.
Definition environment.c:61
const char * name
Environment variable name.
Definition environment.c:62
const char * option_long_name
Long option name (for "(see --option)" suffix), or NULL for manual.
Definition environment.c:64
const char * description
Description text.
Definition environment.c:63
const char * long_name
Long option name (e.g., "port").
Definition builder.h:273

References buffer_capacity(), env_var_entry_t::description, options_config_t::descriptors, option_descriptor_t::env_var_name, option_descriptor_t::help_text, log_debug, option_descriptor_t::long_name, env_var_entry_t::name, options_config_t::num_descriptors, env_var_entry_t::option_long_name, SAFE_FREE, SAFE_MALLOC, and SAFE_REALLOC.