blob: d133a5bd9f8b0e4a734d5b476a0b3260cfb48441 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include <win32lib.h>
#undef _DEBUG
#ifdef _DEBUG
#define TRACE(x) x
#else
#define TRACE(x)
#endif
/*
Return a quoted string if space is found
New string is allocated
*/
static char *
quote_elt(char *elt)
{
int len = 2*strlen(elt)*sizeof(char) + 2;
char *to = xmalloc(len);
char *p = to;
BOOL need_quote = FALSE;
if (strchr(elt, ' ')) {
*to++ = '"';
need_quote = TRUE;
}
while (*elt) {
switch (*elt) {
case '"':
case '\'':
/* case '\\': */
*to++ = '\\';
default:
*to++ = *elt++;
}
}
if (need_quote) {
*to++ = '"';
}
*to = *elt;
to = xstrdup(p);
free(p);
return to;
}
/*
Process an array of strings, quote them
an return their catenation
*/
static char *
quote_args(char **argv)
{
int i;
char *line = NULL, *new_line;
char *new_argv_i;
if (!argv)
return line;
line = quote_elt(argv[0]);
for (i = 1; argv[i]; i++) {
new_argv_i = quote_elt(argv[i]);
new_line = concat3(line, " ", new_argv_i);
TRACE(fprintf(stderr, "quote_args: new_line = %s\n", new_line));
free(line);
free(new_argv_i);
line = new_line;
}
return line;
}
int
main (int argc, char *argv[])
{
char *cmd_line;
argv[0] = "mktextfm";
cmd_line = quote_args(argv);
return system(cmd_line);
}
|