It's a new year.
[u/mdw/putty] / windows / winutils.c
1 /*
2 * winutils.c: miscellaneous Windows utilities for GUI apps
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ctype.h>
8
9 #include "putty.h"
10 #include "misc.h"
11
12 #ifdef TESTMODE
13 /* Definitions to allow this module to be compiled standalone for testing
14 * split_into_argv(). */
15 #define smalloc malloc
16 #define srealloc realloc
17 #define sfree free
18 #endif
19
20 /*
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 {
28 TCHAR cwd[MAX_PATH];
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 {
39 TCHAR cwd[MAX_PATH]; /* process CWD */
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 /*
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 {
97 char *context = NULL;
98 #define CHECK_CTX(name) \
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);
108 #undef CHECK_CTX
109 if (context)
110 launch_help(hwnd, context);
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);
122 /* Assumes the globals `hinst' and `hwnd' have sensible values. */
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;
131 if (helpctxid != 0 && has_help()) mbox.dwStyle |= MB_HELP;
132 return MessageBoxIndirect(&mbox);
133 }
134
135 /*
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 /*
154 * Split a complete command line into argc/argv, attempting to do
155 * it exactly the same way Windows itself would do it (so that
156 * console utilities, which receive argc and argv from Windows,
157 * will have their command lines processed in the same way as GUI
158 * utilities which get a whole command line and must break it
159 * themselves).
160 *
161 * Does not modify the input command line.
162 *
163 * The final parameter (argstart) is used to return a second array
164 * of char * pointers, the same length as argv, each one pointing
165 * at the start of the corresponding element of argv in the
166 * original command line. So if you get half way through processing
167 * your command line in argc/argv form and then decide you want to
168 * treat the rest as a raw string, you can. If you don't want to,
169 * `argstart' can be safely left NULL.
170 */
171 void split_into_argv(char *cmdline, int *argc, char ***argv,
172 char ***argstart)
173 {
174 char *p;
175 char *outputline, *q;
176 char **outputargv, **outputargstart;
177 int outputargc;
178
179 /*
180 * At first glance the rules appeared to be:
181 *
182 * - Single quotes are not special characters.
183 *
184 * - Double quotes are removed, but within them spaces cease
185 * to be special.
186 *
187 * - Backslashes are _only_ special when a sequence of them
188 * appear just before a double quote. In this situation,
189 * they are treated like C backslashes: so \" just gives a
190 * literal quote, \\" gives a literal backslash and then
191 * opens or closes a double-quoted segment, \\\" gives a
192 * literal backslash and then a literal quote, \\\\" gives
193 * two literal backslashes and then opens/closes a
194 * double-quoted segment, and so forth. Note that this
195 * behaviour is identical inside and outside double quotes.
196 *
197 * - Two successive double quotes become one literal double
198 * quote, but only _inside_ a double-quoted segment.
199 * Outside, they just form an empty double-quoted segment
200 * (which may cause an empty argument word).
201 *
202 * - That only leaves the interesting question of what happens
203 * when one or more backslashes precedes two or more double
204 * quotes, starting inside a double-quoted string. And the
205 * answer to that appears somewhat bizarre. Here I tabulate
206 * number of backslashes (across the top) against number of
207 * quotes (down the left), and indicate how many backslashes
208 * are output, how many quotes are output, and whether a
209 * quoted segment is open at the end of the sequence:
210 *
211 * backslashes
212 *
213 * 0 1 2 3 4
214 *
215 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
216 * --------+-----------------------------
217 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
218 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
219 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
220 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
221 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
222 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
223 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
224 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
225 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
226 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
227 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
228 *
229 *
230 * [Test fragment was of the form "a\\\"""b c" d.]
231 *
232 * There is very weird mod-3 behaviour going on here in the
233 * number of quotes, and it even applies when there aren't any
234 * backslashes! How ghastly.
235 *
236 * With a bit of thought, this extremely odd diagram suddenly
237 * coalesced itself into a coherent, if still ghastly, model of
238 * how things work:
239 *
240 * - As before, backslashes are only special when one or more
241 * of them appear contiguously before at least one double
242 * quote. In this situation the backslashes do exactly what
243 * you'd expect: each one quotes the next thing in front of
244 * it, so you end up with n/2 literal backslashes (if n is
245 * even) or (n-1)/2 literal backslashes and a literal quote
246 * (if n is odd). In the latter case the double quote
247 * character right after the backslashes is used up.
248 *
249 * - After that, any remaining double quotes are processed. A
250 * string of contiguous unescaped double quotes has a mod-3
251 * behaviour:
252 *
253 * * inside a quoted segment, a quote ends the segment.
254 * * _immediately_ after ending a quoted segment, a quote
255 * simply produces a literal quote.
256 * * otherwise, outside a quoted segment, a quote begins a
257 * quoted segment.
258 *
259 * So, for example, if we started inside a quoted segment
260 * then two contiguous quotes would close the segment and
261 * produce a literal quote; three would close the segment,
262 * produce a literal quote, and open a new segment. If we
263 * started outside a quoted segment, then two contiguous
264 * quotes would open and then close a segment, producing no
265 * output (but potentially creating a zero-length argument);
266 * but three quotes would open and close a segment and then
267 * produce a literal quote.
268 */
269
270 /*
271 * First deal with the simplest of all special cases: if there
272 * aren't any arguments, return 0,NULL,NULL.
273 */
274 while (*cmdline && isspace(*cmdline)) cmdline++;
275 if (!*cmdline) {
276 if (argc) *argc = 0;
277 if (argv) *argv = NULL;
278 if (argstart) *argstart = NULL;
279 return;
280 }
281
282 /*
283 * This will guaranteeably be big enough; we can realloc it
284 * down later.
285 */
286 outputline = snewn(1+strlen(cmdline), char);
287 outputargv = snewn(strlen(cmdline)+1 / 2, char *);
288 outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
289
290 p = cmdline; q = outputline; outputargc = 0;
291
292 while (*p) {
293 int quote;
294
295 /* Skip whitespace searching for start of argument. */
296 while (*p && isspace(*p)) p++;
297 if (!*p) break;
298
299 /* We have an argument; start it. */
300 outputargv[outputargc] = q;
301 outputargstart[outputargc] = p;
302 outputargc++;
303 quote = 0;
304
305 /* Copy data into the argument until it's finished. */
306 while (*p) {
307 if (!quote && isspace(*p))
308 break; /* argument is finished */
309
310 if (*p == '"' || *p == '\\') {
311 /*
312 * We have a sequence of zero or more backslashes
313 * followed by a sequence of zero or more quotes.
314 * Count up how many of each, and then deal with
315 * them as appropriate.
316 */
317 int i, slashes = 0, quotes = 0;
318 while (*p == '\\') slashes++, p++;
319 while (*p == '"') quotes++, p++;
320
321 if (!quotes) {
322 /*
323 * Special case: if there are no quotes,
324 * slashes are not special at all, so just copy
325 * n slashes to the output string.
326 */
327 while (slashes--) *q++ = '\\';
328 } else {
329 /* Slashes annihilate in pairs. */
330 while (slashes >= 2) slashes -= 2, *q++ = '\\';
331
332 /* One remaining slash takes out the first quote. */
333 if (slashes) quotes--, *q++ = '"';
334
335 if (quotes > 0) {
336 /* Outside a quote segment, a quote starts one. */
337 if (!quote) quotes--, quote = 1;
338
339 /* Now we produce (n+1)/3 literal quotes... */
340 for (i = 3; i <= quotes+1; i += 3) *q++ = '"';
341
342 /* ... and end in a quote segment iff 3 divides n. */
343 quote = (quotes % 3 == 0);
344 }
345 }
346 } else {
347 *q++ = *p++;
348 }
349 }
350
351 /* At the end of an argument, just append a trailing NUL. */
352 *q++ = '\0';
353 }
354
355 outputargv = sresize(outputargv, outputargc, char *);
356 outputargstart = sresize(outputargstart, outputargc, char *);
357
358 if (argc) *argc = outputargc;
359 if (argv) *argv = outputargv; else sfree(outputargv);
360 if (argstart) *argstart = outputargstart; else sfree(outputargstart);
361 }
362
363 #ifdef TESTMODE
364
365 const struct argv_test {
366 const char *cmdline;
367 const char *argv[10];
368 } argv_tests[] = {
369 /*
370 * We generate this set of tests by invoking ourself with
371 * `-generate'.
372 */
373 {"ab c\" d", {"ab", "c d", NULL}},
374 {"a\"b c\" d", {"ab c", "d", NULL}},
375 {"a\"\"b c\" d", {"ab", "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 {"a\\\\\\\"b c\" d", {"a\\\"b", "c d", NULL}},
402 {"a\\\\\\\"\"b c\" d", {"a\\\"b c", "d", NULL}},
403 {"a\\\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL}},
404 {"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b", "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 {"\"ab c\" d", {"ab c", "d", NULL}},
419 {"\"a\"b c\" d", {"ab", "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 {"\"a\\\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL}},
448 {"\"a\\\\\\\"\"\"b c\" d", {"a\\\"\"b", "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 };
464
465 int main(int argc, char **argv)
466 {
467 int i, j;
468
469 if (argc > 1) {
470 /*
471 * Generation of tests.
472 *
473 * Given `-splat <args>', we print out a C-style
474 * representation of each argument (in the form "a", "b",
475 * NULL), backslash-escaping each backslash and double
476 * quote.
477 *
478 * Given `-split <string>', we first doctor `string' by
479 * turning forward slashes into backslashes, single quotes
480 * into double quotes and underscores into spaces; and then
481 * we feed the resulting string to ourself with `-splat'.
482 *
483 * Given `-generate', we concoct a variety of fun test
484 * cases, encode them in quote-safe form (mapping \, " and
485 * space to /, ' and _ respectively) and feed each one to
486 * `-split'.
487 */
488 if (!strcmp(argv[1], "-splat")) {
489 int i;
490 char *p;
491 for (i = 2; i < argc; i++) {
492 putchar('"');
493 for (p = argv[i]; *p; p++) {
494 if (*p == '\\' || *p == '"')
495 putchar('\\');
496 putchar(*p);
497 }
498 printf("\", ");
499 }
500 printf("NULL");
501 return 0;
502 }
503
504 if (!strcmp(argv[1], "-split") && argc > 2) {
505 char *str = malloc(20 + strlen(argv[0]) + strlen(argv[2]));
506 char *p, *q;
507
508 q = str + sprintf(str, "%s -splat ", argv[0]);
509 printf(" {\"");
510 for (p = argv[2]; *p; p++, q++) {
511 switch (*p) {
512 case '/': printf("\\\\"); *q = '\\'; break;
513 case '\'': printf("\\\""); *q = '"'; break;
514 case '_': printf(" "); *q = ' '; break;
515 default: putchar(*p); *q = *p; break;
516 }
517 }
518 *p = '\0';
519 printf("\", {");
520 fflush(stdout);
521
522 system(str);
523
524 printf("}},\n");
525
526 return 0;
527 }
528
529 if (!strcmp(argv[1], "-generate")) {
530 char *teststr, *p;
531 int i, initialquote, backslashes, quotes;
532
533 teststr = malloc(200 + strlen(argv[0]));
534
535 for (initialquote = 0; initialquote <= 1; initialquote++) {
536 for (backslashes = 0; backslashes < 5; backslashes++) {
537 for (quotes = 0; quotes < 9; quotes++) {
538 p = teststr + sprintf(teststr, "%s -split ", argv[0]);
539 if (initialquote) *p++ = '\'';
540 *p++ = 'a';
541 for (i = 0; i < backslashes; i++) *p++ = '/';
542 for (i = 0; i < quotes; i++) *p++ = '\'';
543 *p++ = 'b';
544 *p++ = '_';
545 *p++ = 'c';
546 *p++ = '\'';
547 *p++ = '_';
548 *p++ = 'd';
549 *p = '\0';
550
551 system(teststr);
552 }
553 }
554 }
555 return 0;
556 }
557
558 fprintf(stderr, "unrecognised option: \"%s\"\n", argv[1]);
559 return 1;
560 }
561
562 /*
563 * If we get here, we were invoked with no arguments, so just
564 * run the tests.
565 */
566
567 for (i = 0; i < lenof(argv_tests); i++) {
568 int ac;
569 char **av;
570
571 split_into_argv(argv_tests[i].cmdline, &ac, &av);
572
573 for (j = 0; j < ac && argv_tests[i].argv[j]; j++) {
574 if (strcmp(av[j], argv_tests[i].argv[j])) {
575 printf("failed test %d (|%s|) arg %d: |%s| should be |%s|\n",
576 i, argv_tests[i].cmdline,
577 j, av[j], argv_tests[i].argv[j]);
578 }
579 #ifdef VERBOSE
580 else {
581 printf("test %d (|%s|) arg %d: |%s| == |%s|\n",
582 i, argv_tests[i].cmdline,
583 j, av[j], argv_tests[i].argv[j]);
584 }
585 #endif
586 }
587 if (j < ac)
588 printf("failed test %d (|%s|): %d args returned, should be %d\n",
589 i, argv_tests[i].cmdline, ac, j);
590 if (argv_tests[i].argv[j])
591 printf("failed test %d (|%s|): %d args returned, should be more\n",
592 i, argv_tests[i].cmdline, ac);
593 }
594
595 return 0;
596 }
597
598 #endif