blob: f06583cc9063541afbd450c25232622b7294f2fa [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
Bram Moolenaar651fca82021-11-29 20:39:38 +000035# if !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010036# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020041# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010042# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar0f873732019-12-05 20:28:46 +010045#endif // PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef __MINGW32__
48# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
49# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
50# endif
51# ifndef RIGHTMOST_BUTTON_PRESSED
52# define RIGHTMOST_BUTTON_PRESSED 0x0002
53# endif
54# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
55# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
56# endif
57# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
58# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
59# endif
60# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
61# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
62# endif
63
64/*
65 * EventFlags
66 */
67# ifndef MOUSE_MOVED
68# define MOUSE_MOVED 0x0001
69# endif
70# ifndef DOUBLE_CLICK
71# define DOUBLE_CLICK 0x0002
72# endif
73#endif
74
75/*
76 * When generating prototypes for Win32 on Unix, these lines make the syntax
77 * errors disappear. They do not need to be correct.
78 */
79#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010080# define WINAPI
81# define WINBASEAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000082typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010088typedef int ENUMLOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +010095typedef int INT_PTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010097typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef int LPARAM;
99typedef int LPBOOL;
100typedef int LPCSTR;
101typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200102typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103typedef int LPSTR;
104typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200105typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106typedef int LPWSTR;
107typedef int LRESULT;
108typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100109typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200111typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112typedef int PSECURITY_DESCRIPTOR;
113typedef int PSID;
114typedef int SECURITY_INFORMATION;
115typedef int SHORT;
116typedef int SMALL_RECT;
117typedef int TEXTMETRIC;
118typedef int UINT;
119typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100120typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121typedef int WORD;
122typedef int WPARAM;
123typedef void VOID;
124#endif
125
Bram Moolenaar0f873732019-12-05 20:28:46 +0100126// Record all output and all keyboard & mouse input
127// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef MCH_WRITE_DUMP
130FILE* fdDump = NULL;
131#endif
132
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200133#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134extern char g_szOrigTitle[];
135#endif
136
137#ifdef FEAT_GUI
138extern HWND s_hwnd;
139#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100140static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#endif
142
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100144int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar0f873732019-12-05 20:28:46 +0100147// Don't generate prototypes here, because some systems do have these
148// functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#if defined(__GNUC__) && !defined(PROTO)
150# ifndef __MINGW32__
151int _stricoll(char *a, char *b)
152{
153 // the ANSI-ish correct way is to use strxfrm():
154 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
155 strxfrm(a_buff, a, 512);
156 strxfrm(b_buff, b, 512);
157 return strcoll(a_buff, b_buff);
158}
159
160char * _fullpath(char *buf, char *fname, int len)
161{
162 LPTSTR toss;
163
164 return (char *)GetFullPathName(fname, len, buf, &toss);
165}
166# endif
167
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100168# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169int _chdrive(int drive)
170{
171 char temp [3] = "-:";
172 temp[0] = drive + 'A' - 1;
173 return !SetCurrentDirectory(temp);
174}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177
178
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200179#ifndef PROTO
180/*
181 * Save the instance handle of the exe/dll.
182 */
183 void
184SaveInst(HINSTANCE hInst)
185{
186 g_hinst = hInst;
187}
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
191/*
192 * GUI version of mch_exit().
193 * Shut down and exit with status `r'
194 * Careful: mch_exit() may be called before mch_init()!
195 */
196 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200197mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100199 exiting = TRUE;
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 display_errors();
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205# ifdef FEAT_OLE
206 UninitOLE();
207# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100208# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (WSInitialized)
210 {
211 WSInitialized = FALSE;
212 WSACleanup();
213 }
214# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100215# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219 if (gui.in_use)
220 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000221
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100222# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000223 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100224# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 exit(r);
227}
228
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231
232/*
233 * Init the tables for toupper() and tolower().
234 */
235 void
236mch_early_init(void)
237{
238 int i;
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar0f873732019-12-05 20:28:46 +0100242 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 for (i = 0; i < 256; ++i)
244 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100245 CharUpperBuff((LPSTR)toupper_tab, 256);
246 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249
250/*
251 * Return TRUE if the input comes from a terminal, FALSE otherwise.
252 */
253 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100254mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200257# ifdef VIMDLL
258 if (gui.in_use)
259# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100260 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200261#endif
262#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (isatty(read_cmd_fd))
264 return TRUE;
265 return FALSE;
266#endif
267}
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200275 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000277#ifdef FEAT_GUI_MSWIN
278# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200279 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000280# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200281 {
282 gui_mch_settitle(title, icon);
283 return;
284 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000285#endif
286#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000288 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200289 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200291 if (wp == NULL)
292 return;
293
294 SetConsoleTitleW(wp);
295 vim_free(wp);
296 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200305 * SAVE_RESTORE_TITLE: Just restore title
306 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
307 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100310mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000312#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
313# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200314 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100315# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000316 SetConsoleTitle(g_szOrigTitle);
317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318}
319
320
321/*
322 * Return TRUE if we can restore the title (we can)
323 */
324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 return TRUE;
328}
329
330
331/*
332 * Return TRUE if we can restore the icon title (we can't)
333 */
334 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return FALSE;
338}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
340
341/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000342 * Get absolute file name into buffer "buf" of length "len" bytes,
343 * turning all '/'s into '\\'s and getting the correct case of each component
344 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * When 'shellslash' set do it the other way around.
346 * Return OK or FAIL.
347 */
348 int
349mch_FullName(
350 char_u *fname,
351 char_u *buf,
352 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100353 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200356 WCHAR *wname;
357 WCHAR wbuf[MAX_PATH];
358 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaareae1b912019-05-09 15:12:55 +0200360 wname = enc_to_utf16(fname, NULL);
361 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200363 cname = utf16_to_enc((short_u *)wbuf, NULL);
364 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000365 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200366 vim_strncpy(buf, cname, len - 1);
367 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200370 vim_free(wname);
371 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef USE_FNAME_CASE
374 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000375#else
376 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378
379 return nResult;
380}
381
382
383/*
384 * Return TRUE if "fname" does not depend on the current directory.
385 */
386 int
387mch_isFullName(char_u *fname)
388{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100389 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
390 // Another way to check is to use mch_FullName() and see if the result is
391 // the same as the name or mch_FullName() fails. However, this has quite a
392 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000393 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000394 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100395 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
396 && (fname[2] == '/' || fname[2] == '\\'))
397 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398}
399
400/*
401 * Replace all slashes by backslashes.
402 * This used to be the other way around, but MS-DOS sometimes has problems
403 * with slashes (e.g. in a command name). We can't have mixed slashes and
404 * backslashes, because comparing file names will not work correctly. The
405 * commands that use a file name should try to avoid the need to type a
406 * backslash twice.
407 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200408 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 */
410 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100411slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200413 if (path_with_url(p))
414 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200415
416 if (*p == '`')
417 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200418 size_t len = STRLEN(p);
419
Bram Moolenaar0f873732019-12-05 20:28:46 +0100420 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200421 if (len > 2 && *(p + len - 1) == '`')
422 return;
423 }
424
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000425 while (*p)
426 {
427 if (*p == psepcN)
428 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100429 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431}
432
K.Takatac351dc12022-01-24 11:24:08 +0000433// Use 64-bit stat functions.
434#undef stat
435#undef _stat
436#undef _wstat
437#undef _fstat
438#define stat _stat64
439#define _stat _stat64
440#define _wstat _wstat64
441#define _fstat _fstat64
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200442
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200443 static int
LemonBoy40fd7e62022-05-05 20:18:16 +0100444read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
445{
446 HANDLE h;
447 BOOL ok;
448
449 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
450 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
451 OPEN_EXISTING,
452 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
453 NULL);
454 if (h == INVALID_HANDLE_VALUE)
455 return FAIL;
456
457 ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
458 buf_len, NULL);
459 CloseHandle(h);
460
461 return ok ? OK : FAIL;
462}
463
464 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200465wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200466{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100467#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100468 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
469 // symlinks properly.
470 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
471 // status of a symlink itself.
472 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
473 // support a symlink to a directory (always returns an error).
474 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
475 // directory, but it doesn't set S_IFDIR flag.
476 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200477 int n;
478 BOOL is_symlink = FALSE;
479 HANDLE hFind, h;
480 DWORD attr = 0;
481 WIN32_FIND_DATAW findDataW;
482
483 hFind = FindFirstFileW(name, &findDataW);
484 if (hFind != INVALID_HANDLE_VALUE)
485 {
486 attr = findDataW.dwFileAttributes;
487 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
488 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
489 is_symlink = TRUE;
490 FindClose(hFind);
491 }
492 if (is_symlink)
493 {
494 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
495 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
496 OPEN_EXISTING,
497 (attr & FILE_ATTRIBUTE_DIRECTORY)
498 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
499 NULL);
500 if (h != INVALID_HANDLE_VALUE)
501 {
502 int fd;
503
K.Takatac351dc12022-01-24 11:24:08 +0000504 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200505 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100506 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
507 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508 _close(fd);
509 return n;
510 }
511 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100512#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200513 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200514}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
LemonBoy40fd7e62022-05-05 20:18:16 +0100516 char_u *
517resolve_appexeclink(char_u *fname)
518{
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100519 DWORD attr = 0;
520 int idx;
521 WCHAR *p, *end, *wname;
LemonBoy40fd7e62022-05-05 20:18:16 +0100522 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
523 // ceiling should be around 16k.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100524 char_u buf[4096];
525 DWORD buf_len = sizeof(buf);
LemonBoy40fd7e62022-05-05 20:18:16 +0100526 REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
527
528 wname = enc_to_utf16(fname, NULL);
529 if (wname == NULL)
530 return NULL;
531
532 attr = GetFileAttributesW(wname);
533 if (attr == INVALID_FILE_ATTRIBUTES ||
534 (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
535 {
536 vim_free(wname);
537 return NULL;
538 }
539
540 // The applinks are similar to symlinks but with a huge difference: they can
541 // only be executed, any other I/O operation on them is bound to fail with
542 // ERROR_FILE_NOT_FOUND even though the file exists.
543 if (read_reparse_point(wname, buf, &buf_len) == FAIL)
544 {
545 vim_free(wname);
546 return NULL;
547 }
548 vim_free(wname);
549
550 if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
551 return NULL;
552
553 // The (undocumented) reparse buffer contains a set of N null-terminated
554 // Unicode strings, the application path is stored in the third one.
555 if (rb->AppExecLinkReparseBuffer.StringCount < 3)
556 return NULL;
557
558 p = rb->AppExecLinkReparseBuffer.StringList;
559 end = p + rb->ReparseDataLength / sizeof(WCHAR);
560 for (idx = 0; p < end
561 && idx < (int)rb->AppExecLinkReparseBuffer.StringCount
562 && idx != 2; )
563 {
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +0100564 if (*p++ == L'\0')
LemonBoy40fd7e62022-05-05 20:18:16 +0100565 ++idx;
566 }
567
568 return utf16_to_enc(p, NULL);
569}
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571/*
572 * stat() can't handle a trailing '/' or '\', remove it first.
573 */
574 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200575vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100577 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
578 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
579 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100580 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100581 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200582 WCHAR *wp;
583 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200585 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100586 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100588 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100589
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
592 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100593
594 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
595 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100596 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100597 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100598 if (p != NULL)
599 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100600 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100601 if (p == NULL)
602 STRCAT(buf, "\\");
603 }
604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200606 wp = enc_to_utf16(buf, NULL);
607 if (wp == NULL)
608 return -1;
609
610 n = wstat_symlink_aware(wp, stp);
611 vim_free(wp);
612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200615#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200617mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100619 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620}
621
622 int
623mch_get_shellsize(void)
624{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100625 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 return OK;
627}
628
629 void
630mch_set_shellsize(void)
631{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100632 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
635/*
636 * Rows and/or Columns has changed.
637 */
638 void
639mch_new_shellsize(void)
640{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100641 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642}
643
644#endif
645
646/*
647 * We have no job control, so fake it by starting a new shell.
648 */
649 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100650mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 suspend_shell();
653}
654
655#if defined(USE_MCH_ERRMSG) || defined(PROTO)
656
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200657# ifdef display_errors
658# undef display_errors
659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661/*
662 * Display the saved error message(s).
663 */
664 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100665display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200667# ifdef FEAT_GUI
Bram Moolenaar819ab822022-06-13 22:34:14 +0100668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200670# ifdef VIMDLL
671 if (gui.in_use || gui.starting)
672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200674 if (error_ga.ga_data != NULL)
675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100676 // avoid putting up a message box with blanks only
Bram Moolenaar819ab822022-06-13 22:34:14 +0100677 for (p = (char_u *)error_ga.ga_data; *p; ++p)
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200678 if (!isspace(*p))
679 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100680 // Only use a dialog when not using --gui-dialog-file:
681 // write text to a file.
682 if (!gui_dialog_log((char_u *)"Errors", p))
683 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000684 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000685 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000686 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000687 (char_u *)_("Error"),
Bram Moolenaar819ab822022-06-13 22:34:14 +0100688 p, (char_u *)_("&Ok"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100689 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200690 break;
691 }
692 ga_clear(&error_ga);
693 }
694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200696# endif
697# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100698 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200699# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100700}
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703
704/*
705 * Return TRUE if "p" contain a wildcard that can be expanded by
706 * dos_expandpath().
707 */
708 int
709mch_has_exp_wildcard(char_u *p)
710{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100711 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 if (vim_strchr((char_u *)"?*[", *p) != NULL
714 || (*p == '~' && p[1] != NUL))
715 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717 return FALSE;
718}
719
720/*
721 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
722 * shortened file name).
723 */
724 int
725mch_has_wildcard(char_u *p)
726{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100727 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
729 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100730#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100732#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 , *p) != NULL
736 || (*p == '~' && p[1] != NUL))
737 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739 return FALSE;
740}
741
742
743/*
744 * The normal _chdir() does not change the default drive. This one does.
745 * Returning 0 implies success; -1 implies failure.
746 */
747 int
748mch_chdir(char *path)
749{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200750 WCHAR *p;
751 int n;
752
Bram Moolenaar0f873732019-12-05 20:28:46 +0100753 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 return -1;
755
Bram Moolenaara2974d72009-07-14 16:38:36 +0000756 if (p_verbose >= 5)
757 {
758 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100759 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000760 verbose_leave();
761 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100762 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100764 // If we can change to the drive, skip that part of the path. If we
765 // can't then the current directory may be invalid, try using chdir()
766 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
768 path += 2;
769 }
770
Bram Moolenaar0f873732019-12-05 20:28:46 +0100771 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 return 0;
773
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200774 p = enc_to_utf16((char_u *)path, NULL);
775 if (p == NULL)
776 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000777
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200778 n = _wchdir(p);
779 vim_free(p);
780 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200784#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785/*
786 * return non-zero if a character is available
787 */
788 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100789mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 return TRUE;
793}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200794
795# if defined(FEAT_TERMINAL) || defined(PROTO)
796/*
797 * Check for any pending input or messages.
798 */
799 int
800mch_check_messages(void)
801{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100802 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200803 return TRUE;
804}
805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#endif
807
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#if defined(FEAT_LIBCALL) || defined(PROTO)
810/*
811 * Call a DLL routine which takes either a string or int param
812 * and returns an allocated string.
813 * Return OK if it worked, FAIL if not.
814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
816typedef LPTSTR (*MYINTPROCSTR)(int);
817typedef int (*MYSTRPROCINT)(LPTSTR);
818typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820/*
821 * Check if a pointer points to a valid NUL terminated string.
822 * Return the length of the string, including terminating NUL.
823 * Returns 0 for an invalid pointer, 1 for an empty string.
824 */
825 static size_t
826check_str_len(char_u *str)
827{
828 SYSTEM_INFO si;
829 MEMORY_BASIC_INFORMATION mbi;
830 size_t length = 0;
831 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100832 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar0f873732019-12-05 20:28:46 +0100834 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 GetSystemInfo(&si);
836
Bram Moolenaar0f873732019-12-05 20:28:46 +0100837 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (VirtualQuery(str, &mbi, sizeof(mbi)))
839 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100840 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000841 long_u dwStr = (long_u)str;
842 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000845 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaar0f873732019-12-05 20:28:46 +0100847 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000848 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar442b4222010-05-24 21:34:22 +0200850 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 p += pageLength, pageLength = si.dwPageSize)
852 for (i = 0; i < pageLength; ++i, ++length)
853 if (p[i] == NUL)
854 return length + 1;
855 }
856
857 return 0;
858}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200860/*
861 * Passed to do_in_runtimepath() to load a vim.ico file.
862 */
863 static void
864mch_icon_load_cb(char_u *fname, void *cookie)
865{
866 HANDLE *h = (HANDLE *)cookie;
867
868 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100869 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200870 IMAGE_ICON,
871 64,
872 64,
873 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
874}
875
876/*
877 * Try loading an icon file from 'runtimepath'.
878 */
879 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100880mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200881{
882 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100883 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200884}
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 int
887mch_libcall(
888 char_u *libname,
889 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100890 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100892 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 int *number_result)
894{
895 HINSTANCE hinstLib;
896 MYSTRPROCSTR ProcAdd;
897 MYINTPROCSTR ProcAddI;
898 char_u *retval_str = NULL;
899 int retval_int = 0;
900 size_t len;
901
902 BOOL fRunTimeLinkSuccess = FALSE;
903
904 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100905 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907 // If the handle is valid, try to get the function address.
908 if (hinstLib != NULL)
909 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100910# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 __try
912 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (argstring != NULL)
915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100916 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100917 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
919 {
920 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100921 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100923 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925 }
926 else
927 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100928 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100929 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
931 {
932 if (string_result == NULL)
933 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
934 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100935 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937 }
938
939 // Save the string before we free the library.
940 // Assume that a "1" result is an illegal pointer.
941 if (string_result == NULL)
942 *number_result = retval_int;
943 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200946 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (*string_result != NULL)
948 mch_memmove(*string_result, retval_str, len);
949 }
950
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100951# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953 __except(EXCEPTION_EXECUTE_HANDLER)
954 {
955 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000956 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 fRunTimeLinkSuccess = 0;
958 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 // Free the DLL module.
962 (void)FreeLibrary(hinstLib);
963 }
964
965 if (!fRunTimeLinkSuccess)
966 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000967 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 return FAIL;
969 }
970
971 return OK;
972}
973#endif
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975/*
976 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
977 */
978 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100979DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100981#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (fdDump)
983 {
984 fputs(psz, fdDump);
985 if (psz[strlen(psz) - 1] != '\n')
986 fputc('\n', fdDump);
987 fflush(fdDump);
988 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991
992#ifdef _DEBUG
993
994void __cdecl
995Trace(
996 char *pszFormat,
997 ...)
998{
999 CHAR szBuff[2048];
1000 va_list args;
1001
1002 va_start(args, pszFormat);
1003 vsprintf(szBuff, pszFormat, args);
1004 va_end(args);
1005
1006 OutputDebugString(szBuff);
1007}
1008
1009#endif //_DEBUG
1010
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001011#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014/*
1015 * Showing the printer dialog is tricky since we have no GUI
1016 * window to parent it. The following routines are needed to
1017 * get the window parenting and Z-order to work properly.
1018 */
1019 static void
1020GetConsoleHwnd(void)
1021{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (s_hwnd != 0)
1024 return;
1025
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (g_hWnd != 0)
1028 {
1029 s_hwnd = g_hWnd;
1030 return;
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001033 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001035
1036/*
1037 * Console implementation of ":winpos".
1038 */
1039 int
1040mch_get_winpos(int *x, int *y)
1041{
1042 RECT rect;
1043
1044 GetConsoleHwnd();
1045 GetWindowRect(s_hwnd, &rect);
1046 *x = rect.left;
1047 *y = rect.top;
1048 return OK;
1049}
1050
1051/*
1052 * Console implementation of ":winpos x y".
1053 */
1054 void
1055mch_set_winpos(int x, int y)
1056{
1057 GetConsoleHwnd();
1058 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1059 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062
1063#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1064
Bram Moolenaar0f873732019-12-05 20:28:46 +01001065//=================================================================
1066// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001069static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1071static TEXTMETRIC prt_tm;
1072static int prt_line_height;
1073static int prt_number_width;
1074static int prt_left_margin;
1075static int prt_right_margin;
1076static int prt_top_margin;
1077static char_u szAppName[] = TEXT("VIM");
1078static HWND hDlgPrint;
1079static int *bUserAbort = NULL;
1080static char_u *prt_name = NULL;
1081
Bram Moolenaar0f873732019-12-05 20:28:46 +01001082// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001083# define IDC_BOX1 400
1084# define IDC_PRINTTEXT1 401
1085# define IDC_PRINTTEXT2 402
1086# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001088 static BOOL
1089vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1090{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001091 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001092 BOOL ret;
1093
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001094 wp = enc_to_utf16(s, NULL);
1095 if (wp == NULL)
1096 return FALSE;
1097
1098 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1099 vim_free(wp);
1100 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001101}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Convert BGR to RGB for Windows GDI calls
1105 */
1106 static COLORREF
1107swap_me(COLORREF colorref)
1108{
1109 int temp;
1110 char *ptr = (char *)&colorref;
1111
1112 temp = *(ptr);
1113 *(ptr ) = *(ptr + 2);
1114 *(ptr + 2) = temp;
1115 return colorref;
1116}
1117
K.Takatac351dc12022-01-24 11:24:08 +00001118 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001119PrintDlgProc(
1120 HWND hDlg,
1121 UINT message,
1122 WPARAM wParam UNUSED,
1123 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001125# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 NONCLIENTMETRICS nm;
1127 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 switch (message)
1131 {
1132 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001133# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 nm.cbSize = sizeof(NONCLIENTMETRICS);
1135 if (SystemParametersInfo(
1136 SPI_GETNONCLIENTMETRICS,
1137 sizeof(NONCLIENTMETRICS),
1138 &nm,
1139 0))
1140 {
1141 char buff[MAX_PATH];
1142 int i;
1143
Bram Moolenaar0f873732019-12-05 20:28:46 +01001144 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 hfont = CreateFontIndirect(&nm.lfMessageFont);
1146 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1147 {
1148 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1149 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001150 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 SendDlgItemMessage(hDlg, IDCANCEL,
1153 WM_SETFONT, (WPARAM)hfont, 1);
1154 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001155 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001157# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001158 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (prt_name != NULL)
1160 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001161 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001162 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001165# if !defined(FEAT_GUI) || defined(VIMDLL)
1166# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001167 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001168# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001169 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 return TRUE;
1172
1173 case WM_COMMAND:
1174 *bUserAbort = TRUE;
1175 EnableWindow(GetParent(hDlg), TRUE);
1176 DestroyWindow(hDlg);
1177 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001178# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 return TRUE;
1182 }
1183 return FALSE;
1184}
1185
1186 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001187AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 MSG msg;
1190
K.Takatab7057bd2022-01-21 11:37:07 +00001191 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
K.Takatab7057bd2022-01-21 11:37:07 +00001193 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001196 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 }
1199 return !*bUserAbort;
1200}
1201
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001202# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001204 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205PrintHookProc(
1206 HWND hDlg, // handle to dialog box
1207 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001208 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 LPARAM lParam // message parameter
1210 )
1211{
1212 HWND hwndOwner;
1213 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001214 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 if (uiMsg == WM_INITDIALOG)
1217 {
1218 // Get the owner window and dialog box rectangles.
1219 if ((hwndOwner = GetParent(hDlg)) == NULL)
1220 hwndOwner = GetDesktopWindow();
1221
1222 GetWindowRect(hwndOwner, &rcOwner);
1223 GetWindowRect(hDlg, &rcDlg);
1224 CopyRect(&rc, &rcOwner);
1225
1226 // Offset the owner and dialog box rectangles so that
1227 // right and bottom values represent the width and
1228 // height, and then offset the owner again to discard
1229 // space taken up by the dialog box.
1230
1231 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1232 OffsetRect(&rc, -rc.left, -rc.top);
1233 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1234
1235 // The new position is the sum of half the remaining
1236 // space and the owner's original position.
1237
1238 SetWindowPos(hDlg,
1239 HWND_TOP,
1240 rcOwner.left + (rc.right / 2),
1241 rcOwner.top + (rc.bottom / 2),
1242 0, 0, // ignores size arguments
1243 SWP_NOSIZE);
1244
Bram Moolenaar0f873732019-12-05 20:28:46 +01001245 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001246 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 pPD->nCopies = (WORD)pPD->lCustData;
1248 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001249 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 BringWindowToTop(GetParent(hDlg));
1251 SetForegroundWindow(hDlg);
1252 }
1253
1254 return FALSE;
1255}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 void
1259mch_print_cleanup(void)
1260{
1261 int pifItalic;
1262 int pifBold;
1263 int pifUnderline;
1264
1265 for (pifBold = 0; pifBold <= 1; pifBold++)
1266 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1267 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1268 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1269
1270 if (prt_dlg.hDC != NULL)
1271 DeleteDC(prt_dlg.hDC);
1272 if (!*bUserAbort)
1273 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1274}
1275
1276 static int
1277to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1278{
1279 int ret = 0;
1280 int u;
1281 int nr;
1282
1283 u = prt_get_unit(idx);
1284 if (u == PRT_UNIT_NONE)
1285 {
1286 u = PRT_UNIT_PERC;
1287 nr = def_number;
1288 }
1289 else
1290 nr = printer_opts[idx].number;
1291
1292 switch (u)
1293 {
1294 case PRT_UNIT_PERC:
1295 ret = (physsize * nr) / 100;
1296 break;
1297 case PRT_UNIT_INCH:
1298 ret = (nr * dpi);
1299 break;
1300 case PRT_UNIT_MM:
1301 ret = (nr * 10 * dpi) / 254;
1302 break;
1303 case PRT_UNIT_POINT:
1304 ret = (nr * 10 * dpi) / 720;
1305 break;
1306 }
1307
1308 if (ret < offset)
1309 return 0;
1310 else
1311 return ret - offset;
1312}
1313
1314 static int
1315prt_get_cpl(void)
1316{
1317 int hr;
1318 int phyw;
1319 int dvoff;
1320 int rev_offset;
1321 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1324 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1325
1326 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1328 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1330
1331 rev_offset = phyw - (dvoff + hr);
1332
1333 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1334 if (prt_use_number())
1335 {
1336 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1337 prt_left_margin += prt_number_width;
1338 }
1339 else
1340 prt_number_width = 0;
1341
1342 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1343 rev_offset, 5);
1344
1345 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1346}
1347
1348 static int
1349prt_get_lpp(void)
1350{
1351 int vr;
1352 int phyw;
1353 int dvoff;
1354 int rev_offset;
1355 int bottom_margin;
1356 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1360 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1362
1363 rev_offset = phyw - (dvoff + vr);
1364
1365 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1366
Bram Moolenaar0f873732019-12-05 20:28:46 +01001367 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 prt_top_margin += prt_line_height * prt_header_height();
1369
1370 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1371 rev_offset, 5);
1372
1373 return (bottom_margin - prt_top_margin) / prt_line_height;
1374}
1375
1376 int
1377mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1378{
1379 static HGLOBAL stored_dm = NULL;
1380 static HGLOBAL stored_devn = NULL;
1381 static int stored_nCopies = 1;
1382 static int stored_nFlags = 0;
1383
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001384 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int pifItalic;
1386 int pifBold;
1387 int pifUnderline;
1388
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001389 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 DEVNAMES *devname;
1391 int i;
1392
1393 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001394 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001395 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001396# if !defined(FEAT_GUI) || defined(VIMDLL)
1397# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001398 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001399# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001400 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 prt_dlg.hwndOwner = s_hwnd;
1403 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1404 if (!forceit)
1405 {
1406 prt_dlg.hDevMode = stored_dm;
1407 prt_dlg.hDevNames = stored_devn;
1408 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001409# if !defined(FEAT_GUI) || defined(VIMDLL)
1410# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001411 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001412# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001413 {
1414 /*
1415 * Use hook to prevent console window being sent to back
1416 */
1417 prt_dlg.lpfnPrintHook = PrintHookProc;
1418 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1419 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 prt_dlg.Flags |= stored_nFlags;
1422 }
1423
1424 /*
1425 * If bang present, return default printer setup with no dialog
1426 * never show dialog if we are running over telnet
1427 */
1428 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001429# if !defined(FEAT_GUI) || defined(VIMDLL)
1430# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001431 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001432# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001434# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 )
1437 {
1438 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 /*
1440 * MSDN suggests setting the first parameter to WINSPOOL for
1441 * NT, but NULL appears to work just as well.
1442 */
1443 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001444 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
1447 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001448 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 goto init_fail_dlg;
1450 }
1451 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001452 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 goto init_fail_dlg;
1454 else
1455 {
1456 /*
1457 * keep the previous driver context
1458 */
1459 stored_dm = prt_dlg.hDevMode;
1460 stored_devn = prt_dlg.hDevNames;
1461 stored_nFlags = prt_dlg.Flags;
1462 stored_nCopies = prt_dlg.nCopies;
1463 }
1464
1465 if (prt_dlg.hDC == NULL)
1466 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001467 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 mch_print_cleanup();
1469 return FALSE;
1470 }
1471
Bram Moolenaar0f873732019-12-05 20:28:46 +01001472 // Not all printer drivers report the support of color (or grey) in the
1473 // same way. Let's set has_color if there appears to be some way to print
1474 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1476 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1477 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1478 || i > 2 || i == -1);
1479
Bram Moolenaar0f873732019-12-05 20:28:46 +01001480 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1482
1483 /*
1484 * On some windows systems the nCopies parameter is not
1485 * passed back correctly. It must be retrieved from the
1486 * hDevMode struct.
1487 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001488 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (mem != NULL)
1490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (mem->dmCopies != 1)
1492 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1494 psettings->duplex = TRUE;
1495 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1496 psettings->has_color = TRUE;
1497 }
1498 GlobalUnlock(prt_dlg.hDevMode);
1499
1500 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1501 if (devname != 0)
1502 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001503 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1504 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001505 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001506 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1507 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001509 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001510 prt_name = alloc(STRLEN(printer_name)
1511 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001513 wsprintf((char *)prt_name, (const char *)text,
1514 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001515 vim_free(printer_name);
1516 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 GlobalUnlock(prt_dlg.hDevNames);
1519
1520 /*
1521 * Initialise the font according to 'printfont'
1522 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001523 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001524 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001526 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 mch_print_cleanup();
1528 return FALSE;
1529 }
1530
1531 for (pifBold = 0; pifBold <= 1; pifBold++)
1532 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1533 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1534 {
1535 fLogFont.lfWeight = boldface[pifBold];
1536 fLogFont.lfItalic = pifItalic;
1537 fLogFont.lfUnderline = pifUnderline;
1538 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001539 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
1541
1542 SetBkMode(prt_dlg.hDC, OPAQUE);
1543 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1544
1545 /*
1546 * Fill in the settings struct
1547 */
1548 psettings->chars_per_line = prt_get_cpl();
1549 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001550 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1551 {
1552 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1553 ? prt_dlg.nCopies : 1;
1554 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1555 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001557 if (psettings->n_collated_copies == 0)
1558 psettings->n_collated_copies = 1;
1559
1560 if (psettings->n_uncollated_copies == 0)
1561 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001562 }
1563 else
1564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
1569 psettings->jobname = jobname;
1570
1571 return TRUE;
1572
1573init_fail_dlg:
1574 {
1575 DWORD err = CommDlgExtendedError();
1576
1577 if (err)
1578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 char_u *buf;
1580
Bram Moolenaar0f873732019-12-05 20:28:46 +01001581 // I suspect FormatMessage() doesn't work for values returned by
1582 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1584 FORMAT_MESSAGE_FROM_SYSTEM |
1585 FORMAT_MESSAGE_IGNORE_INSERTS,
1586 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001587 semsg(_(e_print_error_str),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 buf == NULL ? (char_u *)_("Unknown") : buf);
1589 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001592 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 mch_print_cleanup();
1595 return FALSE;
1596 }
1597}
1598
1599
1600 int
1601mch_print_begin(prt_settings_T *psettings)
1602{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001603 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001605 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001607 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001611 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001613 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001614 if (wp != NULL)
1615 {
1616 DOCINFOW di;
1617
Bram Moolenaara80faa82020-04-12 19:37:17 +02001618 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001619 di.cbSize = sizeof(di);
1620 di.lpszDocName = wp;
1621 ret = StartDocW(prt_dlg.hDC, &di);
1622 vim_free(wp);
1623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001625# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001626 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001627# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001628 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001629# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001630 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 return (ret > 0);
1634}
1635
1636 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001637mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 EndDoc(prt_dlg.hDC);
1640 if (!*bUserAbort)
1641 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1642}
1643
1644 int
1645mch_print_end_page(void)
1646{
1647 return (EndPage(prt_dlg.hDC) > 0);
1648}
1649
1650 int
1651mch_print_begin_page(char_u *msg)
1652{
1653 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001654 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 return (StartPage(prt_dlg.hDC) > 0);
1656}
1657
1658 int
1659mch_print_blank_page(void)
1660{
1661 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1662}
1663
1664static int prt_pos_x = 0;
1665static int prt_pos_y = 0;
1666
1667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001668mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 if (margin)
1671 prt_pos_x = -prt_number_width;
1672 else
1673 prt_pos_x = 0;
1674 prt_pos_y = page_line * prt_line_height
1675 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1676}
1677
1678 int
1679mch_print_text_out(char_u *p, int len)
1680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001682 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001683 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001684 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001686 wp = enc_to_utf16(p, &wlen);
1687 if (wp == NULL)
1688 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001689
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001690 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1691 prt_pos_y + prt_top_margin, wp, wlen);
1692 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1693 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001695 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001696 if (p[len] != NUL)
1697 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001698 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001699 wp = enc_to_utf16(p + len, &wlen);
1700 if (wp != NULL)
1701 {
1702 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1703 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1704 vim_free(wp);
1705 }
1706 }
1707 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708}
1709
1710 void
1711mch_print_set_font(int iBold, int iItalic, int iUnderline)
1712{
1713 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1714}
1715
1716 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001717mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001719 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1720 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 /*
1722 * With a white background we can draw characters transparent, which is
1723 * good for italic characters that overlap to the next char cell.
1724 */
1725 if (bgcol == 0xffffffUL)
1726 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1727 else
1728 SetBkMode(prt_dlg.hDC, OPAQUE);
1729}
1730
1731 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001732mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001734 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1735 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
1737
Bram Moolenaar0f873732019-12-05 20:28:46 +01001738#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar58d98232005-07-23 22:25:46 +00001740
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001743# ifndef PROTO
1744# include <shlobj.h>
1745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001747typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001748 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001749 LPWSTR lpszFilePath,
1750 DWORD cchFilePath,
1751 DWORD dwFlags);
1752static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001753
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001754# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1755
1756 static int
1757is_reparse_point_included(LPCWSTR fname)
1758{
1759 LPCWSTR p = fname, q;
1760 WCHAR buf[MAX_PATH];
1761 DWORD attr;
1762
1763 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1764 p += 3;
1765 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1766 p += 2;
1767
1768 while (*p != L'\0')
1769 {
1770 q = wcspbrk(p, L"\\/");
1771 if (q == NULL)
1772 p = q = fname + wcslen(fname);
1773 else
1774 p = q + 1;
1775 if (q - fname >= MAX_PATH)
1776 return FALSE;
1777 wcsncpy(buf, fname, q - fname);
1778 buf[q - fname] = L'\0';
1779 attr = GetFileAttributesW(buf);
1780 if (attr != INVALID_FILE_ATTRIBUTES
1781 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1782 return TRUE;
1783 }
1784 return FALSE;
1785}
1786
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001787 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001788resolve_reparse_point(char_u *fname)
1789{
1790 HANDLE h = INVALID_HANDLE_VALUE;
1791 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001792 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001793 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001794 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001795 static BOOL loaded = FALSE;
1796
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001797 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001798 {
1799 HMODULE hmod = GetModuleHandle("kernel32.dll");
1800
1801 if (loaded == TRUE)
1802 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001803 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1804 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001805 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001806 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001807 return NULL;
1808 }
1809
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001810 p = enc_to_utf16(fname, NULL);
1811 if (p == NULL)
1812 goto fail;
1813
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001814 if (!is_reparse_point_included(p))
1815 {
1816 vim_free(p);
1817 goto fail;
1818 }
1819
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001820 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1821 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1822 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001823
1824 if (h == INVALID_HANDLE_VALUE)
1825 goto fail;
1826
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001827 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1828 if (size == 0)
1829 goto fail;
1830 buff = ALLOC_MULT(WCHAR, size);
1831 if (buff == NULL)
1832 goto fail;
1833 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001834 goto fail;
1835
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001836 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1837 {
1838 buff[6] = L'\\';
1839 wp = buff + 6;
1840 }
1841 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1842 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001843 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001844 wp = buff;
1845
1846 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001847
1848fail:
1849 if (h != INVALID_HANDLE_VALUE)
1850 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001851 if (buff != NULL)
1852 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001853
1854 return rfname;
1855}
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
1858 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1859 * to and return that name in allocated memory.
1860 * Otherwise NULL is returned.
1861 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001862 static char_u *
1863resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 HRESULT hr;
1866 IShellLink *psl = NULL;
1867 IPersistFile *ppf = NULL;
1868 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 char_u *rfname = NULL;
1870 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001871 IShellLinkW *pslw = NULL;
1872 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar0f873732019-12-05 20:28:46 +01001874 // Check if the file name ends in ".lnk". Avoid calling
1875 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (fname == NULL)
1877 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001878 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1880 return rfname;
1881
1882 CoInitialize(NULL);
1883
1884 // create a link manager object and request its interface
1885 hr = CoCreateInstance(
1886 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001887 &IID_IShellLinkW, (void**)&pslw);
1888 if (hr == S_OK)
1889 {
1890 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001892 if (p != NULL)
1893 {
1894 // Get a pointer to the IPersistFile interface.
1895 hr = pslw->lpVtbl->QueryInterface(
1896 pslw, &IID_IPersistFile, (void**)&ppf);
1897 if (hr != S_OK)
1898 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001900 // "load" the name and resolve the link
1901 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1902 if (hr != S_OK)
1903 goto shortcut_errorw;
1904# if 0 // This makes Vim wait a long time if the target does not exist.
1905 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1906 if (hr != S_OK)
1907 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001910 // Get the path to the link target.
1911 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1912 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1913 if (hr == S_OK && wsz[0] != NUL)
1914 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001916shortcut_errorw:
1917 vim_free(p);
1918 }
1919 }
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 // Release all interface pointers (both belong to the same object)
1922 if (ppf != NULL)
1923 ppf->lpVtbl->Release(ppf);
1924 if (psl != NULL)
1925 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001926 if (pslw != NULL)
1927 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 CoUninitialize();
1930 return rfname;
1931}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001932
1933 char_u *
1934mch_resolve_path(char_u *fname, int reparse_point)
1935{
1936 char_u *path = resolve_shortcut(fname);
1937
1938 if (path == NULL && reparse_point)
1939 path = resolve_reparse_point(fname);
1940 return path;
1941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#endif
1943
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001944#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/*
1946 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1947 */
1948 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001949win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001951 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (s_hwnd != 0)
1953 SetForegroundWindow(s_hwnd);
1954}
1955#endif
1956
1957#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1958/*
1959 * Client-server code for Vim
1960 *
1961 * Originally written by Paul Moore
1962 */
1963
Bram Moolenaar0f873732019-12-05 20:28:46 +01001964// In order to handle inter-process messages, we need to have a window. But
1965// the functions in this module can be called before the main GUI window is
1966// created (and may also be called in the console version, where there is no
1967// GUI window at all).
1968//
1969// So we create a hidden window, and arrange to destroy it on exit.
1970HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001972# define VIM_CLASSNAME "VIM_MESSAGES"
1973# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
K.Takataf9f2a332022-06-17 20:05:40 +01001975// Timeout for sending a message to another Vim instance. Normally this works
1976// instantly, but it may hang when the other Vim instance is halted.
1977# define SENDMESSAGE_TIMEOUT (5 * 1000)
1978
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001979// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001980// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001981# define COPYDATA_KEYS 0
1982# define COPYDATA_REPLY 1
1983# define COPYDATA_EXPR 10
1984# define COPYDATA_RESULT 11
1985# define COPYDATA_ERROR_RESULT 12
1986# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
Bram Moolenaar0f873732019-12-05 20:28:46 +01001988// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989struct server_id
1990{
1991 HWND hwnd;
1992 char_u *name;
1993};
1994
Bram Moolenaar0f873732019-12-05 20:28:46 +01001995// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001996static char_u *client_enc = NULL;
1997
1998/*
1999 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01002000 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002001 */
K.Takataf9f2a332022-06-17 20:05:40 +01002002 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002003serverSendEnc(HWND target)
2004{
2005 COPYDATASTRUCT data;
2006
2007 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002008 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002009 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01002010 if (SendMessageTimeout(target, WM_COPYDATA,
2011 (WPARAM)message_window, (LPARAM)&data,
2012 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
2013 return -1;
2014 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002015}
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
2018 * Clean up on exit. This destroys the hidden message window.
2019 */
2020 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021CleanUpMessaging(void)
2022{
2023 if (message_window != 0)
2024 {
2025 DestroyWindow(message_window);
2026 message_window = 0;
2027 }
2028}
2029
2030static int save_reply(HWND server, char_u *reply, int expr);
2031
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 * The window procedure for the hidden message window.
2034 * It handles callback messages and notifications from servers.
2035 * In order to process these messages, it is necessary to run a
2036 * message loop. Code which may run before the main message loop
2037 * is started (in the GUI) is careful to pump messages when it needs
2038 * to. Features which require message delivery during normal use will
2039 * not work in the console version - this basically means those
2040 * features which allow Vim to act as a server, rather than a client.
2041 */
2042 static LRESULT CALLBACK
2043Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2044{
2045 if (msg == WM_COPYDATA)
2046 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002047 // This is a message from another Vim. The dwData member of the
2048 // COPYDATASTRUCT determines the type of message:
2049 // COPYDATA_ENCODING:
2050 // The encoding that the client uses. Following messages will
2051 // use this encoding, convert if needed.
2052 // COPYDATA_KEYS:
2053 // A key sequence. We are a server, and a client wants these keys
2054 // adding to the input queue.
2055 // COPYDATA_REPLY:
2056 // A reply. We are a client, and a server has sent this message
2057 // in response to a request. (server2client())
2058 // COPYDATA_EXPR:
2059 // An expression. We are a server, and a client wants us to
2060 // evaluate this expression.
2061 // COPYDATA_RESULT:
2062 // A reply. We are a client, and a server has sent this message
2063 // in response to a COPYDATA_EXPR.
2064 // COPYDATA_ERROR_RESULT:
2065 // A reply. We are a client, and a server has sent this message
2066 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2068 HWND sender = (HWND)wParam;
2069 COPYDATASTRUCT reply;
2070 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002072 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002073 char_u *str;
2074 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 switch (data->dwData)
2077 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002078 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002079 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002080 vim_free(client_enc);
2081 client_enc = enc_canonize((char_u *)data->lpData);
2082 return 1;
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002085 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 clientWindow = sender;
2087
Bram Moolenaar0f873732019-12-05 20:28:46 +01002088 // Add the received keys to the input buffer. The loop waiting
2089 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002090 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2091 server_to_input_buf(str);
2092 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002095 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002096# ifdef VIMDLL
2097 if (gui.in_use)
2098# endif
2099 if (s_hwnd != 0)
2100 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101# endif
2102 return 1;
2103
2104 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002105 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 clientWindow = sender;
2107
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002108 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2109 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (res == NULL)
2112 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002113 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002114 size_t len = STRLEN(str) + STRLEN(err) + 5;
2115
Bram Moolenaar964b3742019-05-24 18:54:09 +02002116 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002117 if (res != NULL)
2118 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 reply.dwData = COPYDATA_ERROR_RESULT;
2120 }
2121 else
2122 reply.dwData = COPYDATA_RESULT;
2123 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
K.Takataf9f2a332022-06-17 20:05:40 +01002126 if (serverSendEnc(sender) < 0)
2127 retval = -1;
2128 else
2129 {
2130 if (SendMessageTimeout(sender, WM_COPYDATA,
2131 (WPARAM)message_window, (LPARAM)&reply,
2132 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2133 retval = -1;
2134 else
2135 retval = (int)dwret;
2136 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002137 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 vim_free(res);
2139 return retval;
2140
2141 case COPYDATA_REPLY:
2142 case COPYDATA_RESULT:
2143 case COPYDATA_ERROR_RESULT:
2144 if (data->lpData != NULL)
2145 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002146 str = serverConvert(client_enc, (char_u *)data->lpData,
2147 &tofree);
2148 if (tofree == NULL)
2149 str = vim_strsave(str);
2150 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 (data->dwData == COPYDATA_REPLY ? 0 :
2152 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002153 2))) == FAIL)
2154 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002155 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002157 char_u winstr[30];
2158
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002159 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002160 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 TRUE, curbuf);
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 return 1;
2165 }
2166
2167 return 0;
2168 }
2169
2170 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2171 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002172 // When the message window is activated (brought to the foreground),
2173 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002174# if !defined(FEAT_GUI) || defined(VIMDLL)
2175# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002176 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002177# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002178 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (s_hwnd != 0)
2181 {
2182 SetForegroundWindow(s_hwnd);
2183 return 0;
2184 }
2185 }
2186
2187 return DefWindowProc(hwnd, msg, wParam, lParam);
2188}
2189
2190/*
2191 * Initialise the message handling process. This involves creating a window
2192 * to handle messages - the window will not be visible.
2193 */
2194 void
2195serverInitMessaging(void)
2196{
2197 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaar0f873732019-12-05 20:28:46 +01002199 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 atexit(CleanUpMessaging);
2201
Bram Moolenaar0f873732019-12-05 20:28:46 +01002202 // Register a window class - we only really care
2203 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 wndclass.style = 0;
2205 wndclass.lpfnWndProc = Messaging_WndProc;
2206 wndclass.cbClsExtra = 0;
2207 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002208 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 wndclass.hIcon = NULL;
2210 wndclass.hCursor = NULL;
2211 wndclass.hbrBackground = NULL;
2212 wndclass.lpszMenuName = NULL;
2213 wndclass.lpszClassName = VIM_CLASSNAME;
2214 RegisterClass(&wndclass);
2215
Bram Moolenaar0f873732019-12-05 20:28:46 +01002216 // Create the message window. It will be hidden, so the details don't
2217 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2218 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 message_window = CreateWindow(VIM_CLASSNAME, "",
2220 WS_POPUPWINDOW | WS_CAPTION,
2221 CW_USEDEFAULT, CW_USEDEFAULT,
2222 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002223 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224}
2225
Bram Moolenaar0f873732019-12-05 20:28:46 +01002226// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002227static char_u *altname_buf_ptr = NULL;
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229/*
2230 * Get the title of the window "hwnd", which is the Vim server name, in
2231 * "name[namelen]" and return the length.
2232 * Returns zero if window "hwnd" is not a Vim server.
2233 */
2234 static int
2235getVimServerName(HWND hwnd, char *name, int namelen)
2236{
2237 int len;
2238 char buffer[VIM_CLASSNAME_LEN + 1];
2239
Bram Moolenaar0f873732019-12-05 20:28:46 +01002240 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 len = GetClassName(hwnd, buffer, sizeof(buffer));
2242 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2243 return 0;
2244
Bram Moolenaar0f873732019-12-05 20:28:46 +01002245 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 return GetWindowText(hwnd, name, namelen);
2247}
2248
2249 static BOOL CALLBACK
2250enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2251{
2252 struct server_id *id = (struct server_id *)lparam;
2253 char server[MAX_PATH];
2254
Bram Moolenaar0f873732019-12-05 20:28:46 +01002255 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2257 return TRUE;
2258
Bram Moolenaar0f873732019-12-05 20:28:46 +01002259 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (STRICMP(server, id->name) == 0)
2261 {
2262 id->hwnd = hwnd;
2263 return FALSE;
2264 }
2265
Bram Moolenaar0f873732019-12-05 20:28:46 +01002266 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002267 if (altname_buf_ptr != NULL
2268 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2269 && vim_isdigit(server[STRLEN(id->name)]))
2270 {
2271 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002272 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002273 }
2274
Bram Moolenaar0f873732019-12-05 20:28:46 +01002275 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return TRUE;
2277}
2278
2279 static BOOL CALLBACK
2280enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2281{
2282 garray_T *ga = (garray_T *)lparam;
2283 char server[MAX_PATH];
2284
Bram Moolenaar0f873732019-12-05 20:28:46 +01002285 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2287 return TRUE;
2288
Bram Moolenaar0f873732019-12-05 20:28:46 +01002289 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002290 ga_concat(ga, (char_u *)server);
2291 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return TRUE;
2293}
2294
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002295struct enum_windows_s
2296{
2297 WNDENUMPROC lpEnumFunc;
2298 LPARAM lParam;
2299};
2300
2301 static BOOL CALLBACK
2302enum_windows_child(HWND hwnd, LPARAM lParam)
2303{
2304 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2305
2306 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2307}
2308
2309 static BOOL CALLBACK
2310enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2311{
2312 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2313
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002314 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2315 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002316 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2317}
2318
Bram Moolenaar0f873732019-12-05 20:28:46 +01002319/*
2320 * Enumerate all windows including children.
2321 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002322 static BOOL
2323enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2324{
2325 struct enum_windows_s ew;
2326
2327 ew.lpEnumFunc = lpEnumFunc;
2328 ew.lParam = lParam;
2329 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2330}
2331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 static HWND
2333findServer(char_u *name)
2334{
2335 struct server_id id;
2336
2337 id.name = name;
2338 id.hwnd = 0;
2339
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002340 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342 return id.hwnd;
2343}
2344
2345 void
2346serverSetName(char_u *name)
2347{
2348 char_u *ok_name;
2349 HWND hwnd = 0;
2350 int i = 0;
2351 char_u *p;
2352
Bram Moolenaar0f873732019-12-05 20:28:46 +01002353 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002354 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 STRCPY(ok_name, name);
2357 p = ok_name + STRLEN(name);
2358
2359 for (;;)
2360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002361 // This is inefficient - we're doing an EnumWindows loop for each
2362 // possible name. It would be better to grab all names in one go,
2363 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 hwnd = findServer(ok_name);
2365 if (hwnd == 0)
2366 break;
2367
2368 ++i;
2369 if (i >= 1000)
2370 break;
2371
2372 sprintf((char *)p, "%d", i);
2373 }
2374
2375 if (hwnd != 0)
2376 vim_free(ok_name);
2377 else
2378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002379 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002381 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002384 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002386# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002387 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391}
2392
2393 char_u *
2394serverGetVimNames(void)
2395{
2396 garray_T ga;
2397
2398 ga_init2(&ga, 1, 100);
2399
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002400 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002401 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 return ga.ga_data;
2404}
2405
2406 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002407serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002408 char_u *name, // Where to send.
2409 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 HWND target;
2412 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002413 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002414 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaar0f873732019-12-05 20:28:46 +01002416 // The "name" argument is a magic cookie obtained from expand("<client>").
2417 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2418 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002419 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (n == 0)
2421 return -1;
2422
2423 target = (HWND)n;
2424 if (!IsWindow(target))
2425 return -1;
2426
2427 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002428 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 data.lpData = reply;
2430
K.Takataf9f2a332022-06-17 20:05:40 +01002431 if (serverSendEnc(target) < 0)
2432 return -1;
2433 if (SendMessageTimeout(target, WM_COPYDATA,
2434 (WPARAM)message_window, (LPARAM)&data,
2435 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2436 return -1;
2437 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
2440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002441serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002442 char_u *name, // Where to send.
2443 char_u *cmd, // What to send.
2444 char_u **result, // Result of eval'ed expression
2445 void *ptarget, // HWND of server
2446 int asExpr, // Expression or keys?
2447 int timeout, // timeout in seconds or zero
2448 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002450 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 COPYDATASTRUCT data;
2452 char_u *retval = NULL;
2453 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002454 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002455 char_u altname_buf[MAX_PATH];
2456
Bram Moolenaar0f873732019-12-05 20:28:46 +01002457 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002458 if (serverName != NULL && STRICMP(name, serverName) == 0)
2459 return sendToLocalVim(cmd, asExpr, result);
2460
Bram Moolenaar0f873732019-12-05 20:28:46 +01002461 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002462 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002463 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2464 altname_buf_ptr = altname_buf;
2465 altname_buf[0] = NUL;
2466 target = findServer(name);
2467 altname_buf_ptr = NULL;
2468 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002469 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002470 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if (target == 0)
2473 {
2474 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002475 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return -1;
2477 }
2478
2479 if (ptarget)
2480 *(HWND *)ptarget = target;
2481
2482 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002483 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 data.lpData = cmd;
2485
K.Takataf9f2a332022-06-17 20:05:40 +01002486 if (serverSendEnc(target) < 0)
2487 return -1;
2488 if (SendMessageTimeout(target, WM_COPYDATA,
2489 (WPARAM)message_window, (LPARAM)&data,
2490 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2491 return -1;
2492 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 return -1;
2494
2495 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002496 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 if (result == NULL)
2499 vim_free(retval);
2500 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002501 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 return retcode;
2504}
2505
2506/*
2507 * Bring the server to the foreground.
2508 */
2509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002510serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 HWND target = findServer(name);
2513
2514 if (target != 0)
2515 SetForegroundWindow(target);
2516}
2517
Bram Moolenaar0f873732019-12-05 20:28:46 +01002518// Replies from server need to be stored until the client picks them up via
2519// remote_read(). So we maintain a list of server-id/reply pairs.
2520// Note that there could be multiple replies from one server pending if the
2521// client is slow picking them up.
2522// We just store the replies in a simple list. When we remove an entry, we
2523// move list entries down to fill the gap.
2524// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525typedef struct
2526{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002527 HWND server; // server window
2528 char_u *reply; // reply string
2529 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2533
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002534# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2535# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaar0f873732019-12-05 20:28:46 +01002537// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538static int reply_received = 0;
2539
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002540/*
2541 * Store a reply. "reply" must be allocated memory (or NULL).
2542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 static int
2544save_reply(HWND server, char_u *reply, int expr)
2545{
2546 reply_T *rep;
2547
2548 if (ga_grow(&reply_list, 1) == FAIL)
2549 return FAIL;
2550
2551 rep = REPLY_ITEM(REPLY_COUNT);
2552 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002553 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 rep->expr_result = expr;
2555 if (rep->reply == NULL)
2556 return FAIL;
2557
2558 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 reply_received = 1;
2560 return OK;
2561}
2562
2563/*
2564 * Get a reply from server "server".
2565 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2566 * server2client() message.
2567 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2568 * If "remove" is TRUE, consume the message, the caller must free it then.
2569 * if "wait" is TRUE block until a message arrives (or the server exits).
2570 */
2571 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002572serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574 int i;
2575 char_u *reply;
2576 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002577 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002578 time_t start;
2579 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002582 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 for (;;)
2584 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585 // Reset this here, in case a message arrives while we are going
2586 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 reply_received = 0;
2588
2589 for (i = 0; i < REPLY_COUNT; ++i)
2590 {
2591 rep = REPLY_ITEM(i);
2592 if (rep->server == server
2593 && ((rep->expr_result != 0) == (expr_res != NULL)))
2594 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002595 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 reply = rep->reply;
2597 if (expr_res != NULL)
2598 *expr_res = rep->expr_result == 1 ? 0 : -1;
2599
2600 if (remove)
2601 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 mch_memmove(rep, rep + 1,
2604 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2605 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607
Bram Moolenaar0f873732019-12-05 20:28:46 +01002608 // Return the reply to the caller, who takes on responsibility
2609 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 return reply;
2611 }
2612 }
2613
Bram Moolenaar0f873732019-12-05 20:28:46 +01002614 // If we got here, we didn't find a reply. Return immediately if the
2615 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002617 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002618 // Process pending messages once. Without this, looping on
2619 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002620 if (!did_process)
2621 {
2622 did_process = TRUE;
2623 serverProcessPendingMessages();
2624 continue;
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Bram Moolenaar0f873732019-12-05 20:28:46 +01002629 // We need to wait for a reply. Enter a message loop until the
2630 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar0f873732019-12-05 20:28:46 +01002632 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 while (reply_received == 0)
2634 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002635# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002636 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002637 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002639 time(&now);
2640 if (timeout > 0 && (now - start) >= timeout)
2641 break;
2642
Bram Moolenaar0f873732019-12-05 20:28:46 +01002643 // Wait for a SendMessage() call to us. This could be the reply
2644 // we are waiting for. Use a timeout of a second, to catch the
2645 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2647
Bram Moolenaar0f873732019-12-05 20:28:46 +01002648 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (!IsWindow(server))
2650 return NULL;
2651
2652 serverProcessPendingMessages();
2653 }
2654 }
2655
2656 return NULL;
2657}
2658
2659/*
2660 * Process any messages in the Windows message queue.
2661 */
2662 void
2663serverProcessPendingMessages(void)
2664{
2665 MSG msg;
2666
K.Takatab7057bd2022-01-21 11:37:07 +00002667 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002670 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672}
2673
Bram Moolenaar0f873732019-12-05 20:28:46 +01002674#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2677 || defined(PROTO)
2678
2679struct charset_pair
2680{
2681 char *name;
2682 BYTE charset;
2683};
2684
2685static struct charset_pair
2686charset_pairs[] =
2687{
2688 {"ANSI", ANSI_CHARSET},
2689 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2690 {"DEFAULT", DEFAULT_CHARSET},
2691 {"HANGEUL", HANGEUL_CHARSET},
2692 {"OEM", OEM_CHARSET},
2693 {"SHIFTJIS", SHIFTJIS_CHARSET},
2694 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {"ARABIC", ARABIC_CHARSET},
2696 {"BALTIC", BALTIC_CHARSET},
2697 {"EASTEUROPE", EASTEUROPE_CHARSET},
2698 {"GB2312", GB2312_CHARSET},
2699 {"GREEK", GREEK_CHARSET},
2700 {"HEBREW", HEBREW_CHARSET},
2701 {"JOHAB", JOHAB_CHARSET},
2702 {"MAC", MAC_CHARSET},
2703 {"RUSSIAN", RUSSIAN_CHARSET},
2704 {"THAI", THAI_CHARSET},
2705 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002706# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {NULL, 0}
2710};
2711
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002712struct quality_pair
2713{
2714 char *name;
2715 DWORD quality;
2716};
2717
2718static struct quality_pair
2719quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002720# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002721 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002722# endif
2723# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002724 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002725# endif
2726# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002727 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002728# endif
2729# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002730 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002731# endif
2732# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002733 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002734# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002735 {"DEFAULT", DEFAULT_QUALITY},
2736 {NULL, 0}
2737};
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739/*
2740 * Convert a charset ID to a name.
2741 * Return NULL when not recognized.
2742 */
2743 char *
2744charset_id2name(int id)
2745{
2746 struct charset_pair *cp;
2747
2748 for (cp = charset_pairs; cp->name != NULL; ++cp)
2749 if ((BYTE)id == cp->charset)
2750 break;
2751 return cp->name;
2752}
2753
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002754/*
2755 * Convert a quality ID to a name.
2756 * Return NULL when not recognized.
2757 */
2758 char *
2759quality_id2name(DWORD id)
2760{
2761 struct quality_pair *qp;
2762
2763 for (qp = quality_pairs; qp->name != NULL; ++qp)
2764 if (id == qp->quality)
2765 break;
2766 return qp->name;
2767}
2768
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002769static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2772 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2773 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002774 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775};
2776
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002777// Initialise the "current height" to -12 (same as s_lfDefault) just
2778// in case the user specifies a font in "guifont" with no size before a font
2779// with an explicit size has been set. This defaults the size to this value
2780// (-12 equates to roughly 9pt).
2781int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar0f873732019-12-05 20:28:46 +01002783/*
2784 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 * be a positive decimal number, with an optional decimal point (eg, "12", or
2786 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2787 * character is stored in *end. The flag "vertical" says whether this
2788 * calculation is for a vertical (height) size or a horizontal (width) one.
2789 */
2790 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002791points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 int pixels;
2794 int points = 0;
2795 int divisor = 0;
2796 HWND hwnd = (HWND)0;
2797 HDC hdc;
2798 HDC printer_dc = (HDC)pprinter_dc;
2799
2800 while (*str != NUL)
2801 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002802 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002804 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 divisor = 1;
2806 }
2807 else
2808 {
2809 if (!VIM_ISDIGIT(*str))
2810 break;
2811
2812 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002813 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 divisor *= 10;
2815 }
2816 ++str;
2817 }
2818
2819 if (divisor == 0)
2820 divisor = 1;
2821
2822 if (printer_dc == NULL)
2823 {
2824 hwnd = GetDesktopWindow();
2825 hdc = GetWindowDC(hwnd);
2826 }
2827 else
2828 hdc = printer_dc;
2829
2830 pixels = MulDiv(points,
2831 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2832 72 * divisor);
2833
2834 if (printer_dc == NULL)
2835 ReleaseDC(hwnd, hdc);
2836
2837 *end = str;
2838 return pixels;
2839}
2840
2841 static int CALLBACK
2842font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002843 ENUMLOGFONTW *elf,
2844 NEWTEXTMETRICW *ntm UNUSED,
2845 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 LPARAM lparam)
2847{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002848 // Return value:
2849 // 0 = terminate now (monospace & ANSI)
2850 // 1 = continue, still no luck...
2851 // 2 = continue, but we have an acceptable LOGFONTW
2852 // (monospace, not ANSI)
2853 // We use these values, as EnumFontFamilies returns 1 if the
2854 // callback function is never called. So, we check the return as
2855 // 0 = perfect, 2 = OK, 1 = no good...
2856 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002858 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002860# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002861 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if ((ntm->tmPitchAndFamily & 1) != 0)
2863 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar0f873732019-12-05 20:28:46 +01002866 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 *lf = elf->elfLogFont;
2868
Bram Moolenaar0f873732019-12-05 20:28:46 +01002869 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (lf->lfCharSet == ANSI_CHARSET
2871 || lf->lfCharSet == OEM_CHARSET
2872 || lf->lfCharSet == DEFAULT_CHARSET)
2873 return 0;
2874
Bram Moolenaar0f873732019-12-05 20:28:46 +01002875 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 return 2;
2877}
2878
2879 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002880init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 int n;
2883 HWND hwnd = GetDesktopWindow();
2884 HDC hdc = GetWindowDC(hwnd);
2885
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002886 n = EnumFontFamiliesW(hdc,
2887 lf->lfFaceName,
2888 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 (LPARAM)lf);
2890
2891 ReleaseDC(hwnd, hdc);
2892
Bram Moolenaar0f873732019-12-05 20:28:46 +01002893 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (n == 1)
2895 return FAIL;
2896
Bram Moolenaar0f873732019-12-05 20:28:46 +01002897 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2898 // font - get_logfont() sets bold, italic, etc based on the user's
2899 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 lf->lfHeight = current_font_height;
2901 lf->lfWidth = 0;
2902 lf->lfItalic = FALSE;
2903 lf->lfUnderline = FALSE;
2904 lf->lfStrikeOut = FALSE;
2905 lf->lfWeight = FW_NORMAL;
2906
Bram Moolenaar0f873732019-12-05 20:28:46 +01002907 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 return OK;
2909}
2910
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002911/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002912 * Compare a UTF-16 string and an ASCII string literally.
2913 * Only works all the code points are inside ASCII range.
2914 */
2915 static int
2916utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2917{
2918 size_t i;
2919
2920 for (i = 0; i < n; i++)
2921 {
2922 if (w[i] == 0 || w[i] != p[i])
2923 return w[i] - p[i];
2924 }
2925 return 0;
2926}
2927
2928/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002929 * Get font info from "name" into logfont "lf".
2930 * Return OK for a valid name, FAIL otherwise.
2931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int
2933get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002934 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002935 char_u *name,
2936 HDC printer_dc,
2937 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002939 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002941 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002942 static LOGFONTW *lastlf = NULL;
2943 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 *lf = s_lfDefault;
2946 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002947 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002949 wname = enc_to_utf16(name, NULL);
2950 if (wname == NULL)
2951 return FAIL;
2952
2953 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002955# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002956 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002957 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002958 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 cf.lStructSize = sizeof(cf);
2960 cf.hwndOwner = s_hwnd;
2961 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2962 if (lastlf != NULL)
2963 *lf = *lastlf;
2964 cf.lpLogFont = lf;
2965 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002966 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002967 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002968# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002969 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971
2972 /*
2973 * Split name up, it could be <name>:h<height>:w<width> etc.
2974 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002975 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002977 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002978 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002979 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002981 if (p != wname)
2982 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar0f873732019-12-05 20:28:46 +01002984 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 lf->lfHeight = -12;
2986 lf->lfWidth = 0;
2987 lf->lfWeight = FW_NORMAL;
2988 lf->lfItalic = FALSE;
2989 lf->lfUnderline = FALSE;
2990 lf->lfStrikeOut = FALSE;
2991
2992 /*
2993 * If the font can't be found, try replacing '_' by ' '.
2994 */
2995 if (init_logfont(lf) == FAIL)
2996 {
2997 int did_replace = FALSE;
2998
2999 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003000 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003002 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 did_replace = TRUE;
3004 }
3005 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003006 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003009 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 p++;
3011
Bram Moolenaar0f873732019-12-05 20:28:46 +01003012 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 while (*p)
3014 {
3015 switch (*p++)
3016 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003017 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003018 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003020 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003021 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003023 case L'W':
3024 lf->lfWeight = wcstol(p, &p, 10);
3025 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003026 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003029 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003032 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 lf->lfUnderline = TRUE;
3034 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003035 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 lf->lfStrikeOut = TRUE;
3037 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003038 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
3040 struct charset_pair *cp;
3041
3042 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003043 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 lf->lfCharSet = cp->charset;
3046 p += strlen(cp->name);
3047 break;
3048 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003049 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003051 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003052
3053 semsg(_(e_illegal_str_name_str_in_font_name_str),
3054 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003055 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 break;
3057 }
3058 break;
3059 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003060 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003061 {
3062 struct quality_pair *qp;
3063
3064 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003065 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003066 {
3067 lf->lfQuality = qp->quality;
3068 p += strlen(qp->name);
3069 break;
3070 }
3071 if (qp->name == NULL && verbose)
3072 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003073 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003074 semsg(_(e_illegal_str_name_str_in_font_name_str),
3075 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003076 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003077 break;
3078 }
3079 break;
3080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003082 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003083 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003084 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003086 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 p++;
3088 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003089 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003092 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003093 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003096 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003098 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003100 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003102 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
Bram Moolenaar0f873732019-12-05 20:28:46 +01003105#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003106
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003107#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003108/*
3109 * Initialize the Winsock dll.
3110 */
3111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003113{
3114 WSADATA wsaData;
3115 int wsaerr;
3116
3117 if (WSInitialized)
3118 return;
3119
3120 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3121 if (wsaerr == 0)
3122 WSInitialized = TRUE;
3123}
3124#endif