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