blob: 3b8e5a5a65f58d1fa00a88a01eca7a8734ecb834 (
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*=========================================================================
Program: KWSys - Kitware System Library
Module: $RCSfile$
Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "kwsysPrivate.h"
#include KWSYS_HEADER(System.h)
/* Work-around CMake dependency scanning limitation. This must
duplicate the above list of headers. */
#if 0
# include "System.h.in"
#endif
#include <string.h> /* strlen */
/*--------------------------------------------------------------------------*/
static int kwsysSystemWindowsShellArgumentNeedsEscape(const char* in)
{
/* Scan the string for characters that need escaping. Note that
single quotes seem to need escaping for some windows shell
environments (mingw32-make shell for example). Single quotes do
not actually need backslash escapes but must be in a
double-quoted argument. */
const char* c;
for(c=in; *c; ++c)
{
if(*c == ' ' || *c == '\t' || *c == '"' || *c == '\'')
{
return 1;
}
}
return 0;
}
/*--------------------------------------------------------------------------*/
int kwsysSystem_Windows_ShellArgumentSize(const char* in)
{
/* Start with the length of the original argument, plus one for
either a terminating null or a separating space. */
int length = (int)strlen(in) + 1;
/* String iterator. */
const char* c;
/* Keep track of how many backslashes have been encountered in a row. */
int backslashes = 0;
/* If nothing needs escaping, we do not need any extra length. */
if(!kwsysSystemWindowsShellArgumentNeedsEscape(in))
{
return length;
}
/* Add 2 for double quotes since spaces are present. */
length += 2;
/* Scan the string to find characters that need escaping. */
for(c=in; *c; ++c)
{
if(*c == '\\')
{
/* Found a backslash. It may need to be escaped later. */
++backslashes;
}
else if(*c == '"')
{
/* Found a double-quote. We need to escape it and all
immediately preceding backslashes. */
length += backslashes + 1;
backslashes = 0;
}
else
{
/* Found another character. This eliminates the possibility
that any immediately preceding backslashes will be
escaped. */
backslashes = 0;
}
}
/* We need to escape all ending backslashes. */
length += backslashes;
return length;
}
/*--------------------------------------------------------------------------*/
char* kwsysSystem_Windows_ShellArgument(const char* in, char* out)
{
/* String iterator. */
const char* c;
/* Keep track of how many backslashes have been encountered in a row. */
int backslashes = 0;
/* If nothing needs escaping, we can pass the argument verbatim. */
if(!kwsysSystemWindowsShellArgumentNeedsEscape(in))
{
/* Just copy the string. */
for(c=in; *c; ++c)
{
*out++ = *c;
}
/* Store a terminating null without incrementing. */
*out = 0;
return out;
}
/* Add the opening double-quote for this argument. */
*out++ = '"';
/* Add the characters of the argument, possibly escaping them. */
for(c=in; *c; ++c)
{
if(*c == '\\')
{
/* Found a backslash. It may need to be escaped later. */
++backslashes;
*out++ = '\\';
}
else if(*c == '"')
{
/* Add enough backslashes to escape any that preceded the
double-quote. */
while(backslashes > 0)
{
--backslashes;
*out++ = '\\';
}
/* Add the backslash to escape the double-quote. */
*out++ = '\\';
/* Add the double-quote itself. */
*out++ = '"';
}
else
{
/* We encountered a normal character. This eliminates any
escaping needed for preceding backslashes. Add the
character. */
backslashes = 0;
*out++ = *c;
}
}
/* Add enough backslashes to escape any trailing ones. */
while(backslashes > 0)
{
--backslashes;
*out++ = '\\';
}
/* Add the closing double-quote for this argument. */
*out++ = '"';
/* Store a terminating null without incrementing. */
*out = 0;
return out;
}
|