--- main.c	2006-08-21 01:58:23.000000000 -0400
+++ main.c	2006-09-02 13:17:15.000000000 -0400
@@ -48,8 +48,9 @@
 
 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
 
-/* try to launch an app from a comma separated string of app names */
-static int launch_app( char *candidates, const char *argv1 )
+/* try to launch an app from a comma separated string of app names 
+    (deprecated)*/
+static int launch_app_old( char *candidates, const char *argv1 )
 {
     char *app;
     const char *argv_new[3];
@@ -71,10 +72,129 @@
     return 1;
 }
 
+/* try to launch an app from a null separated string of app names */
+static int launch_app( char *candidates, const char *argv1 )
+{
+    char *app, *appname = " ";
+    const char **argv_new;
+    int argcount = 0;
+    int app_char_index = 0, app_arg_index = 0, argv_index = 0, len = 0;
+    BOOL urlinserted = FALSE;
+
+    app = candidates;
+    while(*app)
+    {
+        app_char_index = 0;
+        app_arg_index = 0;
+        argv_index = 0;
+        urlinserted = FALSE;
+        argcount = 0;
+
+        len = strlen(app);
+        appname = (char *)malloc(len+1);
+        strcpy(appname,app);
+        
+        if ( app[0] == '\"' )
+        {
+            for(app_char_index = 1; app_char_index < len; app_char_index++ )
+            {
+                if ( app[app_char_index] == '\"' )
+                {
+                    argcount++;
+                    break;
+                }
+            }
+
+            for(; app_char_index < len; app_char_index++)
+            {
+                if (app[app_char_index] == ' ' )
+                {
+                    argcount++;
+                }
+            }
+        }
+        else
+        {
+            argcount = 1;
+        }
+        argcount += 2; /* Room for the URL and NULL */
+
+        argv_new = (const char **)malloc(++argcount * sizeof(char *));
+
+        if ( app[0] == '\"' )
+        {
+            for(app_char_index = 1; app_char_index < len; app_char_index++ )
+            {
+                if ( app[app_char_index] == '\"' )
+                {
+                    app[app_char_index] = '\0';
+                    break;
+                }
+            }
+            
+            app = app + 1;  /* Get rid of the opening quote */
+            len--;
+            argv_new[argv_index++] = app;
+
+            while( app_char_index < len && app[app_char_index] == ' ' )
+            {
+                /* Skip the space after the quote */
+                ++app_char_index;
+            }
+            app_arg_index = app_char_index;
+
+            for(; app_char_index < len+1; app_char_index++)
+            {
+                if (app[app_char_index] == ' ' ||
+                    (app_char_index >= len &&
+                     strlen(app+app_arg_index) > 0))
+                {
+                    app[app_char_index] = '\0';
+
+                    if ( app[app_char_index-1] == 's' &&
+                            app[app_char_index-2] == '%' )
+                    {
+                        argv_new[argv_index] = argv1;
+                        urlinserted = TRUE;
+                    }
+                    else
+                    {
+                        argv_new[argv_index] = app + app_arg_index;
+                    }
+                    argv_index++;
+                    app_arg_index = app_char_index + 1;
+                }
+            }
+        }
+        else
+        {
+            argv_new[argv_index++] = app;
+        }
+        
+        if (!urlinserted)
+        {
+            argv_new[argv_index++] = argv1;
+        }
+        argv_new[argv_index] = NULL;
+
+        fprintf( stderr, "Considering: %s\n", appname );
+        fprintf( stderr, "argv[1]: %s\n", argv1 );
+
+        spawnvp( _P_OVERLAY, app, argv_new );  /* only returns on error */
+
+        free(argv_new);
+        app += len+1;
+        free(appname);
+    }
+
+    fprintf( stderr, "winebrowser: could not find a suitable app to run\n" );
+    return 1;
+}
+
 static int open_http_url( const char *url )
 {
     static const char *defaultbrowsers =
-        "firefox,konqueror,mozilla,netscape,galeon,opera,dillo";
+        "firefox\0konqueror\0mozilla\0netscape\0galeon\0opera\0dillo\0";
     char browsers[256];
 
     DWORD length, type;
@@ -94,19 +214,30 @@
     if (r != ERROR_SUCCESS)
     {
         /* set value to the default */
-        RegSetValueExA( key, "Browsers", 0, REG_SZ, (LPBYTE)defaultbrowsers,
+        RegSetValueExA( key, "Browsers", 0, REG_MULTI_SZ, (LPBYTE)defaultbrowsers,
                         lstrlen( defaultbrowsers ) + 1 );
         strcpy( browsers, defaultbrowsers );
     }
     RegCloseKey( key );
-
-    return launch_app( browsers, url );
+    
+    if (type == REG_SZ)
+    {
+        return launch_app_old( browsers, url );
+    }
+    else if (type == REG_MULTI_SZ)
+    {
+        return launch_app( browsers, url );
+    }
+    else
+    {
+        return 1;
+    }
 }
 
 static int open_mailto_url( const char *url )
 {
     static const char *defaultmailers =
-        "mozilla-thunderbird,thunderbird,evolution";
+        "mozilla-thunderbird\0thunderbird\0evolution\0kmail";
     char mailers[256];
 
     DWORD length, type;
@@ -126,13 +257,24 @@
     if (r != ERROR_SUCCESS)
     {
         /* set value to the default */
-        RegSetValueExA( key, "Mailers", 0, REG_SZ, (LPBYTE)defaultmailers,
+        RegSetValueExA( key, "Mailers", 0, REG_MULTI_SZ, (LPBYTE)defaultmailers,
                         lstrlen( defaultmailers ) + 1 );
         strcpy( mailers, defaultmailers );
     }
     RegCloseKey( key );
 
-    return launch_app( mailers, url );
+    if (type == REG_SZ)
+    {
+        return launch_app_old( mailers, url );
+    }
+    else if (type == REG_MULTI_SZ)
+    {
+        return launch_app( mailers, url );
+    }
+    else
+    {
+        return 1;
+    }
 }
 
 /*****************************************************************************
