7 #include "../cmdline.h"
9 static char* resize(
char** buffer,
size_t* size,
size_t increment)
11 const size_t nsize = *size + increment;
12 char* tmp = realloc(*buffer, nsize);
16 "Could not reallocate string buffer from %" PRIuz
" to %" PRIuz
" bytes.\n",
21 memset(&tmp[*size],
'\0', increment);
27 static char* append(
char** buffer,
size_t* size,
const char* str)
29 const size_t len = strnlen(*buffer, *size);
30 const size_t add = strlen(str);
31 const size_t required = len + add + 1;
35 if (!resize(buffer, size, required - *size))
38 strncpy(&(*buffer)[len], str, add);
42 static LPSTR tr_esc_str(LPCSTR arg,
bool format)
44 const char* str = NULL;
51 const size_t s = strlen(arg) + 1;
52 if (!resize(&tmp, &ds, s))
55 for (
size_t x = 0; x < s; x++)
62 if (!append(&tmp, &ds, str))
72 if (!append(&tmp, &ds, str))
82 if (!append(&tmp, &ds, str))
88 if (!append(&tmp, &ds, str))
93 if (!append(&tmp, &ds,
"\\&."))
99 if (!append(&tmp, &ds,
"\n.br\n"))
105 if (!append(&tmp, &ds, data))
114 int main(
int argc,
char* argv[])
116 size_t elements =
sizeof(global_cmd_args) /
sizeof(global_cmd_args[0]);
120 (void)fprintf(stderr,
"Usage: %s <output file name>\n", argv[0]);
124 const char* fname = argv[1];
126 (void)fprintf(stdout,
"Generating manpage file '%s'\n", fname);
127 FILE* fp = fopen(fname,
"w");
130 (void)fprintf(stderr,
"Could not open '%s' for writing.\n", fname);
135 (void)fprintf(fp,
".SH \"OPTIONS\"\n");
139 (void)fprintf(stderr,
"The argument array 'args' is empty, writing an empty file.\n");
143 for (
size_t x = 0; x < elements - 1; x++)
146 char* name = tr_esc_str(arg->Name, FALSE);
147 char* alias = tr_esc_str(arg->Alias, FALSE);
148 char* format = tr_esc_str(arg->Format, TRUE);
149 char* text = tr_esc_str(arg->Text, FALSE);
151 (void)fprintf(fp,
".PP\n");
155 (void)fprintf(fp,
"%s\\fB", first ?
"" :
", ");
157 if (arg->Flags == COMMAND_LINE_VALUE_BOOL)
158 (void)fprintf(fp,
"%s", arg->Default ?
"\\-" :
"+");
160 (
void)fprintf(fp,
"/");
162 (void)fprintf(fp,
"%s\\fR", name);
166 if (arg->Flags == COMMAND_LINE_VALUE_OPTIONAL)
167 (void)fprintf(fp,
"[");
169 (void)fprintf(fp,
":%s", format);
171 if (arg->Flags == COMMAND_LINE_VALUE_OPTIONAL)
172 (void)fprintf(fp,
"]");
181 (void)fprintf(fp,
"\n");
185 (void)fprintf(fp,
".RS 4\n");
186 const int hasText = text && (strnlen(text, 2) > 0);
188 (void)fprintf(fp,
"%s", text);
190 if (arg->Flags & COMMAND_LINE_VALUE_BOOL &&
191 (!arg->Default || arg->Default == BoolValueTrue))
192 (
void)fprintf(fp,
" (default:%s)\n", arg->Default ?
"on" :
"off");
193 else if (arg->Default)
195 char* value = tr_esc_str(arg->Default, FALSE);
196 (void)fprintf(fp,
" (default:%s)\n", value);
200 (void)fprintf(fp,
"\n");
203 (void)fprintf(fp,
".RE\n");
212 (void)fprintf(stdout,
"successfully generated '%s'\n", fname);