summaryrefslogtreecommitdiffstats
path: root/agent/w32main.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2007-06-19 11:11:11 +0200
committerWerner Koch <wk@gnupg.org>2007-06-19 11:11:11 +0200
commit540f9164c01dbbd1f8fc9abcd2ee67dbf6e1ee10 (patch)
tree298058ec8adf7c7c059ce7db127334a9ddb49bec /agent/w32main.c
parentjnlib/ (diff)
downloadgnupg2-540f9164c01dbbd1f8fc9abcd2ee67dbf6e1ee10.tar.xz
gnupg2-540f9164c01dbbd1f8fc9abcd2ee67dbf6e1ee10.zip
Made percent_escape more general.
Added regression tests support to jnlib. W32 changes.
Diffstat (limited to 'agent/w32main.c')
-rw-r--r--agent/w32main.c154
1 files changed, 142 insertions, 12 deletions
diff --git a/agent/w32main.c b/agent/w32main.c
index 5fccb7ea2..176050220 100644
--- a/agent/w32main.c
+++ b/agent/w32main.c
@@ -1,5 +1,6 @@
/* w32main.c - W32 main entry pint and taskbar support for the GnuPG Agent
* Copyright (C) 2007 Free Software Foundation, Inc.
+ * Copyright 1996, 1998 Alexandre Julliard
*
* This file is part of GnuPG.
*
@@ -37,6 +38,136 @@ static HINSTANCE glob_hinst;
static HWND glob_hwnd;
+/* Build an argv array from the command in CMDLINE. RESERVED is the
+ number of args to reserve before the first one. This code is based
+ on Alexandre Julliard's LGPLed wine-0.9.34/dlls/kernel32/process.c
+ and modified to fit into our framework. The function returns NULL
+ on error; on success an arry with the argiments is returned. This
+ array has been allocaqted using a plain malloc (and not the usual
+ xtrymalloc). */
+static char **
+build_argv (char *cmdline_arg, int reserved)
+{
+ int argc;
+ char **argv;
+ char *cmdline, *s, *arg, *d;
+ int in_quotes, bs_count;
+
+ cmdline = malloc (strlen (cmdline_arg) + 1);
+ if (!cmdline)
+ return NULL;
+ strcpy (cmdline, cmdline_arg);
+
+ /* First determine the required size of the array. */
+ argc = reserved + 1;
+ bs_count = 0;
+ in_quotes = 0;
+ s = cmdline;
+ for (;;)
+ {
+ if ( !*s || ((*s==' ' || *s=='\t') && !in_quotes)) /* A space. */
+ {
+ argc++;
+ /* Skip the remaining spaces. */
+ while (*s==' ' || *s=='\t')
+ s++;
+ if (!*s)
+ break;
+ bs_count = 0;
+ }
+ else if (*s=='\\')
+ {
+ bs_count++;
+ s++;
+ }
+ else if ( (*s == '\"') && !(bs_count & 1))
+ {
+ /* Unescaped '\"' */
+ in_quotes = !in_quotes;
+ bs_count=0;
+ s++;
+ }
+ else /* A regular character. */
+ {
+ bs_count = 0;
+ s++;
+ }
+ }
+
+ argv = malloc (argc * sizeof *argv);
+ if (!argv)
+ {
+ free (cmdline);
+ return NULL;
+ }
+
+ /* Now actually parse the command line. */
+ argc = reserved;
+ bs_count = 0;
+ in_quotes=0;
+ arg = d = s = cmdline;
+ while (*s)
+ {
+ if ((*s==' ' || *s=='\t') && !in_quotes)
+ {
+ /* Close the argument and copy it. */
+ *d = 0;
+ argv[argc++] = arg;
+
+ /* Skip the remaining spaces. */
+ do
+ s++;
+ while (*s==' ' || *s=='\t');
+
+ /* Start with a new argument */
+ arg = d = s;
+ bs_count = 0;
+ }
+ else if (*s=='\\')
+ {
+ *d++ = *s++;
+ bs_count++;
+ }
+ else if (*s=='\"')
+ {
+ if ( !(bs_count & 1) )
+ {
+ /* Preceded by an even number of backslashes, this is
+ half that number of backslashes, plus a '\"' which we
+ discard. */
+ d -= bs_count/2;
+ s++;
+ in_quotes = !in_quotes;
+ }
+ else
+ {
+ /* Preceded by an odd number of backslashes, this is
+ half that number of backslashes followed by a '\"'. */
+ d = d - bs_count/2 - 1;
+ *d++ ='\"';
+ s++;
+ }
+ bs_count=0;
+ }
+ else /* A regular character. */
+ {
+ *d++ = *s++;
+ bs_count = 0;
+ }
+ }
+
+ if (*arg)
+ {
+ *d = 0;
+ argv[argc++] = arg;
+ }
+ argv[argc] = NULL;
+
+ return argv;
+}
+
+
+
/* Our window message processing function. */
static LRESULT CALLBACK
wndw_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
@@ -155,24 +286,23 @@ w32_setup_taskbar (void)
/* The main entry point for the Windows version. We save away all GUI
- related stuff, parse the commandline and finally call the real
+ related stuff, parse the command line and finally call the real
main. */
int WINAPI
WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
{
- /* Fixme: We need a parser for the command line. Should be
- available in some CRT code - need to see whether we can find a
- GNU version. For nopw we call gpg-agent with a couple of fixed arguments
- */
- char *argv[] = { "gpg-agent.exe", "--daemon", "-v", "--debug-all", NULL };
+ char **argv;
+ int argc;
+ /* We use the GetCommandLine function because that also includes the
+ program name in contrast to the CMDLINE arg. */
+ argv = build_argv (GetCommandLineA (), 0);
+ if (!argv)
+ return 2; /* Can't do much about a malloc failure. */
+ for (argc=0; argv[argc]; argc++)
+ ;
glob_hinst = hinst;
- return w32_main (DIM(argv)-1, argv);
+ return w32_main (argc, argv);
}
-
-
-
-
-