OSDN Git Service

Add PuTTY 0.61 to contrib directory.
[ffftp/ffftp.git] / contrib / putty / WINDOWS / WINUTILS.C
diff --git a/contrib/putty/WINDOWS/WINUTILS.C b/contrib/putty/WINDOWS/WINUTILS.C
new file mode 100644 (file)
index 0000000..2daf1eb
--- /dev/null
@@ -0,0 +1,598 @@
+/*\r
+ * winutils.c: miscellaneous Windows utilities for GUI apps\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <ctype.h>\r
+\r
+#include "putty.h"\r
+#include "misc.h"\r
+\r
+#ifdef TESTMODE\r
+/* Definitions to allow this module to be compiled standalone for testing\r
+ * split_into_argv(). */\r
+#define smalloc malloc\r
+#define srealloc realloc\r
+#define sfree free\r
+#endif\r
+\r
+/*\r
+ * GetOpenFileName/GetSaveFileName tend to muck around with the process'\r
+ * working directory on at least some versions of Windows.\r
+ * Here's a wrapper that gives more control over this, and hides a little\r
+ * bit of other grottiness.\r
+ */\r
+\r
+struct filereq_tag {\r
+    TCHAR cwd[MAX_PATH];\r
+};\r
+\r
+/*\r
+ * `of' is expected to be initialised with most interesting fields, but\r
+ * this function does some administrivia. (assume `of' was memset to 0)\r
+ * save==1 -> GetSaveFileName; save==0 -> GetOpenFileName\r
+ * `state' is optional.\r
+ */\r
+BOOL request_file(filereq *state, OPENFILENAME *of, int preserve, int save)\r
+{\r
+    TCHAR cwd[MAX_PATH]; /* process CWD */\r
+    BOOL ret;\r
+\r
+    /* Get process CWD */\r
+    if (preserve) {\r
+       DWORD r = GetCurrentDirectory(lenof(cwd), cwd);\r
+       if (r == 0 || r >= lenof(cwd))\r
+           /* Didn't work, oh well. Stop trying to be clever. */\r
+           preserve = 0;\r
+    }\r
+\r
+    /* Open the file requester, maybe setting lpstrInitialDir */\r
+    {\r
+#ifdef OPENFILENAME_SIZE_VERSION_400\r
+       of->lStructSize = OPENFILENAME_SIZE_VERSION_400;\r
+#else\r
+       of->lStructSize = sizeof(*of);\r
+#endif\r
+       of->lpstrInitialDir = (state && state->cwd[0]) ? state->cwd : NULL;\r
+       /* Actually put up the requester. */\r
+       ret = save ? GetSaveFileName(of) : GetOpenFileName(of);\r
+    }\r
+\r
+    /* Get CWD left by requester */\r
+    if (state) {\r
+       DWORD r = GetCurrentDirectory(lenof(state->cwd), state->cwd);\r
+       if (r == 0 || r >= lenof(state->cwd))\r
+           /* Didn't work, oh well. */\r
+           state->cwd[0] = '\0';\r
+    }\r
+    \r
+    /* Restore process CWD */\r
+    if (preserve)\r
+       /* If it fails, there's not much we can do. */\r
+       (void) SetCurrentDirectory(cwd);\r
+\r
+    return ret;\r
+}\r
+\r
+filereq *filereq_new(void)\r
+{\r
+    filereq *ret = snew(filereq);\r
+    ret->cwd[0] = '\0';\r
+    return ret;\r
+}\r
+\r
+void filereq_free(filereq *state)\r
+{\r
+    sfree(state);\r
+}\r
+\r
+/*\r
+ * Message box with optional context help.\r
+ */\r
+\r
+/* Callback function to launch context help. */\r
+static VOID CALLBACK message_box_help_callback(LPHELPINFO lpHelpInfo)\r
+{\r
+    char *context = NULL;\r
+#define CHECK_CTX(name) \\r
+    do { \\r
+       if (lpHelpInfo->dwContextId == WINHELP_CTXID_ ## name) \\r
+           context = WINHELP_CTX_ ## name; \\r
+    } while (0)\r
+    CHECK_CTX(errors_hostkey_absent);\r
+    CHECK_CTX(errors_hostkey_changed);\r
+    CHECK_CTX(errors_cantloadkey);\r
+    CHECK_CTX(option_cleanup);\r
+    CHECK_CTX(pgp_fingerprints);\r
+#undef CHECK_CTX\r
+    if (context)\r
+       launch_help(hwnd, context);\r
+}\r
+\r
+int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid)\r
+{\r
+    MSGBOXPARAMS mbox;\r
+    \r
+    /*\r
+     * We use MessageBoxIndirect() because it allows us to specify a\r
+     * callback function for the Help button.\r
+     */\r
+    mbox.cbSize = sizeof(mbox);\r
+    /* Assumes the globals `hinst' and `hwnd' have sensible values. */\r
+    mbox.hInstance = hinst;\r
+    mbox.hwndOwner = hwnd;\r
+    mbox.lpfnMsgBoxCallback = &message_box_help_callback;\r
+    mbox.dwLanguageId = LANG_NEUTRAL;\r
+    mbox.lpszText = text;\r
+    mbox.lpszCaption = caption;\r
+    mbox.dwContextHelpId = helpctxid;\r
+    mbox.dwStyle = style;\r
+    if (helpctxid != 0 && has_help()) mbox.dwStyle |= MB_HELP;\r
+    return MessageBoxIndirect(&mbox);\r
+}\r
+\r
+/*\r
+ * Display the fingerprints of the PGP Master Keys to the user.\r
+ */\r
+void pgp_fingerprints(void)\r
+{\r
+    message_box("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"\r
+               "be used to establish a trust path from this executable to another\n"\r
+               "one. See the manual for more information.\n"\r
+               "(Note: these fingerprints have nothing to do with SSH!)\n"\r
+               "\n"\r
+               "PuTTY Master Key (RSA), 1024-bit:\n"\r
+               "  " PGP_RSA_MASTER_KEY_FP "\n"\r
+               "PuTTY Master Key (DSA), 1024-bit:\n"\r
+               "  " PGP_DSA_MASTER_KEY_FP,\r
+               "PGP fingerprints", MB_ICONINFORMATION | MB_OK,\r
+               HELPCTXID(pgp_fingerprints));\r
+}\r
+\r
+/*\r
+ * Split a complete command line into argc/argv, attempting to do\r
+ * it exactly the same way Windows itself would do it (so that\r
+ * console utilities, which receive argc and argv from Windows,\r
+ * will have their command lines processed in the same way as GUI\r
+ * utilities which get a whole command line and must break it\r
+ * themselves).\r
+ * \r
+ * Does not modify the input command line.\r
+ * \r
+ * The final parameter (argstart) is used to return a second array\r
+ * of char * pointers, the same length as argv, each one pointing\r
+ * at the start of the corresponding element of argv in the\r
+ * original command line. So if you get half way through processing\r
+ * your command line in argc/argv form and then decide you want to\r
+ * treat the rest as a raw string, you can. If you don't want to,\r
+ * `argstart' can be safely left NULL.\r
+ */\r
+void split_into_argv(char *cmdline, int *argc, char ***argv,\r
+                    char ***argstart)\r
+{\r
+    char *p;\r
+    char *outputline, *q;\r
+    char **outputargv, **outputargstart;\r
+    int outputargc;\r
+\r
+    /*\r
+     * At first glance the rules appeared to be:\r
+     *\r
+     *  - Single quotes are not special characters.\r
+     *\r
+     *  - Double quotes are removed, but within them spaces cease\r
+     *    to be special.\r
+     *\r
+     *  - Backslashes are _only_ special when a sequence of them\r
+     *    appear just before a double quote. In this situation,\r
+     *    they are treated like C backslashes: so \" just gives a\r
+     *    literal quote, \\" gives a literal backslash and then\r
+     *    opens or closes a double-quoted segment, \\\" gives a\r
+     *    literal backslash and then a literal quote, \\\\" gives\r
+     *    two literal backslashes and then opens/closes a\r
+     *    double-quoted segment, and so forth. Note that this\r
+     *    behaviour is identical inside and outside double quotes.\r
+     *\r
+     *  - Two successive double quotes become one literal double\r
+     *    quote, but only _inside_ a double-quoted segment.\r
+     *    Outside, they just form an empty double-quoted segment\r
+     *    (which may cause an empty argument word).\r
+     *\r
+     *  - That only leaves the interesting question of what happens\r
+     *    when one or more backslashes precedes two or more double\r
+     *    quotes, starting inside a double-quoted string. And the\r
+     *    answer to that appears somewhat bizarre. Here I tabulate\r
+     *    number of backslashes (across the top) against number of\r
+     *    quotes (down the left), and indicate how many backslashes\r
+     *    are output, how many quotes are output, and whether a\r
+     *    quoted segment is open at the end of the sequence:\r
+     * \r
+     *                      backslashes\r
+     * \r
+     *               0         1      2      3      4\r
+     * \r
+     *         0   0,0,y  |  1,0,y  2,0,y  3,0,y  4,0,y\r
+     *            --------+-----------------------------\r
+     *         1   0,0,n  |  0,1,y  1,0,n  1,1,y  2,0,n\r
+     *    q    2   0,1,n  |  0,1,n  1,1,n  1,1,n  2,1,n\r
+     *    u    3   0,1,y  |  0,2,n  1,1,y  1,2,n  2,1,y\r
+     *    o    4   0,1,n  |  0,2,y  1,1,n  1,2,y  2,1,n\r
+     *    t    5   0,2,n  |  0,2,n  1,2,n  1,2,n  2,2,n\r
+     *    e    6   0,2,y  |  0,3,n  1,2,y  1,3,n  2,2,y\r
+     *    s    7   0,2,n  |  0,3,y  1,2,n  1,3,y  2,2,n\r
+     *         8   0,3,n  |  0,3,n  1,3,n  1,3,n  2,3,n\r
+     *         9   0,3,y  |  0,4,n  1,3,y  1,4,n  2,3,y\r
+     *        10   0,3,n  |  0,4,y  1,3,n  1,4,y  2,3,n\r
+     *        11   0,4,n  |  0,4,n  1,4,n  1,4,n  2,4,n\r
+     * \r
+     * \r
+     *      [Test fragment was of the form "a\\\"""b c" d.]\r
+     * \r
+     * There is very weird mod-3 behaviour going on here in the\r
+     * number of quotes, and it even applies when there aren't any\r
+     * backslashes! How ghastly.\r
+     * \r
+     * With a bit of thought, this extremely odd diagram suddenly\r
+     * coalesced itself into a coherent, if still ghastly, model of\r
+     * how things work:\r
+     * \r
+     *  - As before, backslashes are only special when one or more\r
+     *    of them appear contiguously before at least one double\r
+     *    quote. In this situation the backslashes do exactly what\r
+     *    you'd expect: each one quotes the next thing in front of\r
+     *    it, so you end up with n/2 literal backslashes (if n is\r
+     *    even) or (n-1)/2 literal backslashes and a literal quote\r
+     *    (if n is odd). In the latter case the double quote\r
+     *    character right after the backslashes is used up.\r
+     * \r
+     *  - After that, any remaining double quotes are processed. A\r
+     *    string of contiguous unescaped double quotes has a mod-3\r
+     *    behaviour:\r
+     * \r
+     *     * inside a quoted segment, a quote ends the segment.\r
+     *     * _immediately_ after ending a quoted segment, a quote\r
+     *       simply produces a literal quote.\r
+     *     * otherwise, outside a quoted segment, a quote begins a\r
+     *       quoted segment.\r
+     * \r
+     *    So, for example, if we started inside a quoted segment\r
+     *    then two contiguous quotes would close the segment and\r
+     *    produce a literal quote; three would close the segment,\r
+     *    produce a literal quote, and open a new segment. If we\r
+     *    started outside a quoted segment, then two contiguous\r
+     *    quotes would open and then close a segment, producing no\r
+     *    output (but potentially creating a zero-length argument);\r
+     *    but three quotes would open and close a segment and then\r
+     *    produce a literal quote.\r
+     */\r
+\r
+    /*\r
+     * First deal with the simplest of all special cases: if there\r
+     * aren't any arguments, return 0,NULL,NULL.\r
+     */\r
+    while (*cmdline && isspace(*cmdline)) cmdline++;\r
+    if (!*cmdline) {\r
+       if (argc) *argc = 0;\r
+       if (argv) *argv = NULL;\r
+       if (argstart) *argstart = NULL;\r
+       return;\r
+    }\r
+\r
+    /*\r
+     * This will guaranteeably be big enough; we can realloc it\r
+     * down later.\r
+     */\r
+    outputline = snewn(1+strlen(cmdline), char);\r
+    outputargv = snewn(strlen(cmdline)+1 / 2, char *);\r
+    outputargstart = snewn(strlen(cmdline)+1 / 2, char *);\r
+\r
+    p = cmdline; q = outputline; outputargc = 0;\r
+\r
+    while (*p) {\r
+       int quote;\r
+\r
+       /* Skip whitespace searching for start of argument. */\r
+       while (*p && isspace(*p)) p++;\r
+       if (!*p) break;\r
+\r
+       /* We have an argument; start it. */\r
+       outputargv[outputargc] = q;\r
+       outputargstart[outputargc] = p;\r
+       outputargc++;\r
+       quote = 0;\r
+\r
+       /* Copy data into the argument until it's finished. */\r
+       while (*p) {\r
+           if (!quote && isspace(*p))\r
+               break;                 /* argument is finished */\r
+\r
+           if (*p == '"' || *p == '\\') {\r
+               /*\r
+                * We have a sequence of zero or more backslashes\r
+                * followed by a sequence of zero or more quotes.\r
+                * Count up how many of each, and then deal with\r
+                * them as appropriate.\r
+                */\r
+               int i, slashes = 0, quotes = 0;\r
+               while (*p == '\\') slashes++, p++;\r
+               while (*p == '"') quotes++, p++;\r
+\r
+               if (!quotes) {\r
+                   /*\r
+                    * Special case: if there are no quotes,\r
+                    * slashes are not special at all, so just copy\r
+                    * n slashes to the output string.\r
+                    */\r
+                   while (slashes--) *q++ = '\\';\r
+               } else {\r
+                   /* Slashes annihilate in pairs. */\r
+                   while (slashes >= 2) slashes -= 2, *q++ = '\\';\r
+\r
+                   /* One remaining slash takes out the first quote. */\r
+                   if (slashes) quotes--, *q++ = '"';\r
+\r
+                   if (quotes > 0) {\r
+                       /* Outside a quote segment, a quote starts one. */\r
+                       if (!quote) quotes--, quote = 1;\r
+\r
+                       /* Now we produce (n+1)/3 literal quotes... */\r
+                       for (i = 3; i <= quotes+1; i += 3) *q++ = '"';\r
+\r
+                       /* ... and end in a quote segment iff 3 divides n. */\r
+                       quote = (quotes % 3 == 0);\r
+                   }\r
+               }\r
+           } else {\r
+               *q++ = *p++;\r
+           }\r
+       }\r
+\r
+       /* At the end of an argument, just append a trailing NUL. */\r
+       *q++ = '\0';\r
+    }\r
+\r
+    outputargv = sresize(outputargv, outputargc, char *);\r
+    outputargstart = sresize(outputargstart, outputargc, char *);\r
+\r
+    if (argc) *argc = outputargc;\r
+    if (argv) *argv = outputargv; else sfree(outputargv);\r
+    if (argstart) *argstart = outputargstart; else sfree(outputargstart);\r
+}\r
+\r
+#ifdef TESTMODE\r
+\r
+const struct argv_test {\r
+    const char *cmdline;\r
+    const char *argv[10];\r
+} argv_tests[] = {\r
+    /*\r
+     * We generate this set of tests by invoking ourself with\r
+     * `-generate'.\r
+     */\r
+    {"ab c\" d", {"ab", "c d", NULL}},\r
+    {"a\"b c\" d", {"ab c", "d", NULL}},\r
+    {"a\"\"b c\" d", {"ab", "c d", NULL}},\r
+    {"a\"\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"a\"\"\"\"b c\" d", {"a\"b c", "d", NULL}},\r
+    {"a\"\"\"\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"a\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}},\r
+    {"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"a\\b c\" d", {"a\\b", "c d", NULL}},\r
+    {"a\\\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"a\\\"\"b c\" d", {"a\"b c", "d", NULL}},\r
+    {"a\\\"\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"a\\\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"a\\\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}},\r
+    {"a\\\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}},\r
+    {"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL}},\r
+    {"a\\\\b c\" d", {"a\\\\b", "c d", NULL}},\r
+    {"a\\\\\"b c\" d", {"a\\b c", "d", NULL}},\r
+    {"a\\\\\"\"b c\" d", {"a\\b", "c d", NULL}},\r
+    {"a\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"a\\\\\"\"\"\"b c\" d", {"a\\\"b c", "d", NULL}},\r
+    {"a\\\\\"\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}},\r
+    {"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"a\\\\\\b c\" d", {"a\\\\\\b", "c d", NULL}},\r
+    {"a\\\\\\\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"a\\\\\\\"\"b c\" d", {"a\\\"b c", "d", NULL}},\r
+    {"a\\\\\\\"\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}},\r
+    {"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}},\r
+    {"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL}},\r
+    {"a\\\\\\\\b c\" d", {"a\\\\\\\\b", "c d", NULL}},\r
+    {"a\\\\\\\\\"b c\" d", {"a\\\\b c", "d", NULL}},\r
+    {"a\\\\\\\\\"\"b c\" d", {"a\\\\b", "c d", NULL}},\r
+    {"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}},\r
+    {"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL}},\r
+    {"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}},\r
+    {"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}},\r
+    {"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL}},\r
+    {"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}},\r
+    {"\"ab c\" d", {"ab c", "d", NULL}},\r
+    {"\"a\"b c\" d", {"ab", "c d", NULL}},\r
+    {"\"a\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"\"a\"\"\"b c\" d", {"a\"b c", "d", NULL}},\r
+    {"\"a\"\"\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"\"a\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"\"a\"\"\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}},\r
+    {"\"a\"\"\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"\"a\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}},\r
+    {"\"a\\b c\" d", {"a\\b c", "d", NULL}},\r
+    {"\"a\\\"b c\" d", {"a\"b c", "d", NULL}},\r
+    {"\"a\\\"\"b c\" d", {"a\"b", "c d", NULL}},\r
+    {"\"a\\\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"\"a\\\"\"\"\"b c\" d", {"a\"\"b c", "d", NULL}},\r
+    {"\"a\\\"\"\"\"\"b c\" d", {"a\"\"b", "c d", NULL}},\r
+    {"\"a\\\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}},\r
+    {"\"a\\\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b c", "d", NULL}},\r
+    {"\"a\\\"\"\"\"\"\"\"\"b c\" d", {"a\"\"\"b", "c d", NULL}},\r
+    {"\"a\\\\b c\" d", {"a\\\\b c", "d", NULL}},\r
+    {"\"a\\\\\"b c\" d", {"a\\b", "c d", NULL}},\r
+    {"\"a\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"\"a\\\\\"\"\"b c\" d", {"a\\\"b c", "d", NULL}},\r
+    {"\"a\\\\\"\"\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"\"a\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}},\r
+    {"\"a\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\b c\" d", {"a\\\\\\b c", "d", NULL}},\r
+    {"\"a\\\\\\\"b c\" d", {"a\\\"b c", "d", NULL}},\r
+    {"\"a\\\\\\\"\"b c\" d", {"a\\\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\"\"\"\"b c\" d", {"a\\\"\"b c", "d", NULL}},\r
+    {"\"a\\\\\\\"\"\"\"\"b c\" d", {"a\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b c", "d", NULL}},\r
+    {"\"a\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\"\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\\b c\" d", {"a\\\\\\\\b c", "d", NULL}},\r
+    {"\"a\\\\\\\\\"b c\" d", {"a\\\\b", "c d", NULL}},\r
+    {"\"a\\\\\\\\\"\"b c\" d", {"a\\\\\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"b c\" d", {"a\\\\\"b c", "d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"\"b c\" d", {"a\\\\\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b c", "d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}},\r
+    {"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL}},\r
+};\r
+\r
+int main(int argc, char **argv)\r
+{\r
+    int i, j;\r
+\r
+    if (argc > 1) {\r
+       /*\r
+        * Generation of tests.\r
+        * \r
+        * Given `-splat <args>', we print out a C-style\r
+        * representation of each argument (in the form "a", "b",\r
+        * NULL), backslash-escaping each backslash and double\r
+        * quote.\r
+        * \r
+        * Given `-split <string>', we first doctor `string' by\r
+        * turning forward slashes into backslashes, single quotes\r
+        * into double quotes and underscores into spaces; and then\r
+        * we feed the resulting string to ourself with `-splat'.\r
+        * \r
+        * Given `-generate', we concoct a variety of fun test\r
+        * cases, encode them in quote-safe form (mapping \, " and\r
+        * space to /, ' and _ respectively) and feed each one to\r
+        * `-split'.\r
+        */\r
+       if (!strcmp(argv[1], "-splat")) {\r
+           int i;\r
+           char *p;\r
+           for (i = 2; i < argc; i++) {\r
+               putchar('"');\r
+               for (p = argv[i]; *p; p++) {\r
+                   if (*p == '\\' || *p == '"')\r
+                       putchar('\\');\r
+                   putchar(*p);\r
+               }\r
+               printf("\", ");\r
+           }\r
+           printf("NULL");\r
+           return 0;\r
+       }\r
+\r
+       if (!strcmp(argv[1], "-split") && argc > 2) {\r
+           char *str = malloc(20 + strlen(argv[0]) + strlen(argv[2]));\r
+           char *p, *q;\r
+\r
+           q = str + sprintf(str, "%s -splat ", argv[0]);\r
+           printf("    {\"");\r
+           for (p = argv[2]; *p; p++, q++) {\r
+               switch (*p) {\r
+                 case '/':  printf("\\\\"); *q = '\\'; break;\r
+                 case '\'': printf("\\\""); *q = '"';  break;\r
+                 case '_':  printf(" ");    *q = ' ';  break;\r
+                 default:   putchar(*p);    *q = *p;   break;\r
+               }\r
+           }\r
+           *p = '\0';\r
+           printf("\", {");\r
+           fflush(stdout);\r
+\r
+           system(str);\r
+\r
+           printf("}},\n");\r
+\r
+           return 0;\r
+       }\r
+\r
+       if (!strcmp(argv[1], "-generate")) {\r
+           char *teststr, *p;\r
+           int i, initialquote, backslashes, quotes;\r
+\r
+           teststr = malloc(200 + strlen(argv[0]));\r
+\r
+           for (initialquote = 0; initialquote <= 1; initialquote++) {\r
+               for (backslashes = 0; backslashes < 5; backslashes++) {\r
+                   for (quotes = 0; quotes < 9; quotes++) {\r
+                       p = teststr + sprintf(teststr, "%s -split ", argv[0]);\r
+                       if (initialquote) *p++ = '\'';\r
+                       *p++ = 'a';\r
+                       for (i = 0; i < backslashes; i++) *p++ = '/';\r
+                       for (i = 0; i < quotes; i++) *p++ = '\'';\r
+                       *p++ = 'b';\r
+                       *p++ = '_';\r
+                       *p++ = 'c';\r
+                       *p++ = '\'';\r
+                       *p++ = '_';\r
+                       *p++ = 'd';\r
+                       *p = '\0';\r
+\r
+                       system(teststr);\r
+                   }\r
+               }\r
+           }\r
+           return 0;\r
+       }\r
+\r
+       fprintf(stderr, "unrecognised option: \"%s\"\n", argv[1]);\r
+       return 1;\r
+    }\r
+\r
+    /*\r
+     * If we get here, we were invoked with no arguments, so just\r
+     * run the tests.\r
+     */\r
+\r
+    for (i = 0; i < lenof(argv_tests); i++) {\r
+       int ac;\r
+       char **av;\r
+\r
+       split_into_argv(argv_tests[i].cmdline, &ac, &av);\r
+\r
+       for (j = 0; j < ac && argv_tests[i].argv[j]; j++) {\r
+           if (strcmp(av[j], argv_tests[i].argv[j])) {\r
+               printf("failed test %d (|%s|) arg %d: |%s| should be |%s|\n",\r
+                      i, argv_tests[i].cmdline,\r
+                      j, av[j], argv_tests[i].argv[j]);\r
+           }\r
+#ifdef VERBOSE\r
+           else {\r
+               printf("test %d (|%s|) arg %d: |%s| == |%s|\n",\r
+                      i, argv_tests[i].cmdline,\r
+                      j, av[j], argv_tests[i].argv[j]);\r
+           }\r
+#endif\r
+       }\r
+       if (j < ac)\r
+           printf("failed test %d (|%s|): %d args returned, should be %d\n",\r
+                  i, argv_tests[i].cmdline, ac, j);\r
+       if (argv_tests[i].argv[j])\r
+           printf("failed test %d (|%s|): %d args returned, should be more\n",\r
+                  i, argv_tests[i].cmdline, ac);\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+#endif\r