2 * winutils.c: miscellaneous Windows utilities
12 /* Definitions to allow this module to be compiled standalone for testing. */
13 #define smalloc malloc
17 * Split a complete command line into argc/argv, attempting to do
18 * it exactly the same way Windows itself would do it (so that
19 * console utilities, which receive argc and argv from Windows,
20 * will have their command lines processed in the same way as GUI
21 * utilities which get a whole command line and must break it
24 * Does not modify the input command line.
26 * The final parameter (argstart) is used to return a second array
27 * of char * pointers, the same length as argv, each one pointing
28 * at the start of the corresponding element of argv in the
29 * original command line. So if you get half way through processing
30 * your command line in argc/argv form and then decide you want to
31 * treat the rest as a raw string, you can. If you don't want to,
32 * `argstart' can be safely left NULL.
34 void split_into_argv(char *cmdline
, int *argc
, char ***argv
,
39 char **outputargv
, **outputargstart
;
43 * At first glance the rules appeared to be:
45 * - Single quotes are not special characters.
47 * - Double quotes are removed, but within them spaces cease
50 * - Backslashes are _only_ special when a sequence of them
51 * appear just before a double quote. In this situation,
52 * they are treated like C backslashes: so \" just gives a
53 * literal quote, \\" gives a literal backslash and then
54 * opens or closes a double-quoted segment, \\\" gives a
55 * literal backslash and then a literal quote, \\\\" gives
56 * two literal backslashes and then opens/closes a
57 * double-quoted segment, and so forth. Note that this
58 * behaviour is identical inside and outside double quotes.
60 * - Two successive double quotes become one literal double
61 * quote, but only _inside_ a double-quoted segment.
62 * Outside, they just form an empty double-quoted segment
63 * (which may cause an empty argument word).
65 * - That only leaves the interesting question of what happens
66 * when one or more backslashes precedes two or more double
67 * quotes, starting inside a double-quoted string. And the
68 * answer to that appears somewhat bizarre. Here I tabulate
69 * number of backslashes (across the top) against number of
70 * quotes (down the left), and indicate how many backslashes
71 * are output, how many quotes are output, and whether a
72 * quoted segment is open at the end of the sequence:
78 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
79 * --------+-----------------------------
80 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
81 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
82 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
83 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
84 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
85 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
86 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
87 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
88 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
89 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
90 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
93 * [Test fragment was of the form "a\\\"""b c" d.]
95 * There is very weird mod-3 behaviour going on here in the
96 * number of quotes, and it even applies when there aren't any
97 * backslashes! How ghastly.
99 * With a bit of thought, this extremely odd diagram suddenly
100 * coalesced itself into a coherent, if still ghastly, model of
103 * - As before, backslashes are only special when one or more
104 * of them appear contiguously before at least one double
105 * quote. In this situation the backslashes do exactly what
106 * you'd expect: each one quotes the next thing in front of
107 * it, so you end up with n/2 literal backslashes (if n is
108 * even) or (n-1)/2 literal backslashes and a literal quote
109 * (if n is odd). In the latter case the double quote
110 * character right after the backslashes is used up.
112 * - After that, any remaining double quotes are processed. A
113 * string of contiguous unescaped double quotes has a mod-3
116 * * inside a quoted segment, a quote ends the segment.
117 * * _immediately_ after ending a quoted segment, a quote
118 * simply produces a literal quote.
119 * * otherwise, outside a quoted segment, a quote begins a
122 * So, for example, if we started inside a quoted segment
123 * then two contiguous quotes would close the segment and
124 * produce a literal quote; three would close the segment,
125 * produce a literal quote, and open a new segment. If we
126 * started outside a quoted segment, then two contiguous
127 * quotes would open and then close a segment, producing no
128 * output (but potentially creating a zero-length argument);
129 * but three quotes would open and close a segment and then
130 * produce a literal quote.
134 * This will guaranteeably be big enough; we can realloc it
137 outputline
= smalloc(1+strlen(cmdline
));
138 outputargv
= smalloc(sizeof(char *) * (strlen(cmdline
)+1 / 2));
139 outputargstart
= smalloc(sizeof(char *) * (strlen(cmdline
)+1 / 2));
141 p
= cmdline
; q
= outputline
; outputargc
= 0;
146 /* Skip whitespace searching for start of argument. */
147 while (*p
&& isspace(*p
)) p
++;
150 /* We have an argument; start it. */
151 outputargv
[outputargc
] = q
;
152 outputargstart
[outputargc
] = p
;
156 /* Copy data into the argument until it's finished. */
158 if (!quote
&& isspace(*p
))
159 break; /* argument is finished */
161 if (*p
== '"' || *p
== '\\') {
163 * We have a sequence of zero or more backslashes
164 * followed by a sequence of zero or more quotes.
165 * Count up how many of each, and then deal with
166 * them as appropriate.
168 int i
, slashes
= 0, quotes
= 0;
169 while (*p
== '\\') slashes
++, p
++;
170 while (*p
== '"') quotes
++, p
++;
174 * Special case: if there are no quotes,
175 * slashes are not special at all, so just copy
176 * n slashes to the output string.
178 while (slashes
--) *q
++ = '\\';
180 /* Slashes annihilate in pairs. */
181 while (slashes
>= 2) slashes
-= 2, *q
++ = '\\';
183 /* One remaining slash takes out the first quote. */
184 if (slashes
) quotes
--, *q
++ = '"';
187 /* Outside a quote segment, a quote starts one. */
188 if (!quote
) quotes
--, quote
= 1;
190 /* Now we produce (n+1)/3 literal quotes... */
191 for (i
= 3; i
<= quotes
+1; i
+= 3) *q
++ = '"';
193 /* ... and end in a quote segment iff 3 divides n. */
194 quote
= (quotes
% 3 == 0);
202 /* At the end of an argument, just append a trailing NUL. */
206 outputargv
= srealloc(outputargv
, sizeof(char *) * outputargc
);
207 outputargstart
= srealloc(outputargstart
, sizeof(char *) * outputargc
);
209 if (argc
) *argc
= outputargc
;
210 if (argv
) *argv
= outputargv
; else sfree(outputargv
);
211 if (argstart
) *argstart
= outputargstart
; else sfree(outputargstart
);
216 const struct argv_test
{
218 const char *argv
[10];
221 * We generate this set of tests by invoking ourself with
224 {"ab c\" d", {"ab", "c d", NULL
}},
225 {"a\"b c\" d", {"ab c", "d", NULL
}},
226 {"a\"\"b c\" d", {"ab", "c d", NULL
}},
227 {"a\"\"\"b c\" d", {"a\"b", "c d", NULL
}},
228 {"a\"\"\"\"b c\" d", {"a\"b c", "d", NULL
}},
229 {"a\"\"\"\"\"b c\" d", {"a\"b", "c d", NULL
}},
230 {"a\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
231 {"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL
}},
232 {"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
233 {"a\\b c\" d", {"a\\b", "c d", NULL
}},
234 {"a\\\"b c\" d", {"a\"b", "c d", NULL
}},
235 {"a\\\"\"b c\" d", {"a\"b c", "d", NULL
}},
236 {"a\\\"\"\"b c\" d", {"a\"b", "c d", NULL
}},
237 {"a\\\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
238 {"a\\\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL
}},
239 {"a\\\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
240 {"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL
}},
241 {"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL
}},
242 {"a\\\\b c\" d", {"a\\\\b", "c d", NULL
}},
243 {"a\\\\\"b c\" d", {"a\\b c", "d", NULL
}},
244 {"a\\\\\"\"b c\" d", {"a\\b", "c d", NULL
}},
245 {"a\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
246 {"a\\\\\"\"\"\"b c\" d", {"a\\\"b c", "d", NULL
}},
247 {"a\\\\\"\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
248 {"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
249 {"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL
}},
250 {"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
251 {"a\\\\\\b c\" d", {"a\\\\\\b", "c d", NULL
}},
252 {"a\\\\\\\"b c\" d", {"a\\\"b", "c d", NULL
}},
253 {"a\\\\\\\"\"b c\" d", {"a\\\"b c", "d", NULL
}},
254 {"a\\\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
255 {"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
256 {"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL
}},
257 {"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
258 {"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL
}},
259 {"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL
}},
260 {"a\\\\\\\\b c\" d", {"a\\\\\\\\b", "c d", NULL
}},
261 {"a\\\\\\\\\"b c\" d", {"a\\\\b c", "d", NULL
}},
262 {"a\\\\\\\\\"\"b c\" d", {"a\\\\b", "c d", NULL
}},
263 {"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL
}},
264 {"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL
}},
265 {"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL
}},
266 {"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL
}},
267 {"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL
}},
268 {"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL
}},
269 {"\"ab c\" d", {"ab c", "d", NULL
}},
270 {"\"a\"b c\" d", {"ab", "c d", NULL
}},
271 {"\"a\"\"b c\" d", {"a\"b", "c d", NULL
}},
272 {"\"a\"\"\"b c\" d", {"a\"b c", "d", NULL
}},
273 {"\"a\"\"\"\"b c\" d", {"a\"b", "c d", NULL
}},
274 {"\"a\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
275 {"\"a\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL
}},
276 {"\"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
277 {"\"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL
}},
278 {"\"a\\b c\" d", {"a\\b c", "d", NULL
}},
279 {"\"a\\\"b c\" d", {"a\"b c", "d", NULL
}},
280 {"\"a\\\"\"b c\" d", {"a\"b", "c d", NULL
}},
281 {"\"a\\\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
282 {"\"a\\\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL
}},
283 {"\"a\\\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL
}},
284 {"\"a\\\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL
}},
285 {"\"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL
}},
286 {"\"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL
}},
287 {"\"a\\\\b c\" d", {"a\\\\b c", "d", NULL
}},
288 {"\"a\\\\\"b c\" d", {"a\\b", "c d", NULL
}},
289 {"\"a\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
290 {"\"a\\\\\"\"\"b c\" d", {"a\\\"b c", "d", NULL
}},
291 {"\"a\\\\\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
292 {"\"a\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
293 {"\"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL
}},
294 {"\"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
295 {"\"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL
}},
296 {"\"a\\\\\\b c\" d", {"a\\\\\\b c", "d", NULL
}},
297 {"\"a\\\\\\\"b c\" d", {"a\\\"b c", "d", NULL
}},
298 {"\"a\\\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL
}},
299 {"\"a\\\\\\\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
300 {"\"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL
}},
301 {"\"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL
}},
302 {"\"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL
}},
303 {"\"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL
}},
304 {"\"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL
}},
305 {"\"a\\\\\\\\b c\" d", {"a\\\\\\\\b c", "d", NULL
}},
306 {"\"a\\\\\\\\\"b c\" d", {"a\\\\b", "c d", NULL
}},
307 {"\"a\\\\\\\\\"\"b c\" d", {"a\\\\\"b", "c d", NULL
}},
308 {"\"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL
}},
309 {"\"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL
}},
310 {"\"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL
}},
311 {"\"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL
}},
312 {"\"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL
}},
313 {"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL
}},
316 int main(int argc
, char **argv
)
322 * Generation of tests.
324 * Given `-splat <args>', we print out a C-style
325 * representation of each argument (in the form "a", "b",
326 * NULL), backslash-escaping each backslash and double
329 * Given `-split <string>', we first doctor `string' by
330 * turning forward slashes into backslashes, single quotes
331 * into double quotes and underscores into spaces; and then
332 * we feed the resulting string to ourself with `-splat'.
334 * Given `-generate', we concoct a variety of fun test
335 * cases, encode them in quote-safe form (mapping \, " and
336 * space to /, ' and _ respectively) and feed each one to
339 if (!strcmp(argv
[1], "-splat")) {
342 for (i
= 2; i
< argc
; i
++) {
344 for (p
= argv
[i
]; *p
; p
++) {
345 if (*p
== '\\' || *p
== '"')
355 if (!strcmp(argv
[1], "-split") && argc
> 2) {
356 char *str
= malloc(20 + strlen(argv
[0]) + strlen(argv
[2]));
359 q
= str
+ sprintf(str
, "%s -splat ", argv
[0]);
361 for (p
= argv
[2]; *p
; p
++, q
++) {
363 case '/': printf("\\\\"); *q
= '\\'; break;
364 case '\'': printf("\\\""); *q
= '"'; break;
365 case '_': printf(" "); *q
= ' '; break;
366 default: putchar(*p
); *q
= *p
; break;
380 if (!strcmp(argv
[1], "-generate")) {
382 int i
, initialquote
, backslashes
, quotes
;
384 teststr
= malloc(200 + strlen(argv
[0]));
386 for (initialquote
= 0; initialquote
<= 1; initialquote
++) {
387 for (backslashes
= 0; backslashes
< 5; backslashes
++) {
388 for (quotes
= 0; quotes
< 9; quotes
++) {
389 p
= teststr
+ sprintf(teststr
, "%s -split ", argv
[0]);
390 if (initialquote
) *p
++ = '\'';
392 for (i
= 0; i
< backslashes
; i
++) *p
++ = '/';
393 for (i
= 0; i
< quotes
; i
++) *p
++ = '\'';
409 fprintf(stderr
, "unrecognised option: \"%s\"\n", argv
[1]);
414 * If we get here, we were invoked with no arguments, so just
418 for (i
= 0; i
< lenof(argv_tests
); i
++) {
422 split_into_argv(argv_tests
[i
].cmdline
, &ac
, &av
);
424 for (j
= 0; j
< ac
&& argv_tests
[i
].argv
[j
]; j
++) {
425 if (strcmp(av
[j
], argv_tests
[i
].argv
[j
])) {
426 printf("failed test %d (|%s|) arg %d: |%s| should be |%s|\n",
427 i
, argv_tests
[i
].cmdline
,
428 j
, av
[j
], argv_tests
[i
].argv
[j
]);
432 printf("test %d (|%s|) arg %d: |%s| == |%s|\n",
433 i
, argv_tests
[i
].cmdline
,
434 j
, av
[j
], argv_tests
[i
].argv
[j
]);
439 printf("failed test %d (|%s|): %d args returned, should be %d\n",
440 i
, argv_tests
[i
].cmdline
, ac
, j
);
441 if (argv_tests
[i
].argv
[j
])
442 printf("failed test %d (|%s|): %d args returned, should be more\n",
443 i
, argv_tests
[i
].cmdline
, ac
);