80 if (!out_content || !out_len) {
81 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid output parameters for manpage_merger_generate_usage");
84 if (!config || config->num_usage_lines == 0) {
85 *out_content = SAFE_MALLOC(1,
char *);
86 (*out_content)[0] =
'\0';
92 size_t buffer_capacity = 4096;
93 char *buffer = SAFE_MALLOC(buffer_capacity,
char *);
96 for (
size_t i = 0; i < config->num_usage_lines; i++) {
97 const usage_descriptor_t *
usage = &config->usage_lines[i];
100 if (offset + 512 >= buffer_capacity) {
101 buffer_capacity *= 2;
102 buffer = SAFE_REALLOC(buffer, buffer_capacity,
char *);
105 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
".TP\n");
106 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
".B ascii-chat");
108 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
" %s",
usage->mode);
110 if (
usage->positional) {
111 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
" %s",
usage->positional);
113 if (
usage->show_options) {
115 if (
usage->mode && strcmp(
usage->mode,
"<mode>") != 0) {
116 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
" [mode-options...]");
118 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
" [options...]");
121 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
"\n");
123 if (
usage->description) {
124 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
"%s\n",
usage->description);
128 offset +=
safe_snprintf(buffer + offset, buffer_capacity - offset,
"\n");
130 *out_content = buffer;
133 log_debug(
"Generated usage section (%zu bytes)", offset);
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.