-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandline.c
207 lines (198 loc) · 5.13 KB
/
commandline.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "commandline.h"
LPSTR* WINAPI CommandLineToArgvA(LPSTR lpCmdline, int* numargs)
{
DWORD argc;
LPSTR* argv;
LPSTR s;
LPSTR d;
LPSTR cmdline;
int qcount, bcount;
if (!numargs || *lpCmdline == 0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
/* --- First count the arguments */
argc = 1;
s = lpCmdline;
/* The first argument, the executable path, follows special rules */
if (*s == '"')
{
/* The executable path ends at the next quote, no matter what */
s++;
while (*s)
if (*s++ == '"')
break;
}
else
{
/* The executable path ends at the next space, no matter what */
while (*s && *s != ' ' && *s != '\t')
s++;
}
/* skip to the first argument, if any */
while (*s == ' ' || *s == '\t')
s++;
if (*s)
argc++;
/* Analyze the remaining arguments */
qcount = bcount = 0;
while (*s)
{
if ((*s == ' ' || *s == '\t') && qcount == 0)
{
/* skip to the next argument and count it if any */
while (*s == ' ' || *s == '\t')
s++;
if (*s)
argc++;
bcount = 0;
}
else if (*s == '\\')
{
/* '\', count them */
bcount++;
s++;
}
else if (*s == '"')
{
/* '"' */
if ((bcount & 1) == 0)
qcount++; /* unescaped '"' */
s++;
bcount = 0;
/* consecutive quotes, see comment in copying code below */
while (*s == '"')
{
qcount++;
s++;
}
qcount = qcount % 3;
if (qcount == 2)
qcount = 0;
}
else
{
/* a regular character */
bcount = 0;
s++;
}
}
/* Allocate in a single lump, the string array, and the strings that go
* with it. This way the caller can make a single LocalFree() call to free
* both, as per MSDN.
*/
argv = LocalAlloc(LMEM_FIXED, (argc + 1) * sizeof(LPSTR) + (lstrlenA(lpCmdline) + 1) * sizeof(char));
if (!argv)
return NULL;
cmdline = (LPSTR)(argv + argc + 1);
lstrcpyA(cmdline, lpCmdline);
/* --- Then split and copy the arguments */
argv[0] = d = cmdline;
argc = 1;
/* The first argument, the executable path, follows special rules */
if (*d == '"')
{
/* The executable path ends at the next quote, no matter what */
s = d + 1;
while (*s)
{
if (*s == '"')
{
s++;
break;
}
*d++ = *s++;
}
}
else
{
/* The executable path ends at the next space, no matter what */
while (*d && *d != ' ' && *d != '\t')
d++;
s = d;
if (*s)
s++;
}
/* close the executable path */
*d++ = 0;
/* skip to the first argument and initialize it if any */
while (*s == ' ' || *s == '\t')
s++;
if (!*s)
{
/* There are no parameters so we are all done */
argv[argc] = NULL;
*numargs = argc;
return argv;
}
/* Split and copy the remaining arguments */
argv[argc++] = d;
qcount = bcount = 0;
while (*s)
{
if ((*s == ' ' || *s == '\t') && qcount == 0)
{
/* close the argument */
*d++ = 0;
bcount = 0;
/* skip to the next one and initialize it if any */
do {
s++;
} while (*s == ' ' || *s == '\t');
if (*s)
argv[argc++] = d;
}
else if (*s == '\\')
{
*d++ = *s++;
bcount++;
}
else if (*s == '"')
{
if ((bcount & 1) == 0)
{
/* Preceded by an even number of '\', this is half that
* number of '\', plus a quote which we erase.
*/
d -= bcount / 2;
qcount++;
}
else
{
/* Preceded by an odd number of '\', this is half that
* number of '\' followed by a '"'
*/
d = d - bcount / 2 - 1;
*d++ = '"';
}
s++;
bcount = 0;
/* Now count the number of consecutive quotes. Note that qcount
* already takes into account the opening quote if any, as well as
* the quote that lead us here.
*/
while (*s == '"')
{
if (++qcount == 3)
{
*d++ = '"';
qcount = 0;
}
s++;
}
if (qcount == 2)
qcount = 0;
}
else
{
/* a regular character */
*d++ = *s++;
bcount = 0;
}
}
*d = '\0';
argv[argc] = NULL;
*numargs = argc;
return argv;
}