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