c0a81592 |
1 | /* |
f4ff9455 |
2 | * winutils.c: miscellaneous Windows utilities for GUI apps |
c0a81592 |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
d3a1a808 |
7 | #include <ctype.h> |
9754e5ca |
8 | #include <limits.h> |
c0a81592 |
9 | |
9754e5ca |
10 | #include "winstuff.h" |
d3a1a808 |
11 | #include "misc.h" |
c0a81592 |
12 | |
13 | #ifdef TESTMODE |
9754e5ca |
14 | /* Definitions to allow this module to be compiled standalone for testing |
15 | * split_into_argv(). */ |
c0a81592 |
16 | #define smalloc malloc |
5a71c4ea |
17 | #define srealloc realloc |
18 | #define sfree free |
c0a81592 |
19 | #endif |
20 | |
21 | /* |
9754e5ca |
22 | * GetOpenFileName/GetSaveFileName tend to muck around with the process' |
23 | * working directory on at least some versions of Windows. |
24 | * Here's a wrapper that gives more control over this, and hides a little |
25 | * bit of other grottiness. |
26 | */ |
27 | |
28 | struct filereq_tag { |
29 | TCHAR cwd[PATH_MAX]; |
30 | }; |
31 | |
32 | /* |
33 | * `of' is expected to be initialised with most interesting fields, but |
34 | * this function does some administrivia. (assume `of' was memset to 0) |
35 | * save==1 -> GetSaveFileName; save==0 -> GetOpenFileName |
36 | * `state' is optional. |
37 | */ |
38 | BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save) |
39 | { |
40 | TCHAR cwd[PATH_MAX]; /* process CWD */ |
41 | BOOL ret; |
42 | |
43 | /* Get process CWD */ |
44 | if (preserve) { |
45 | DWORD r = GetCurrentDirectory(lenof(cwd), cwd); |
46 | if (r == 0 || r >= lenof(cwd)) |
47 | /* Didn't work, oh well. Stop trying to be clever. */ |
48 | preserve = 0; |
49 | } |
50 | |
51 | /* Open the file requester, maybe setting lpstrInitialDir */ |
52 | { |
53 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
54 | of->lStructSize = OPENFILENAME_SIZE_VERSION_400; |
55 | #else |
56 | of->lStructSize = sizeof(*of); |
57 | #endif |
58 | of->lpstrInitialDir = (state && state->cwd[0]) ? state->cwd : NULL; |
59 | /* Actually put up the requester. */ |
60 | ret = save ? GetSaveFileName(of) : GetOpenFileName(of); |
61 | } |
62 | |
63 | /* Get CWD left by requester */ |
64 | if (state) { |
65 | DWORD r = GetCurrentDirectory(lenof(state->cwd), state->cwd); |
66 | if (r == 0 || r >= lenof(state->cwd)) |
67 | /* Didn't work, oh well. */ |
68 | state->cwd[0] = '\0'; |
69 | } |
70 | |
71 | /* Restore process CWD */ |
72 | if (preserve) |
73 | /* If it fails, there's not much we can do. */ |
74 | (void) SetCurrentDirectory(cwd); |
75 | |
76 | return ret; |
77 | } |
78 | |
79 | filereq *filereq_new(void) |
80 | { |
81 | filereq *ret = snew(filereq); |
82 | ret->cwd[0] = '\0'; |
83 | return ret; |
84 | } |
85 | |
86 | void filereq_free(filereq *state) |
87 | { |
88 | sfree(state); |
89 | } |
90 | |
91 | /* |
c0a81592 |
92 | * Split a complete command line into argc/argv, attempting to do |
93 | * it exactly the same way Windows itself would do it (so that |
94 | * console utilities, which receive argc and argv from Windows, |
95 | * will have their command lines processed in the same way as GUI |
96 | * utilities which get a whole command line and must break it |
97 | * themselves). |
98 | * |
d3a1a808 |
99 | * Does not modify the input command line. |
100 | * |
101 | * The final parameter (argstart) is used to return a second array |
102 | * of char * pointers, the same length as argv, each one pointing |
103 | * at the start of the corresponding element of argv in the |
104 | * original command line. So if you get half way through processing |
105 | * your command line in argc/argv form and then decide you want to |
106 | * treat the rest as a raw string, you can. If you don't want to, |
107 | * `argstart' can be safely left NULL. |
c0a81592 |
108 | */ |
d3a1a808 |
109 | void split_into_argv(char *cmdline, int *argc, char ***argv, |
110 | char ***argstart) |
c0a81592 |
111 | { |
d3a1a808 |
112 | char *p; |
c0a81592 |
113 | char *outputline, *q; |
d3a1a808 |
114 | char **outputargv, **outputargstart; |
c0a81592 |
115 | int outputargc; |
116 | |
117 | /* |
118 | * At first glance the rules appeared to be: |
119 | * |
120 | * - Single quotes are not special characters. |
121 | * |
122 | * - Double quotes are removed, but within them spaces cease |
123 | * to be special. |
124 | * |
125 | * - Backslashes are _only_ special when a sequence of them |
126 | * appear just before a double quote. In this situation, |
127 | * they are treated like C backslashes: so \" just gives a |
128 | * literal quote, \\" gives a literal backslash and then |
129 | * opens or closes a double-quoted segment, \\\" gives a |
130 | * literal backslash and then a literal quote, \\\\" gives |
131 | * two literal backslashes and then opens/closes a |
132 | * double-quoted segment, and so forth. Note that this |
133 | * behaviour is identical inside and outside double quotes. |
134 | * |
135 | * - Two successive double quotes become one literal double |
136 | * quote, but only _inside_ a double-quoted segment. |
137 | * Outside, they just form an empty double-quoted segment |
138 | * (which may cause an empty argument word). |
139 | * |
140 | * - That only leaves the interesting question of what happens |
141 | * when one or more backslashes precedes two or more double |
142 | * quotes, starting inside a double-quoted string. And the |
143 | * answer to that appears somewhat bizarre. Here I tabulate |
144 | * number of backslashes (across the top) against number of |
145 | * quotes (down the left), and indicate how many backslashes |
146 | * are output, how many quotes are output, and whether a |
147 | * quoted segment is open at the end of the sequence: |
148 | * |
149 | * backslashes |
150 | * |
151 | * 0 1 2 3 4 |
152 | * |
153 | * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y |
154 | * --------+----------------------------- |
155 | * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n |
156 | * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n |
157 | * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y |
158 | * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n |
159 | * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n |
160 | * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y |
161 | * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n |
162 | * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n |
163 | * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y |
164 | * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n |
165 | * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n |
166 | * |
167 | * |
168 | * [Test fragment was of the form "a\\\"""b c" d.] |
169 | * |
170 | * There is very weird mod-3 behaviour going on here in the |
171 | * number of quotes, and it even applies when there aren't any |
172 | * backslashes! How ghastly. |
173 | * |
174 | * With a bit of thought, this extremely odd diagram suddenly |
175 | * coalesced itself into a coherent, if still ghastly, model of |
176 | * how things work: |
177 | * |
178 | * - As before, backslashes are only special when one or more |
179 | * of them appear contiguously before at least one double |
180 | * quote. In this situation the backslashes do exactly what |
181 | * you'd expect: each one quotes the next thing in front of |
182 | * it, so you end up with n/2 literal backslashes (if n is |
183 | * even) or (n-1)/2 literal backslashes and a literal quote |
184 | * (if n is odd). In the latter case the double quote |
185 | * character right after the backslashes is used up. |
186 | * |
187 | * - After that, any remaining double quotes are processed. A |
188 | * string of contiguous unescaped double quotes has a mod-3 |
189 | * behaviour: |
190 | * |
191 | * * inside a quoted segment, a quote ends the segment. |
192 | * * _immediately_ after ending a quoted segment, a quote |
193 | * simply produces a literal quote. |
194 | * * otherwise, outside a quoted segment, a quote begins a |
195 | * quoted segment. |
196 | * |
197 | * So, for example, if we started inside a quoted segment |
198 | * then two contiguous quotes would close the segment and |
199 | * produce a literal quote; three would close the segment, |
200 | * produce a literal quote, and open a new segment. If we |
201 | * started outside a quoted segment, then two contiguous |
202 | * quotes would open and then close a segment, producing no |
203 | * output (but potentially creating a zero-length argument); |
204 | * but three quotes would open and close a segment and then |
205 | * produce a literal quote. |
206 | */ |
207 | |
208 | /* |
5a71c4ea |
209 | * First deal with the simplest of all special cases: if there |
210 | * aren't any arguments, return 0,NULL,NULL. |
211 | */ |
212 | while (*cmdline && isspace(*cmdline)) cmdline++; |
213 | if (!*cmdline) { |
214 | if (argc) *argc = 0; |
215 | if (argv) *argv = NULL; |
216 | if (argstart) *argstart = NULL; |
217 | return; |
218 | } |
219 | |
220 | /* |
c0a81592 |
221 | * This will guaranteeably be big enough; we can realloc it |
222 | * down later. |
223 | */ |
3d88e64d |
224 | outputline = snewn(1+strlen(cmdline), char); |
225 | outputargv = snewn(strlen(cmdline)+1 / 2, char *); |
226 | outputargstart = snewn(strlen(cmdline)+1 / 2, char *); |
c0a81592 |
227 | |
228 | p = cmdline; q = outputline; outputargc = 0; |
229 | |
230 | while (*p) { |
231 | int quote; |
232 | |
233 | /* Skip whitespace searching for start of argument. */ |
234 | while (*p && isspace(*p)) p++; |
235 | if (!*p) break; |
236 | |
237 | /* We have an argument; start it. */ |
d3a1a808 |
238 | outputargv[outputargc] = q; |
239 | outputargstart[outputargc] = p; |
240 | outputargc++; |
c0a81592 |
241 | quote = 0; |
242 | |
243 | /* Copy data into the argument until it's finished. */ |
244 | while (*p) { |
245 | if (!quote && isspace(*p)) |
246 | break; /* argument is finished */ |
247 | |
248 | if (*p == '"' || *p == '\\') { |
249 | /* |
250 | * We have a sequence of zero or more backslashes |
251 | * followed by a sequence of zero or more quotes. |
252 | * Count up how many of each, and then deal with |
253 | * them as appropriate. |
254 | */ |
255 | int i, slashes = 0, quotes = 0; |
256 | while (*p == '\\') slashes++, p++; |
257 | while (*p == '"') quotes++, p++; |
258 | |
259 | if (!quotes) { |
260 | /* |
261 | * Special case: if there are no quotes, |
262 | * slashes are not special at all, so just copy |
263 | * n slashes to the output string. |
264 | */ |
265 | while (slashes--) *q++ = '\\'; |
266 | } else { |
267 | /* Slashes annihilate in pairs. */ |
268 | while (slashes >= 2) slashes -= 2, *q++ = '\\'; |
269 | |
270 | /* One remaining slash takes out the first quote. */ |
271 | if (slashes) quotes--, *q++ = '"'; |
272 | |
273 | if (quotes > 0) { |
274 | /* Outside a quote segment, a quote starts one. */ |
275 | if (!quote) quotes--, quote = 1; |
276 | |
277 | /* Now we produce (n+1)/3 literal quotes... */ |
278 | for (i = 3; i <= quotes+1; i += 3) *q++ = '"'; |
279 | |
280 | /* ... and end in a quote segment iff 3 divides n. */ |
281 | quote = (quotes % 3 == 0); |
282 | } |
283 | } |
284 | } else { |
285 | *q++ = *p++; |
286 | } |
287 | } |
288 | |
289 | /* At the end of an argument, just append a trailing NUL. */ |
290 | *q++ = '\0'; |
291 | } |
292 | |
3d88e64d |
293 | outputargv = sresize(outputargv, outputargc, char *); |
294 | outputargstart = sresize(outputargstart, outputargc, char *); |
c0a81592 |
295 | |
296 | if (argc) *argc = outputargc; |
d3a1a808 |
297 | if (argv) *argv = outputargv; else sfree(outputargv); |
298 | if (argstart) *argstart = outputargstart; else sfree(outputargstart); |
c0a81592 |
299 | } |
300 | |
301 | #ifdef TESTMODE |
302 | |
303 | const struct argv_test { |
304 | const char *cmdline; |
305 | const char *argv[10]; |
306 | } argv_tests[] = { |
307 | /* |
308 | * We generate this set of tests by invoking ourself with |
309 | * `-generate'. |
310 | */ |
311 | {"ab c\" d", {"ab", "c d", NULL}}, |
312 | {"a\"b c\" d", {"ab c", "d", NULL}}, |
313 | {"a\"\"b c\" d", {"ab", "c d", NULL}}, |
314 | {"a\"\"\"b c\" d", {"a\"b", "c d", NULL}}, |
315 | {"a\"\"\"\"b c\" d", {"a\"b c", "d", NULL}}, |
316 | {"a\"\"\"\"\"b c\" d", {"a\"b", "c d", NULL}}, |
317 | {"a\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
318 | {"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}}, |
319 | {"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
320 | {"a\\b c\" d", {"a\\b", "c d", NULL}}, |
321 | {"a\\\"b c\" d", {"a\"b", "c d", NULL}}, |
322 | {"a\\\"\"b c\" d", {"a\"b c", "d", NULL}}, |
323 | {"a\\\"\"\"b c\" d", {"a\"b", "c d", NULL}}, |
324 | {"a\\\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
325 | {"a\\\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}}, |
326 | {"a\\\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
327 | {"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}}, |
328 | {"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL}}, |
329 | {"a\\\\b c\" d", {"a\\\\b", "c d", NULL}}, |
330 | {"a\\\\\"b c\" d", {"a\\b c", "d", NULL}}, |
331 | {"a\\\\\"\"b c\" d", {"a\\b", "c d", NULL}}, |
332 | {"a\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
333 | {"a\\\\\"\"\"\"b c\" d", {"a\\\"b c", "d", NULL}}, |
334 | {"a\\\\\"\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
335 | {"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
336 | {"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}}, |
337 | {"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
338 | {"a\\\\\\b c\" d", {"a\\\\\\b", "c d", NULL}}, |
339 | {"a\\\\\\\"b c\" d", {"a\\\"b", "c d", NULL}}, |
340 | {"a\\\\\\\"\"b c\" d", {"a\\\"b c", "d", NULL}}, |
341 | {"a\\\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
342 | {"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
343 | {"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}}, |
344 | {"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
345 | {"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}}, |
346 | {"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL}}, |
347 | {"a\\\\\\\\b c\" d", {"a\\\\\\\\b", "c d", NULL}}, |
348 | {"a\\\\\\\\\"b c\" d", {"a\\\\b c", "d", NULL}}, |
349 | {"a\\\\\\\\\"\"b c\" d", {"a\\\\b", "c d", NULL}}, |
350 | {"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}}, |
351 | {"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL}}, |
352 | {"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}}, |
353 | {"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}}, |
354 | {"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL}}, |
355 | {"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}}, |
356 | {"\"ab c\" d", {"ab c", "d", NULL}}, |
357 | {"\"a\"b c\" d", {"ab", "c d", NULL}}, |
358 | {"\"a\"\"b c\" d", {"a\"b", "c d", NULL}}, |
359 | {"\"a\"\"\"b c\" d", {"a\"b c", "d", NULL}}, |
360 | {"\"a\"\"\"\"b c\" d", {"a\"b", "c d", NULL}}, |
361 | {"\"a\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
362 | {"\"a\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}}, |
363 | {"\"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
364 | {"\"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}}, |
365 | {"\"a\\b c\" d", {"a\\b c", "d", NULL}}, |
366 | {"\"a\\\"b c\" d", {"a\"b c", "d", NULL}}, |
367 | {"\"a\\\"\"b c\" d", {"a\"b", "c d", NULL}}, |
368 | {"\"a\\\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
369 | {"\"a\\\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}}, |
370 | {"\"a\\\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}}, |
371 | {"\"a\\\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}}, |
372 | {"\"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL}}, |
373 | {"\"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}}, |
374 | {"\"a\\\\b c\" d", {"a\\\\b c", "d", NULL}}, |
375 | {"\"a\\\\\"b c\" d", {"a\\b", "c d", NULL}}, |
376 | {"\"a\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
377 | {"\"a\\\\\"\"\"b c\" d", {"a\\\"b c", "d", NULL}}, |
378 | {"\"a\\\\\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
379 | {"\"a\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
380 | {"\"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}}, |
381 | {"\"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
382 | {"\"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}}, |
383 | {"\"a\\\\\\b c\" d", {"a\\\\\\b c", "d", NULL}}, |
384 | {"\"a\\\\\\\"b c\" d", {"a\\\"b c", "d", NULL}}, |
385 | {"\"a\\\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL}}, |
386 | {"\"a\\\\\\\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
387 | {"\"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}}, |
388 | {"\"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}}, |
389 | {"\"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}}, |
390 | {"\"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL}}, |
391 | {"\"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}}, |
392 | {"\"a\\\\\\\\b c\" d", {"a\\\\\\\\b c", "d", NULL}}, |
393 | {"\"a\\\\\\\\\"b c\" d", {"a\\\\b", "c d", NULL}}, |
394 | {"\"a\\\\\\\\\"\"b c\" d", {"a\\\\\"b", "c d", NULL}}, |
395 | {"\"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL}}, |
396 | {"\"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}}, |
397 | {"\"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}}, |
398 | {"\"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL}}, |
399 | {"\"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}}, |
400 | {"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL}}, |
401 | }; |
402 | |
403 | int main(int argc, char **argv) |
404 | { |
405 | int i, j; |
406 | |
407 | if (argc > 1) { |
408 | /* |
409 | * Generation of tests. |
410 | * |
411 | * Given `-splat <args>', we print out a C-style |
412 | * representation of each argument (in the form "a", "b", |
413 | * NULL), backslash-escaping each backslash and double |
414 | * quote. |
415 | * |
416 | * Given `-split <string>', we first doctor `string' by |
417 | * turning forward slashes into backslashes, single quotes |
418 | * into double quotes and underscores into spaces; and then |
419 | * we feed the resulting string to ourself with `-splat'. |
420 | * |
421 | * Given `-generate', we concoct a variety of fun test |
422 | * cases, encode them in quote-safe form (mapping \, " and |
423 | * space to /, ' and _ respectively) and feed each one to |
424 | * `-split'. |
425 | */ |
426 | if (!strcmp(argv[1], "-splat")) { |
427 | int i; |
428 | char *p; |
429 | for (i = 2; i < argc; i++) { |
430 | putchar('"'); |
431 | for (p = argv[i]; *p; p++) { |
432 | if (*p == '\\' || *p == '"') |
433 | putchar('\\'); |
434 | putchar(*p); |
435 | } |
436 | printf("\", "); |
437 | } |
438 | printf("NULL"); |
439 | return 0; |
440 | } |
441 | |
442 | if (!strcmp(argv[1], "-split") && argc > 2) { |
443 | char *str = malloc(20 + strlen(argv[0]) + strlen(argv[2])); |
444 | char *p, *q; |
445 | |
446 | q = str + sprintf(str, "%s -splat ", argv[0]); |
447 | printf(" {\""); |
448 | for (p = argv[2]; *p; p++, q++) { |
449 | switch (*p) { |
450 | case '/': printf("\\\\"); *q = '\\'; break; |
451 | case '\'': printf("\\\""); *q = '"'; break; |
452 | case '_': printf(" "); *q = ' '; break; |
453 | default: putchar(*p); *q = *p; break; |
454 | } |
455 | } |
456 | *p = '\0'; |
457 | printf("\", {"); |
458 | fflush(stdout); |
459 | |
460 | system(str); |
461 | |
462 | printf("}},\n"); |
463 | |
464 | return 0; |
465 | } |
466 | |
467 | if (!strcmp(argv[1], "-generate")) { |
468 | char *teststr, *p; |
469 | int i, initialquote, backslashes, quotes; |
470 | |
471 | teststr = malloc(200 + strlen(argv[0])); |
472 | |
473 | for (initialquote = 0; initialquote <= 1; initialquote++) { |
474 | for (backslashes = 0; backslashes < 5; backslashes++) { |
475 | for (quotes = 0; quotes < 9; quotes++) { |
476 | p = teststr + sprintf(teststr, "%s -split ", argv[0]); |
477 | if (initialquote) *p++ = '\''; |
478 | *p++ = 'a'; |
479 | for (i = 0; i < backslashes; i++) *p++ = '/'; |
480 | for (i = 0; i < quotes; i++) *p++ = '\''; |
481 | *p++ = 'b'; |
482 | *p++ = '_'; |
483 | *p++ = 'c'; |
484 | *p++ = '\''; |
485 | *p++ = '_'; |
486 | *p++ = 'd'; |
487 | *p = '\0'; |
488 | |
489 | system(teststr); |
490 | } |
491 | } |
492 | } |
493 | return 0; |
494 | } |
495 | |
496 | fprintf(stderr, "unrecognised option: \"%s\"\n", argv[1]); |
497 | return 1; |
498 | } |
499 | |
500 | /* |
501 | * If we get here, we were invoked with no arguments, so just |
502 | * run the tests. |
503 | */ |
504 | |
505 | for (i = 0; i < lenof(argv_tests); i++) { |
506 | int ac; |
507 | char **av; |
508 | |
509 | split_into_argv(argv_tests[i].cmdline, &ac, &av); |
510 | |
511 | for (j = 0; j < ac && argv_tests[i].argv[j]; j++) { |
512 | if (strcmp(av[j], argv_tests[i].argv[j])) { |
513 | printf("failed test %d (|%s|) arg %d: |%s| should be |%s|\n", |
514 | i, argv_tests[i].cmdline, |
515 | j, av[j], argv_tests[i].argv[j]); |
516 | } |
517 | #ifdef VERBOSE |
518 | else { |
519 | printf("test %d (|%s|) arg %d: |%s| == |%s|\n", |
520 | i, argv_tests[i].cmdline, |
521 | j, av[j], argv_tests[i].argv[j]); |
522 | } |
523 | #endif |
524 | } |
525 | if (j < ac) |
526 | printf("failed test %d (|%s|): %d args returned, should be %d\n", |
527 | i, argv_tests[i].cmdline, ac, j); |
528 | if (argv_tests[i].argv[j]) |
529 | printf("failed test %d (|%s|): %d args returned, should be more\n", |
530 | i, argv_tests[i].cmdline, ac); |
531 | } |
532 | |
533 | return 0; |
534 | } |
535 | |
5a71c4ea |
536 | #endif |