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