ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 152 of file environment.c.

152 {
153 if (content) {
154 SAFE_FREE(content);
155 }
156}
#define SAFE_FREE(ptr)
Definition common.h:377

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",
43 desc->help_text ? desc->help_text : "NULL");
44
45 // Generate .TP tagged paragraph for this environment variable
46 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".TP\n");
47 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, ".B %s\n", desc->env_var_name);
48
49 if (desc->help_text) {
50 offset += safe_snprintf(buffer + offset, buffer_capacity - offset, "%s\n", desc->help_text);
51 }
52 }
53
54 log_debug("Generated ENVIRONMENT ASCII_CHAT_* section (%zu bytes)", offset);
55 return buffer;
56}
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
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
The complete registry entry for one command-line/configuration option.
Definition builder.h:249
const char * help_text
Description for –help.
Definition builder.h:259
const char * env_var_name
Environment variable fallback (or NULL).
Definition builder.h:268
option_descriptor_t * descriptors
Array of option descriptors.
Definition builder.h:415
size_t num_descriptors
Number of descriptors.
Definition builder.h:416

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

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 75 of file environment.c.

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

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