main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /*****************************************************************************
  163. * DDE helper functions.
  164. */
  165. static WCHAR *ddeString = NULL;
  166. static HSZ hszTopic = 0, hszReturn = 0;
  167. static DWORD ddeInst = 0;
  168. /* Dde callback, save the execute or request string for processing */
  169. static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
  170. HSZ hsz1, HSZ hsz2, HDDEDATA hData,
  171. ULONG_PTR dwData1, ULONG_PTR dwData2)
  172. {
  173. DWORD size = 0, ret = 0;
  174. WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
  175. uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
  176. switch (uType)
  177. {
  178. case XTYP_CONNECT:
  179. if (!DdeCmpStringHandles(hsz1, hszTopic))
  180. return (HDDEDATA)TRUE;
  181. return (HDDEDATA)FALSE;
  182. case XTYP_EXECUTE:
  183. if (!(size = DdeGetData(hData, NULL, 0, 0)))
  184. WINE_ERR("DdeGetData returned zero size of execute string\n");
  185. else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
  186. WINE_ERR("Out of memory\n");
  187. else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
  188. WINE_WARN("DdeGetData did not return %d bytes\n", size);
  189. DdeFreeDataHandle(hData);
  190. return (HDDEDATA)DDE_FACK;
  191. case XTYP_REQUEST:
  192. ret = -3; /* error */
  193. if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
  194. WINE_ERR("DdeQueryString returned zero size of request string\n");
  195. else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))))
  196. WINE_ERR("Out of memory\n");
  197. else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
  198. WINE_WARN("DdeQueryString did not return %d characters\n", size);
  199. else
  200. ret = -2; /* acknowledgment */
  201. return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
  202. hszReturn, CF_TEXT, 0);
  203. default:
  204. return NULL;
  205. }
  206. }
  207. static WCHAR *get_url_from_dde(void)
  208. {
  209. static const WCHAR szApplication[] = {'I','E','x','p','l','o','r','e',0};
  210. static const WCHAR szTopic[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
  211. static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
  212. HSZ hszApplication = 0;
  213. UINT_PTR timer = 0;
  214. int rc;
  215. WCHAR *ret = NULL;
  216. rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
  217. if (rc != DMLERR_NO_ERROR)
  218. {
  219. WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
  220. goto done;
  221. }
  222. hszApplication = DdeCreateStringHandleW(ddeInst, szApplication, CP_WINUNICODE);
  223. if (!hszApplication)
  224. {
  225. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  226. goto done;
  227. }
  228. hszTopic = DdeCreateStringHandleW(ddeInst, szTopic, CP_WINUNICODE);
  229. if (!hszTopic)
  230. {
  231. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  232. goto done;
  233. }
  234. hszReturn = DdeCreateStringHandleW(ddeInst, szReturn, CP_WINUNICODE);
  235. if (!hszReturn)
  236. {
  237. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  238. goto done;
  239. }
  240. if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
  241. {
  242. WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
  243. goto done;
  244. }
  245. timer = SetTimer(NULL, 0, 5000, NULL);
  246. if (!timer)
  247. {
  248. WINE_ERR("SetTimer failed to create timer\n");
  249. goto done;
  250. }
  251. while (!ddeString)
  252. {
  253. MSG msg;
  254. if (!GetMessageW(&msg, NULL, 0, 0)) break;
  255. if (msg.message == WM_TIMER) break;
  256. DispatchMessageW(&msg);
  257. }
  258. if (ddeString)
  259. {
  260. if (*ddeString == '"')
  261. {
  262. WCHAR *endquote = strchrW(ddeString + 1, '"');
  263. if (!endquote)
  264. {
  265. WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
  266. goto done;
  267. }
  268. *endquote = 0;
  269. ret = ddeString+1;
  270. }
  271. else
  272. ret = ddeString;
  273. }
  274. done:
  275. if (timer) KillTimer(NULL, timer);
  276. if (ddeInst)
  277. {
  278. if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
  279. if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
  280. if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
  281. if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
  282. DdeUninitialize(ddeInst);
  283. }
  284. return ret;
  285. }
  286. static WCHAR *encode_unix_path(const char *src)
  287. {
  288. const char *tmp_src;
  289. WCHAR *dst, *tmp_dst;
  290. const char safe_chars[] = "/-_.~@&=+$,:";
  291. const char hex_digits[] = "0123456789ABCDEF";
  292. const WCHAR schema[] = {'f','i','l','e',':','/','/',0};
  293. int len = ARRAY_SIZE(schema);
  294. tmp_src = src;
  295. while (*tmp_src != 0)
  296. {
  297. if ((*tmp_src >= 'a' && *tmp_src <= 'z') ||
  298. (*tmp_src >= 'A' && *tmp_src <= 'Z') ||
  299. (*tmp_src >= '0' && *tmp_src <= '9') ||
  300. strchr(safe_chars, *tmp_src))
  301. len += 1;
  302. else
  303. len += 3;
  304. tmp_src++;
  305. }
  306. dst = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
  307. if (!dst)
  308. return NULL;
  309. strcpyW(dst, schema);
  310. tmp_src = src;
  311. tmp_dst = dst + strlenW(dst);
  312. while (*tmp_src != 0)
  313. {
  314. if ((*tmp_src >= 'a' && *tmp_src <= 'z') ||
  315. (*tmp_src >= 'A' && *tmp_src <= 'Z') ||
  316. (*tmp_src >= '0' && *tmp_src <= '9') ||
  317. strchr(safe_chars, *tmp_src))
  318. {
  319. *tmp_dst++ = *tmp_src;
  320. }
  321. else
  322. {
  323. *tmp_dst++ = '%';
  324. *tmp_dst++ = hex_digits[*(unsigned char*)(tmp_src) / 16];
  325. *tmp_dst++ = hex_digits[*tmp_src & 0xf];
  326. }
  327. tmp_src++;
  328. }
  329. *tmp_dst = 0;
  330. return dst;
  331. }
  332. static WCHAR *convert_file_uri(IUri *uri)
  333. {
  334. wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
  335. struct stat dummy;
  336. WCHAR *new_path;
  337. char *unixpath;
  338. BSTR filename;
  339. HRESULT hres;
  340. /* check if the argument is a local file */
  341. wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
  342. GetProcAddress( GetModuleHandleA( "KERNEL32" ), "wine_get_unix_file_name" );
  343. if(!wine_get_unix_file_name_ptr)
  344. return NULL;
  345. hres = IUri_GetPath(uri, &filename);
  346. if(FAILED(hres))
  347. return NULL;
  348. WINE_TRACE("Windows path: %s\n", wine_dbgstr_w(filename));
  349. unixpath = wine_get_unix_file_name_ptr(filename);
  350. SysFreeString(filename);
  351. if(unixpath && stat(unixpath, &dummy) >= 0) {
  352. WINE_TRACE("Unix path: %s\n", wine_dbgstr_a(unixpath));
  353. new_path = encode_unix_path(unixpath);
  354. HeapFree(GetProcessHeap(), 0, unixpath);
  355. }else {
  356. WINE_WARN("File %s does not exist\n", wine_dbgstr_a(unixpath));
  357. HeapFree(GetProcessHeap(), 0, unixpath);
  358. new_path = NULL;
  359. }
  360. WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path));
  361. return new_path;
  362. }
  363. /*****************************************************************************
  364. * Main entry point. This is a console application so we have a wmain() not a
  365. * winmain().
  366. */
  367. int __cdecl wmain(int argc, WCHAR *argv[])
  368. {
  369. static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
  370. WCHAR *url = argv[1];
  371. BSTR display_uri = NULL;
  372. DWORD scheme;
  373. IUri *uri;
  374. HRESULT hres;
  375. int ret = 1;
  376. /* DDE used only if -nohome is specified; avoids delay in printing usage info
  377. * when no parameters are passed */
  378. if (url && !strcmpiW( url, nohomeW ))
  379. url = argc > 2 ? argv[2] : get_url_from_dde();
  380. if (!url) {
  381. WINE_ERR( "Usage: winebrowser URL\n" );
  382. return -1;
  383. }
  384. hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
  385. if(FAILED(hres)) {
  386. WINE_ERR("Failed to parse URL\n");
  387. ret = open_http_url(url);
  388. HeapFree(GetProcessHeap(), 0, ddeString);
  389. return ret;
  390. }
  391. HeapFree(GetProcessHeap(), 0, ddeString);
  392. IUri_GetScheme(uri, &scheme);
  393. if(scheme == URL_SCHEME_FILE) {
  394. display_uri = convert_file_uri(uri);
  395. if(!display_uri) {
  396. WINE_ERR("Failed to convert file URL to unix path\n");
  397. }
  398. }
  399. if (!display_uri)
  400. hres = IUri_GetDisplayUri(uri, &display_uri);
  401. IUri_Release(uri);
  402. if(FAILED(hres))
  403. return -1;
  404. WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
  405. if(scheme == URL_SCHEME_MAILTO)
  406. ret = open_mailto_url(display_uri);
  407. else
  408. /* let the browser decide how to handle the given url */
  409. ret = open_http_url(display_uri);
  410. SysFreeString(display_uri);
  411. return ret;
  412. }