2013-08-05 18:39:49 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2018-02-16 15:42:10 +03:00
|
|
|
#include <string.h>
|
2013-08-05 18:39:49 +04:00
|
|
|
|
2018-02-01 18:59:55 +03:00
|
|
|
#include "../common/cmdline.h"
|
2013-08-05 18:39:49 +04:00
|
|
|
|
2018-02-01 18:59:55 +03:00
|
|
|
#define TAG FREERDP_TAG("generate_argument_docbook")
|
2017-09-25 12:37:43 +03:00
|
|
|
LPSTR tr_esc_str(LPCSTR arg, bool format)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2014-05-23 15:07:37 +04:00
|
|
|
LPSTR tmp = NULL;
|
2019-10-04 15:49:30 +03:00
|
|
|
LPSTR tmp2 = NULL;
|
2017-09-25 12:37:43 +03:00
|
|
|
size_t cs = 0, x, ds, len;
|
2013-08-05 18:39:49 +04:00
|
|
|
size_t s;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == arg)
|
2013-08-05 18:39:49 +04:00
|
|
|
return NULL;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
s = strlen(arg);
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
/* Find trailing whitespaces */
|
2018-08-22 14:13:37 +03:00
|
|
|
while ((s > 0) && isspace(arg[s - 1]))
|
2013-08-05 18:39:49 +04:00
|
|
|
s--;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
/* Prepare a initial buffer with the size of the result string. */
|
2014-05-23 15:07:37 +04:00
|
|
|
ds = s + 1;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2019-11-25 17:54:05 +03:00
|
|
|
if (ds)
|
2019-10-04 15:49:30 +03:00
|
|
|
{
|
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not allocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-2);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
/* Copy character for character and check, if it is necessary to escape. */
|
2014-05-23 15:07:37 +04:00
|
|
|
memset(tmp, 0, ds * sizeof(CHAR));
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
for (x = 0; x < s; x++)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2018-08-22 14:13:37 +03:00
|
|
|
switch (arg[x])
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
|
|
|
case '<':
|
2017-09-25 12:37:43 +03:00
|
|
|
len = format ? 13 : 4;
|
|
|
|
ds += len - 1;
|
2019-10-04 15:49:30 +03:00
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not reallocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-3);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 12:37:43 +03:00
|
|
|
if (format)
|
2018-08-21 10:07:15 +03:00
|
|
|
/* coverity[buffer_size] */
|
2018-08-22 14:13:37 +03:00
|
|
|
strncpy(&tmp[cs], "<replaceable>", len);
|
|
|
|
else
|
2018-08-21 10:07:15 +03:00
|
|
|
/* coverity[buffer_size] */
|
2018-08-22 14:13:37 +03:00
|
|
|
strncpy(&tmp[cs], "<", len);
|
|
|
|
|
2017-09-25 12:37:43 +03:00
|
|
|
cs += len;
|
2013-08-05 18:39:49 +04:00
|
|
|
break;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
case '>':
|
2017-09-25 12:37:43 +03:00
|
|
|
len = format ? 14 : 4;
|
|
|
|
ds += len - 1;
|
2019-10-04 15:49:30 +03:00
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not reallocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-4);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 12:37:43 +03:00
|
|
|
if (format)
|
2018-08-21 10:07:15 +03:00
|
|
|
/* coverity[buffer_size] */
|
2018-08-22 14:13:37 +03:00
|
|
|
strncpy(&tmp[cs], "</replaceable>", len);
|
|
|
|
else
|
2018-08-21 10:07:15 +03:00
|
|
|
/* coverity[buffer_size] */
|
2020-05-30 12:57:10 +03:00
|
|
|
strncpy(&tmp[cs], ">", len);
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 12:37:43 +03:00
|
|
|
cs += len;
|
2013-08-05 18:39:49 +04:00
|
|
|
break;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
case '\'':
|
|
|
|
ds += 5;
|
2019-10-04 15:49:30 +03:00
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not reallocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-5);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
tmp[cs++] = '&';
|
|
|
|
tmp[cs++] = 'a';
|
|
|
|
tmp[cs++] = 'p';
|
|
|
|
tmp[cs++] = 'o';
|
|
|
|
tmp[cs++] = 's';
|
|
|
|
tmp[cs++] = ';';
|
|
|
|
break;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
case '"':
|
|
|
|
ds += 5;
|
2019-10-04 15:49:30 +03:00
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not reallocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-6);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
tmp[cs++] = '&';
|
|
|
|
tmp[cs++] = 'q';
|
|
|
|
tmp[cs++] = 'u';
|
|
|
|
tmp[cs++] = 'o';
|
|
|
|
tmp[cs++] = 't';
|
|
|
|
tmp[cs++] = ';';
|
|
|
|
break;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
case '&':
|
|
|
|
ds += 4;
|
2019-10-04 15:49:30 +03:00
|
|
|
tmp2 = (LPSTR)realloc(tmp, ds * sizeof(CHAR));
|
|
|
|
if (!tmp2)
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == tmp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not reallocate string buffer.\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
exit(-7);
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
tmp[cs++] = '&';
|
|
|
|
tmp[cs++] = 'a';
|
|
|
|
tmp[cs++] = 'm';
|
|
|
|
tmp[cs++] = 'p';
|
|
|
|
tmp[cs++] = ';';
|
|
|
|
break;
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
default:
|
|
|
|
tmp[cs++] = arg[x];
|
|
|
|
break;
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
/* Assure, the string is '\0' terminated. */
|
2018-08-22 14:13:37 +03:00
|
|
|
tmp[ds - 1] = '\0';
|
2013-08-05 18:39:49 +04:00
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:37 +03:00
|
|
|
int main(int argc, char* argv[])
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2018-08-22 14:13:37 +03:00
|
|
|
size_t elements = sizeof(args) / sizeof(args[0]);
|
2013-08-05 18:39:49 +04:00
|
|
|
size_t x;
|
2018-08-22 14:13:37 +03:00
|
|
|
const char* fname = "xfreerdp-argument.1.xml";
|
|
|
|
FILE* fp = NULL;
|
2013-08-05 18:39:49 +04:00
|
|
|
/* Open output file for writing, truncate if existing. */
|
|
|
|
fp = fopen(fname, "w");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
|
|
|
if (NULL == fp)
|
2013-08-05 18:39:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "Could not open '%s' for writing.\n", fname);
|
2018-02-16 15:42:10 +03:00
|
|
|
return -1;
|
2013-08-05 18:39:49 +04:00
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
/* The tag used as header in the manpage */
|
|
|
|
fprintf(fp, "<refsect1>\n");
|
2013-08-06 13:41:38 +04:00
|
|
|
fprintf(fp, "\t<title>Options</title>\n");
|
2013-08-05 18:39:49 +04:00
|
|
|
fprintf(fp, "\t\t<variablelist>\n");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2014-05-23 15:07:37 +04:00
|
|
|
/* Iterate over argument struct and write data to docbook 4.5
|
2013-08-05 18:39:49 +04:00
|
|
|
* compatible XML */
|
2018-08-22 14:13:37 +03:00
|
|
|
if (elements < 2)
|
2013-08-06 12:23:43 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
fprintf(stderr, "The argument array 'args' is empty, writing an empty file.\n");
|
2013-08-06 12:23:43 +04:00
|
|
|
elements = 1;
|
|
|
|
}
|
2017-09-25 12:33:03 +03:00
|
|
|
|
2018-08-22 14:13:37 +03:00
|
|
|
for (x = 0; x < elements - 1; x++)
|
|
|
|
{
|
|
|
|
const COMMAND_LINE_ARGUMENT_A* arg = &args[x];
|
2019-11-06 17:24:51 +03:00
|
|
|
char* name = tr_esc_str((LPSTR)arg->Name, FALSE);
|
|
|
|
char* alias = tr_esc_str((LPSTR)arg->Alias, FALSE);
|
2018-08-22 14:13:37 +03:00
|
|
|
char* format = tr_esc_str(arg->Format, TRUE);
|
2019-11-06 17:24:51 +03:00
|
|
|
char* text = tr_esc_str((LPSTR)arg->Text, FALSE);
|
2013-08-05 18:39:49 +04:00
|
|
|
fprintf(fp, "\t\t\t<varlistentry>\n");
|
2016-06-15 23:39:03 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
do
|
2017-09-25 12:37:43 +03:00
|
|
|
{
|
2017-09-25 13:41:40 +03:00
|
|
|
fprintf(fp, "\t\t\t\t<term><option>");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
if (arg->Flags == COMMAND_LINE_VALUE_BOOL)
|
|
|
|
fprintf(fp, "%s", arg->Default ? "-" : "+");
|
|
|
|
else
|
|
|
|
fprintf(fp, "/");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
fprintf(fp, "%s</option>", name);
|
2017-09-25 12:37:43 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
if (format)
|
|
|
|
{
|
|
|
|
if (arg->Flags == COMMAND_LINE_VALUE_OPTIONAL)
|
|
|
|
fprintf(fp, "[");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
fprintf(fp, ":%s", format);
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
if (arg->Flags == COMMAND_LINE_VALUE_OPTIONAL)
|
|
|
|
fprintf(fp, "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "</term>\n");
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2017-09-25 13:41:40 +03:00
|
|
|
if (alias == name)
|
|
|
|
break;
|
|
|
|
|
2018-08-22 14:13:37 +03:00
|
|
|
free(name);
|
2017-09-25 13:41:40 +03:00
|
|
|
name = alias;
|
2019-11-06 17:24:51 +03:00
|
|
|
} while (alias);
|
2016-06-15 23:39:03 +03:00
|
|
|
|
2017-09-25 12:37:43 +03:00
|
|
|
if (text)
|
2016-06-15 23:39:03 +03:00
|
|
|
{
|
|
|
|
fprintf(fp, "\t\t\t\t<listitem>\n");
|
2017-09-25 12:37:43 +03:00
|
|
|
fprintf(fp, "\t\t\t\t\t<para>");
|
|
|
|
|
2016-06-15 23:39:03 +03:00
|
|
|
if (text)
|
|
|
|
fprintf(fp, "%s", text);
|
2017-09-25 12:37:43 +03:00
|
|
|
|
2020-03-19 00:00:06 +03:00
|
|
|
if (arg->Flags & COMMAND_LINE_VALUE_BOOL &&
|
|
|
|
(!arg->Default || arg->Default == BoolValueTrue))
|
2017-09-25 12:37:43 +03:00
|
|
|
fprintf(fp, " (default:%s)", arg->Default ? "on" : "off");
|
|
|
|
else if (arg->Default)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
char* value = tr_esc_str((LPSTR)arg->Default, FALSE);
|
2017-09-25 12:37:43 +03:00
|
|
|
fprintf(fp, " (default:%s)", value);
|
2018-08-22 14:13:37 +03:00
|
|
|
free(value);
|
2016-06-15 23:39:03 +03:00
|
|
|
}
|
2017-09-25 12:37:43 +03:00
|
|
|
|
2016-06-15 23:39:03 +03:00
|
|
|
fprintf(fp, "</para>\n");
|
|
|
|
fprintf(fp, "\t\t\t\t</listitem>\n");
|
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
fprintf(fp, "\t\t\t</varlistentry>\n");
|
2017-09-25 12:33:03 +03:00
|
|
|
free(name);
|
|
|
|
free(format);
|
|
|
|
free(text);
|
2013-08-05 18:39:49 +04:00
|
|
|
}
|
2018-08-22 14:13:37 +03:00
|
|
|
|
2013-08-05 18:39:49 +04:00
|
|
|
fprintf(fp, "\t\t</variablelist>\n");
|
|
|
|
fprintf(fp, "\t</refsect1>\n");
|
|
|
|
fclose(fp);
|
2018-02-16 15:42:10 +03:00
|
|
|
return 0;
|
2013-08-05 18:39:49 +04:00
|
|
|
}
|