urlmon: Don't create stgmed_obj for binding to object.
[wine] / programs / start / start.c
1 /*
2  * Start a program using ShellExecuteEx, optionally wait for it to finish
3  * Compatible with Microsoft's "c:\windows\command\start.exe"
4  *
5  * Copyright 2003 Dan Kegel
6  * Copyright 2007 Lyutin Anatoly (Etersoft)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <windows.h>
24 #include <winuser.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <shlobj.h>
28
29 #include <wine/unicode.h>
30 #include <wine/debug.h>
31
32 #include "resources.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(start);
35
36 /**
37  Output given message to stdout without formatting.
38 */
39 static void output(const WCHAR *message)
40 {
41         DWORD count;
42         DWORD   res;
43         int    wlen = strlenW(message);
44
45         if (!wlen) return;
46
47         res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL);
48
49         /* If writing to console fails, assume its file
50         i/o so convert to OEM codepage and output                  */
51         if (!res)
52         {
53                 DWORD len;
54                 char  *mesA;
55                 /* Convert to OEM, then output */
56                 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
57                 mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
58                 if (!mesA) return;
59                 WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
60                 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
61                 HeapFree(GetProcessHeap(), 0, mesA);
62         }
63 }
64
65 /**
66  Output given message from string table,
67  followed by ": ",
68  followed by description of given GetLastError() value to stdout,
69  followed by a trailing newline,
70  then terminate.
71 */
72
73 static void fatal_error(const WCHAR *msg, DWORD error_code)
74 {
75     LPVOID lpMsgBuf;
76     int status;
77     static const WCHAR colonsW[] = { ':', ' ', 0 };
78     static const WCHAR newlineW[] = { '\n', 0 };
79
80     output(msg);
81     output(colonsW);
82     status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, (LPWSTR) & lpMsgBuf, 0, NULL);
83     if (!status)
84     {
85         WINE_ERR("FormatMessage failed\n");
86     } else
87     {
88         output(lpMsgBuf);
89         LocalFree((HLOCAL) lpMsgBuf);
90         output(newlineW);
91     }
92     ExitProcess(1);
93 }
94
95 static void fatal_string_error(int which, DWORD error_code)
96 {
97         WCHAR msg[2048];
98
99         if (!LoadStringW(GetModuleHandle(NULL), which,
100                                         msg, sizeof(msg)/sizeof(WCHAR)))
101                 WINE_ERR("LoadString failed, error %d\n", GetLastError());
102
103         fatal_error(msg, error_code);
104 }
105         
106 static void fatal_string(int which)
107 {
108         WCHAR msg[2048];
109
110         if (!LoadStringW(GetModuleHandle(NULL), which,
111                                         msg, sizeof(msg)/sizeof(WCHAR)))
112                 WINE_ERR("LoadString failed, error %d\n", GetLastError());
113
114         output(msg);
115         ExitProcess(1);
116 }
117
118 static void usage(void)
119 {
120         fatal_string(STRING_USAGE);
121 }
122
123 static void license(void)
124 {
125         fatal_string(STRING_LICENSE);
126 }
127
128 static WCHAR *build_args( int argc, WCHAR **argvW )
129 {
130         int i, wlen = 1;
131         WCHAR *ret, *p;
132         static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
133         static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
134
135         for (i = 0; i < argc; i++ )
136         {
137                 wlen += strlenW(argvW[i]) + 1;
138                 if (strchrW(argvW[i], ' '))
139                         wlen += 2;
140         }
141         ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
142         ret[0] = 0;
143
144         for (i = 0, p = ret; i < argc; i++ )
145         {
146                 if (strchrW(argvW[i], ' '))
147                         p += sprintfW(p, FormatQuotesW, argvW[i]);
148                 else
149                         p += sprintfW(p, FormatW, argvW[i]);
150         }
151         return ret;
152 }
153
154 int wmain (int argc, WCHAR *argv[])
155 {
156         SHELLEXECUTEINFOW sei;
157         WCHAR *args = NULL;
158         int i;
159
160         static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
161
162         memset(&sei, 0, sizeof(sei));
163         sei.cbSize = sizeof(sei);
164         sei.lpVerb = openW;
165         sei.nShow = SW_SHOWNORMAL;
166         /* Dunno what these mean, but it looks like winMe's start uses them */
167         sei.fMask = SEE_MASK_FLAG_DDEWAIT|
168                     SEE_MASK_FLAG_NO_UI|
169                     SEE_MASK_NO_CONSOLE;
170
171         /* Canonical Microsoft commandline flag processing:
172          * flags start with /, are case insensitive,
173          * and may be run together in same word.
174          */
175         for (i=1; i<argc; i++) {
176                 int ci;
177
178                 if (argv[i][0] != '/')
179                         break;
180
181                 /* Handle all options in this word */
182                 for (ci=0; argv[i][ci]; ) {
183                         /* Skip slash */
184                         ci++;
185                         switch(argv[i][ci]) {
186                         case 'l':
187                         case 'L':
188                                 license();
189                                 break;  /* notreached */
190                         case 'm':
191                         case 'M':
192                                 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
193                                         sei.nShow = SW_SHOWMAXIMIZED;
194                                 else
195                                         sei.nShow = SW_SHOWMINIMIZED;
196                                 break;
197                         case 'r':
198                         case 'R':
199                                 /* sei.nShow = SW_SHOWNORMAL; */
200                                 break;
201                         case 'w':
202                         case 'W':
203                                 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
204                                 break;
205                         default:
206                                 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
207                                 usage();
208                         }
209                         /* Skip to next slash */
210                         while (argv[i][ci] && (argv[i][ci] != '/'))
211                                 ci++;
212                 }
213         }
214
215         if (i == argc)
216                 usage();
217
218         sei.lpFile = argv[i++];
219
220         args = build_args( argc - i, &argv[i] );
221         sei.lpParameters = args;
222
223         if (!ShellExecuteExW(&sei))
224                 fatal_string_error(STRING_EXECFAIL, GetLastError());
225
226         HeapFree( GetProcessHeap(), 0, args );
227
228         if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
229                 DWORD exitcode;
230                 WaitForSingleObject(sei.hProcess, INFINITE);
231                 GetExitCodeProcess(sei.hProcess, &exitcode);
232                 ExitProcess(exitcode);
233         }
234
235         ExitProcess(0);
236 }