Various cosmetic changes.
[wine] / dlls / shell32 / shlexec.c
1 /*
2  *                              Shell Library Functions
3  *
4  *  1998 Marcus Meissner
5  *  2002 Eric Pouech
6  */
7
8 #include "config.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <assert.h>
15
16 #include "windef.h"
17 #include "winerror.h"
18 #include "winreg.h"
19 #include "shellapi.h"
20 #include "shlobj.h"
21 #include "shlwapi.h"
22 #include "ddeml.h"
23
24 #include "wine/winbase16.h"
25 #include "shell32_main.h"
26
27 #include "debugtools.h"
28
29 DEFAULT_DEBUG_CHANNEL(exec);
30
31 /* this function is supposed to expand the escape sequences found in the registry
32  * some diving reported that the following were used:
33  * + %1, %2...  seem to report to parameter of index N in ShellExecute pmts 
34  + + %*         seem to report to all parameter (or all remaining, ie after removing
35  *              the already used %1 %2...) 
36  * + %L         seems to be %1 as long filename followed by the 8+3 variation 
37  * + %l         unknown
38  * + %S         unknown
39  * + %I         unknown
40  */
41 static void argify(char* res, int len, const char* fmt, const char* lpFile)
42 {
43     char        xlpFile[1024];
44
45     while (*fmt)
46     {
47         if (*fmt == '%')
48         {
49             switch (*++fmt)
50             {
51             case '\0':
52             case '%': 
53                 *res++ = '%'; 
54                 break;
55             case '1': 
56                 if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
57                 {
58                     strcpy(res, xlpFile); 
59                     res += strlen(xlpFile); 
60                 }
61                 else
62                 {
63                     strcpy(res, lpFile); 
64                     res += strlen(lpFile); 
65                 }
66                 break;
67             default: FIXME("Unknown escape sequence %%%c\n", *fmt);
68             }
69             fmt++;
70         }
71         else
72             *res++ = *fmt++;
73     }
74     *res = '\0';
75 }
76
77 /*************************************************************************
78  *      SHELL_FindExecutable [Internal]
79  *
80  * Utility for code sharing between FindExecutable and ShellExecute
81  * in: 
82  *      lpFile the name of a file
83  *      lpOperation the operation on it (open)
84  * out:
85  *      lpResult a buffer, big enough :-(, to store the command to do the
86  *              operation on the file
87  *      key a buffer, big enough, to get the key name to do actually the
88  *              command (it'll be used afterwards for more information
89  *              on the operation)
90  */
91 static HINSTANCE SHELL_FindExecutable(LPCSTR lpFile, LPCSTR lpOperation,
92                                       LPSTR lpResult, LPSTR key)
93 {
94     char *extension = NULL; /* pointer to file extension */
95     char tmpext[5];         /* local copy to mung as we please */
96     char filetype[256];     /* registry name for this filetype */
97     LONG filetypelen = 256; /* length of above */
98     char command[256];      /* command from registry */
99     LONG commandlen = 256;  /* This is the most DOS can handle :) */
100     char buffer[256];       /* Used to GetProfileString */
101     HINSTANCE retval = 31;  /* default - 'No association was found' */
102     char *tok;              /* token pointer */
103     int i;                  /* random counter */
104     char xlpFile[256] = ""; /* result of SearchPath */
105     
106     TRACE("%s\n", (lpFile != NULL) ? lpFile : "-");
107     
108     lpResult[0] = '\0'; /* Start off with an empty return string */
109     
110     /* trap NULL parameters on entry */
111     if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
112     {
113         WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
114              lpFile, lpOperation, lpResult);
115         return 2; /* File not found. Close enough, I guess. */
116     }
117     
118     if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
119     {
120         TRACE("SearchPathA returned non-zero\n");
121         lpFile = xlpFile;
122     }
123     
124     /* First thing we need is the file's extension */
125     extension = strrchr(xlpFile, '.'); /* Assume last "." is the one; */
126                                        /* File->Run in progman uses */
127                                        /* .\FILE.EXE :( */
128     TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
129     
130     if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
131     {
132         WARN("Returning 31 - No association\n");
133         return 31; /* no association */
134     }
135     
136     /* Make local copy & lowercase it for reg & 'programs=' lookup */
137     lstrcpynA(tmpext, extension, 5);
138     CharLowerA(tmpext);
139     TRACE("%s file\n", tmpext);
140     
141     /* Three places to check: */
142     /* 1. win.ini, [windows], programs (NB no leading '.') */
143     /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
144     /* 3. win.ini, [extensions], extension (NB no leading '.' */
145     /* All I know of the order is that registry is checked before */
146     /* extensions; however, it'd make sense to check the programs */
147     /* section first, so that's what happens here. */
148     
149     if (key) *key = '\0';
150
151     /* See if it's a program - if GetProfileString fails, we skip this
152      * section. Actually, if GetProfileString fails, we've probably
153      * got a lot more to worry about than running a program... */
154     if (GetProfileStringA("windows", "programs", "exe pif bat com",
155                           buffer, sizeof(buffer)) > 0)
156     {
157         for (i = 0;i<strlen(buffer); i++) buffer[i] = tolower(buffer[i]);
158         
159         tok = strtok(buffer, " \t"); /* ? */
160         while (tok!= NULL)
161         {
162             if (strcmp(tok, &tmpext[1]) == 0) /* have to skip the leading "." */
163             {
164                 strcpy(lpResult, xlpFile);
165                 /* Need to perhaps check that the file has a path
166                  * attached */
167                 TRACE("found %s\n", lpResult);
168                 return 33;
169                 
170                 /* Greater than 32 to indicate success FIXME According to the
171                  * docs, I should be returning a handle for the
172                  * executable. Does this mean I'm supposed to open the
173                  * executable file or something? More RTFM, I guess... */
174             }
175             tok = strtok(NULL, " \t");
176         }
177     }
178     
179     /* Check registry */
180     if (RegQueryValueA(HKEY_CLASSES_ROOT, tmpext, filetype,
181                        &filetypelen) == ERROR_SUCCESS)
182     {
183         filetype[filetypelen] = '\0';
184         TRACE("File type: %s\n", filetype);
185         
186         /* Looking for ...buffer\shell\lpOperation\command */
187         strcat(filetype, "\\shell\\");
188         strcat(filetype, lpOperation);
189         strcat(filetype, "\\command");
190         
191         if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, command,
192                            &commandlen) == ERROR_SUCCESS)
193         {
194             if (key) strcpy(key, filetype);
195 #if 0
196             LPSTR tmp;
197             char param[256];
198             LONG paramlen = 256;
199
200             /* FIXME: it seems all Windows version don't behave the same here.
201              * the doc states that this ddeexec information can be found after
202              * the exec names.
203              * on Win98, it doesn't appear, but I think it does on Win2k
204              */
205             /* Get the parameters needed by the application 
206                from the associated ddeexec key */ 
207             tmp = strstr(filetype, "command");
208             tmp[0] = '\0';
209             strcat(filetype, "ddeexec");
210             
211             if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, param, &paramlen) == ERROR_SUCCESS)
212             {
213                 strcat(command, " ");
214                 strcat(command, param);
215                 commandlen += paramlen;
216             }
217 #endif
218             command[commandlen] = '\0';
219             argify(lpResult, sizeof(lpResult), command, xlpFile);
220             retval = 33; /* FIXME see above */
221         }
222     }
223     else /* Check win.ini */
224     {
225         /* Toss the leading dot */
226         extension++;
227         if (GetProfileStringA("extensions", extension, "", command,
228                               sizeof(command)) > 0)
229         {
230             if (strlen(command) != 0)
231             {
232                 strcpy(lpResult, command);
233                 tok = strstr(lpResult, "^"); /* should be ^.extension? */
234                 if (tok != NULL)
235                 {
236                     tok[0] = '\0';
237                     strcat(lpResult, xlpFile); /* what if no dir in xlpFile? */
238                     tok = strstr(command, "^"); /* see above */
239                     if ((tok != NULL) && (strlen(tok)>5))
240                     {
241                         strcat(lpResult, &tok[5]);
242                     }
243                 }
244                 retval = 33; /* FIXME - see above */
245             }
246         }
247     }
248     
249     TRACE("returning %s\n", lpResult);
250     return retval;
251 }
252
253 /******************************************************************
254  *              dde_cb
255  *
256  * callback for the DDE connection. not really usefull
257  */
258 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv, 
259                                 HSZ hsz1, HSZ hsz2,
260                                 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
261 {
262     return (HDDEDATA)0;
263 }
264
265 /******************************************************************
266  *              dde_connect
267  *
268  * ShellExecute helper. Used to do an operation with a DDE connection
269  *
270  * Handles both the direct connection (try #1), and if it fails, 
271  * launching an application and trying (#2) to connect to it
272  *
273  */
274 static unsigned dde_connect(char* key, char* start, char* ddeexec, 
275                             const char* lpFile, 
276                             int iCmdShow, BOOL is32)
277 {
278     char*       endkey = key + strlen(key);
279     char        app[256], topic[256], ifexec[256], res[256];
280     LONG        applen, topiclen, ifexeclen;
281     char*       exec;
282     DWORD       ddeInst = 0;
283     DWORD       tid;
284     HSZ         hszApp, hszTopic;
285     HCONV       hConv;
286     unsigned    ret = 31;
287
288     strcpy(endkey, "\\application");
289     applen = sizeof(app);
290     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
291     {
292         FIXME("default app name NIY %s\n", key);
293         return 2;
294     }
295     
296     strcpy(endkey, "\\topic");
297     topiclen = sizeof(topic);
298     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
299     {
300         strcpy(topic, "System");
301     }
302     
303     if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
304     {
305         return 2;
306     }
307     
308     hszApp = DdeCreateStringHandleA(ddeInst, app, CP_WINANSI);
309     hszTopic = DdeCreateStringHandleA(ddeInst, topic, CP_WINANSI);
310     
311     hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
312     exec = ddeexec;
313     if (!hConv)
314     {
315         TRACE("Launching '%s'\n", start);
316         ret = (is32) ? WinExec(start, iCmdShow) : WinExec16(start, iCmdShow);
317         if (ret < 32)
318         {
319             TRACE("Couldn't launch\n");
320             goto error;
321         }
322         hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
323         if (!hConv)
324         {
325             ret = 30; /* whatever */
326             goto error;
327         }
328         strcpy(endkey, "\\ifexec");
329         ifexeclen = sizeof(ifexec);
330         if (RegQueryValueA(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
331         {
332             exec = ifexec;
333         }
334     }
335     
336     argify(res, sizeof(res), exec, lpFile);
337     TRACE("%s %s => %s\n", exec, lpFile, res);
338     
339     ret = (DdeClientTransaction(res, strlen(res) + 1, hConv, 0L, 0, 
340                                 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 32;
341     DdeDisconnect(hConv);
342  error:
343     DdeUninitialize(ddeInst);
344     return ret;
345 }
346
347 static HINSTANCE execute_from_key(LPSTR key, LPCSTR lpFile, INT iShowCmd, BOOL is32)
348 {
349     char cmd[1024] = "";
350     LONG cmdlen = sizeof(cmd);
351     HINSTANCE retval = 31;
352
353     /* Get the application for the registry */
354     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
355     {
356         LPSTR tmp;
357         char param[256] = "";
358         LONG paramlen = 256;
359                 
360         /* Get the parameters needed by the application 
361            from the associated ddeexec key */ 
362         tmp = strstr(key, "command");
363         assert(tmp);
364         strcpy(tmp, "ddeexec");
365                 
366         if (RegQueryValueA(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
367         {
368             TRACE("Got ddeexec %s => %s\n", key, param);
369             retval = dde_connect(key, cmd, param, lpFile, iShowCmd, is32);
370         }
371         else
372         {
373             /* Is there a replace() function anywhere? */
374             cmd[cmdlen] = '\0';
375             argify(param, sizeof(param), cmd, lpFile);
376
377             retval = (is32) ? WinExec(param, iShowCmd) : WinExec16(param, iShowCmd);
378         }
379     }
380     else TRACE("ooch\n");
381
382     return retval;
383 }
384
385 static HINSTANCE SHELL_Execute(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile, 
386                                LPCSTR lpParameters, LPCSTR lpDirectory, 
387                                INT iShowCmd, BOOL is32)
388 {
389     HINSTANCE retval = 31;
390     char old_dir[1024];
391     char cmd[1024];
392     
393     TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
394           hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
395           lpParameters ? lpParameters : "<null>", 
396           lpDirectory ? lpDirectory : "<null>", iShowCmd);
397     
398     if (lpFile == NULL) return 0; /* should not happen */
399     if (lpOperation == NULL) /* default is open */
400         lpOperation = "open";
401     
402     if (lpDirectory)
403     {
404         GetCurrentDirectoryA(sizeof(old_dir), old_dir);
405         SetCurrentDirectoryA(lpDirectory);
406     }
407     
408     /* First try to execute lpFile with lpParameters directly */ 
409     strcpy(cmd, lpFile);
410     if (lpParameters) 
411     {
412         strcat(cmd, " ");
413         strcat(cmd, lpParameters);
414     }
415     
416     retval = (is32) ? WinExec(cmd, iShowCmd) : WinExec16(cmd, iShowCmd);
417     
418     /* Unable to execute lpFile directly
419        Check if we can match an application to lpFile */
420     if (retval < 32)
421     { 
422         char lpstrProtocol[256];
423
424         cmd[0] = '\0';
425         retval = SHELL_FindExecutable(lpFile, lpOperation, cmd, lpstrProtocol);
426         
427         if (retval > 32)  /* Found */
428         {
429             TRACE("%s/%s => %s/%s\n", lpFile, lpOperation, cmd, lpstrProtocol);
430             if (*lpstrProtocol)
431                 retval = execute_from_key(lpstrProtocol, lpFile, iShowCmd, is32);
432             else
433                 retval = (is32) ? WinExec(cmd, iShowCmd) : WinExec16(cmd, iShowCmd);
434         }
435         else if (PathIsURLA((LPSTR)lpFile))    /* File not found, check for URL */
436         {
437             LPSTR lpstrRes;
438             INT iSize;
439             
440             lpstrRes = strchr(lpFile, ':');
441             iSize = lpstrRes - lpFile;
442             
443             TRACE("Got URL: %s\n", lpFile);
444             /* Looking for ...protocol\shell\lpOperation\command */
445             strncpy(lpstrProtocol, lpFile, iSize);
446             lpstrProtocol[iSize] = '\0';
447             strcat(lpstrProtocol, "\\shell\\");
448             strcat(lpstrProtocol, lpOperation);
449             strcat(lpstrProtocol, "\\command");
450             
451             /* Remove File Protocol from lpFile */
452             /* In the case file://path/file     */
453             if (!strncasecmp(lpFile, "file", iSize))
454             {
455                 lpFile += iSize;
456                 while (*lpFile == ':') lpFile++;
457             }
458             
459             retval = execute_from_key(lpstrProtocol, lpFile, iShowCmd, is32);
460         }       
461         /* Check if file specified is in the form www.??????.*** */
462         else if (!strncasecmp(lpFile, "www", 3))
463         {
464             /* if so, append lpFile http:// and call ShellExecute */ 
465             char lpstrTmpFile[256] = "http://" ;
466             strcat(lpstrTmpFile, lpFile);
467             retval = ShellExecuteA(hWnd, lpOperation, lpstrTmpFile, NULL, NULL, 0);
468         }
469     }
470     if (lpDirectory)
471         SetCurrentDirectoryA(old_dir);
472     return retval;
473 }
474
475 /*************************************************************************
476  * FindExecutableA                      [SHELL32.@]
477  */
478 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
479
480     HINSTANCE retval = 31;    /* default - 'No association was found' */
481     char old_dir[1024];
482     
483     TRACE("File %s, Dir %s\n", 
484           (lpFile != NULL ? lpFile : "-"), (lpDirectory != NULL ? lpDirectory : "-"));
485
486     lpResult[0] = '\0'; /* Start off with an empty return string */
487
488     /* trap NULL parameters on entry */
489     if ((lpFile == NULL) || (lpResult == NULL))
490     {
491         /* FIXME - should throw a warning, perhaps! */
492         return 2; /* File not found. Close enough, I guess. */
493     }
494
495     if (lpDirectory)
496     {
497         GetCurrentDirectoryA(sizeof(old_dir), old_dir);
498         SetCurrentDirectoryA(lpDirectory);
499     }
500     
501     retval = SHELL_FindExecutable(lpFile, "open", lpResult, NULL);
502     
503     TRACE("returning %s\n", lpResult);
504     if (lpDirectory)
505         SetCurrentDirectoryA(old_dir);
506     return retval;
507 }
508
509 /*************************************************************************
510  * FindExecutableW                      [SHELL32.@]
511  */
512 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
513 {
514     FIXME("(%p,%p,%p): stub\n", lpFile, lpDirectory, lpResult);
515     return 31;    /* default - 'No association was found' */
516 }
517
518 /*************************************************************************
519  *                              ShellExecute            [SHELL.20]
520  */
521 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
522                                    LPCSTR lpFile, LPCSTR lpParameters,
523                                    LPCSTR lpDirectory, INT16 iShowCmd )
524 {   
525     return (HINSTANCE16)SHELL_Execute(hWnd, lpOperation, lpFile, 
526                                       lpParameters, lpDirectory, iShowCmd, FALSE );
527 }
528
529 /*************************************************************************
530  * ShellExecuteA                        [SHELL32.290]
531  */
532 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile, 
533                                LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
534 {
535     TRACE("\n");
536     return SHELL_Execute( hWnd, lpOperation, lpFile, lpParameters,
537                           lpDirectory, iShowCmd, TRUE );
538 }
539
540 /*************************************************************************
541  * ShellExecuteW                        [SHELL32.294]
542  * from shellapi.h
543  * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, 
544  * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);   
545  */
546 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile, 
547                                LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
548 {
549        FIXME(": stub\n");
550        return 0;
551 }
552