main.c 15 KB

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