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