From 6a1a2eb661eb5ab055f2c0c0e9b4bbb1798ad211 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 20 Dec 2023 20:35:05 +0100 Subject: [PATCH] [client,common] update manpage helper binary improve formatting of docbook xml from argument description --- client/common/man/generate_argument_docbook.c | 195 ++++++------------ cmake/InstallFreeRDPMan.cmake | 2 +- 2 files changed, 68 insertions(+), 129 deletions(-) diff --git a/client/common/man/generate_argument_docbook.c b/client/common/man/generate_argument_docbook.c index a4909faa0..b1fcb9410 100644 --- a/client/common/man/generate_argument_docbook.c +++ b/client/common/man/generate_argument_docbook.c @@ -1,168 +1,107 @@ #include +#include #include #include #include #include "../cmdline.h" -#define TAG FREERDP_TAG("generate_argument_docbook") -LPSTR tr_esc_str(LPCSTR arg, bool format) +static char* resize(char** buffer, size_t* size, size_t increment) { + const size_t nsize = *size + increment; + char* tmp = realloc(*buffer, nsize); + if (!tmp) + { + fprintf(stderr, "Could not reallocate string buffer from %" PRIuz " to %" PRIuz " bytes.\n", + *size, nsize); + free(*buffer); + } + memset(&tmp[*size], '\0', increment); + *size = nsize; + *buffer = tmp; + return tmp; +} + +static char* append(char** buffer, size_t* size, const char* str) +{ + const size_t len = strnlen(*buffer, *size); + const size_t add = strlen(str); + const size_t required = len + add + 1; + + if (required > *size) + { + if (!resize(buffer, size, required - *size)) + return NULL; + } + strcat(*buffer, str); + return *buffer; +} + +static LPSTR tr_esc_str(LPCSTR arg, bool format) +{ + const char* str; LPSTR tmp = NULL; - LPSTR tmp2 = NULL; - size_t cs = 0, x, ds, len; - size_t s; + size_t ds = 0; if (NULL == arg) return NULL; - s = strlen(arg); - - /* Find trailing whitespaces */ - while ((s > 0) && isspace(arg[s - 1])) - s--; - - /* Prepare a initial buffer with the size of the result string. */ - ds = s + 1; - - if (ds) - { - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; - } - - if (NULL == tmp) - { - fprintf(stderr, "Could not allocate string buffer.\n"); + const size_t s = strlen(arg) + 1; + if (!resize(&tmp, &ds, s)) exit(-2); - } - /* Copy character for character and check, if it is necessary to escape. */ - memset(tmp, 0, ds * sizeof(CHAR)); - - for (x = 0; x < s; x++) + for (size_t x = 0; x < s; x++) { + char data[2] = { 0 }; switch (arg[x]) { case '<': - len = format ? 13 : 4; - ds += len - 1; - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; - - if (NULL == tmp) - { - fprintf(stderr, "Could not reallocate string buffer.\n"); - exit(-3); - } - if (format) - /* coverity[buffer_size] */ - strncpy(&tmp[cs], "", len); + str = ""; else - /* coverity[buffer_size] */ - strncpy(&tmp[cs], "<", len); + str = "<"; - cs += len; + if (!append(&tmp, &ds, str)) + exit(-3); break; case '>': - len = format ? 14 : 4; - ds += len - 1; - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; - - if (NULL == tmp) - { - fprintf(stderr, "Could not reallocate string buffer.\n"); - exit(-4); - } - if (format) - /* coverity[buffer_size] */ - strncpy(&tmp[cs], "", len); + str = ""; else - /* coverity[buffer_size] */ - strncpy(&tmp[cs], ">", len); + str = ">"; - cs += len; + if (!append(&tmp, &ds, str)) + exit(-4); break; case '\'': - ds += 5; - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; - - if (NULL == tmp) - { - fprintf(stderr, "Could not reallocate string buffer.\n"); + if (!append(&tmp, &ds, "'")) exit(-5); - } - - tmp[cs++] = '&'; - tmp[cs++] = 'a'; - tmp[cs++] = 'p'; - tmp[cs++] = 'o'; - tmp[cs++] = 's'; - tmp[cs++] = ';'; break; case '"': - ds += 5; - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; - - if (NULL == tmp) - { - fprintf(stderr, "Could not reallocate string buffer.\n"); + if (!append(&tmp, &ds, """)) exit(-6); - } - - tmp[cs++] = '&'; - tmp[cs++] = 'q'; - tmp[cs++] = 'u'; - tmp[cs++] = 'o'; - tmp[cs++] = 't'; - tmp[cs++] = ';'; break; case '&': - ds += 4; - tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if (!tmp2) - free(tmp); - tmp = tmp2; + if (!append(&tmp, &ds, "&")) + exit(-6); + break; - if (NULL == tmp) - { - fprintf(stderr, "Could not reallocate string buffer.\n"); + case '\r': + case '\n': + if (!append(&tmp, &ds, "")) exit(-7); - } - - tmp[cs++] = '&'; - tmp[cs++] = 'a'; - tmp[cs++] = 'm'; - tmp[cs++] = 'p'; - tmp[cs++] = ';'; break; default: - tmp[cs++] = arg[x]; + data[0] = arg[x]; + if (!append(&tmp, &ds, data)) + exit(-8); break; } - - /* Assure, the string is '\0' terminated. */ - tmp[ds - 1] = '\0'; } return tmp; @@ -171,12 +110,10 @@ LPSTR tr_esc_str(LPCSTR arg, bool format) int main(int argc, char* argv[]) { size_t elements = sizeof(global_cmd_args) / sizeof(global_cmd_args[0]); - size_t x; const char* fname = "freerdp-argument.1.xml"; - FILE* fp = NULL; - /* Open output file for writing, truncate if existing. */ - fp = fopen(fname, "w"); + fprintf(stdout, "Generating docbook file '%s'\n", fname); + FILE* fp = fopen(fname, "w"); if (NULL == fp) { fprintf(stderr, "Could not open '%s' for writing.\n", fname); @@ -196,13 +133,13 @@ int main(int argc, char* argv[]) elements = 1; } - for (x = 0; x < elements - 1; x++) + for (size_t x = 0; x < elements - 1; x++) { const COMMAND_LINE_ARGUMENT_A* arg = &global_cmd_args[x]; - char* name = tr_esc_str((LPSTR)arg->Name, FALSE); - char* alias = tr_esc_str((LPSTR)arg->Alias, FALSE); + char* name = tr_esc_str(arg->Name, FALSE); + char* alias = tr_esc_str(arg->Alias, FALSE); char* format = tr_esc_str(arg->Format, TRUE); - char* text = tr_esc_str((LPSTR)arg->Text, FALSE); + char* text = tr_esc_str(arg->Text, FALSE); fprintf(fp, "\t\t\t\n"); do @@ -249,7 +186,7 @@ int main(int argc, char* argv[]) fprintf(fp, " (default:%s)", arg->Default ? "on" : "off"); else if (arg->Default) { - char* value = tr_esc_str((LPSTR)arg->Default, FALSE); + char* value = tr_esc_str(arg->Default, FALSE); fprintf(fp, " (default:%s)", value); free(value); } @@ -267,5 +204,7 @@ int main(int argc, char* argv[]) fprintf(fp, "\t\t\n"); fprintf(fp, "\t\n"); fclose(fp); + + fprintf(stdout, "successfully generated '%s'\n", fname); return 0; } diff --git a/cmake/InstallFreeRDPMan.cmake b/cmake/InstallFreeRDPMan.cmake index e3409f985..69cea4f0f 100644 --- a/cmake/InstallFreeRDPMan.cmake +++ b/cmake/InstallFreeRDPMan.cmake @@ -39,7 +39,7 @@ function(generate_and_install_freerdp_man_from_xml manpage dependencies) add_custom_target( ${manpage}.manpage ALL - DEPENDS + DEPENDSge ${manpage} ${manpage}.xml ${manpage}.xml.in