mirrored from https://gitlab.winehq.org/wine/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.c
448 lines (384 loc) · 13 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* winebrowser - winelib app to launch native OS browser or mail client.
*
* Copyright (C) 2004 Chris Morgan
* Copyright (C) 2005 Hans Leidekker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* NOTES:
* Winebrowser is a winelib application that will start the appropriate
* native browser or mail client for a wine installation that lacks a
* windows browser/mail client. For example, you will be able to open
* URLs via native mozilla if no browser has yet been installed in wine.
*
* The application to launch is chosen from a default set or, if set,
* taken from a registry key.
*
* The argument may be a regular Windows file name, a file URL, an
* URL or a mailto URL. In the first three cases the argument
* will be fed to a web browser. In the last case the argument is fed
* to a mail client. A mailto URL is composed as follows:
*
* mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
*/
#define WIN32_LEAN_AND_MEAN
#define COBJMACROS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <urlmon.h>
#include <ddeml.h>
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
static char *strdup_unixcp( const WCHAR *str )
{
char *ret;
int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
if ((ret = malloc( len )))
WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
return ret;
}
/* try to launch a unix app from a comma separated string of app names */
static int launch_app( const WCHAR *candidates, const WCHAR *argv1 )
{
char *cmdline;
int i, count;
char **argv_new;
if (!(cmdline = strdup_unixcp( argv1 ))) return 1;
while (*candidates)
{
WCHAR **args = CommandLineToArgvW( candidates, &count );
if (!(argv_new = malloc( (count + 2) * sizeof(*argv_new) ))) break;
for (i = 0; i < count; i++) argv_new[i] = strdup_unixcp( args[i] );
argv_new[count] = cmdline;
argv_new[count + 1] = NULL;
TRACE( "Trying" );
for (i = 0; i <= count; i++) TRACE( " %s", wine_dbgstr_a( argv_new[i] ));
TRACE( "\n" );
if (!__wine_unix_spawnvp( argv_new, FALSE )) ExitProcess(0);
for (i = 0; i < count; i++) free( argv_new[i] );
free( argv_new );
candidates += lstrlenW( candidates ) + 1; /* grab the next app */
}
WINE_ERR( "could not find a suitable app to open %s\n", debugstr_w( argv1 ));
free( cmdline );
return 1;
}
static LSTATUS get_commands( HKEY key, const WCHAR *value, WCHAR *buffer, DWORD size )
{
DWORD type;
LSTATUS res;
size -= sizeof(WCHAR);
if (!(res = RegQueryValueExW( key, value, 0, &type, (LPBYTE)buffer, &size )) && (type == REG_SZ))
{
/* convert to REG_MULTI_SZ type */
WCHAR *p = buffer;
p[lstrlenW(p) + 1] = 0;
while ((p = wcschr( p, ',' ))) *p++ = 0;
}
return res;
}
static int open_http_url( const WCHAR *url )
{
static const WCHAR defaultbrowsers[] =
L"xdg-open\0"
"/usr/bin/open\0"
"firefox\0"
"konqueror\0"
"mozilla\0"
"opera\0"
"dillo\0";
WCHAR browsers[256];
HKEY key;
LONG r;
/* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
{
r = get_commands( key, L"Browsers", browsers, sizeof(browsers) );
RegCloseKey( key );
}
if (r != ERROR_SUCCESS)
memcpy( browsers, defaultbrowsers, sizeof(defaultbrowsers) );
return launch_app( browsers, url );
}
static int open_mailto_url( const WCHAR *url )
{
static const WCHAR defaultmailers[] =
L"/usr/bin/open\0"
"xdg-email\0"
"mozilla-thunderbird\0"
"thunderbird\0"
"evolution\0";
WCHAR mailers[256];
HKEY key;
LONG r;
/* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
{
r = get_commands( key, L"Mailers", mailers, sizeof(mailers) );
RegCloseKey( key );
}
if (r != ERROR_SUCCESS)
memcpy( mailers, defaultmailers, sizeof(defaultmailers) );
return launch_app( mailers, url );
}
static int open_invalid_url( const WCHAR *url )
{
WCHAR *url_prefixed;
int ret;
url_prefixed = malloc( (ARRAY_SIZE(L"http://") + lstrlenW( url )) * sizeof(WCHAR) );
if (!url_prefixed)
{
WINE_ERR("Out of memory\n");
return 1;
}
lstrcpyW( url_prefixed, L"http://" );
lstrcatW( url_prefixed, url );
ret = open_http_url( url_prefixed );
free( url_prefixed );
return ret;
}
/*****************************************************************************
* DDE helper functions.
*/
static WCHAR *ddeString = NULL;
static HSZ hszTopic = 0, hszReturn = 0;
static DWORD ddeInst = 0;
/* Dde callback, save the execute or request string for processing */
static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
HSZ hsz1, HSZ hsz2, HDDEDATA hData,
ULONG_PTR dwData1, ULONG_PTR dwData2)
{
DWORD size = 0, ret = 0;
WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
switch (uType)
{
case XTYP_CONNECT:
if (!DdeCmpStringHandles(hsz1, hszTopic))
return (HDDEDATA)TRUE;
return (HDDEDATA)FALSE;
case XTYP_EXECUTE:
if (!(size = DdeGetData(hData, NULL, 0, 0)))
WINE_ERR("DdeGetData returned zero size of execute string\n");
else if (!(ddeString = malloc(size)))
WINE_ERR("Out of memory\n");
else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
WINE_WARN("DdeGetData did not return %ld bytes\n", size);
DdeFreeDataHandle(hData);
return (HDDEDATA)DDE_FACK;
case XTYP_REQUEST:
ret = -3; /* error */
if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
WINE_ERR("DdeQueryString returned zero size of request string\n");
else if (!(ddeString = malloc( (size + 1) * sizeof(WCHAR))))
WINE_ERR("Out of memory\n");
else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
WINE_WARN("DdeQueryString did not return %ld characters\n", size);
else
ret = -2; /* acknowledgment */
return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
hszReturn, CF_TEXT, 0);
default:
return NULL;
}
}
static WCHAR *get_url_from_dde(void)
{
HSZ hszApplication = 0;
UINT_PTR timer = 0;
int rc;
WCHAR *ret = NULL;
rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
if (rc != DMLERR_NO_ERROR)
{
WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
goto done;
}
hszApplication = DdeCreateStringHandleW(ddeInst, L"IExplore", CP_WINUNICODE);
if (!hszApplication)
{
WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
goto done;
}
hszTopic = DdeCreateStringHandleW(ddeInst, L"WWW_OpenURL", CP_WINUNICODE);
if (!hszTopic)
{
WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
goto done;
}
hszReturn = DdeCreateStringHandleW(ddeInst, L"Return", CP_WINUNICODE);
if (!hszReturn)
{
WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
goto done;
}
if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
{
WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
goto done;
}
timer = SetTimer(NULL, 0, 5000, NULL);
if (!timer)
{
WINE_ERR("SetTimer failed to create timer\n");
goto done;
}
while (!ddeString)
{
MSG msg;
if (!GetMessageW(&msg, NULL, 0, 0)) break;
if (msg.message == WM_TIMER) break;
DispatchMessageW(&msg);
}
if (ddeString)
{
if (*ddeString == '"')
{
WCHAR *endquote = wcschr(ddeString + 1, '"');
if (!endquote)
{
WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
goto done;
}
*endquote = 0;
ret = ddeString+1;
}
else
ret = ddeString;
}
done:
if (timer) KillTimer(NULL, timer);
if (ddeInst)
{
if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
DdeUninitialize(ddeInst);
}
return ret;
}
static WCHAR *encode_unix_path(const char *src)
{
WCHAR *dst, *tmp_dst;
static const char safe_chars[] = "/-_.~@&=+$,:";
static const char hex_digits[] = "0123456789ABCDEF";
dst = malloc( sizeof(L"file://") + 3 * strlen(src) * sizeof(WCHAR) );
lstrcpyW(dst, L"file://");
tmp_dst = dst + lstrlenW(dst);
while (*src != 0)
{
if ((*src >= 'a' && *src <= 'z') ||
(*src >= 'A' && *src <= 'Z') ||
(*src >= '0' && *src <= '9') ||
strchr(safe_chars, *src))
{
*tmp_dst++ = *src;
}
else
{
*tmp_dst++ = '%';
*tmp_dst++ = hex_digits[*(unsigned char*)src / 16];
*tmp_dst++ = hex_digits[*src & 0xf];
}
src++;
}
*tmp_dst = 0;
return dst;
}
static WCHAR *convert_file_uri(IUri *uri)
{
UNICODE_STRING nt_name;
OBJECT_ATTRIBUTES attr;
NTSTATUS status;
ULONG size = 256;
char *buffer;
WCHAR *new_path = NULL;
BSTR filename;
HRESULT hres;
hres = IUri_GetPath(uri, &filename);
if(FAILED(hres))
return NULL;
WINE_TRACE("Windows path: %s\n", wine_dbgstr_w(filename));
if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL )) return NULL;
InitializeObjectAttributes( &attr, &nt_name, 0, 0, NULL );
for (;;)
{
if (!(buffer = malloc( size )))
{
RtlFreeUnicodeString( &nt_name );
return NULL;
}
status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN );
if (status != STATUS_BUFFER_TOO_SMALL) break;
free( buffer );
}
if (!status) new_path = encode_unix_path( buffer );
SysFreeString(filename);
free( buffer );
WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path));
return new_path;
}
/*****************************************************************************
* Main entry point.
*/
int wmain(int argc, WCHAR *argv[])
{
WCHAR *url = argv[1];
BSTR display_uri = NULL;
DWORD scheme;
IUri *uri;
HRESULT hres;
/* DDE used only if -nohome is specified; avoids delay in printing usage info
* when no parameters are passed */
if (url && !wcsicmp( url, L"-nohome" ))
url = argc > 2 ? argv[2] : get_url_from_dde();
if (!url) {
WINE_ERR( "Usage: winebrowser URL\n" );
return -1;
}
hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
if(FAILED(hres)) {
WINE_ERR("Failed to parse URL %s, treating as HTTP\n", wine_dbgstr_w(url));
return open_invalid_url(url);
}
free(ddeString);
IUri_GetScheme(uri, &scheme);
if(scheme == URL_SCHEME_FILE) {
display_uri = convert_file_uri(uri);
if(!display_uri) {
WINE_ERR("Failed to convert file URL to unix path\n");
}
}
if (!display_uri)
hres = IUri_GetDisplayUri(uri, &display_uri);
IUri_Release(uri);
if(FAILED(hres))
return -1;
WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
if(scheme == URL_SCHEME_MAILTO)
return open_mailto_url(display_uri);
else
/* let the browser decide how to handle the given url */
return open_http_url(display_uri);
}