blob: edd5cdac4e716a70bcb899423aea38e0c5d2c602 [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 * os_win32.c
11 *
12 * Used for both the console version and the Win32 GUI. A lot of code is for
Bram Moolenaar4f974752019-02-17 17:44:42 +010013 * the console version only, so there is a lot of "#ifndef FEAT_GUI_MSWIN".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * Win32 (Windows NT and Windows 95) system-dependent routines.
16 * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
17 * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
18 *
19 * George V. Reilly <george@reilly.org> wrote most of this.
20 * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0.
21 */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
Bram Moolenaar325b7a22004-07-05 15:58:32 +000025#ifdef FEAT_MZSCHEME
26# include "if_mzsch.h"
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#include <signal.h>
31#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010032
Bram Moolenaar0f873732019-12-05 20:28:46 +010033// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# include <process.h>
36#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38#undef chdir
39#ifdef __GNUC__
40# ifndef __MINGW32__
41# include <dirent.h>
42# endif
43#else
44# include <direct.h>
45#endif
46
Bram Moolenaar82881492012-11-20 16:53:39 +010047#ifndef PROTO
Bram Moolenaar4f974752019-02-17 17:44:42 +010048# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010049# include <shellapi.h>
50# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
52
Bram Moolenaarfb630902016-10-29 14:55:00 +020053#ifdef FEAT_JOB_CHANNEL
54# include <tlhelp32.h>
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef __MINGW32__
58# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
59# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
60# endif
61# ifndef RIGHTMOST_BUTTON_PRESSED
62# define RIGHTMOST_BUTTON_PRESSED 0x0002
63# endif
64# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
65# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
66# endif
67# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
68# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
69# endif
70# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
71# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
72# endif
73
74/*
75 * EventFlags
76 */
77# ifndef MOUSE_MOVED
78# define MOUSE_MOVED 0x0001
79# endif
80# ifndef DOUBLE_CLICK
81# define DOUBLE_CLICK 0x0002
82# endif
83#endif
84
Bram Moolenaar0f873732019-12-05 20:28:46 +010085// Record all output and all keyboard & mouse input
86// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88#ifdef MCH_WRITE_DUMP
89FILE* fdDump = NULL;
90#endif
91
92/*
93 * When generating prototypes for Win32 on Unix, these lines make the syntax
94 * errors disappear. They do not need to be correct.
95 */
96#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010097# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000099typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100typedef int ACCESS_MASK;
101typedef int BOOL;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100107typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HINSTANCE;
112typedef int HWND;
113typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200114typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115typedef int KEY_EVENT_RECORD;
116typedef int LOGFONT;
117typedef int LPBOOL;
118typedef int LPCTSTR;
119typedef int LPDWORD;
120typedef int LPSTR;
121typedef int LPTSTR;
122typedef int LPVOID;
123typedef int MOUSE_EVENT_RECORD;
124typedef int PACL;
125typedef int PDWORD;
126typedef int PHANDLE;
127typedef int PRINTDLG;
128typedef int PSECURITY_DESCRIPTOR;
129typedef int PSID;
130typedef int SECURITY_INFORMATION;
131typedef int SHORT;
132typedef int SMALL_RECT;
133typedef int TEXTMETRIC;
134typedef int TOKEN_INFORMATION_CLASS;
135typedef int TRUSTEE;
136typedef int WORD;
137typedef int WCHAR;
138typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100139typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200140typedef int SE_OBJECT_TYPE;
141typedef int PSNSECINFO;
142typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100143typedef int STARTUPINFO;
144typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200145typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
148
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200149#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100150// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
152static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
153
Bram Moolenaar0f873732019-12-05 20:28:46 +0100154// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar0f873732019-12-05 20:28:46 +0100158// The attribute of the screen when the editor was started
159static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static WORD g_attrCurrent;
161
Bram Moolenaar0f873732019-12-05 20:28:46 +0100162static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
163static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
164static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static void scroll(unsigned cLines);
167static void set_scroll_region(unsigned left, unsigned top,
168 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100169static void set_scroll_region_tb(unsigned top, unsigned bottom);
170static void set_scroll_region_lr(unsigned left, unsigned right);
171static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void delete_lines(unsigned cLines);
173static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int s_cursor_visible = TRUE;
176static int did_create_conin = FALSE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200177#endif
178#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static int s_dont_use_vimrun = TRUE;
180static int need_vimrun_warning = FALSE;
181static char *vimrun_path = "vimrun ";
182#endif
183
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200184static int win32_getattrs(char_u *name);
185static int win32_setattrs(char_u *name, int attrs);
186static int win32_set_archive(char_u *name);
187
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100188static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200189static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100190static int conpty_stable = 0;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +0200191static int conpty_fix_type = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100192static void vtp_flag_init();
193
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200194#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100195static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100196static void vtp_init();
197static void vtp_exit();
198static int vtp_printf(char *format, ...);
199static void vtp_sgr_bulk(int arg);
200static void vtp_sgr_bulks(int argc, int *argv);
201
202static guicolor_T save_console_bg_rgb;
203static guicolor_T save_console_fg_rgb;
Bram Moolenaardf543822020-01-30 11:53:59 +0100204static guicolor_T store_console_bg_rgb;
205static guicolor_T store_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100206
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200207static int g_color_index_bg = 0;
208static int g_color_index_fg = 7;
209
210# ifdef FEAT_TERMGUICOLORS
211static int default_console_color_bg = 0x000000; // black
212static int default_console_color_fg = 0xc0c0c0; // white
213# endif
214
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100215# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200216# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100217# else
218# define USE_VTP 0
219# endif
220
221static void set_console_color_rgb(void);
222static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100223static void restore_console_color_rgb(void);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100224#endif
225
Bram Moolenaar0f873732019-12-05 20:28:46 +0100226// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100227#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
228# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
229#endif
230
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200231#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100232static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
234
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200235static char_u *exe_path = NULL;
236
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100237static BOOL win8_or_later = FALSE;
238
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200239#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100240// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100241typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
242{
243 ULONG cbSize;
244 COORD dwSize;
245 COORD dwCursorPosition;
246 WORD wAttributes;
247 SMALL_RECT srWindow;
248 COORD dwMaximumWindowSize;
249 WORD wPopupAttributes;
250 BOOL bFullscreenSupported;
251 COLORREF ColorTable[16];
252} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
253typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
254static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
255typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
256static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
257static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100258#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100259
260/*
261 * Get version number including build number
262 */
263typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100264#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100265 (((major) << 24) | ((minor) << 16) | (build))
266
267 static DWORD
268get_build_number(void)
269{
270 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
271 HMODULE hNtdll;
272 PfnRtlGetVersion pRtlGetVersion;
273 DWORD ver = MAKE_VER(0, 0, 0);
274
275 hNtdll = GetModuleHandle("ntdll.dll");
276 if (hNtdll != NULL)
277 {
278 pRtlGetVersion =
279 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
280 pRtlGetVersion(&osver);
281 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
282 min(osver.dwMinorVersion, 255),
283 min(osver.dwBuildNumber, 32767));
284 }
285 return ver;
286}
287
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200288#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100289/*
290 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100291 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100292 */
293 static BOOL
294read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100295 HANDLE hInput,
296 INPUT_RECORD *lpBuffer,
297 DWORD nLength,
298 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100299{
300 enum
301 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100302 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100303 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100304 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100305 static DWORD s_dwIndex = 0;
306 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100307 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100308 int head;
309 int tail;
310 int i;
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200311 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100312
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200313 if (nLength == -2)
314 return (s_dwMax > 0) ? TRUE : FALSE;
315
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100316 if (!win8_or_later)
317 {
318 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200319 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
320 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100321 }
322
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323 if (s_dwMax == 0)
324 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100325 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200326 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
327 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100328 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100329 s_dwIndex = 0;
330 s_dwMax = dwEvents;
331 if (dwEvents == 0)
332 {
333 *lpEvents = 0;
334 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100335 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100336
337 if (s_dwMax > 1)
338 {
339 head = 0;
340 tail = s_dwMax - 1;
341 while (head != tail)
342 {
343 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
344 && s_irCache[head + 1].EventType
345 == WINDOW_BUFFER_SIZE_EVENT)
346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100347 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100348 for (i = head; i < tail; ++i)
349 s_irCache[i] = s_irCache[i + 1];
350 --tail;
351 continue;
352 }
353 head++;
354 }
355 s_dwMax = tail + 1;
356 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100357 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100358
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200359 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
360 {
361 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
362 {
363 s_irPseudo = s_irCache[s_dwIndex];
364 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
365 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
366 *lpBuffer = s_irPseudo;
367 *lpEvents = 1;
368 return TRUE;
369 }
370 }
371
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100372 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200373 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100374 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100375 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100376 return TRUE;
377}
378
379/*
380 * Version of PeekConsoleInput() that works with IME.
381 */
382 static BOOL
383peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100384 HANDLE hInput,
385 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200386 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100387 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100388{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100389 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100390}
391
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100392# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200393 static DWORD
394msg_wait_for_multiple_objects(
395 DWORD nCount,
396 LPHANDLE pHandles,
397 BOOL fWaitAll,
398 DWORD dwMilliseconds,
399 DWORD dwWakeMask)
400{
401 if (read_console_input(NULL, NULL, -2, NULL))
402 return WAIT_OBJECT_0;
403 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
404 dwMilliseconds, dwWakeMask);
405}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100406# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200407
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100408# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200409 static DWORD
410wait_for_single_object(
411 HANDLE hHandle,
412 DWORD dwMilliseconds)
413{
414 if (read_console_input(NULL, NULL, -2, NULL))
415 return WAIT_OBJECT_0;
416 return WaitForSingleObject(hHandle, dwMilliseconds);
417}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100418# endif
419#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 static void
422get_exe_name(void)
423{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100424 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
425 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100426#define MAX_ENV_PATH_LEN 8192
427 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200428 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
430 if (exe_name == NULL)
431 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 if (*temp != NUL)
435 exe_name = FullName_save((char_u *)temp, FALSE);
436 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000437
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200438 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000439 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200440 exe_path = vim_strnsave(exe_name,
441 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200442 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000443 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100444 // Append our starting directory to $PATH, so that when doing
445 // "!xxd" it's found in our starting directory. Needed because
446 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200447 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100448 if (p == NULL
449 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200450 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100451 if (p == NULL || *p == NUL)
452 temp[0] = NUL;
453 else
454 {
455 STRCPY(temp, p);
456 STRCAT(temp, ";");
457 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200458 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100459 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200460 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000461 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463}
464
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200465/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100466 * Unescape characters in "p" that appear in "escaped".
467 */
468 static void
469unescape_shellxquote(char_u *p, char_u *escaped)
470{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100471 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100472 int n;
473
474 while (*p != NUL)
475 {
476 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
477 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100478 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100479 p += n;
480 l -= n;
481 }
482}
483
484/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200485 * Load library "name".
486 */
487 HINSTANCE
488vimLoadLib(char *name)
489{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200490 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200491
Bram Moolenaar0f873732019-12-05 20:28:46 +0100492 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
493 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200494 if (exe_path == NULL)
495 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200496 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200497 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200498 WCHAR old_dirw[MAXPATHL];
499
500 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
501 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100502 // Change directory to where the executable is, both to make
503 // sure we find a .dll there and to avoid looking for a .dll
504 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100505 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200506 dll = LoadLibrary(name);
507 SetCurrentDirectoryW(old_dirw);
508 return dll;
509 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200510 }
511 return dll;
512}
513
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200514#if defined(VIMDLL) || defined(PROTO)
515/*
516 * Check if the current executable file is for the GUI subsystem.
517 */
518 int
519mch_is_gui_executable(void)
520{
521 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
522 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
523 PIMAGE_NT_HEADERS pPE;
524
525 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
526 return FALSE;
527 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
528 if (pPE->Signature != IMAGE_NT_SIGNATURE)
529 return FALSE;
530 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
531 return TRUE;
532 return FALSE;
533}
534#endif
535
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100536#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
537/*
538 * Get related information about 'funcname' which is imported by 'hInst'.
539 * If 'info' is 0, return the function address.
540 * If 'info' is 1, return the module name which the function is imported from.
541 */
542 static void *
543get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
544{
545 PBYTE pImage = (PBYTE)hInst;
546 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
547 PIMAGE_NT_HEADERS pPE;
548 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100549 PIMAGE_THUNK_DATA pIAT; // Import Address Table
550 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100551 PIMAGE_IMPORT_BY_NAME pImpName;
552
553 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
554 return NULL;
555 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
556 if (pPE->Signature != IMAGE_NT_SIGNATURE)
557 return NULL;
558 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
559 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
560 .VirtualAddress);
561 for (; pImpDesc->FirstThunk; ++pImpDesc)
562 {
563 if (!pImpDesc->OriginalFirstThunk)
564 continue;
565 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
566 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
567 for (; pIAT->u1.Function; ++pIAT, ++pINT)
568 {
569 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
570 continue;
571 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
572 + (UINT_PTR)(pINT->u1.AddressOfData));
573 if (strcmp((char *)pImpName->Name, funcname) == 0)
574 {
575 switch (info)
576 {
577 case 0:
578 return (void *)pIAT->u1.Function;
579 case 1:
580 return (void *)(pImage + pImpDesc->Name);
581 default:
582 return NULL;
583 }
584 }
585 }
586 }
587 return NULL;
588}
589
590/*
591 * Get the module handle which 'funcname' in 'hInst' is imported from.
592 */
593 HINSTANCE
594find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
595{
596 char *modulename;
597
598 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
599 if (modulename != NULL)
600 return GetModuleHandleA(modulename);
601 return NULL;
602}
603
604/*
605 * Get the address of 'funcname' which is imported by 'hInst' DLL.
606 */
607 void *
608get_dll_import_func(HINSTANCE hInst, const char *funcname)
609{
610 return get_imported_func_info(hInst, funcname, 0);
611}
612#endif
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
615# ifndef GETTEXT_DLL
616# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200617# define GETTEXT_DLL_ALT1 "libintl-8.dll"
618# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100620// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000621static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200622static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000623static char *null_libintl_textdomain(const char *);
624static char *null_libintl_bindtextdomain(const char *, const char *);
625static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100626static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200628static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000629char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200630char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
631 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000632char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
633char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000635char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
636 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100637int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100640dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 int i;
643 static struct
644 {
645 char *name;
646 FARPROC *ptr;
647 } libintl_entry[] =
648 {
649 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200650 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
652 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
653 {NULL, NULL}
654 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100655 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar7554c542018-10-06 15:03:15 +0200657 // No need to initialize twice.
658 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200660 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100661 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100662# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100663 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200664 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100665# endif
666# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200667 if (!hLibintlDLL)
668 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100669# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100670 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200672 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200674 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200676 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200678 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 for (i = 0; libintl_entry[i].name != NULL
681 && libintl_entry[i].ptr != NULL; ++i)
682 {
683 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
684 libintl_entry[i].name)) == NULL)
685 {
686 dyn_libintl_end();
687 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000688 {
689 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100690 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000691 verbose_leave();
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return 0;
694 }
695 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000696
Bram Moolenaar0f873732019-12-05 20:28:46 +0100697 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000698 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000699 "bind_textdomain_codeset");
700 if (dyn_libintl_bind_textdomain_codeset == NULL)
701 dyn_libintl_bind_textdomain_codeset =
702 null_libintl_bind_textdomain_codeset;
703
Bram Moolenaar0f873732019-12-05 20:28:46 +0100704 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100705 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
706 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100708 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
709 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return 1;
712}
713
714 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100715dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 if (hLibintlDLL)
718 FreeLibrary(hLibintlDLL);
719 hLibintlDLL = NULL;
720 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200721 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 dyn_libintl_textdomain = null_libintl_textdomain;
723 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000724 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100725 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726}
727
728 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000729null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 return (char*)msgid;
732}
733
734 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200735null_libintl_ngettext(
736 const char *msgid,
737 const char *msgid_plural,
738 unsigned long n)
739{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200740 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200741}
742
Bram Moolenaaree695f72016-08-03 22:08:45 +0200743 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100744null_libintl_bindtextdomain(
745 const char *domainname UNUSED,
746 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
748 return NULL;
749}
750
751 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100752null_libintl_bind_textdomain_codeset(
753 const char *domainname UNUSED,
754 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000755{
756 return NULL;
757}
758
759 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100760null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762 return NULL;
763}
764
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200765 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100766null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100767{
768 return 0;
769}
770
Bram Moolenaar0f873732019-12-05 20:28:46 +0100771#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar0f873732019-12-05 20:28:46 +0100773// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775#ifndef VER_PLATFORM_WIN32_WINDOWS
776# define VER_PLATFORM_WIN32_WINDOWS 1
777#endif
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100780# ifndef PROTO
781# include <aclapi.h>
782# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200783# ifndef PROTECTED_DACL_SECURITY_INFORMATION
784# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#endif
787
Bram Moolenaar27515922013-06-29 15:36:26 +0200788#ifdef HAVE_ACL
789/*
790 * Enables or disables the specified privilege.
791 */
792 static BOOL
793win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
794{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100795 BOOL bResult;
796 LUID luid;
797 HANDLE hToken;
798 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200799
800 if (!OpenProcessToken(GetCurrentProcess(),
801 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
802 return FALSE;
803
804 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
805 {
806 CloseHandle(hToken);
807 return FALSE;
808 }
809
Bram Moolenaar45500912014-07-09 20:51:07 +0200810 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200811 tokenPrivileges.Privileges[0].Luid = luid;
812 tokenPrivileges.Privileges[0].Attributes = bEnable ?
813 SE_PRIVILEGE_ENABLED : 0;
814
815 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
816 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
817
818 CloseHandle(hToken);
819
820 return bResult && GetLastError() == ERROR_SUCCESS;
821}
822#endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100825 * Set "win8_or_later" and fill in "windowsVersion".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
827 void
828PlatformId(void)
829{
830 static int done = FALSE;
831
832 if (!done)
833 {
834 OSVERSIONINFO ovi;
835
836 ovi.dwOSVersionInfoSize = sizeof(ovi);
837 GetVersionEx(&ovi);
838
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100839 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
840 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100842 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
843 || ovi.dwMajorVersion > 6)
844 win8_or_later = TRUE;
845
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100847 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200848 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#endif
850 done = TRUE;
851 }
852}
853
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200854#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100856# define SHIFT (SHIFT_PRESSED)
857# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
858# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
859# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861
Bram Moolenaar0f873732019-12-05 20:28:46 +0100862// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
863// We map function keys to their ANSI terminal equivalents, as produced
864// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
865// ANSI key with a value >= '\300' is nonstandard, but provided anyway
866// so that the user can have access to all SHIFT-, CTRL-, and ALT-
867// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000869static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 WORD wVirtKey;
872 BOOL fAnsiKey;
873 int chAlone;
874 int chShift;
875 int chCtrl;
876 int chAlt;
877} VirtKeyMap[] =
878{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200879// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
881
882 { VK_F1, TRUE, ';', 'T', '^', 'h', },
883 { VK_F2, TRUE, '<', 'U', '_', 'i', },
884 { VK_F3, TRUE, '=', 'V', '`', 'j', },
885 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
886 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
887 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
888 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
889 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
890 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
891 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200892 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
893 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200895 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
896 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
897 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
898 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
899 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
900 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
901 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
902 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
903 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
904 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100905 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200907 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200910 // Most people don't have F13-F20, but what the hell...
911 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
912 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
913 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
914 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
915 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
916 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
917 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
918 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100919# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200920 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
921 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
922 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
923 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200925 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
926 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
927 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
928 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
929 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
930 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
931 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
932 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
933 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
934 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100935 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936};
937
938
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100939# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
940# define UChar UnicodeChar
941# else
942# define UChar uChar.UnicodeChar
943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
Bram Moolenaar0f873732019-12-05 20:28:46 +0100945/*
946 * The return code indicates key code size.
947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000950 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 UINT uMods = pker->dwControlKeyState;
953 static int s_iIsDead = 0;
954 static WORD awAnsiCode[2];
955 static BYTE abKeystate[256];
956
957
958 if (s_iIsDead == 2)
959 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200960 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 s_iIsDead = 0;
962 return 1;
963 }
964
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200965 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 return 1;
967
Bram Moolenaara80faa82020-04-12 19:37:17 +0200968 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar0f873732019-12-05 20:28:46 +0100970 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200971 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 if (uMods & SHIFT_PRESSED)
974 abKeystate[VK_SHIFT] = 0x80;
975 if (uMods & CAPSLOCK_ON)
976 abKeystate[VK_CAPITAL] = 1;
977
978 if ((uMods & ALT_GR) == ALT_GR)
979 {
980 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
981 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
982 }
983
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200984 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
985 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200988 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 return s_iIsDead;
991}
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993static BOOL g_fJustGotFocus = FALSE;
994
995/*
996 * Decode a KEY_EVENT into one or two keystrokes
997 */
998 static BOOL
999decode_key_event(
1000 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001001 WCHAR *pch,
1002 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001004 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 int i;
1007 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1008
1009 *pch = *pch2 = NUL;
1010 g_fJustGotFocus = FALSE;
1011
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (!pker->bKeyDown)
1014 return FALSE;
1015
Bram Moolenaar0f873732019-12-05 20:28:46 +01001016 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 switch (pker->wVirtualKeyCode)
1018 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001019 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 case VK_SHIFT:
1021 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return FALSE;
1024
1025 default:
1026 break;
1027 }
1028
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001030 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001032 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (pker->wVirtualKeyCode == '6')
1034 {
1035 *pch = Ctrl_HAT;
1036 return TRUE;
1037 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001038 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 else if (pker->wVirtualKeyCode == '2')
1040 {
1041 *pch = NUL;
1042 return TRUE;
1043 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 else if (pker->wVirtualKeyCode == 0xBD)
1046 {
1047 *pch = Ctrl__;
1048 return TRUE;
1049 }
1050 }
1051
Bram Moolenaar0f873732019-12-05 20:28:46 +01001052 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1054 {
1055 *pch = K_NUL;
1056 *pch2 = '\017';
1057 return TRUE;
1058 }
1059
1060 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1061 {
1062 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1063 {
1064 if (nModifs == 0)
1065 *pch = VirtKeyMap[i].chAlone;
1066 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1067 *pch = VirtKeyMap[i].chShift;
1068 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1069 *pch = VirtKeyMap[i].chCtrl;
1070 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1071 *pch = VirtKeyMap[i].chAlt;
1072
1073 if (*pch != 0)
1074 {
1075 if (VirtKeyMap[i].fAnsiKey)
1076 {
1077 *pch2 = *pch;
1078 *pch = K_NUL;
1079 }
1080
1081 return TRUE;
1082 }
1083 }
1084 }
1085
1086 i = win32_kbd_patch_key(pker);
1087
1088 if (i < 0)
1089 *pch = NUL;
1090 else
1091 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001092 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 if (pmodifiers != NULL)
1095 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // Pass on the ALT key as a modifier, but only when not combined
1097 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1099 *pmodifiers |= MOD_MASK_ALT;
1100
Bram Moolenaar0f873732019-12-05 20:28:46 +01001101 // Pass on SHIFT only for special keys, because we don't know when
1102 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1104 *pmodifiers |= MOD_MASK_SHIFT;
1105
Bram Moolenaar0f873732019-12-05 20:28:46 +01001106 // Pass on CTRL only for non-special keys, because we don't know
1107 // when it's already included with the character. And not when
1108 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1110 && *pch >= 0x20 && *pch < 0x80)
1111 *pmodifiers |= MOD_MASK_CTRL;
1112 }
1113 }
1114
1115 return (*pch != NUL);
1116}
1117
Bram Moolenaar0f873732019-12-05 20:28:46 +01001118#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
1122 * For the GUI the mouse handling is in gui_w32.c.
1123 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001124#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001126mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001129#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001130static int g_fMouseAvail = FALSE; // mouse present
1131static int g_fMouseActive = FALSE; // mouse enabled
1132static int g_nMouseClick = -1; // mouse status
1133static int g_xMouse; // mouse x coordinate
1134static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136/*
1137 * Enable or disable mouse input
1138 */
1139 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001140mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
1142 DWORD cmodein;
1143
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001144# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001145 if (gui.in_use)
1146 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (!g_fMouseAvail)
1149 return;
1150
1151 g_fMouseActive = on;
1152 GetConsoleMode(g_hConIn, &cmodein);
1153
1154 if (g_fMouseActive)
1155 cmodein |= ENABLE_MOUSE_INPUT;
1156 else
1157 cmodein &= ~ENABLE_MOUSE_INPUT;
1158
1159 SetConsoleMode(g_hConIn, cmodein);
1160}
1161
Bram Moolenaar157d8132018-03-06 17:09:20 +01001162
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001163# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001164/*
1165 * Called when 'balloonevalterm' changed.
1166 */
1167 void
1168mch_bevalterm_changed(void)
1169{
1170 mch_setmouse(g_fMouseActive);
1171}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001172# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174/*
1175 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1176 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1177 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1178 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1179 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1180 * and we return the mouse position in g_xMouse and g_yMouse.
1181 *
1182 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1183 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1184 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1185 *
1186 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1187 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1188 *
1189 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1190 * moves, even if it stays within the same character cell. We ignore
1191 * all MOUSE_MOVED messages if the position hasn't really changed, and
1192 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1193 * we're only interested in MOUSE_DRAG).
1194 *
1195 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1196 * 2-button mouses by pressing the left & right buttons simultaneously.
1197 * In practice, it's almost impossible to click both at the same time,
1198 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1199 * in such cases, if the user is clicking quickly.
1200 */
1201 static BOOL
1202decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001203 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 static int s_nOldButton = -1;
1206 static int s_nOldMouseClick = -1;
1207 static int s_xOldMouse = -1;
1208 static int s_yOldMouse = -1;
1209 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001210# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 static int s_cClicks = 1;
1214 static BOOL s_fReleased = TRUE;
1215 static DWORD s_dwLastClickTime = 0;
1216 static BOOL s_fNextIsMiddle = FALSE;
1217
Bram Moolenaar0f873732019-12-05 20:28:46 +01001218 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1221 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1222 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1223 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1224
1225 int nButton;
1226
1227 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1228 cButtons = 2;
1229
1230 if (!g_fMouseAvail || !g_fMouseActive)
1231 {
1232 g_nMouseClick = -1;
1233 return FALSE;
1234 }
1235
Bram Moolenaar0f873732019-12-05 20:28:46 +01001236 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (g_fJustGotFocus)
1238 {
1239 g_fJustGotFocus = FALSE;
1240 return FALSE;
1241 }
1242
Bram Moolenaar0f873732019-12-05 20:28:46 +01001243 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (g_nMouseClick != -1)
1245 return TRUE;
1246
1247 nButton = -1;
1248 g_xMouse = pmer->dwMousePosition.X;
1249 g_yMouse = pmer->dwMousePosition.Y;
1250
1251 if (pmer->dwEventFlags == MOUSE_MOVED)
1252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001253 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1254 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1256 return FALSE;
1257 }
1258
Bram Moolenaar0f873732019-12-05 20:28:46 +01001259 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1261 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001262 nButton = MOUSE_RELEASE;
1263
Bram Moolenaar0f873732019-12-05 20:28:46 +01001264 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001266 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001267# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001268 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001269 if (p_bevalterm)
1270 nButton = MOUSE_DRAG;
1271 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001272# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001273 return FALSE;
1274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 s_fReleased = TRUE;
1277 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001278 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001280 // on a 2-button mouse, hold down left and right buttons
1281 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1284 {
1285 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1286
Bram Moolenaar0f873732019-12-05 20:28:46 +01001287 // if either left or right button only is pressed, see if the
1288 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (dwLR == LEFT || dwLR == RIGHT)
1290 {
1291 for (;;)
1292 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001293 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1295 != WAIT_OBJECT_0)
1296 break;
1297 else
1298 {
1299 DWORD cRecords = 0;
1300 INPUT_RECORD ir;
1301 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1302
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001303 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1306 || !(pmer2->dwButtonState & LEFT_RIGHT))
1307 break;
1308 else
1309 {
1310 if (pmer2->dwEventFlags != MOUSE_MOVED)
1311 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001312 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 return decode_mouse_event(pmer2);
1315 }
1316 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1317 s_yOldMouse == pmer2->dwMousePosition.Y)
1318 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001319 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001320 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar0f873732019-12-05 20:28:46 +01001322 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001323 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1326 break;
1327 }
1328 else
1329 break;
1330 }
1331 }
1332 }
1333 }
1334 }
1335
1336 if (s_fNextIsMiddle)
1337 {
1338 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1339 ? MOUSE_DRAG : MOUSE_MIDDLE;
1340 s_fNextIsMiddle = FALSE;
1341 }
1342 else if (cButtons == 2 &&
1343 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1344 {
1345 nButton = MOUSE_MIDDLE;
1346
1347 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1348 {
1349 s_fNextIsMiddle = TRUE;
1350 nButton = MOUSE_RELEASE;
1351 }
1352 }
1353 else if ((pmer->dwButtonState & LEFT) == LEFT)
1354 nButton = MOUSE_LEFT;
1355 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1356 nButton = MOUSE_MIDDLE;
1357 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1358 nButton = MOUSE_RIGHT;
1359
1360 if (! s_fReleased && ! s_fNextIsMiddle
1361 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1362 return FALSE;
1363
1364 s_fReleased = s_fNextIsMiddle;
1365 }
1366
1367 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1368 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001369 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (nButton != -1 && nButton != MOUSE_RELEASE)
1371 {
1372 DWORD dwCurrentTime = GetTickCount();
1373
1374 if (s_xOldMouse != g_xMouse
1375 || s_yOldMouse != g_yMouse
1376 || s_nOldButton != nButton
1377 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001378# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1382 {
1383 s_cClicks = 1;
1384 }
1385 else if (++s_cClicks > 4)
1386 {
1387 s_cClicks = 1;
1388 }
1389
1390 s_dwLastClickTime = dwCurrentTime;
1391 }
1392 }
1393 else if (pmer->dwEventFlags == MOUSE_MOVED)
1394 {
1395 if (nButton != -1 && nButton != MOUSE_RELEASE)
1396 nButton = MOUSE_DRAG;
1397
1398 s_cClicks = 1;
1399 }
1400
1401 if (nButton == -1)
1402 return FALSE;
1403
1404 if (nButton != MOUSE_RELEASE)
1405 s_nOldButton = nButton;
1406
1407 g_nMouseClick = nButton;
1408
1409 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1410 g_nMouseClick |= MOUSE_SHIFT;
1411 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1412 g_nMouseClick |= MOUSE_CTRL;
1413 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1414 g_nMouseClick |= MOUSE_ALT;
1415
1416 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1417 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1418
Bram Moolenaar0f873732019-12-05 20:28:46 +01001419 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (s_xOldMouse == g_xMouse
1421 && s_yOldMouse == g_yMouse
1422 && s_nOldMouseClick == g_nMouseClick)
1423 {
1424 g_nMouseClick = -1;
1425 return FALSE;
1426 }
1427
1428 s_xOldMouse = g_xMouse;
1429 s_yOldMouse = g_yMouse;
1430 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001431# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 s_nOldMouseClick = g_nMouseClick;
1435
1436 return TRUE;
1437}
1438
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001439#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441
1442#ifdef MCH_CURSOR_SHAPE
1443/*
1444 * Set the shape of the cursor.
1445 * 'thickness' can be from 1 (thin) to 99 (block)
1446 */
1447 static void
1448mch_set_cursor_shape(int thickness)
1449{
1450 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1451 ConsoleCursorInfo.dwSize = thickness;
1452 ConsoleCursorInfo.bVisible = s_cursor_visible;
1453
1454 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1455 if (s_cursor_visible)
1456 SetConsoleCursorPosition(g_hConOut, g_coord);
1457}
1458
1459 void
1460mch_update_cursor(void)
1461{
1462 int idx;
1463 int thickness;
1464
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001465# ifdef VIMDLL
1466 if (gui.in_use)
1467 return;
1468# endif
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 /*
1471 * How the cursor is drawn depends on the current mode.
1472 */
1473 idx = get_shape_idx(FALSE);
1474
1475 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001476 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
1478 thickness = shape_table[idx].percentage;
1479 mch_set_cursor_shape(thickness);
1480}
1481#endif
1482
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001483#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484/*
1485 * Handle FOCUS_EVENT.
1486 */
1487 static void
1488handle_focus_event(INPUT_RECORD ir)
1489{
1490 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1491 ui_focus_change((int)g_fJustGotFocus);
1492}
1493
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001494static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496/*
1497 * Wait until console input from keyboard or mouse is available,
1498 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001499 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * Return TRUE if something is available FALSE if not.
1501 */
1502 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001503WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 DWORD dwNow = 0, dwEndTime = 0;
1506 INPUT_RECORD ir;
1507 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001508 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001509# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001510 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001514 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 dwEndTime = GetTickCount() + msec;
1516 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001517 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 dwEndTime = INFINITE;
1519
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001520 // We need to loop until the end of the time period, because
1521 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 for (;;)
1523 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001524 // Only process messages when waiting.
1525 if (msec != 0)
1526 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001527# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001528 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001529# endif
1530# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001531 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001532# endif
1533# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001534 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001535# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001536 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001537
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001538 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001539# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001540 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 )
1543 return TRUE;
1544
1545 if (msec > 0)
1546 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 // If the specified wait time has passed, return. Beware that
1548 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001550 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 break;
1552 }
1553 if (msec != 0)
1554 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001555 DWORD dwWaitTime = dwEndTime - dwNow;
1556
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001557# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001558 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001559 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001560 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001561 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001562 // If there is readahead then parse_queued_messages() timed out
1563 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001564 if (channel_any_readahead())
1565 dwWaitTime = 10;
1566 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001567# endif
1568# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001569 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001570 // The 'balloonexpr' may indirectly invoke a callback while
1571 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001572 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001573# endif
1574# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001575 if (mzthreads_allowed() && p_mzq > 0
1576 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001577 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001578# endif
1579# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001580 // When waiting very briefly don't trigger timers.
1581 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001582 {
1583 long due_time;
1584
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001585 // Trigger timers and then get the time in msec until the next
1586 // one is due. Wait up to that time.
1587 due_time = check_due_timer();
1588 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001589 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001590 // timer may have used feedkeys().
1591 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001592 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001593 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1594 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001595 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001596# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001597 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001598# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001599 // Wait for either an event on the console input or a
1600 // message in the client-server window.
1601 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1602 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001603# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001604 wait_for_single_object(g_hConIn, dwWaitTime)
1605 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001606# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001607 )
1608 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
1611 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001612 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001614# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (State & CMDLINE && msg_row == Rows - 1)
1616 {
1617 CONSOLE_SCREEN_BUFFER_INFO csbi;
1618
1619 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1620 {
1621 if (csbi.dwCursorPosition.Y != msg_row)
1622 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001623 // The screen is now messed up, must redraw the
1624 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 redraw_all_later(CLEAR);
1626 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1627 redrawcmd();
1628 }
1629 }
1630 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 if (cRecords > 0)
1634 {
1635 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1636 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001637# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001638 // Windows IME sends two '\n's with only one 'ENTER'. First:
1639 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001640 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1642 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001643 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 continue;
1645 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1648 NULL, FALSE))
1649 return TRUE;
1650 }
1651
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001652 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (ir.EventType == FOCUS_EVENT)
1655 handle_focus_event(ir);
1656 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001657 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001658 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1659
Bram Moolenaar36698e32019-12-11 22:57:40 +01001660 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001661 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001662 if (dwSize.X != Columns || dwSize.Y != Rows)
1663 {
1664 CONSOLE_SCREEN_BUFFER_INFO csbi;
1665 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001666 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001667 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001668 if (dwSize.X != Columns || dwSize.Y != Rows)
1669 {
1670 ResizeConBuf(g_hConOut, dwSize);
1671 shell_resized();
1672 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001673 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (ir.EventType == MOUSE_EVENT
1676 && decode_mouse_event(&ir.Event.MouseEvent))
1677 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679 else if (msec == 0)
1680 break;
1681 }
1682
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001683# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001684 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (input_available())
1686 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001687# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return FALSE;
1690}
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * return non-zero if a character is available
1694 */
1695 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001696mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001698# ifdef VIMDLL
1699 if (gui.in_use)
1700 return TRUE;
1701# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001702 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001704
1705# if defined(FEAT_TERMINAL) || defined(PROTO)
1706/*
1707 * Check for any pending input or messages.
1708 */
1709 int
1710mch_check_messages(void)
1711{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001712# ifdef VIMDLL
1713 if (gui.in_use)
1714 return TRUE;
1715# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001716 return WaitForChar(0L, TRUE);
1717}
1718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720/*
1721 * Create the console input. Used when reading stdin doesn't work.
1722 */
1723 static void
1724create_conin(void)
1725{
1726 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1727 FILE_SHARE_READ|FILE_SHARE_WRITE,
1728 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001729 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 did_create_conin = TRUE;
1731}
1732
1733/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001734 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001736 static WCHAR
1737tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001739 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741 for (;;)
1742 {
1743 INPUT_RECORD ir;
1744 DWORD cRecords = 0;
1745
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001746# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001747 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (input_available())
1749 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (g_nMouseClick != -1)
1751 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001752# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001753 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 if (did_create_conin)
1756 read_error_exit();
1757 create_conin();
1758 continue;
1759 }
1760
1761 if (ir.EventType == KEY_EVENT)
1762 {
1763 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1764 pmodifiers, TRUE))
1765 return ch;
1766 }
1767 else if (ir.EventType == FOCUS_EVENT)
1768 handle_focus_event(ir);
1769 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1770 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else if (ir.EventType == MOUSE_EVENT)
1772 {
1773 if (decode_mouse_event(&ir.Event.MouseEvent))
1774 return 0;
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001778#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780
1781/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001782 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 * Get one or more characters from the keyboard or the mouse.
1784 * If time == 0, do not wait for characters.
1785 * If time == n, wait a short time for characters.
1786 * If time == -1, wait forever for characters.
1787 * Returns the number of characters read into buf.
1788 */
1789 int
1790mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001791 char_u *buf UNUSED,
1792 int maxlen UNUSED,
1793 long time UNUSED,
1794 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001796#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 int len;
1799 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001800# ifdef VIMDLL
1801// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1802# define TYPEAHEADSPACE 6
1803# else
1804# define TYPEAHEADSPACE 0
1805# endif
1806# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001807 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 static int typeaheadlen = 0;
1809
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001810# ifdef VIMDLL
1811 if (gui.in_use)
1812 return 0;
1813# endif
1814
Bram Moolenaar0f873732019-12-05 20:28:46 +01001815 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (typeaheadlen > 0)
1817 goto theend;
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (time >= 0)
1820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001821 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001824 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar3918c952005-03-15 22:34:55 +00001828 /*
1829 * If there is no character available within 2 seconds (default)
1830 * write the autoscript file to disk. Or cause the CursorHold event
1831 * to be triggered.
1832 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001833 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001835 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001837 buf[0] = K_SPECIAL;
1838 buf[1] = KS_EXTRA;
1839 buf[2] = (int)KE_CURSORHOLD;
1840 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001842 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
1845
1846 /*
1847 * Try to read as many characters as there are, until the buffer is full.
1848 */
1849
Bram Moolenaar0f873732019-12-05 20:28:46 +01001850 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 g_fCBrkPressed = FALSE;
1852
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001853# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (fdDump)
1855 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar0f873732019-12-05 20:28:46 +01001858 // Keep looping until there is something in the typeahead buffer and more
1859 // to get and still room in the buffer (up to two bytes for a char and
1860 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001861 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001862 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 if (typebuf_changed(tb_change_cnt))
1865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001866 // "buf" may be invalid now if a client put something in the
1867 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 typeaheadlen = 0;
1869 break;
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (g_nMouseClick != -1)
1872 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001873# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (fdDump)
1875 fprintf(fdDump, "{%02x @ %d, %d}",
1876 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 typeahead[typeaheadlen++] = ESC + 128;
1879 typeahead[typeaheadlen++] = 'M';
1880 typeahead[typeaheadlen++] = g_nMouseClick;
1881 typeahead[typeaheadlen++] = g_xMouse + '!';
1882 typeahead[typeaheadlen++] = g_yMouse + '!';
1883 g_nMouseClick = -1;
1884 }
1885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001887 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 int modifiers = 0;
1889
1890 c = tgetch(&modifiers, &ch2);
1891
1892 if (typebuf_changed(tb_change_cnt))
1893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001894 // "buf" may be invalid now if a client put something in the
1895 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 typeaheadlen = 0;
1897 break;
1898 }
1899
1900 if (c == Ctrl_C && ctrl_c_interrupts)
1901 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001902# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 got_int = TRUE;
1906 }
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 int n = 1;
1911
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001912 if (ch2 == NUL)
1913 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001914 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001915 char_u *p;
1916 WCHAR ch[2];
1917
1918 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001919 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001920 {
1921 ch[1] = tgetch(&modifiers, &ch2);
1922 n++;
1923 }
1924 p = utf16_to_enc(ch, &n);
1925 if (p != NULL)
1926 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001927 for (i = 0, j = 0; i < n; i++)
1928 {
1929 typeahead[typeaheadlen + j++] = p[i];
1930# ifdef VIMDLL
1931 if (p[i] == CSI)
1932 {
1933 typeahead[typeaheadlen + j++] = KS_EXTRA;
1934 typeahead[typeaheadlen + j++] = KE_CSI;
1935 }
1936# endif
1937 }
1938 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001939 vim_free(p);
1940 }
1941 }
1942 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001943 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001944 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001945# ifdef VIMDLL
1946 if (c == CSI)
1947 {
1948 typeahead[typeaheadlen + 1] = KS_EXTRA;
1949 typeahead[typeaheadlen + 2] = KE_CSI;
1950 n = 3;
1951 }
1952# endif
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (ch2 != NUL)
1955 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001956 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001957 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001958 switch (ch2)
1959 {
1960 case (WCHAR)'\324': // SHIFT+Insert
1961 case (WCHAR)'\325': // CTRL+Insert
1962 case (WCHAR)'\327': // SHIFT+Delete
1963 case (WCHAR)'\330': // CTRL+Delete
1964 typeahead[typeaheadlen + n] = (char_u)ch2;
1965 n++;
1966 break;
1967
1968 default:
1969 typeahead[typeaheadlen + n] = 3;
1970 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1971 n += 2;
1972 break;
1973 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001974 }
1975 else
1976 {
1977 typeahead[typeaheadlen + n] = 3;
1978 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1979 n += 2;
1980 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001981 }
1982
Bram Moolenaar0f873732019-12-05 20:28:46 +01001983 // Use the ALT key to set the 8th bit of the character
1984 // when it's one byte, the 8th bit isn't set yet and not
1985 // using a double-byte encoding (would become a lead
1986 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if ((modifiers & MOD_MASK_ALT)
1988 && n == 1
1989 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 )
1992 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001993 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1994 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 modifiers &= ~MOD_MASK_ALT;
1996 }
1997
1998 if (modifiers != 0)
1999 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002000 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 mch_memmove(typeahead + typeaheadlen + 3,
2002 typeahead + typeaheadlen, n);
2003 typeahead[typeaheadlen++] = K_SPECIAL;
2004 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2005 typeahead[typeaheadlen++] = modifiers;
2006 }
2007
2008 typeaheadlen += n;
2009
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002010# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (fdDump)
2012 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 }
2016 }
2017
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002018# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (fdDump)
2020 {
2021 fputs("]\n", fdDump);
2022 fflush(fdDump);
2023 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002027 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 len = 0;
2029 while (len < maxlen && typeaheadlen > 0)
2030 {
2031 buf[len++] = typeahead[0];
2032 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2033 }
2034 return len;
2035
Bram Moolenaar0f873732019-12-05 20:28:46 +01002036#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002038#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
Bram Moolenaar82881492012-11-20 16:53:39 +01002041#ifndef PROTO
2042# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002043# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002047/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002048 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2049 * If "use_path" is FALSE: Return TRUE if "name" exists.
2050 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2051 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002052 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002055executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002057 WCHAR *p;
2058 WCHAR fnamew[_MAX_PATH];
2059 WCHAR *dumw;
2060 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002061 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaar43663192017-03-05 14:29:12 +01002063 if (!use_path)
2064 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002065 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002066 {
2067 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002068 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002069 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002070 *path = vim_strsave((char_u *)name);
2071 else
2072 *path = FullName_save((char_u *)name, FALSE);
2073 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002074 return TRUE;
2075 }
2076 return FALSE;
2077 }
2078
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002079 p = enc_to_utf16((char_u *)name, NULL);
2080 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002081 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002082
2083 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002084 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002085 if (wnewpath == NULL)
2086 return FALSE;
2087 wcscpy(wnewpath, L".;");
2088 wcscat(wnewpath, wcurpath);
2089 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2090 vim_free(wnewpath);
2091 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002092 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002093 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002094 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002095 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002096 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002097 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002098 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
2100
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002101#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2102 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002103/*
2104 * Bad parameter handler.
2105 *
2106 * Certain MS CRT functions will intentionally crash when passed invalid
2107 * parameters to highlight possible security holes. Setting this function as
2108 * the bad parameter handler will prevent the crash.
2109 *
2110 * In debug builds the parameters contain CRT information that might help track
2111 * down the source of a problem, but in non-debug builds the arguments are all
2112 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2113 * worth allowing these to make debugging of issues easier.
2114 */
2115 static void
2116bad_param_handler(const wchar_t *expression,
2117 const wchar_t *function,
2118 const wchar_t *file,
2119 unsigned int line,
2120 uintptr_t pReserved)
2121{
2122}
2123
2124# define SET_INVALID_PARAM_HANDLER \
2125 ((void)_set_invalid_parameter_handler(bad_param_handler))
2126#else
2127# define SET_INVALID_PARAM_HANDLER
2128#endif
2129
Bram Moolenaar4f974752019-02-17 17:44:42 +01002130#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132/*
2133 * GUI version of mch_init().
2134 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002135 static void
2136mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002138# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaar0f873732019-12-05 20:28:46 +01002142 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002143 SET_INVALID_PARAM_HANDLER;
2144
Bram Moolenaar0f873732019-12-05 20:28:46 +01002145 // Let critical errors result in a failure, not in a dialog box. Required
2146 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 SetErrorMode(SEM_FAILCRITICALERRORS);
2148
Bram Moolenaar0f873732019-12-05 20:28:46 +01002149 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar0f873732019-12-05 20:28:46 +01002151 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 Rows = 25;
2153 Columns = 80;
2154
Bram Moolenaar0f873732019-12-05 20:28:46 +01002155 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 char_u vimrun_location[_MAX_PATH + 4];
2158
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 STRCPY(vimrun_location, exe_name);
2161 STRCPY(gettail(vimrun_location), "vimrun.exe");
2162 if (mch_getperm(vimrun_location) >= 0)
2163 {
2164 if (*skiptowhite(vimrun_location) != NUL)
2165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002166 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 mch_memmove(vimrun_location + 1, vimrun_location,
2168 STRLEN(vimrun_location) + 1);
2169 *vimrun_location = '"';
2170 STRCPY(gettail(vimrun_location), "vimrun\" ");
2171 }
2172 else
2173 STRCPY(gettail(vimrun_location), "vimrun ");
2174
2175 vimrun_path = (char *)vim_strsave(vimrun_location);
2176 s_dont_use_vimrun = FALSE;
2177 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002178 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 s_dont_use_vimrun = FALSE;
2180
Bram Moolenaar0f873732019-12-05 20:28:46 +01002181 // Don't give the warning for a missing vimrun.exe right now, but only
2182 // when vimrun was supposed to be used. Don't bother people that do
2183 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (s_dont_use_vimrun)
2185 need_vimrun_warning = TRUE;
2186 }
2187
2188 /*
2189 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2190 * Otherwise the default "findstr /n" is used.
2191 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002192 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2194
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002195# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002196 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002197# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002198
2199 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200}
2201
2202
Bram Moolenaar0f873732019-12-05 20:28:46 +01002203#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002204
2205#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002207# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2208# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210/*
2211 * ClearConsoleBuffer()
2212 * Description:
2213 * Clears the entire contents of the console screen buffer, using the
2214 * specified attribute.
2215 * Returns:
2216 * TRUE on success
2217 */
2218 static BOOL
2219ClearConsoleBuffer(WORD wAttribute)
2220{
2221 CONSOLE_SCREEN_BUFFER_INFO csbi;
2222 COORD coord;
2223 DWORD NumCells, dummy;
2224
2225 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2226 return FALSE;
2227
2228 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2229 coord.X = 0;
2230 coord.Y = 0;
2231 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2232 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2235 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 return TRUE;
2239}
2240
2241/*
2242 * FitConsoleWindow()
2243 * Description:
2244 * Checks if the console window will fit within given buffer dimensions.
2245 * Also, if requested, will shrink the window to fit.
2246 * Returns:
2247 * TRUE on success
2248 */
2249 static BOOL
2250FitConsoleWindow(
2251 COORD dwBufferSize,
2252 BOOL WantAdjust)
2253{
2254 CONSOLE_SCREEN_BUFFER_INFO csbi;
2255 COORD dwWindowSize;
2256 BOOL NeedAdjust = FALSE;
2257
2258 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2259 {
2260 /*
2261 * A buffer resize will fail if the current console window does
2262 * not lie completely within that buffer. To avoid this, we might
2263 * have to move and possibly shrink the window.
2264 */
2265 if (csbi.srWindow.Right >= dwBufferSize.X)
2266 {
2267 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2268 if (dwWindowSize.X > dwBufferSize.X)
2269 dwWindowSize.X = dwBufferSize.X;
2270 csbi.srWindow.Right = dwBufferSize.X - 1;
2271 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2272 NeedAdjust = TRUE;
2273 }
2274 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2275 {
2276 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2277 if (dwWindowSize.Y > dwBufferSize.Y)
2278 dwWindowSize.Y = dwBufferSize.Y;
2279 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2280 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2281 NeedAdjust = TRUE;
2282 }
2283 if (NeedAdjust && WantAdjust)
2284 {
2285 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2286 return FALSE;
2287 }
2288 return TRUE;
2289 }
2290
2291 return FALSE;
2292}
2293
2294typedef struct ConsoleBufferStruct
2295{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002296 BOOL IsValid;
2297 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002298 PCHAR_INFO Buffer;
2299 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002300 PSMALL_RECT Regions;
2301 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302} ConsoleBuffer;
2303
2304/*
2305 * SaveConsoleBuffer()
2306 * Description:
2307 * Saves important information about the console buffer, including the
2308 * actual buffer contents. The saved information is suitable for later
2309 * restoration by RestoreConsoleBuffer().
2310 * Returns:
2311 * TRUE if all information was saved; FALSE otherwise
2312 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2313 */
2314 static BOOL
2315SaveConsoleBuffer(
2316 ConsoleBuffer *cb)
2317{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002318 DWORD NumCells;
2319 COORD BufferCoord;
2320 SMALL_RECT ReadRegion;
2321 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002322 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (cb == NULL)
2325 return FALSE;
2326
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002327 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 cb->IsValid = FALSE;
2330 return FALSE;
2331 }
2332 cb->IsValid = TRUE;
2333
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002334 /*
2335 * Allocate a buffer large enough to hold the entire console screen
2336 * buffer. If this ConsoleBuffer structure has already been initialized
2337 * with a buffer of the correct size, then just use that one.
2338 */
2339 if (!cb->IsValid || cb->Buffer == NULL ||
2340 cb->BufferSize.X != cb->Info.dwSize.X ||
2341 cb->BufferSize.Y != cb->Info.dwSize.Y)
2342 {
2343 cb->BufferSize.X = cb->Info.dwSize.X;
2344 cb->BufferSize.Y = cb->Info.dwSize.Y;
2345 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2346 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002347 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002348 if (cb->Buffer == NULL)
2349 return FALSE;
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002353 * We will now copy the console screen buffer into our buffer.
2354 * ReadConsoleOutput() seems to be limited as far as how much you
2355 * can read at a time. Empirically, this number seems to be about
2356 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2357 * in chunks until it is all copied. The chunks will all have the
2358 * same horizontal characteristics, so initialize them now. The
2359 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002361 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002362 ReadRegion.Left = 0;
2363 ReadRegion.Right = cb->Info.dwSize.X - 1;
2364 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002365
2366 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2367 if (cb->Regions == NULL || numregions != cb->NumRegions)
2368 {
2369 cb->NumRegions = numregions;
2370 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002371 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002372 if (cb->Regions == NULL)
2373 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002374 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002375 return FALSE;
2376 }
2377 }
2378
2379 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002381 /*
2382 * Read into position (0, Y) in our buffer.
2383 */
2384 BufferCoord.Y = Y;
2385 /*
2386 * Read the region whose top left corner is (0, Y) and whose bottom
2387 * right corner is (width - 1, Y + Y_incr - 1). This should define
2388 * a region of size width by Y_incr. Don't worry if this region is
2389 * too large for the remaining buffer; it will be cropped.
2390 */
2391 ReadRegion.Top = Y;
2392 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002393 if (!ReadConsoleOutputW(g_hConOut, // output handle
2394 cb->Buffer, // our buffer
2395 cb->BufferSize, // dimensions of our buffer
2396 BufferCoord, // offset in our buffer
2397 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002398 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002399 VIM_CLEAR(cb->Buffer);
2400 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002401 return FALSE;
2402 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002403 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405
2406 return TRUE;
2407}
2408
2409/*
2410 * RestoreConsoleBuffer()
2411 * Description:
2412 * Restores important information about the console buffer, including the
2413 * actual buffer contents, if desired. The information to restore is in
2414 * the same format used by SaveConsoleBuffer().
2415 * Returns:
2416 * TRUE on success
2417 */
2418 static BOOL
2419RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002420 ConsoleBuffer *cb,
2421 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002423 COORD BufferCoord;
2424 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002425 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 if (cb == NULL || !cb->IsValid)
2428 return FALSE;
2429
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002430 /*
2431 * Before restoring the buffer contents, clear the current buffer, and
2432 * restore the cursor position and window information. Doing this now
2433 * prevents old buffer contents from "flashing" onto the screen.
2434 */
2435 if (RestoreScreen)
2436 ClearConsoleBuffer(cb->Info.wAttributes);
2437
2438 FitConsoleWindow(cb->Info.dwSize, TRUE);
2439 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2440 return FALSE;
2441 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2442 return FALSE;
2443
2444 if (!RestoreScreen)
2445 {
2446 /*
2447 * No need to restore the screen buffer contents, so we're done.
2448 */
2449 return TRUE;
2450 }
2451
2452 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2453 return FALSE;
2454 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2455 return FALSE;
2456
2457 /*
2458 * Restore the screen buffer contents.
2459 */
2460 if (cb->Buffer != NULL)
2461 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002462 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002463 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002464 BufferCoord.X = cb->Regions[i].Left;
2465 BufferCoord.Y = cb->Regions[i].Top;
2466 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002467 if (!WriteConsoleOutputW(g_hConOut, // output handle
2468 cb->Buffer, // our buffer
2469 cb->BufferSize, // dimensions of our buffer
2470 BufferCoord, // offset in our buffer
2471 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002472 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002473 }
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 return TRUE;
2477}
2478
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002479# define FEAT_RESTORE_ORIG_SCREEN
2480# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002481static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483static ConsoleBuffer g_cbNonTermcap = { 0 };
2484static ConsoleBuffer g_cbTermcap = { 0 };
2485
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002486# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002488HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489static HICON g_hOrigIconSmall = NULL;
2490static HICON g_hOrigIcon = NULL;
2491static HICON g_hVimIcon = NULL;
2492static BOOL g_fCanChangeIcon = FALSE;
2493
Bram Moolenaar0f873732019-12-05 20:28:46 +01002494// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002495# ifndef ICON_SMALL
2496# define ICON_SMALL 0
2497# endif
2498# ifndef ICON_BIG
2499# define ICON_BIG 1
2500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * GetConsoleIcon()
2503 * Description:
2504 * Attempts to retrieve the small icon and/or the big icon currently in
2505 * use by a given window.
2506 * Returns:
2507 * TRUE on success
2508 */
2509 static BOOL
2510GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002511 HWND hWnd,
2512 HICON *phIconSmall,
2513 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 if (hWnd == NULL)
2516 return FALSE;
2517
2518 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002519 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2520 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002522 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2523 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 return TRUE;
2525}
2526
2527/*
2528 * SetConsoleIcon()
2529 * Description:
2530 * Attempts to change the small icon and/or the big icon currently in
2531 * use by a given window.
2532 * Returns:
2533 * TRUE on success
2534 */
2535 static BOOL
2536SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002537 HWND hWnd,
2538 HICON hIconSmall,
2539 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (hWnd == NULL)
2542 return FALSE;
2543
2544 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002545 SendMessage(hWnd, WM_SETICON,
2546 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002548 SendMessage(hWnd, WM_SETICON,
2549 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return TRUE;
2551}
2552
2553/*
2554 * SaveConsoleTitleAndIcon()
2555 * Description:
2556 * Saves the current console window title in g_szOrigTitle, for later
2557 * restoration. Also, attempts to obtain a handle to the console window,
2558 * and use it to save the small and big icons currently in use by the
2559 * console window. This is not always possible on some versions of Windows;
2560 * nor is it possible when running Vim remotely using Telnet (since the
2561 * console window the user sees is owned by a remote process).
2562 */
2563 static void
2564SaveConsoleTitleAndIcon(void)
2565{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2568 return;
2569
2570 /*
2571 * Obtain a handle to the console window using GetConsoleWindow() from
2572 * KERNEL32.DLL; we need to handle in order to change the window icon.
2573 * This function only exists on NT-based Windows, starting with Windows
2574 * 2000. On older operating systems, we can't change the window icon
2575 * anyway.
2576 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002577 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (g_hWnd == NULL)
2579 return;
2580
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2583 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2584 return;
2585
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002587 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002588 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (g_hVimIcon != NULL)
2590 g_fCanChangeIcon = TRUE;
2591}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594static int g_fWindInitCalled = FALSE;
2595static int g_fTermcapMode = FALSE;
2596static CONSOLE_CURSOR_INFO g_cci;
2597static DWORD g_cmodein = 0;
2598static DWORD g_cmodeout = 0;
2599
2600/*
2601 * non-GUI version of mch_init().
2602 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002603 static void
2604mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002606# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002607 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002608# endif
2609# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002614 SET_INVALID_PARAM_HANDLER;
2615
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Let critical errors result in a failure, not in a dialog box. Required
2617 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 SetErrorMode(SEM_FAILCRITICALERRORS);
2619
Bram Moolenaar0f873732019-12-05 20:28:46 +01002620 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 out_flush();
2622
Bram Moolenaar0f873732019-12-05 20:28:46 +01002623 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (read_cmd_fd == 0)
2625 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2626 else
2627 create_conin();
2628 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2629
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002630# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002631 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002632 SaveConsoleBuffer(&g_cbOrig);
2633 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002635 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002636 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2637 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (cterm_normal_fg_color == 0)
2640 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2641 if (cterm_normal_bg_color == 0)
2642 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2643
Bram Moolenaarbdace832019-03-02 10:13:42 +01002644 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002645 g_color_index_fg = g_attrDefault & 0xf;
2646 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2647
Bram Moolenaar0f873732019-12-05 20:28:46 +01002648 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 update_tcap(g_attrCurrent);
2650
2651 GetConsoleCursorInfo(g_hConOut, &g_cci);
2652 GetConsoleMode(g_hConIn, &g_cmodein);
2653 GetConsoleMode(g_hConOut, &g_cmodeout);
2654
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002655# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 SaveConsoleTitleAndIcon();
2657 /*
2658 * Set both the small and big icons of the console window to Vim's icon.
2659 * Note that Vim presently only has one size of icon (32x32), but it
2660 * automatically gets scaled down to 16x16 when setting the small icon.
2661 */
2662 if (g_fCanChangeIcon)
2663 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 ui_get_shellsize();
2667
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002668# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 fdDump = fopen("dump", "wt");
2670
2671 if (fdDump)
2672 {
2673 time_t t;
2674
2675 time(&t);
2676 fputs(ctime(&t), fdDump);
2677 fflush(fdDump);
2678 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 g_fWindInitCalled = TRUE;
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002685# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002686 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002687# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002688
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002689 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002690 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691}
2692
2693/*
2694 * non-GUI version of mch_exit().
2695 * Shut down and exit with status `r'
2696 * Careful: mch_exit() may be called before mch_init()!
2697 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002698 static void
2699mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002701 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002703 vtp_exit();
2704
Bram Moolenaar955f1982017-02-05 15:10:51 +01002705 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (g_fWindInitCalled)
2707 settmode(TMODE_COOK);
2708
Bram Moolenaar0f873732019-12-05 20:28:46 +01002709 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 if (g_fWindInitCalled)
2712 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002713# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002714 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /*
2716 * Restore both the small and big icons of the console window to
2717 * what they were at startup. Don't do this when the window is
2718 * closed, Vim would hang here.
2719 */
2720 if (g_fCanChangeIcon && !g_fForceExit)
2721 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002724# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (fdDump)
2726 {
2727 time_t t;
2728
2729 time(&t);
2730 fputs(ctime(&t), fdDump);
2731 fclose(fdDump);
2732 }
2733 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 SetConsoleCursorInfo(g_hConOut, &g_cci);
2738 SetConsoleMode(g_hConIn, g_cmodein);
2739 SetConsoleMode(g_hConOut, g_cmodeout);
2740
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002741# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 exit(r);
2746}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002747#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002749 void
2750mch_init(void)
2751{
2752#ifdef VIMDLL
2753 if (gui.starting)
2754 mch_init_g();
2755 else
2756 mch_init_c();
2757#elif defined(FEAT_GUI_MSWIN)
2758 mch_init_g();
2759#else
2760 mch_init_c();
2761#endif
2762}
2763
2764 void
2765mch_exit(int r)
2766{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002767#ifdef FEAT_NETBEANS_INTG
2768 netbeans_send_disconnect();
2769#endif
2770
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002771#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002772 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002773 mch_exit_g(r);
2774 else
2775 mch_exit_c(r);
2776#elif defined(FEAT_GUI_MSWIN)
2777 mch_exit_g(r);
2778#else
2779 mch_exit_c(r);
2780#endif
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Do we have an interactive window?
2785 */
2786 int
2787mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002788 int argc UNUSED,
2789 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 get_exe_name();
2792
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002793#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002796# ifdef VIMDLL
2797 if (gui.in_use)
2798 return OK;
2799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (isatty(1))
2801 return OK;
2802 return FAIL;
2803#endif
2804}
2805
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002806/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002807 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 * When "len" is > 0, also expand short to long filenames.
2809 */
2810 void
2811fname_case(
2812 char_u *name,
2813 int len)
2814{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002815 int flen;
2816 WCHAR *p;
2817 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002819 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002820 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return;
2822
2823 slash_adjust(name);
2824
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002825 p = enc_to_utf16(name, NULL);
2826 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002827 return;
2828
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002829 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002831 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002833 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002835 if (len > 0 || flen >= (int)STRLEN(q))
2836 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2837 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002840 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841}
2842
2843
2844/*
2845 * Insert user name in s[len].
2846 */
2847 int
2848mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002849 char_u *s,
2850 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002852 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002853 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002855 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002856 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002857 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002858
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002859 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002860 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002861 vim_strncpy(s, p, len - 1);
2862 vim_free(p);
2863 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002864 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 s[0] = NUL;
2867 return FAIL;
2868}
2869
2870
2871/*
2872 * Insert host name in s[len].
2873 */
2874 void
2875mch_get_host_name(
2876 char_u *s,
2877 int len)
2878{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002879 WCHAR wszHostName[256 + 1];
2880 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002882 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002883 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002884 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002885
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002886 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002887 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002888 vim_strncpy(s, p, len - 1);
2889 vim_free(p);
2890 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002891 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893}
2894
2895
2896/*
2897 * return process ID
2898 */
2899 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002900mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 return (long)GetCurrentProcessId();
2903}
2904
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002905/*
2906 * return TRUE if process "pid" is still running
2907 */
2908 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002909mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002910{
2911 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2912 DWORD status = 0;
2913 int ret = FALSE;
2914
2915 if (hProcess == NULL)
2916 return FALSE; // might not have access
2917 if (GetExitCodeProcess(hProcess, &status) )
2918 ret = status == STILL_ACTIVE;
2919 CloseHandle(hProcess);
2920 return ret;
2921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923/*
2924 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2925 * Return OK for success, FAIL for failure.
2926 */
2927 int
2928mch_dirname(
2929 char_u *buf,
2930 int len)
2931{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002932 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 /*
2935 * Originally this was:
2936 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2937 * But the Win32s known bug list says that getcwd() doesn't work
2938 * so use the Win32 system call instead. <Negri>
2939 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002940 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002942 WCHAR wcbuf[_MAX_PATH + 1];
2943 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002945 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002947 p = utf16_to_enc(wcbuf, NULL);
2948 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002949 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002950 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002952 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002955 if (p == NULL)
2956 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002957
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002958 if (p != NULL)
2959 {
2960 vim_strncpy(buf, p, len - 1);
2961 vim_free(p);
2962 return OK;
2963 }
2964 }
2965 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966}
2967
2968/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002969 * Get file permissions for "name".
2970 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
2972 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002973mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002975 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002976 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002978 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002979 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982
2983/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002984 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002985 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002986 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
2988 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002989mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002991 long n;
2992 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002993
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002994 p = enc_to_utf16(name, NULL);
2995 if (p == NULL)
2996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002998 n = _wchmod(p, perm);
2999 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003000 if (n == -1)
3001 return FAIL;
3002
3003 win32_set_archive(name);
3004
3005 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008/*
3009 * Set hidden flag for "name".
3010 */
3011 void
3012mch_hide(char_u *name)
3013{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003014 int attrs = win32_getattrs(name);
3015 if (attrs == -1)
3016 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003018 attrs |= FILE_ATTRIBUTE_HIDDEN;
3019 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003023 * Return TRUE if file "name" exists and is hidden.
3024 */
3025 int
3026mch_ishidden(char_u *name)
3027{
3028 int f = win32_getattrs(name);
3029
3030 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003032
3033 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3034}
3035
3036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 * return TRUE if "name" is a directory
3038 * return FALSE if "name" is not a directory or upon error
3039 */
3040 int
3041mch_isdir(char_u *name)
3042{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003043 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003046 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
3048 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3049}
3050
3051/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003052 * return TRUE if "name" is a directory, NOT a symlink to a directory
3053 * return FALSE if "name" is not a directory
3054 * return FALSE for error
3055 */
3056 int
3057mch_isrealdir(char_u *name)
3058{
3059 return mch_isdir(name) && !mch_is_symbolic_link(name);
3060}
3061
3062/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003063 * Create directory "name".
3064 * Return 0 on success, -1 on error.
3065 */
3066 int
3067mch_mkdir(char_u *name)
3068{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003069 WCHAR *p;
3070 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003071
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003072 p = enc_to_utf16(name, NULL);
3073 if (p == NULL)
3074 return -1;
3075 retval = _wmkdir(p);
3076 vim_free(p);
3077 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003078}
3079
3080/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003081 * Delete directory "name".
3082 * Return 0 on success, -1 on error.
3083 */
3084 int
3085mch_rmdir(char_u *name)
3086{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003087 WCHAR *p;
3088 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003089
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003090 p = enc_to_utf16(name, NULL);
3091 if (p == NULL)
3092 return -1;
3093 retval = _wrmdir(p);
3094 vim_free(p);
3095 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003096}
3097
3098/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003099 * Return TRUE if file "fname" has more than one link.
3100 */
3101 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003102mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003103{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003104 BY_HANDLE_FILE_INFORMATION info;
3105
3106 return win32_fileinfo(fname, &info) == FILEINFO_OK
3107 && info.nNumberOfLinks > 1;
3108}
3109
3110/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003111 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003112 */
3113 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003114mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003115{
3116 HANDLE hFind;
3117 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003118 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003119 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003120 WIN32_FIND_DATAW findDataW;
3121
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003122 wn = enc_to_utf16(name, NULL);
3123 if (wn == NULL)
3124 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003125
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003126 hFind = FindFirstFileW(wn, &findDataW);
3127 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003128 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003129 {
3130 fileFlags = findDataW.dwFileAttributes;
3131 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003132 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003133 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003134
3135 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003136 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3137 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003138 res = TRUE;
3139
3140 return res;
3141}
3142
3143/*
3144 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3145 * link.
3146 */
3147 int
3148mch_is_linked(char_u *fname)
3149{
3150 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3151 return TRUE;
3152 return FALSE;
3153}
3154
3155/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003156 * Get the by-handle-file-information for "fname".
3157 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003158 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003159 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3160 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3161 */
3162 int
3163win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3164{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003165 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003166 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003167 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003168
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003169 wn = enc_to_utf16(fname, NULL);
3170 if (wn == NULL)
3171 return FILEINFO_ENC_FAIL;
3172
3173 hFile = CreateFileW(wn, // file name
3174 GENERIC_READ, // access mode
3175 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3176 NULL, // security descriptor
3177 OPEN_EXISTING, // creation disposition
3178 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3179 NULL); // handle to template file
3180 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003181
3182 if (hFile != INVALID_HANDLE_VALUE)
3183 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003184 if (GetFileInformationByHandle(hFile, info) != 0)
3185 res = FILEINFO_OK;
3186 else
3187 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003188 CloseHandle(hFile);
3189 }
3190
Bram Moolenaar03f48552006-02-28 23:52:23 +00003191 return res;
3192}
3193
3194/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003195 * get file attributes for `name'
3196 * -1 : error
3197 * else FILE_ATTRIBUTE_* defined in winnt.h
3198 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003199 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003200win32_getattrs(char_u *name)
3201{
3202 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003203 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003204
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003205 p = enc_to_utf16(name, NULL);
3206 if (p == NULL)
3207 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003208
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003209 attr = GetFileAttributesW(p);
3210 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003211
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003212 return attr;
3213}
3214
3215/*
3216 * set file attributes for `name' to `attrs'
3217 *
3218 * return -1 for failure, 0 otherwise
3219 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003220 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003221win32_setattrs(char_u *name, int attrs)
3222{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003223 int res;
3224 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003225
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003226 p = enc_to_utf16(name, NULL);
3227 if (p == NULL)
3228 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003229
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003230 res = SetFileAttributesW(p, attrs);
3231 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003232
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003233 return res ? 0 : -1;
3234}
3235
3236/*
3237 * Set archive flag for "name".
3238 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003239 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003240win32_set_archive(char_u *name)
3241{
3242 int attrs = win32_getattrs(name);
3243 if (attrs == -1)
3244 return -1;
3245
3246 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3247 return win32_setattrs(name, attrs);
3248}
3249
3250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Return TRUE if file or directory "name" is writable (not readonly).
3252 * Strange semantics of Win32: a readonly directory is writable, but you can't
3253 * delete a file. Let's say this means it is writable.
3254 */
3255 int
3256mch_writable(char_u *name)
3257{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003258 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003260 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3261 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003265 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003266 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003267 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3268 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 */
3270 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003271mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar86621892019-03-30 21:51:28 +01003273 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3274 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3275 // UTF-8.
3276 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003277 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003278 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003279
Bram Moolenaar86621892019-03-30 21:51:28 +01003280 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003281 return FALSE;
3282
Bram Moolenaar86621892019-03-30 21:51:28 +01003283 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003284 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003285 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003286 return TRUE;
3287
3288 /*
3289 * Loop over all extensions in $PATHEXT.
3290 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003291 p = mch_getenv("PATHEXT");
3292 if (p == NULL)
3293 p = (char_u *)".com;.exe;.bat;.cmd";
3294 saved = vim_strsave(p);
3295 if (saved == NULL)
3296 return FALSE;
3297 p = saved;
3298 while (*p)
3299 {
3300 char_u *tmp = vim_strchr(p, ';');
3301
3302 if (tmp != NULL)
3303 *tmp = NUL;
3304 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3305 && executable_exists((char *)name, path, use_path))
3306 {
3307 vim_free(saved);
3308 return TRUE;
3309 }
3310 if (tmp == NULL)
3311 break;
3312 p = tmp + 1;
3313 }
3314 vim_free(saved);
3315
Bram Moolenaar86621892019-03-30 21:51:28 +01003316 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003317 p = mch_getenv("PATHEXT");
3318 if (p == NULL)
3319 p = (char_u *)".com;.exe;.bat;.cmd";
3320 while (*p)
3321 {
3322 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3323 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003324 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003325 buf[len] = NUL;
3326 ++p;
3327 if (*p)
3328 ++p;
3329 }
3330 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003331 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003332 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003333 return TRUE;
3334 }
3335 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338/*
3339 * Check what "name" is:
3340 * NODE_NORMAL: file or directory (or doesn't exist)
3341 * NODE_WRITABLE: writable device, socket, fifo, etc.
3342 * NODE_OTHER: non-writable things
3343 */
3344 int
3345mch_nodetype(char_u *name)
3346{
3347 HANDLE hFile;
3348 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003349 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar0f873732019-12-05 20:28:46 +01003351 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3352 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3353 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003354 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3355 return NODE_WRITABLE;
3356
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003357 wn = enc_to_utf16(name, NULL);
3358 if (wn == NULL)
3359 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003360
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003361 hFile = CreateFileW(wn, // file name
3362 GENERIC_WRITE, // access mode
3363 0, // share mode
3364 NULL, // security descriptor
3365 OPEN_EXISTING, // creation disposition
3366 0, // file attributes
3367 NULL); // handle to template file
3368 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (hFile == INVALID_HANDLE_VALUE)
3370 return NODE_NORMAL;
3371
3372 type = GetFileType(hFile);
3373 CloseHandle(hFile);
3374 if (type == FILE_TYPE_CHAR)
3375 return NODE_WRITABLE;
3376 if (type == FILE_TYPE_DISK)
3377 return NODE_NORMAL;
3378 return NODE_OTHER;
3379}
3380
3381#ifdef HAVE_ACL
3382struct my_acl
3383{
3384 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3385 PSID pSidOwner;
3386 PSID pSidGroup;
3387 PACL pDacl;
3388 PACL pSacl;
3389};
3390#endif
3391
3392/*
3393 * Return a pointer to the ACL of file "fname" in allocated memory.
3394 * Return NULL if the ACL is not available for whatever reason.
3395 */
3396 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003397mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399#ifndef HAVE_ACL
3400 return (vim_acl_T)NULL;
3401#else
3402 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003403 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003405 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003406 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003408 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003410 wn = enc_to_utf16(fname, NULL);
3411 if (wn == NULL)
3412 return NULL;
3413
3414 // Try to retrieve the entire security descriptor.
3415 err = GetNamedSecurityInfoW(
3416 wn, // Abstract filename
3417 SE_FILE_OBJECT, // File Object
3418 OWNER_SECURITY_INFORMATION |
3419 GROUP_SECURITY_INFORMATION |
3420 DACL_SECURITY_INFORMATION |
3421 SACL_SECURITY_INFORMATION,
3422 &p->pSidOwner, // Ownership information.
3423 &p->pSidGroup, // Group membership.
3424 &p->pDacl, // Discretionary information.
3425 &p->pSacl, // For auditing purposes.
3426 &p->pSecurityDescriptor);
3427 if (err == ERROR_ACCESS_DENIED ||
3428 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003429 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003430 // Retrieve only DACL.
3431 (void)GetNamedSecurityInfoW(
3432 wn,
3433 SE_FILE_OBJECT,
3434 DACL_SECURITY_INFORMATION,
3435 NULL,
3436 NULL,
3437 &p->pDacl,
3438 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003439 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003440 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003441 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003442 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003443 mch_free_acl((vim_acl_T)p);
3444 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003446 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448
3449 return (vim_acl_T)p;
3450#endif
3451}
3452
Bram Moolenaar27515922013-06-29 15:36:26 +02003453#ifdef HAVE_ACL
3454/*
3455 * Check if "acl" contains inherited ACE.
3456 */
3457 static BOOL
3458is_acl_inherited(PACL acl)
3459{
3460 DWORD i;
3461 ACL_SIZE_INFORMATION acl_info;
3462 PACCESS_ALLOWED_ACE ace;
3463
3464 acl_info.AceCount = 0;
3465 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3466 for (i = 0; i < acl_info.AceCount; i++)
3467 {
3468 GetAce(acl, i, (LPVOID *)&ace);
3469 if (ace->Header.AceFlags & INHERITED_ACE)
3470 return TRUE;
3471 }
3472 return FALSE;
3473}
3474#endif
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476/*
3477 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3478 * Errors are ignored.
3479 * This must only be called with "acl" equal to what mch_get_acl() returned.
3480 */
3481 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003482mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484#ifdef HAVE_ACL
3485 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003486 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003487 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003489 if (p == NULL)
3490 return;
3491
3492 wn = enc_to_utf16(fname, NULL);
3493 if (wn == NULL)
3494 return;
3495
3496 // Set security flags
3497 if (p->pSidOwner)
3498 sec_info |= OWNER_SECURITY_INFORMATION;
3499 if (p->pSidGroup)
3500 sec_info |= GROUP_SECURITY_INFORMATION;
3501 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003502 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003503 sec_info |= DACL_SECURITY_INFORMATION;
3504 // Do not inherit its parent's DACL.
3505 // If the DACL is inherited, Cygwin permissions would be changed.
3506 if (!is_acl_inherited(p->pDacl))
3507 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003508 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003509 if (p->pSacl)
3510 sec_info |= SACL_SECURITY_INFORMATION;
3511
3512 (void)SetNamedSecurityInfoW(
3513 wn, // Abstract filename
3514 SE_FILE_OBJECT, // File Object
3515 sec_info,
3516 p->pSidOwner, // Ownership information.
3517 p->pSidGroup, // Group membership.
3518 p->pDacl, // Discretionary information.
3519 p->pSacl // For auditing purposes.
3520 );
3521 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523}
3524
3525 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003526mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528#ifdef HAVE_ACL
3529 struct my_acl *p = (struct my_acl *)acl;
3530
3531 if (p != NULL)
3532 {
3533 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3534 vim_free(p);
3535 }
3536#endif
3537}
3538
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003539#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541/*
3542 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3543 */
3544 static BOOL WINAPI
3545handler_routine(
3546 DWORD dwCtrlType)
3547{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003548 INPUT_RECORD ir;
3549 DWORD out;
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 switch (dwCtrlType)
3552 {
3553 case CTRL_C_EVENT:
3554 if (ctrl_c_interrupts)
3555 g_fCtrlCPressed = TRUE;
3556 return TRUE;
3557
3558 case CTRL_BREAK_EVENT:
3559 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003560 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003561 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003562 ir.EventType = KEY_EVENT;
3563 ir.Event.KeyEvent.bKeyDown = TRUE;
3564 ir.Event.KeyEvent.wRepeatCount = 1;
3565 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3566 ir.Event.KeyEvent.wVirtualScanCode = 0;
3567 ir.Event.KeyEvent.dwControlKeyState = 0;
3568 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3569 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return TRUE;
3571
Bram Moolenaar0f873732019-12-05 20:28:46 +01003572 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 case CTRL_CLOSE_EVENT:
3574 case CTRL_LOGOFF_EVENT:
3575 case CTRL_SHUTDOWN_EVENT:
3576 windgoto((int)Rows - 1, 0);
3577 g_fForceExit = TRUE;
3578
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003579 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 (dwCtrlType == CTRL_CLOSE_EVENT
3581 ? _("close")
3582 : dwCtrlType == CTRL_LOGOFF_EVENT
3583 ? _("logoff")
3584 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003585# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar0f873732019-12-05 20:28:46 +01003589 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar0f873732019-12-05 20:28:46 +01003591 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 default:
3594 return FALSE;
3595 }
3596}
3597
3598
3599/*
3600 * set the tty in (raw) ? "raw" : "cooked" mode
3601 */
3602 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003603mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 DWORD cmodein;
3606 DWORD cmodeout;
3607 BOOL bEnableHandler;
3608
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003609# ifdef VIMDLL
3610 if (gui.in_use)
3611 return;
3612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 GetConsoleMode(g_hConIn, &cmodein);
3614 GetConsoleMode(g_hConOut, &cmodeout);
3615 if (tmode == TMODE_RAW)
3616 {
3617 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3618 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (g_fMouseActive)
3620 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003621 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003622# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003623 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3624 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003625 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003626# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003627 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003628# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003629 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 bEnableHandler = TRUE;
3631 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003632 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3635 ENABLE_ECHO_INPUT);
3636 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3637 bEnableHandler = FALSE;
3638 }
3639 SetConsoleMode(g_hConIn, cmodein);
3640 SetConsoleMode(g_hConOut, cmodeout);
3641 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3642
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003643# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (fdDump)
3645 {
3646 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3647 tmode == TMODE_RAW ? "raw" :
3648 tmode == TMODE_COOK ? "cooked" : "normal",
3649 cmodein, cmodeout);
3650 fflush(fdDump);
3651 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653}
3654
3655
3656/*
3657 * Get the size of the current window in `Rows' and `Columns'
3658 * Return OK when size could be determined, FAIL otherwise.
3659 */
3660 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003661mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 CONSOLE_SCREEN_BUFFER_INFO csbi;
3664
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003665# ifdef VIMDLL
3666 if (gui.in_use)
3667 return OK;
3668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3670 {
3671 /*
3672 * For some reason, we are trying to get the screen dimensions
3673 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3674 * variables are really intended to mean the size of Vim screen
3675 * while in termcap mode.
3676 */
3677 Rows = g_cbTermcap.Info.dwSize.Y;
3678 Columns = g_cbTermcap.Info.dwSize.X;
3679 }
3680 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3681 {
3682 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3683 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3684 }
3685 else
3686 {
3687 Rows = 25;
3688 Columns = 80;
3689 }
3690 return OK;
3691}
3692
3693/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003694 * Resize console buffer to 'COORD'
3695 */
3696 static void
3697ResizeConBuf(
3698 HANDLE hConsole,
3699 COORD coordScreen)
3700{
3701 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3702 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003703# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003704 if (fdDump)
3705 {
3706 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3707 GetLastError());
3708 fflush(fdDump);
3709 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003710# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003711 }
3712}
3713
3714/*
3715 * Resize console window size to 'srWindowRect'
3716 */
3717 static void
3718ResizeWindow(
3719 HANDLE hConsole,
3720 SMALL_RECT srWindowRect)
3721{
3722 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3723 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003724# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003725 if (fdDump)
3726 {
3727 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3728 GetLastError());
3729 fflush(fdDump);
3730 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003731# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003732 }
3733}
3734
3735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Set a console window to `xSize' * `ySize'
3737 */
3738 static void
3739ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003740 HANDLE hConsole,
3741 int xSize,
3742 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003744 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3745 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003747 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003748 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003750# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (fdDump)
3752 {
3753 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3754 fflush(fdDump);
3755 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar0f873732019-12-05 20:28:46 +01003758 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 coordScreen = GetLargestConsoleWindowSize(hConsole);
3760
Bram Moolenaar0f873732019-12-05 20:28:46 +01003761 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3763 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3764 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3765
3766 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3767 {
3768 int sx, sy;
3769
3770 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3771 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3772 if (sy < ySize || sx < xSize)
3773 {
3774 /*
3775 * Increasing number of lines/columns, do buffer first.
3776 * Use the maximal size in x and y direction.
3777 */
3778 if (sy < ySize)
3779 coordScreen.Y = ySize;
3780 else
3781 coordScreen.Y = sy;
3782 if (sx < xSize)
3783 coordScreen.X = xSize;
3784 else
3785 coordScreen.X = sx;
3786 SetConsoleScreenBufferSize(hConsole, coordScreen);
3787 }
3788 }
3789
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003790 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 coordScreen.X = xSize;
3792 coordScreen.Y = ySize;
3793
Bram Moolenaar2551c032018-08-23 22:38:31 +02003794 // In the new console call API, only the first time in reverse order
3795 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003797 ResizeWindow(hConsole, srWindowRect);
3798 ResizeConBuf(hConsole, coordScreen);
3799 }
3800 else
3801 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003802 // Workaround for a Windows 10 bug
3803 cursor.X = srWindowRect.Left;
3804 cursor.Y = srWindowRect.Top;
3805 SetConsoleCursorPosition(hConsole, cursor);
3806
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003807 ResizeConBuf(hConsole, coordScreen);
3808 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003809 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811}
3812
3813
3814/*
3815 * Set the console window to `Rows' * `Columns'
3816 */
3817 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003818mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 COORD coordScreen;
3821
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003822# ifdef VIMDLL
3823 if (gui.in_use)
3824 return;
3825# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003826 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (suppress_winsize != 0)
3828 {
3829 suppress_winsize = 2;
3830 return;
3831 }
3832
3833 if (term_console)
3834 {
3835 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3836
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (Rows > coordScreen.Y)
3839 Rows = coordScreen.Y;
3840 if (Columns > coordScreen.X)
3841 Columns = coordScreen.X;
3842
3843 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3844 }
3845}
3846
3847/*
3848 * Rows and/or Columns has changed.
3849 */
3850 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003851mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003853# ifdef VIMDLL
3854 if (gui.in_use)
3855 return;
3856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3858}
3859
3860
3861/*
3862 * Called when started up, to set the winsize that was delayed.
3863 */
3864 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003865mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 if (suppress_winsize == 2)
3868 {
3869 suppress_winsize = 0;
3870 mch_set_shellsize();
3871 shell_resized();
3872 }
3873 suppress_winsize = 0;
3874}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003875#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003877 static BOOL
3878vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003879 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003880 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003881 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003882 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003883 PROCESS_INFORMATION *pi,
3884 LPVOID *env,
3885 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003886{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003887 BOOL ret = FALSE;
3888 WCHAR *wcmd, *wcwd = NULL;
3889
3890 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3891 if (wcmd == NULL)
3892 return FALSE;
3893 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003894 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003895 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3896 if (wcwd == NULL)
3897 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003898 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003899
3900 ret = CreateProcessW(
3901 NULL, // Executable name
3902 wcmd, // Command to execute
3903 NULL, // Process security attributes
3904 NULL, // Thread security attributes
3905 inherit_handles, // Inherit handles
3906 flags, // Creation flags
3907 env, // Environment
3908 wcwd, // Current directory
3909 (LPSTARTUPINFOW)si, // Startup information
3910 pi); // Process information
3911theend:
3912 vim_free(wcmd);
3913 vim_free(wcwd);
3914 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003915}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003918 static HINSTANCE
3919vim_shell_execute(
3920 char *cmd,
3921 INT n_show_cmd)
3922{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003923 HINSTANCE ret;
3924 WCHAR *wcmd;
3925
3926 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3927 if (wcmd == NULL)
3928 return (HINSTANCE) 0;
3929
3930 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3931 vim_free(wcmd);
3932 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003933}
3934
3935
Bram Moolenaar4f974752019-02-17 17:44:42 +01003936#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938/*
3939 * Specialised version of system() for Win32 GUI mode.
3940 * This version proceeds as follows:
3941 * 1. Create a console window for use by the subprocess
3942 * 2. Run the subprocess (it gets the allocated console by default)
3943 * 3. Wait for the subprocess to terminate and get its exit code
3944 * 4. Prompt the user to press a key to close the console window
3945 */
3946 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003947mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 STARTUPINFO si;
3950 PROCESS_INFORMATION pi;
3951 DWORD ret = 0;
3952 HWND hwnd = GetFocus();
3953
3954 si.cb = sizeof(si);
3955 si.lpReserved = NULL;
3956 si.lpDesktop = NULL;
3957 si.lpTitle = NULL;
3958 si.dwFlags = STARTF_USESHOWWINDOW;
3959 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003960 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003961 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003963 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003964 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 else
3966 si.wShowWindow = SW_SHOWNORMAL;
3967 si.cbReserved2 = 0;
3968 si.lpReserved2 = NULL;
3969
Bram Moolenaar0f873732019-12-05 20:28:46 +01003970 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003971 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003972 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3973 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar0f873732019-12-05 20:28:46 +01003975 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003977# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int delay = 1;
3979
Bram Moolenaar0f873732019-12-05 20:28:46 +01003980 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 for (;;)
3982 {
3983 MSG msg;
3984
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003985 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003988 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003989 delay = 1;
3990 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3993 break;
3994
Bram Moolenaar0f873732019-12-05 20:28:46 +01003995 // We start waiting for a very short time and then increase it, so
3996 // that we respond quickly when the process is quick, and don't
3997 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (delay < 50)
3999 delay += 10;
4000 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004001# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar0f873732019-12-05 20:28:46 +01004005 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 GetExitCodeProcess(pi.hProcess, &ret);
4007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 CloseHandle(pi.hThread);
4011 CloseHandle(pi.hProcess);
4012
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4015
4016 return ret;
4017}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004018
4019/*
4020 * Thread launched by the gui to send the current buffer data to the
4021 * process. This way avoid to hang up vim totally if the children
4022 * process take a long time to process the lines.
4023 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004024 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004025sub_process_writer(LPVOID param)
4026{
4027 HANDLE g_hChildStd_IN_Wr = param;
4028 linenr_T lnum = curbuf->b_op_start.lnum;
4029 DWORD len = 0;
4030 DWORD l;
4031 char_u *lp = ml_get(lnum);
4032 char_u *s;
4033 int written = 0;
4034
4035 for (;;)
4036 {
4037 l = (DWORD)STRLEN(lp + written);
4038 if (l == 0)
4039 len = 0;
4040 else if (lp[written] == NL)
4041 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004042 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004043 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4044 }
4045 else
4046 {
4047 s = vim_strchr(lp + written, NL);
4048 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4049 s == NULL ? l : (DWORD)(s - (lp + written)),
4050 &len, NULL);
4051 }
4052 if (len == (int)l)
4053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004054 // Finished a line, add a NL, unless this line should not have
4055 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004056 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004057 || (!curbuf->b_p_bin
4058 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004059 || (lnum != curbuf->b_no_eol_lnum
4060 && (lnum != curbuf->b_ml.ml_line_count
4061 || curbuf->b_p_eol)))
4062 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004063 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4064 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004065 }
4066
4067 ++lnum;
4068 if (lnum > curbuf->b_op_end.lnum)
4069 break;
4070
4071 lp = ml_get(lnum);
4072 written = 0;
4073 }
4074 else if (len > 0)
4075 written += len;
4076 }
4077
Bram Moolenaar0f873732019-12-05 20:28:46 +01004078 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004079 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004080 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004081}
4082
4083
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004085
4086/*
4087 * This function read from the children's stdout and write the
4088 * data on screen or in the buffer accordingly.
4089 */
4090 static void
4091dump_pipe(int options,
4092 HANDLE g_hChildStd_OUT_Rd,
4093 garray_T *ga,
4094 char_u buffer[],
4095 DWORD *buffer_off)
4096{
4097 DWORD availableBytes = 0;
4098 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004099 int ret;
4100 DWORD len;
4101 DWORD toRead;
4102 int repeatCount;
4103
Bram Moolenaar0f873732019-12-05 20:28:46 +01004104 // we query the pipe to see if there is any data to read
4105 // to avoid to perform a blocking read
4106 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4107 NULL, // optional buffer
4108 0, // buffer size
4109 NULL, // number of read bytes
4110 &availableBytes, // available bytes total
4111 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004112
4113 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004115 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004116 {
4117 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004118 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004119 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004120 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004121
Bram Moolenaar0f873732019-12-05 20:28:46 +01004122 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004123 if (len == 0)
4124 break;
4125
4126 availableBytes -= len;
4127
4128 if (options & SHELL_READ)
4129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004130 // Do NUL -> NL translation, append NL separated
4131 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004132 for (i = 0; i < len; ++i)
4133 {
4134 if (buffer[i] == NL)
4135 append_ga_line(ga);
4136 else if (buffer[i] == NUL)
4137 ga_append(ga, NL);
4138 else
4139 ga_append(ga, buffer[i]);
4140 }
4141 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004142 else if (has_mbyte)
4143 {
4144 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004145 int c;
4146 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004147
4148 len += *buffer_off;
4149 buffer[len] = NUL;
4150
Bram Moolenaar0f873732019-12-05 20:28:46 +01004151 // Check if the last character in buffer[] is
4152 // incomplete, keep these bytes for the next
4153 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004154 for (p = buffer; p < buffer + len; p += l)
4155 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004156 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004157 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004158 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004159 else if (MB_BYTE2LEN(*p) != l)
4160 break;
4161 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004162 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004163 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004164 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004165 if (len >= 12)
4166 ++p;
4167 else
4168 {
4169 *buffer_off = len;
4170 return;
4171 }
4172 }
4173 c = *p;
4174 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004176 if (p < buffer + len)
4177 {
4178 *p = c;
4179 *buffer_off = (DWORD)((buffer + len) - p);
4180 mch_memmove(buffer, p, *buffer_off);
4181 return;
4182 }
4183 *buffer_off = 0;
4184 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004185 else
4186 {
4187 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004188 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004189 }
4190
4191 windgoto(msg_row, msg_col);
4192 cursor_on();
4193 out_flush();
4194 }
4195}
4196
4197/*
4198 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4199 * for communication and doesn't open any new window.
4200 */
4201 static int
4202mch_system_piped(char *cmd, int options)
4203{
4204 STARTUPINFO si;
4205 PROCESS_INFORMATION pi;
4206 DWORD ret = 0;
4207
4208 HANDLE g_hChildStd_IN_Rd = NULL;
4209 HANDLE g_hChildStd_IN_Wr = NULL;
4210 HANDLE g_hChildStd_OUT_Rd = NULL;
4211 HANDLE g_hChildStd_OUT_Wr = NULL;
4212
Bram Moolenaar0f873732019-12-05 20:28:46 +01004213 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004214 DWORD len;
4215
Bram Moolenaar0f873732019-12-05 20:28:46 +01004216 // buffer used to receive keys
4217 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4218 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004219
4220 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004221 int noread_cnt = 0;
4222 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004223 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004224 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004225 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004226
4227 SECURITY_ATTRIBUTES saAttr;
4228
Bram Moolenaar0f873732019-12-05 20:28:46 +01004229 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004230 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4231 saAttr.bInheritHandle = TRUE;
4232 saAttr.lpSecurityDescriptor = NULL;
4233
4234 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004235 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004236 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004237 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004238 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004239 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004240 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004241 {
4242 CloseHandle(g_hChildStd_IN_Rd);
4243 CloseHandle(g_hChildStd_IN_Wr);
4244 CloseHandle(g_hChildStd_OUT_Rd);
4245 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004246 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004247 }
4248
4249 si.cb = sizeof(si);
4250 si.lpReserved = NULL;
4251 si.lpDesktop = NULL;
4252 si.lpTitle = NULL;
4253 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4254
Bram Moolenaar0f873732019-12-05 20:28:46 +01004255 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004256 si.hStdError = g_hChildStd_OUT_Wr;
4257 si.hStdOutput = g_hChildStd_OUT_Wr;
4258 si.hStdInput = g_hChildStd_IN_Rd;
4259 si.wShowWindow = SW_HIDE;
4260 si.cbReserved2 = 0;
4261 si.lpReserved2 = NULL;
4262
4263 if (options & SHELL_READ)
4264 ga_init2(&ga, 1, BUFLEN);
4265
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004266 if (cmd != NULL)
4267 {
4268 p = (char *)vim_strsave((char_u *)cmd);
4269 if (p != NULL)
4270 unescape_shellxquote((char_u *)p, p_sxe);
4271 else
4272 p = cmd;
4273 }
4274
Bram Moolenaar0f873732019-12-05 20:28:46 +01004275 // Now, run the command.
4276 // About "Inherit handles" being TRUE: this command can be litigious,
4277 // handle inheritance was deactivated for pending temp file, but, if we
4278 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004279 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4280 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004281
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004282 if (p != cmd)
4283 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004284
Bram Moolenaar0f873732019-12-05 20:28:46 +01004285 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004286 CloseHandle(g_hChildStd_IN_Rd);
4287 CloseHandle(g_hChildStd_OUT_Wr);
4288
4289 if (options & SHELL_WRITE)
4290 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004291 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004292 _beginthreadex(NULL, // security attributes
4293 0, // default stack size
4294 sub_process_writer, // function to be executed
4295 g_hChildStd_IN_Wr, // parameter
4296 0, // creation flag, start immediately
4297 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004298 CloseHandle(thread);
4299 g_hChildStd_IN_Wr = NULL;
4300 }
4301
Bram Moolenaar0f873732019-12-05 20:28:46 +01004302 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004303 for (;;)
4304 {
4305 MSG msg;
4306
Bram Moolenaar175d0702013-12-11 18:36:33 +01004307 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004308 {
4309 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004310 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004311 }
4312
Bram Moolenaar0f873732019-12-05 20:28:46 +01004313 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004314 if ((options & (SHELL_READ|SHELL_WRITE))
4315# ifdef FEAT_GUI
4316 || gui.in_use
4317# endif
4318 )
4319 {
4320 len = 0;
4321 if (!(options & SHELL_EXPAND)
4322 && ((options &
4323 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4324 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4325# ifdef FEAT_GUI
4326 || gui.in_use
4327# endif
4328 )
4329 && (ta_len > 0 || noread_cnt > 4))
4330 {
4331 if (ta_len == 0)
4332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004333 // Get extra characters when we don't have any. Reset the
4334 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004335 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004336 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4337 }
4338 if (ta_len > 0 || len > 0)
4339 {
4340 /*
4341 * For pipes: Check for CTRL-C: send interrupt signal to
4342 * child. Check for CTRL-D: EOF, close pipe to child.
4343 */
4344 if (len == 1 && cmd != NULL)
4345 {
4346 if (ta_buf[ta_len] == Ctrl_C)
4347 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004348 // Learn what exit code is expected, for
4349 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004350 TerminateProcess(pi.hProcess, 9);
4351 }
4352 if (ta_buf[ta_len] == Ctrl_D)
4353 {
4354 CloseHandle(g_hChildStd_IN_Wr);
4355 g_hChildStd_IN_Wr = NULL;
4356 }
4357 }
4358
Bram Moolenaarf4140482020-02-15 23:06:45 +01004359 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004360
4361 /*
4362 * For pipes: echo the typed characters. For a pty this
4363 * does not seem to work.
4364 */
4365 for (i = ta_len; i < ta_len + len; ++i)
4366 {
4367 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4368 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004369 else if (has_mbyte)
4370 {
4371 int l = (*mb_ptr2len)(ta_buf + i);
4372
4373 msg_outtrans_len(ta_buf + i, l);
4374 i += l - 1;
4375 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004376 else
4377 msg_outtrans_len(ta_buf + i, 1);
4378 }
4379 windgoto(msg_row, msg_col);
4380 out_flush();
4381
4382 ta_len += len;
4383
4384 /*
4385 * Write the characters to the child, unless EOF has been
4386 * typed for pipes. Write one character at a time, to
4387 * avoid losing too much typeahead. When writing buffer
4388 * lines, drop the typed characters (only check for
4389 * CTRL-C).
4390 */
4391 if (options & SHELL_WRITE)
4392 ta_len = 0;
4393 else if (g_hChildStd_IN_Wr != NULL)
4394 {
4395 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4396 1, &len, NULL);
4397 // if we are typing in, we want to keep things reactive
4398 delay = 1;
4399 if (len > 0)
4400 {
4401 ta_len -= len;
4402 mch_memmove(ta_buf, ta_buf + len, ta_len);
4403 }
4404 }
4405 }
4406 }
4407 }
4408
4409 if (ta_len)
4410 ui_inchar_undo(ta_buf, ta_len);
4411
4412 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4413 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004414 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004415 break;
4416 }
4417
4418 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004419 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004420
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 // We start waiting for a very short time and then increase it, so
4422 // that we respond quickly when the process is quick, and don't
4423 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004424 if (delay < 50)
4425 delay += 10;
4426 }
4427
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004429 CloseHandle(g_hChildStd_OUT_Rd);
4430 if (g_hChildStd_IN_Wr != NULL)
4431 CloseHandle(g_hChildStd_IN_Wr);
4432
4433 WaitForSingleObject(pi.hProcess, INFINITE);
4434
Bram Moolenaar0f873732019-12-05 20:28:46 +01004435 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004436 GetExitCodeProcess(pi.hProcess, &ret);
4437
4438 if (options & SHELL_READ)
4439 {
4440 if (ga.ga_len > 0)
4441 {
4442 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004443 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004444 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4445 }
4446 else
4447 curbuf->b_no_eol_lnum = 0;
4448 ga_clear(&ga);
4449 }
4450
Bram Moolenaar0f873732019-12-05 20:28:46 +01004451 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004452 CloseHandle(pi.hThread);
4453 CloseHandle(pi.hProcess);
4454
4455 return ret;
4456}
4457
4458 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004459mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004460{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004461 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004462 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004463 return mch_system_piped(cmd, options);
4464 else
4465 return mch_system_classic(cmd, options);
4466}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004469#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004470 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004471mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004472{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004473 int ret;
4474 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004475 char_u *buf;
4476 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004477
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004478 // If the command starts and ends with double quotes, enclose the command
4479 // in parentheses.
4480 len = STRLEN(cmd);
4481 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4482 {
4483 len += 3;
4484 buf = alloc(len);
4485 if (buf == NULL)
4486 return -1;
4487 vim_snprintf((char *)buf, len, "(%s)", cmd);
4488 wcmd = enc_to_utf16(buf, NULL);
4489 free(buf);
4490 }
4491 else
4492 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4493
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004494 if (wcmd == NULL)
4495 return -1;
4496
4497 ret = _wsystem(wcmd);
4498 vim_free(wcmd);
4499 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502#endif
4503
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004504 static int
4505mch_system(char *cmd, int options)
4506{
4507#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004508 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004509 return mch_system_g(cmd, options);
4510 else
4511 return mch_system_c(cmd, options);
4512#elif defined(FEAT_GUI_MSWIN)
4513 return mch_system_g(cmd, options);
4514#else
4515 return mch_system_c(cmd, options);
4516#endif
4517}
4518
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004519#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4520/*
4521 * Use a terminal window to run a shell command in.
4522 */
4523 static int
4524mch_call_shell_terminal(
4525 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004526 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004527{
4528 jobopt_T opt;
4529 char_u *newcmd = NULL;
4530 typval_T argvar[2];
4531 long_u cmdlen;
4532 int retval = -1;
4533 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004534 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004535 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004537
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004538 if (cmd == NULL)
4539 cmdlen = STRLEN(p_sh) + 1;
4540 else
4541 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004542 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004543 if (newcmd == NULL)
4544 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004545 if (cmd == NULL)
4546 {
4547 STRCPY(newcmd, p_sh);
4548 ch_log(NULL, "starting terminal to run a shell");
4549 }
4550 else
4551 {
4552 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4553 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4554 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004555
4556 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004557
4558 argvar[0].v_type = VAR_STRING;
4559 argvar[0].vval.v_string = newcmd;
4560 argvar[1].v_type = VAR_UNKNOWN;
4561 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004562 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004563 {
4564 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004565 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004566 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004567
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004568 job = term_getjob(buf->b_term);
4569 ++job->jv_refcount;
4570
Bram Moolenaar0f873732019-12-05 20:28:46 +01004571 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004572 aucmd_prepbuf(&aco, buf);
4573
4574 clear_oparg(&oa);
4575 while (term_use_loop())
4576 {
4577 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4578 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004579 // If terminal_loop() returns OK we got a key that is handled
4580 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004581 if (terminal_loop(TRUE) == OK)
4582 normal_cmd(&oa, TRUE);
4583 }
4584 else
4585 normal_cmd(&oa, TRUE);
4586 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004587 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004588 ch_log(NULL, "system command finished");
4589
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004590 job_unref(job);
4591
Bram Moolenaar0f873732019-12-05 20:28:46 +01004592 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004593 aucmd_restbuf(&aco);
4594
4595 wait_return(TRUE);
4596 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4597
4598 vim_free(newcmd);
4599 return retval;
4600}
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603/*
4604 * Either execute a command by calling the shell or start a new shell
4605 */
4606 int
4607mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004608 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 int x = 0;
4612 int tmode = cur_tmode;
4613#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004614 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004615
Bram Moolenaar0f873732019-12-05 20:28:46 +01004616 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004617 if (GetConsoleTitleW(szShellTitle,
4618 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004619 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004620 if (cmd == NULL)
4621 wcscat(szShellTitle, L" :sh");
4622 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004623 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004624 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004625
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004626 if (wn != NULL)
4627 {
4628 wcscat(szShellTitle, L" - !");
4629 if ((wcslen(szShellTitle) + wcslen(wn) <
4630 sizeof(szShellTitle)/sizeof(WCHAR)))
4631 wcscat(szShellTitle, wn);
4632 SetConsoleTitleW(szShellTitle);
4633 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004634 }
4635 }
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
4639 out_flush();
4640
4641#ifdef MCH_WRITE_DUMP
4642 if (fdDump)
4643 {
4644 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4645 fflush(fdDump);
4646 }
4647#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004648#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004649 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004650 if (
4651# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004652 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004653# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004654 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004655 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4656 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004657 char_u *cmdbase = cmd;
4658
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004659 if (cmdbase != NULL)
4660 // Skip a leading quote and (.
4661 while (*cmdbase == '"' || *cmdbase == '(')
4662 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004663
4664 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004665 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4666 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004667 {
4668 // Use a terminal window to run the command in.
4669 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004670# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004671 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004672# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004673 return x;
4674 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004675 }
4676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /*
4679 * Catch all deadly signals while running the external command, because a
4680 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4681 */
4682 signal(SIGINT, SIG_IGN);
4683#if defined(__GNUC__) && !defined(__MINGW32__)
4684 signal(SIGKILL, SIG_IGN);
4685#else
4686 signal(SIGBREAK, SIG_IGN);
4687#endif
4688 signal(SIGILL, SIG_IGN);
4689 signal(SIGFPE, SIG_IGN);
4690 signal(SIGSEGV, SIG_IGN);
4691 signal(SIGTERM, SIG_IGN);
4692 signal(SIGABRT, SIG_IGN);
4693
4694 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004695 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 if (cmd == NULL)
4698 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004699 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701 else
4702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004704 char_u *newcmd = NULL;
4705 char_u *cmdbase = cmd;
4706 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004707
Bram Moolenaar0f873732019-12-05 20:28:46 +01004708 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004709 if (*cmdbase == '"' )
4710 ++cmdbase;
4711 if (*cmdbase == '(')
4712 ++cmdbase;
4713
Bram Moolenaar1c465442017-03-12 20:10:05 +01004714 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004715 {
4716 STARTUPINFO si;
4717 PROCESS_INFORMATION pi;
4718 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004719 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004720 char_u *p;
4721
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004722 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004723 si.cb = sizeof(si);
4724 si.lpReserved = NULL;
4725 si.lpDesktop = NULL;
4726 si.lpTitle = NULL;
4727 si.dwFlags = 0;
4728 si.cbReserved2 = 0;
4729 si.lpReserved2 = NULL;
4730
4731 cmdbase = skipwhite(cmdbase + 5);
4732 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004733 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004734 {
4735 cmdbase = skipwhite(cmdbase + 4);
4736 si.dwFlags = STARTF_USESHOWWINDOW;
4737 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004738 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004739 }
4740 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004741 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004742 {
4743 cmdbase = skipwhite(cmdbase + 2);
4744 flags = CREATE_NO_WINDOW;
4745 si.dwFlags = STARTF_USESTDHANDLES;
4746 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004747 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004748 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004749 NULL, // Security att.
4750 OPEN_EXISTING, // Open flags
4751 FILE_ATTRIBUTE_NORMAL, // File att.
4752 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004753 si.hStdOutput = si.hStdInput;
4754 si.hStdError = si.hStdInput;
4755 }
4756
Bram Moolenaar0f873732019-12-05 20:28:46 +01004757 // Remove a trailing ", ) and )" if they have a match
4758 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004759 if (cmdbase > cmd)
4760 {
4761 p = cmdbase + STRLEN(cmdbase);
4762 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4763 *--p = NUL;
4764 if (p > cmdbase && p[-1] == ')'
4765 && (*cmd =='(' || cmd[1] == '('))
4766 *--p = NUL;
4767 }
4768
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004769 newcmd = cmdbase;
4770 unescape_shellxquote(cmdbase, p_sxe);
4771
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004772 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004773 * If creating new console, arguments are passed to the
4774 * 'cmd.exe' as-is. If it's not, arguments are not treated
4775 * correctly for current 'cmd.exe'. So unescape characters in
4776 * shellxescape except '|' for avoiding to be treated as
4777 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004778 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004779 if (flags != CREATE_NEW_CONSOLE)
4780 {
4781 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004782 char_u *cmd_shell = mch_getenv("COMSPEC");
4783
4784 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004785 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004786
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004787 subcmd = vim_strsave_escaped_ext(cmdbase,
4788 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004789 if (subcmd != NULL)
4790 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004791 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004792 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004793 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004794 if (newcmd != NULL)
4795 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004796 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004797 else
4798 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004799 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004800 }
4801 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004802
4803 /*
4804 * Now, start the command as a process, so that it doesn't
4805 * inherit our handles which causes unpleasant dangling swap
4806 * files if we exit before the spawned process
4807 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004808 if (vim_create_process((char *)newcmd, FALSE, flags,
4809 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004810 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004811 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4812 > (HINSTANCE)32)
4813 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004814 else
4815 {
4816 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004817#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004818# ifdef VIMDLL
4819 if (gui.in_use)
4820# endif
4821 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004822#endif
4823 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004824
4825 if (newcmd != cmdbase)
4826 vim_free(newcmd);
4827
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004828 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004829 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004830 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004831 CloseHandle(si.hStdInput);
4832 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004833 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004834 CloseHandle(pi.hThread);
4835 CloseHandle(pi.hProcess);
4836 }
4837 else
4838 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004839 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004840#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004841 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004842 (!s_dont_use_vimrun && p_stmp ?
4843 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4844 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004846 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004847
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004848 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004849 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004851#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004852 if (
4853# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004854 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004855# endif
4856 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004858 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4859 "External commands will not pause after completion.\n"
4860 "See :help win32-vimrun for more information.");
4861 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004862 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4863 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004864
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004865 if (wmsg != NULL && wtitle != NULL)
4866 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4867 vim_free(wmsg);
4868 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 need_vimrun_warning = FALSE;
4870 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004871 if (
4872# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004873 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004874# endif
4875 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004876 // Use vimrun to execute the command. It opens a console
4877 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004878 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 vimrun_path,
4880 (msg_silent != 0 || (options & SHELL_DOOUT))
4881 ? "-s " : "",
4882 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004883 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004884# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004885 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004886# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004887 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004888 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004889 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4890 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004893 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004894 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004896 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 }
4900
4901 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004902 {
4903 // The shell may have messed with the mode, always set it.
4904 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004905 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar0f873732019-12-05 20:28:46 +01004908 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004910#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004911 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004912 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
4914 )
4915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004916 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 msg_putchar('\n');
4918 }
4919#ifdef FEAT_TITLE
4920 resettitle();
4921#endif
4922
4923 signal(SIGINT, SIG_DFL);
4924#if defined(__GNUC__) && !defined(__MINGW32__)
4925 signal(SIGKILL, SIG_DFL);
4926#else
4927 signal(SIGBREAK, SIG_DFL);
4928#endif
4929 signal(SIGILL, SIG_DFL);
4930 signal(SIGFPE, SIG_DFL);
4931 signal(SIGSEGV, SIG_DFL);
4932 signal(SIGTERM, SIG_DFL);
4933 signal(SIGABRT, SIG_DFL);
4934
4935 return x;
4936}
4937
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004938#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004939 static HANDLE
4940job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004941 char_u *fname,
4942 DWORD dwDesiredAccess,
4943 DWORD dwShareMode,
4944 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4945 DWORD dwCreationDisposition,
4946 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004947{
4948 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004949 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004950
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004951 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004952 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004953 return INVALID_HANDLE_VALUE;
4954
4955 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4956 lpSecurityAttributes, dwCreationDisposition,
4957 dwFlagsAndAttributes, NULL);
4958 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004959 return h;
4960}
4961
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004962/*
4963 * Turn the dictionary "env" into a NUL separated list that can be used as the
4964 * environment argument of vim_create_process().
4965 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004966 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004967win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004968{
4969 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004970 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004971 LPVOID base = GetEnvironmentStringsW();
4972
Bram Moolenaar0f873732019-12-05 20:28:46 +01004973 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004974 if (ga_grow(gap, 1) == FAIL)
4975 return;
4976
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004977 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004978 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004979 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004980 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004981 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004982 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004983 typval_T *item = &dict_lookup(hi)->di_tv;
4984 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004985 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004986 --todo;
4987 if (wkey != NULL && wval != NULL)
4988 {
4989 size_t n;
4990 size_t lkey = wcslen(wkey);
4991 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004992
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004993 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4994 continue;
4995 for (n = 0; n < lkey; n++)
4996 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4997 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4998 for (n = 0; n < lval; n++)
4999 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
5000 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5001 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01005002 vim_free(wkey);
5003 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005004 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005005 }
5006 }
5007
Bram Moolenaar355757a2020-02-10 22:06:32 +01005008 if (base)
5009 {
5010 WCHAR *p = (WCHAR*) base;
5011
5012 // for last \0
5013 if (ga_grow(gap, 1) == FAIL)
5014 return;
5015
5016 while (*p != 0 || *(p + 1) != 0)
5017 {
5018 if (ga_grow(gap, 1) == OK)
5019 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5020 p++;
5021 }
5022 FreeEnvironmentStrings(base);
5023 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5024 }
5025
Bram Moolenaar493359e2018-06-12 20:25:52 +02005026# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005027 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005028# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005029 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005030 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005031# endif
5032# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005033 char_u *version = get_vim_var_str(VV_VERSION);
5034 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005035# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005036 // size of "VIM_SERVERNAME=" and value,
5037 // plus "VIM_TERMINAL=" and value,
5038 // plus two terminating NULs
5039 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005040# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005041 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005042# endif
5043# ifdef FEAT_TERMINAL
5044 + 13 + version_len + 2
5045# endif
5046 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005047
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005048 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005049 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005050# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005051 for (n = 0; n < 15; n++)
5052 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5053 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005054 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005055 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5056 (WCHAR)servername[n];
5057 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005058# endif
5059# ifdef FEAT_TERMINAL
5060 if (is_terminal)
5061 {
5062 for (n = 0; n < 13; n++)
5063 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5064 (WCHAR)"VIM_TERMINAL="[n];
5065 for (n = 0; n < version_len; n++)
5066 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5067 (WCHAR)version[n];
5068 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5069 }
5070# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005071 }
5072 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005073# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005074}
5075
Bram Moolenaarb091f302019-01-19 14:37:00 +01005076/*
5077 * Create a pair of pipes.
5078 * Return TRUE for success, FALSE for failure.
5079 */
5080 static BOOL
5081create_pipe_pair(HANDLE handles[2])
5082{
5083 static LONG s;
5084 char name[64];
5085 SECURITY_ATTRIBUTES sa;
5086
5087 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5088 GetCurrentProcessId(),
5089 InterlockedIncrement(&s));
5090
5091 // Create named pipe. Max size of named pipe is 65535.
5092 handles[1] = CreateNamedPipe(
5093 name,
5094 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5095 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005096 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005097
5098 if (handles[1] == INVALID_HANDLE_VALUE)
5099 return FALSE;
5100
5101 sa.nLength = sizeof(sa);
5102 sa.bInheritHandle = TRUE;
5103 sa.lpSecurityDescriptor = NULL;
5104
5105 handles[0] = CreateFile(name,
5106 FILE_GENERIC_READ,
5107 FILE_SHARE_READ, &sa,
5108 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5109
5110 if (handles[0] == INVALID_HANDLE_VALUE)
5111 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005112 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005113 return FALSE;
5114 }
5115
5116 return TRUE;
5117}
5118
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005119 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005120mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005121{
5122 STARTUPINFO si;
5123 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005124 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005125 SECURITY_ATTRIBUTES saAttr;
5126 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005127 HANDLE ifd[2];
5128 HANDLE ofd[2];
5129 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005130 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005131
5132 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5133 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5134 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5135 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5136 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5137 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5138 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5139
5140 if (use_out_for_err && use_null_for_out)
5141 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005142
5143 ifd[0] = INVALID_HANDLE_VALUE;
5144 ifd[1] = INVALID_HANDLE_VALUE;
5145 ofd[0] = INVALID_HANDLE_VALUE;
5146 ofd[1] = INVALID_HANDLE_VALUE;
5147 efd[0] = INVALID_HANDLE_VALUE;
5148 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005149 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005150
Bram Moolenaar14207f42016-10-27 21:13:10 +02005151 jo = CreateJobObject(NULL, NULL);
5152 if (jo == NULL)
5153 {
5154 job->jv_status = JOB_FAILED;
5155 goto failed;
5156 }
5157
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005158 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005159 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005160
Bram Moolenaar76467df2016-02-12 19:30:26 +01005161 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005162 ZeroMemory(&si, sizeof(si));
5163 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005164 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005165 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005166
Bram Moolenaard8070362016-02-15 21:56:54 +01005167 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5168 saAttr.bInheritHandle = TRUE;
5169 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005170
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005171 if (use_file_for_in)
5172 {
5173 char_u *fname = options->jo_io_name[PART_IN];
5174
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005175 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5176 FILE_SHARE_READ | FILE_SHARE_WRITE,
5177 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5178 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005180 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005181 goto failed;
5182 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005183 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005184 else if (!use_null_for_in
5185 && (!create_pipe_pair(ifd)
5186 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005187 goto failed;
5188
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005189 if (use_file_for_out)
5190 {
5191 char_u *fname = options->jo_io_name[PART_OUT];
5192
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005193 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5194 FILE_SHARE_READ | FILE_SHARE_WRITE,
5195 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5196 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005198 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005199 goto failed;
5200 }
5201 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005202 else if (!use_null_for_out &&
5203 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005204 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005205 goto failed;
5206
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005207 if (use_file_for_err)
5208 {
5209 char_u *fname = options->jo_io_name[PART_ERR];
5210
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005211 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5212 FILE_SHARE_READ | FILE_SHARE_WRITE,
5213 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5214 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005216 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005217 goto failed;
5218 }
5219 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005220 else if (!use_out_for_err && !use_null_for_err &&
5221 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005222 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005223 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005224
Bram Moolenaard8070362016-02-15 21:56:54 +01005225 si.dwFlags |= STARTF_USESTDHANDLES;
5226 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005227 si.hStdOutput = ofd[1];
5228 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5229
5230 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5231 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005232 if (options->jo_set & JO_CHANNEL)
5233 {
5234 channel = options->jo_channel;
5235 if (channel != NULL)
5236 ++channel->ch_refcount;
5237 }
5238 else
5239 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005240 if (channel == NULL)
5241 goto failed;
5242 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005243
5244 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005245 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005246 CREATE_DEFAULT_ERROR_MODE |
5247 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005248 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005249 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005250 &si, &pi,
5251 ga.ga_data,
5252 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005253 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005254 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005255 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005256 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005257 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005258
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005259 ga_clear(&ga);
5260
Bram Moolenaar14207f42016-10-27 21:13:10 +02005261 if (!AssignProcessToJobObject(jo, pi.hProcess))
5262 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005263 // if failing, switch the way to terminate
5264 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005265 CloseHandle(jo);
5266 jo = NULL;
5267 }
5268 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005269 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005270 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005271 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005272 job->jv_status = JOB_STARTED;
5273
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005274 CloseHandle(ifd[0]);
5275 CloseHandle(ofd[1]);
5276 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005277 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005278
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005279 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005280 if (channel != NULL)
5281 {
5282 channel_set_pipes(channel,
5283 use_file_for_in || use_null_for_in
5284 ? INVALID_FD : (sock_T)ifd[1],
5285 use_file_for_out || use_null_for_out
5286 ? INVALID_FD : (sock_T)ofd[0],
5287 use_out_for_err || use_file_for_err || use_null_for_err
5288 ? INVALID_FD : (sock_T)efd[0]);
5289 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005290 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005291 return;
5292
5293failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005294 CloseHandle(ifd[0]);
5295 CloseHandle(ofd[0]);
5296 CloseHandle(efd[0]);
5297 CloseHandle(ifd[1]);
5298 CloseHandle(ofd[1]);
5299 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005300 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005301 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005302}
5303
5304 char *
5305mch_job_status(job_T *job)
5306{
5307 DWORD dwExitCode = 0;
5308
Bram Moolenaar76467df2016-02-12 19:30:26 +01005309 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5310 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005311 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005312 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005313 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005314 {
5315 ch_log(job->jv_channel, "Job ended");
5316 job->jv_status = JOB_ENDED;
5317 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005318 return "dead";
5319 }
5320 return "run";
5321}
5322
Bram Moolenaar97792de2016-10-15 18:36:49 +02005323 job_T *
5324mch_detect_ended_job(job_T *job_list)
5325{
5326 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5327 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5328 job_T *job = job_list;
5329
5330 while (job != NULL)
5331 {
5332 DWORD n;
5333 DWORD result;
5334
5335 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5336 && job != NULL; job = job->jv_next)
5337 {
5338 if (job->jv_status == JOB_STARTED)
5339 {
5340 jobHandles[n] = job->jv_proc_info.hProcess;
5341 jobArray[n] = job;
5342 ++n;
5343 }
5344 }
5345 if (n == 0)
5346 continue;
5347 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5348 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5349 {
5350 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5351
5352 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5353 return wait_job;
5354 }
5355 }
5356 return NULL;
5357}
5358
Bram Moolenaarfb630902016-10-29 14:55:00 +02005359 static BOOL
5360terminate_all(HANDLE process, int code)
5361{
5362 PROCESSENTRY32 pe;
5363 HANDLE h = INVALID_HANDLE_VALUE;
5364 DWORD pid = GetProcessId(process);
5365
5366 if (pid != 0)
5367 {
5368 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5369 if (h != INVALID_HANDLE_VALUE)
5370 {
5371 pe.dwSize = sizeof(PROCESSENTRY32);
5372 if (!Process32First(h, &pe))
5373 goto theend;
5374
5375 do
5376 {
5377 if (pe.th32ParentProcessID == pid)
5378 {
5379 HANDLE ph = OpenProcess(
5380 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5381 if (ph != NULL)
5382 {
5383 terminate_all(ph, code);
5384 CloseHandle(ph);
5385 }
5386 }
5387 } while (Process32Next(h, &pe));
5388
5389 CloseHandle(h);
5390 }
5391 }
5392
5393theend:
5394 return TerminateProcess(process, code);
5395}
5396
5397/*
5398 * Send a (deadly) signal to "job".
5399 * Return FAIL if it didn't work.
5400 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005401 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005402mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005403{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005404 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005405
Bram Moolenaar923d9262016-02-25 20:56:01 +01005406 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005407 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005408 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005409 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005410 {
5411 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5412 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005413 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005414 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005415 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005416 }
5417
5418 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5419 return FAIL;
5420 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005421 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5422 job->jv_proc_info.dwProcessId)
5423 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005424 FreeConsole();
5425 return ret;
5426}
5427
5428/*
5429 * Clear the data related to "job".
5430 */
5431 void
5432mch_clear_job(job_T *job)
5433{
5434 if (job->jv_status != JOB_FAILED)
5435 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005436 if (job->jv_job_object != NULL)
5437 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005438 CloseHandle(job->jv_proc_info.hProcess);
5439 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005440}
5441#endif
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005444#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446/*
5447 * Start termcap mode
5448 */
5449 static void
5450termcap_mode_start(void)
5451{
5452 DWORD cmodein;
5453
5454 if (g_fTermcapMode)
5455 return;
5456
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005457 if (!p_rs && USE_VTP)
5458 vtp_printf("\033[?1049h");
5459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 SaveConsoleBuffer(&g_cbNonTermcap);
5461
5462 if (g_cbTermcap.IsValid)
5463 {
5464 /*
5465 * We've been in termcap mode before. Restore certain screen
5466 * characteristics, including the buffer size and the window
5467 * size. Since we will be redrawing the screen, we don't need
5468 * to restore the actual contents of the buffer.
5469 */
5470 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005471 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5473 Rows = g_cbTermcap.Info.dwSize.Y;
5474 Columns = g_cbTermcap.Info.dwSize.X;
5475 }
5476 else
5477 {
5478 /*
5479 * This is our first time entering termcap mode. Clear the console
5480 * screen buffer, and resize the buffer to match the current window
5481 * size. We will use this as the size of our editing environment.
5482 */
5483 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005484 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5486 }
5487
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005488# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (g_fMouseActive)
5494 cmodein |= ENABLE_MOUSE_INPUT;
5495 else
5496 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 cmodein |= ENABLE_WINDOW_INPUT;
5498 SetConsoleMode(g_hConIn, cmodein);
5499
5500 redraw_later_clear();
5501 g_fTermcapMode = TRUE;
5502}
5503
5504
5505/*
5506 * End termcap mode
5507 */
5508 static void
5509termcap_mode_end(void)
5510{
5511 DWORD cmodein;
5512 ConsoleBuffer *cb;
5513 COORD coord;
5514 DWORD dwDummy;
5515
5516 if (!g_fTermcapMode)
5517 return;
5518
5519 SaveConsoleBuffer(&g_cbTermcap);
5520
5521 GetConsoleMode(g_hConIn, &cmodein);
5522 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5523 SetConsoleMode(g_hConIn, cmodein);
5524
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005525# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005526 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005527# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005529# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005530 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005531 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 SetConsoleCursorInfo(g_hConOut, &g_cci);
5533
5534 if (p_rs || exiting)
5535 {
5536 /*
5537 * Clear anything that happens to be on the current line.
5538 */
5539 coord.X = 0;
5540 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5541 FillConsoleOutputCharacter(g_hConOut, ' ',
5542 cb->Info.dwSize.X, coord, &dwDummy);
5543 /*
5544 * The following is just for aesthetics. If we are exiting without
5545 * restoring the screen, then we want to have a prompt string
5546 * appear at the bottom line. However, the command interpreter
5547 * seems to always advance the cursor one line before displaying
5548 * the prompt string, which causes the screen to scroll. To
5549 * counter this, move the cursor up one line before exiting.
5550 */
5551 if (exiting && !p_rs)
5552 coord.Y--;
5553 /*
5554 * Position the cursor at the leftmost column of the desired row.
5555 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005556 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005559 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005560 vtp_printf("\033[?1049l");
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 g_fTermcapMode = FALSE;
5563}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005564#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005567#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 void
5569mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005570 char_u *s UNUSED,
5571 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574}
5575
5576#else
5577
5578/*
5579 * clear `n' chars, starting from `coord'
5580 */
5581 static void
5582clear_chars(
5583 COORD coord,
5584 DWORD n)
5585{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005586 if (!USE_VTP)
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02005587 {
5588 DWORD dwDummy;
5589
5590 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
5591 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord,
5592 &dwDummy);
5593 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005594 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005595 {
5596 set_console_color_rgb();
5597 gotoxy(coord.X + 1, coord.Y + 1);
5598 vtp_printf("\033[%dX", n);
5599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600}
5601
5602
5603/*
5604 * Clear the screen
5605 */
5606 static void
5607clear_screen(void)
5608{
5609 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005610
5611 if (!USE_VTP)
5612 clear_chars(g_coord, Rows * Columns);
5613 else
5614 {
5615 set_console_color_rgb();
5616 gotoxy(1, 1);
5617 vtp_printf("\033[2J");
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
5620
5621
5622/*
5623 * Clear to end of display
5624 */
5625 static void
5626clear_to_end_of_display(void)
5627{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005628 COORD save = g_coord;
5629
5630 if (!USE_VTP)
5631 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005633 else
5634 {
5635 set_console_color_rgb();
5636 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5637 vtp_printf("\033[0J");
5638
5639 gotoxy(save.X + 1, save.Y + 1);
5640 g_coord = save;
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
5644
5645/*
5646 * Clear to end of line
5647 */
5648 static void
5649clear_to_end_of_line(void)
5650{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005651 COORD save = g_coord;
5652
5653 if (!USE_VTP)
5654 clear_chars(g_coord, Columns - g_coord.X);
5655 else
5656 {
5657 set_console_color_rgb();
5658 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5659 vtp_printf("\033[0K");
5660
5661 gotoxy(save.X + 1, save.Y + 1);
5662 g_coord = save;
5663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666
5667/*
5668 * Scroll the scroll region up by `cLines' lines
5669 */
5670 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005671scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 COORD oldcoord = g_coord;
5674
5675 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5676 delete_lines(cLines);
5677
5678 g_coord = oldcoord;
5679}
5680
5681
5682/*
5683 * Set the scroll region
5684 */
5685 static void
5686set_scroll_region(
5687 unsigned left,
5688 unsigned top,
5689 unsigned right,
5690 unsigned bottom)
5691{
5692 if (left >= right
5693 || top >= bottom
5694 || right > (unsigned) Columns - 1
5695 || bottom > (unsigned) Rows - 1)
5696 return;
5697
5698 g_srScrollRegion.Left = left;
5699 g_srScrollRegion.Top = top;
5700 g_srScrollRegion.Right = right;
5701 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005702}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005703
Bram Moolenaar6982f422019-02-16 16:48:01 +01005704 static void
5705set_scroll_region_tb(
5706 unsigned top,
5707 unsigned bottom)
5708{
5709 if (top >= bottom || bottom > (unsigned)Rows - 1)
5710 return;
5711
5712 g_srScrollRegion.Top = top;
5713 g_srScrollRegion.Bottom = bottom;
5714}
5715
5716 static void
5717set_scroll_region_lr(
5718 unsigned left,
5719 unsigned right)
5720{
5721 if (left >= right || right > (unsigned)Columns - 1)
5722 return;
5723
5724 g_srScrollRegion.Left = left;
5725 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726}
5727
5728
5729/*
5730 * Insert `cLines' lines at the current cursor position
5731 */
5732 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005733insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005735 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 COORD dest;
5737 CHAR_INFO fill;
5738
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005739 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5740
Bram Moolenaar6982f422019-02-16 16:48:01 +01005741 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 dest.Y = g_coord.Y + cLines;
5743
Bram Moolenaar6982f422019-02-16 16:48:01 +01005744 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 source.Top = g_coord.Y;
5746 source.Right = g_srScrollRegion.Right;
5747 source.Bottom = g_srScrollRegion.Bottom - cLines;
5748
Bram Moolenaar6982f422019-02-16 16:48:01 +01005749 clip.Left = g_srScrollRegion.Left;
5750 clip.Top = g_coord.Y;
5751 clip.Right = g_srScrollRegion.Right;
5752 clip.Bottom = g_srScrollRegion.Bottom;
5753
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005754 fill.Char.AsciiChar = ' ';
5755 if (!USE_VTP)
5756 fill.Attributes = g_attrCurrent;
5757 else
5758 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005760 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005761
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005762 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5763
Bram Moolenaar6982f422019-02-16 16:48:01 +01005764 // Here we have to deal with a win32 console flake: If the scroll
5765 // region looks like abc and we scroll c to a and fill with d we get
5766 // cbd... if we scroll block c one line at a time to a, we get cdd...
5767 // vim expects cdd consistently... So we have to deal with that
5768 // here... (this also occurs scrolling the same way in the other
5769 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
5771 if (source.Bottom < dest.Y)
5772 {
5773 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005774 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar6982f422019-02-16 16:48:01 +01005776 coord.X = source.Left;
5777 for (i = clip.Top; i < dest.Y; ++i)
5778 {
5779 coord.Y = i;
5780 clear_chars(coord, source.Right - source.Left + 1);
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783}
5784
5785
5786/*
5787 * Delete `cLines' lines at the current cursor position
5788 */
5789 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005790delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005792 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 COORD dest;
5794 CHAR_INFO fill;
5795 int nb;
5796
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005797 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5798
Bram Moolenaar6982f422019-02-16 16:48:01 +01005799 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 dest.Y = g_coord.Y;
5801
Bram Moolenaar6982f422019-02-16 16:48:01 +01005802 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 source.Top = g_coord.Y + cLines;
5804 source.Right = g_srScrollRegion.Right;
5805 source.Bottom = g_srScrollRegion.Bottom;
5806
Bram Moolenaar6982f422019-02-16 16:48:01 +01005807 clip.Left = g_srScrollRegion.Left;
5808 clip.Top = g_coord.Y;
5809 clip.Right = g_srScrollRegion.Right;
5810 clip.Bottom = g_srScrollRegion.Bottom;
5811
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005812 fill.Char.AsciiChar = ' ';
5813 if (!USE_VTP)
5814 fill.Attributes = g_attrCurrent;
5815 else
5816 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005818 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005819
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005820 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5821
Bram Moolenaar6982f422019-02-16 16:48:01 +01005822 // Here we have to deal with a win32 console flake; See insert_lines()
5823 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 nb = dest.Y + (source.Bottom - source.Top) + 1;
5826
5827 if (nb < source.Top)
5828 {
5829 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005830 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar6982f422019-02-16 16:48:01 +01005832 coord.X = source.Left;
5833 for (i = nb; i < clip.Bottom; ++i)
5834 {
5835 coord.Y = i;
5836 clear_chars(coord, source.Right - source.Left + 1);
5837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839}
5840
5841
5842/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005843 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
5845 static void
5846gotoxy(
5847 unsigned x,
5848 unsigned y)
5849{
5850 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5851 return;
5852
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005853 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005854 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01005855 // There are reports of double-width characters not displayed
5856 // correctly. This workaround should fix it, similar to how it's done
5857 // for VTP.
5858 g_coord.X = 0;
5859 SetConsoleCursorPosition(g_hConOut, g_coord);
5860
Bram Moolenaar2313b612019-09-27 14:14:32 +02005861 // external cursor coords are 1-based; internal are 0-based
5862 g_coord.X = x - 1;
5863 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005864 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005865 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005866 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005867 {
5868 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005869 // destruction. Insider build bug. Always enabled because it's cheap
5870 // and avoids mistakes with recognizing the build.
5871 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005872
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005873 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005874
5875 g_coord.X = x - 1;
5876 g_coord.Y = y - 1;
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879
5880
5881/*
5882 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005883 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
5885 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005886textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005888 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 SetConsoleTextAttribute(g_hConOut, wAttr);
5891}
5892
5893
5894 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005895textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005897 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005899 if (!USE_VTP)
5900 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5901 else
5902 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903}
5904
5905
5906 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005907textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005909 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005911 if (!USE_VTP)
5912 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5913 else
5914 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915}
5916
5917
5918/*
5919 * restore the default text attribute (whatever we started with)
5920 */
5921 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005922normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005924 if (!USE_VTP)
5925 textattr(g_attrDefault);
5926 else
5927 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928}
5929
5930
5931static WORD g_attrPreStandout = 0;
5932
5933/*
5934 * Make the text standout, by brightening it
5935 */
5936 static void
5937standout(void)
5938{
5939 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5942}
5943
5944
5945/*
5946 * Turn off standout mode
5947 */
5948 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005949standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005953
5954 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}
5956
5957
5958/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005959 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 */
5961 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005962mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 char_u *p;
5965 int n;
5966
5967 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5968 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005969 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005970# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005971 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005972# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005973 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 p = T_ME + 2;
5976 n = getdigits(&p);
5977 if (*p == 'm' && n > 0)
5978 {
5979 cterm_normal_fg_color = (n & 0xf) + 1;
5980 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5981 }
5982 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005983# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005984 cterm_normal_fg_gui_color = INVALCOLOR;
5985 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987}
5988
5989
5990/*
5991 * visual bell: flash the screen
5992 */
5993 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005994visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 COORD coordOrigin = {0, 0};
5997 WORD attrFlash = ~g_attrCurrent & 0xff;
5998
5999 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006000 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
6002 if (oldattrs == NULL)
6003 return;
6004 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
6005 coordOrigin, &dwDummy);
6006 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6007 coordOrigin, &dwDummy);
6008
Bram Moolenaar0f873732019-12-05 20:28:46 +01006009 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006010 if (!USE_VTP)
6011 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 coordOrigin, &dwDummy);
6013 vim_free(oldattrs);
6014}
6015
6016
6017/*
6018 * Make the cursor visible or invisible
6019 */
6020 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006021cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006024
6025 if (USE_VTP)
6026 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6027
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006028# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
6033
6034/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006035 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006036 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006038 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006040 char_u *pchBuf,
6041 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006043 COORD coord = g_coord;
6044 DWORD written;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006045 DWORD n, cchwritten;
6046 static DWORD cells;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006047 static WCHAR *unicodebuf = NULL;
6048 static int unibuflen = 0;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006049 static int length;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006050 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006051 static WCHAR *utf8spbuf = NULL;
6052 static int utf8splength;
6053 static DWORD utf8spcells;
6054 static WCHAR **utf8usingbuf = &unicodebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006056 if (cbToWrite != 1 || *pchBuf != ' ' || !enc_utf8)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006057 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006058 utf8usingbuf = &unicodebuf;
6059 do
6060 {
6061 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6062 unicodebuf, unibuflen);
6063 if (length && length <= unibuflen)
6064 break;
6065 vim_free(unicodebuf);
6066 unicodebuf = length ? LALLOC_MULT(WCHAR, length) : NULL;
6067 unibuflen = unibuflen ? 0 : length;
6068 } while(1);
6069 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006070 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006071 else // cbToWrite == 1 && *pchBuf == ' ' && enc_utf8
6072 {
6073 if (utf8usingbuf != &utf8spbuf)
6074 {
6075 if (utf8spbuf == NULL)
6076 {
6077 cells = mb_string2cells((char_u *)" ", 1);
6078 length = MultiByteToWideChar(CP_UTF8, 0, " ", 1, NULL, 0);
6079 utf8spbuf = LALLOC_MULT(WCHAR, length);
6080 if (utf8spbuf != NULL)
6081 {
6082 MultiByteToWideChar(CP_UTF8, 0, " ", 1, utf8spbuf, length);
6083 utf8usingbuf = &utf8spbuf;
6084 utf8splength = length;
6085 utf8spcells = cells;
6086 }
6087 }
6088 else
6089 {
6090 utf8usingbuf = &utf8spbuf;
6091 length = utf8splength;
6092 cells = utf8spcells;
6093 }
6094 }
6095 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006096
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006097 if (!USE_VTP)
6098 {
6099 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6100 coord, &written);
6101 // When writing fails or didn't write a single character, pretend one
6102 // character was written, otherwise we get stuck.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006103 if (WriteConsoleOutputCharacterW(g_hConOut, *utf8usingbuf, length,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006104 coord, &cchwritten) == 0
6105 || cchwritten == 0 || cchwritten == (DWORD)-1)
6106 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006107 }
6108 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006109 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006110 if (WriteConsoleW(g_hConOut, *utf8usingbuf, length, &cchwritten,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006111 NULL) == 0 || cchwritten == 0)
6112 cchwritten = 1;
6113 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006114
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006115 if (cchwritten == length)
6116 {
6117 written = cbToWrite;
6118 g_coord.X += (SHORT)cells;
6119 }
6120 else
6121 {
6122 char_u *p = pchBuf;
6123 for (n = 0; n < cchwritten; n++)
6124 MB_CPTR_ADV(p);
6125 written = p - pchBuf;
6126 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 while (g_coord.X > g_srScrollRegion.Right)
6130 {
6131 g_coord.X -= (SHORT) Columns;
6132 if (g_coord.Y < g_srScrollRegion.Bottom)
6133 g_coord.Y++;
6134 }
6135
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006136 // Cursor under VTP is always in the correct position, no need to reset.
6137 if (!USE_VTP)
6138 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 return written;
6141}
6142
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006143 static char_u *
6144get_seq(
6145 int *args,
6146 int *count,
6147 char_u *head)
6148{
6149 int argc;
6150 char_u *p;
6151
6152 if (head == NULL || *head != '\033')
6153 return NULL;
6154
6155 argc = 0;
6156 p = head;
6157 ++p;
6158 do
6159 {
6160 ++p;
6161 args[argc] = getdigits(&p);
6162 argc += (argc < 15) ? 1 : 0;
6163 } while (*p == ';');
6164 *count = argc;
6165
6166 return p;
6167}
6168
6169 static char_u *
6170get_sgr(
6171 int *args,
6172 int *count,
6173 char_u *head)
6174{
6175 char_u *p = get_seq(args, count, head);
6176
6177 return (p && *p == 'm') ? ++p : NULL;
6178}
6179
6180/*
6181 * Pointer to next if SGR (^[[n;2;*;*;*m), NULL otherwise.
6182 */
6183 static char_u *
6184sgrn2(
6185 char_u *head,
6186 int n)
6187{
6188 int argc;
6189 int args[16];
6190 char_u *p = get_sgr(args, &argc, head);
6191
6192 return p && argc == 5 && args[0] == n && args[1] == 2 ? p : NULL;
6193}
6194
6195/*
6196 * Pointer to next if SGR(^[[nm)<space>ESC, NULL otherwise.
6197 */
6198 static char_u *
6199sgrnc(
6200 char_u *head,
6201 int n)
6202{
6203 int argc;
6204 int args[16];
6205 char_u *p = get_sgr(args, &argc, head);
6206
6207 return p && argc == 1 && args[0] == n && (p = skipwhite(p)) && *p == '\033'
6208 ? p : NULL;
6209}
6210
6211 static char_u *
6212skipblank(char_u *q)
6213{
6214 char_u *p = q;
6215
6216 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
6217 ++p;
6218 return p;
6219}
6220
6221/*
6222 * Pointer to the next if any whitespace that may follow SGR is ESC, otherwise
6223 * NULL.
6224 */
6225 static char_u *
6226sgrn2c(
6227 char_u *head,
6228 int n)
6229{
6230 char_u *p = sgrn2(head, n);
6231
6232 return p && *p != NUL && (p = skipblank(p)) && *p == '\033' ? p : NULL;
6233}
6234
6235/*
6236 * If there is only a newline between the sequence immediately following it,
6237 * a pointer to the character following the newline is returned.
6238 * Otherwise NULL.
6239 */
6240 static char_u *
6241sgrn2cn(
6242 char_u *head,
6243 int n)
6244{
6245 char_u *p = sgrn2(head, n);
6246
6247 return p && p[0] == 0x0a && p[1] == '\033' ? ++p : NULL;
6248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250/*
6251 * mch_write(): write the output buffer to the screen, translating ESC
6252 * sequences into calls to console output routines.
6253 */
6254 void
6255mch_write(
6256 char_u *s,
6257 int len)
6258{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006259# ifdef VIMDLL
6260 if (gui.in_use)
6261 return;
6262# endif
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 s[len] = NUL;
6265
6266 if (!term_console)
6267 {
6268 write(1, s, (unsigned)len);
6269 return;
6270 }
6271
Bram Moolenaar0f873732019-12-05 20:28:46 +01006272 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 while (len--)
6274 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006275 int prefix = -1;
6276 char_u ch;
6277
6278 // While processing a sequence, on rare occasions it seems that another
6279 // sequence may be inserted asynchronously.
6280 if (len < 0)
6281 {
6282 redraw_all_later(CLEAR);
6283 return;
6284 }
6285
6286 while((ch = s[++prefix]))
6287 if (ch <= 0x1e && !(ch != '\n' && ch != '\r' && ch != '\b'
6288 && ch != '\a' && ch != '\033'))
6289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 if (p_wd)
6292 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006293 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (prefix != 0)
6295 prefix = 1;
6296 }
6297
6298 if (prefix != 0)
6299 {
6300 DWORD nWritten;
6301
6302 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006303# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 if (fdDump)
6305 {
6306 fputc('>', fdDump);
6307 fwrite(s, sizeof(char_u), nWritten, fdDump);
6308 fputs("<\n", fdDump);
6309 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 len -= (nWritten - 1);
6312 s += nWritten;
6313 }
6314 else if (s[0] == '\n')
6315 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006316 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (g_coord.Y == g_srScrollRegion.Bottom)
6318 {
6319 scroll(1);
6320 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6321 }
6322 else
6323 {
6324 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6325 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006326# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (fdDump)
6328 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 s++;
6331 }
6332 else if (s[0] == '\r')
6333 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006334 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006336# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (fdDump)
6338 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 s++;
6341 }
6342 else if (s[0] == '\b')
6343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006344 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (g_coord.X > g_srScrollRegion.Left)
6346 g_coord.X--;
6347 else if (g_coord.Y > g_srScrollRegion.Top)
6348 {
6349 g_coord.X = g_srScrollRegion.Right;
6350 g_coord.Y--;
6351 }
6352 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006353# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (fdDump)
6355 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 s++;
6358 }
6359 else if (s[0] == '\a')
6360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006361 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006363# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 if (fdDump)
6365 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 s++;
6368 }
6369 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6370 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006371# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006372 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006373# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006374 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006375 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006376 char_u *sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 switch (s[2])
6379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 case '0': case '1': case '2': case '3': case '4':
6381 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006382 if (*(p = get_seq(args, &argc, s)) != 'm')
6383 goto notsgr;
6384
6385 p = s;
6386
6387 // Handling frequent optional sequences. Output to the screen
6388 // takes too long, so do not output as much as possible.
6389
6390 // If resetFG,FG,BG,<cr>,BG,FG are connected, the preceding
6391 // resetFG,FG,BG are omitted.
6392 if (sgrn2(sgrn2(sgrn2cn(sgrn2(sgrnc(p, 39), 38), 48), 48), 38))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006393 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006394 p = sgrn2(sgrn2(sgrnc(p, 39), 38), 48);
6395 len = len + 1 - (int)(p - s);
6396 s = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 break;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006400 // If FG,BG,BG,FG of SGR are connected, the first FG can be
6401 // omitted.
6402 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 48), 38))
6403 p = sp;
6404
6405 // If FG,BG,FG,BG of SGR are connected, the first FG can be
6406 // omitted.
6407 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 38), 48))
6408 p = sp;
6409
6410 // If BG,BG of SGR are connected, the first BG can be omitted.
6411 if (sgrn2((sp = sgrn2(p, 48)), 48))
6412 p = sp;
6413
6414 // If restoreFG and FG are connected, the restoreFG can be
6415 // omitted.
6416 if (sgrn2((sp = sgrnc(p, 39)), 38))
6417 p = sp;
6418
6419 p = get_seq(args, &argc, p);
6420
6421notsgr:
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006422 arg1 = args[0];
6423 arg2 = args[1];
6424 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006426 if (argc == 1 && args[0] == 0)
6427 normvideo();
6428 else if (argc == 1)
6429 {
6430 if (USE_VTP)
6431 textcolor((WORD) arg1);
6432 else
6433 textattr((WORD) arg1);
6434 }
6435 else if (USE_VTP)
6436 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006438 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006440 gotoxy(arg2, arg1);
6441 }
6442 else if (argc == 2 && *p == 'r')
6443 {
6444 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6445 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006446 else if (argc == 2 && *p == 'R')
6447 {
6448 set_scroll_region_tb(arg1, arg2);
6449 }
6450 else if (argc == 2 && *p == 'V')
6451 {
6452 set_scroll_region_lr(arg1, arg2);
6453 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006454 else if (argc == 1 && *p == 'A')
6455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 gotoxy(g_coord.X + 1,
6457 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6458 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006459 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
6461 textbackground((WORD) arg1);
6462 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006463 else if (argc == 1 && *p == 'C')
6464 {
6465 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6466 g_coord.Y + 1);
6467 }
6468 else if (argc == 1 && *p == 'f')
6469 {
6470 textcolor((WORD) arg1);
6471 }
6472 else if (argc == 1 && *p == 'H')
6473 {
6474 gotoxy(1, arg1);
6475 }
6476 else if (argc == 1 && *p == 'L')
6477 {
6478 insert_lines(arg1);
6479 }
6480 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 {
6482 delete_lines(arg1);
6483 }
6484
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006485 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 s = p + 1;
6487 break;
6488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 gotoxy(g_coord.X + 1,
6491 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6492 goto got3;
6493
6494 case 'B':
6495 visual_bell();
6496 goto got3;
6497
6498 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6500 g_coord.Y + 1);
6501 goto got3;
6502
6503 case 'E':
6504 termcap_mode_end();
6505 goto got3;
6506
6507 case 'F':
6508 standout();
6509 goto got3;
6510
6511 case 'f':
6512 standend();
6513 goto got3;
6514
6515 case 'H':
6516 gotoxy(1, 1);
6517 goto got3;
6518
6519 case 'j':
6520 clear_to_end_of_display();
6521 goto got3;
6522
6523 case 'J':
6524 clear_screen();
6525 goto got3;
6526
6527 case 'K':
6528 clear_to_end_of_line();
6529 goto got3;
6530
6531 case 'L':
6532 insert_lines(1);
6533 goto got3;
6534
6535 case 'M':
6536 delete_lines(1);
6537 goto got3;
6538
6539 case 'S':
6540 termcap_mode_start();
6541 goto got3;
6542
6543 case 'V':
6544 cursor_visible(TRUE);
6545 goto got3;
6546
6547 case 'v':
6548 cursor_visible(FALSE);
6549 goto got3;
6550
6551 got3:
6552 s += 3;
6553 len -= 2;
6554 }
6555
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006556# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (fdDump)
6558 {
6559 fputs("ESC | ", fdDump);
6560 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6561 fputc('\n', fdDump);
6562 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 else
6566 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 DWORD nWritten;
6569
6570 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006571# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (fdDump)
6573 {
6574 fputc('>', fdDump);
6575 fwrite(s, sizeof(char_u), nWritten, fdDump);
6576 fputs("<\n", fdDump);
6577 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580 len -= (nWritten - 1);
6581 s += nWritten;
6582 }
6583 }
6584
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006585# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (fdDump)
6587 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
6590
Bram Moolenaar0f873732019-12-05 20:28:46 +01006591#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593
6594/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006595 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 */
6597 void
6598mch_delay(
6599 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006600 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006602#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006603 Sleep((int)msec); // never wait for input
6604#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006605# ifdef VIMDLL
6606 if (gui.in_use)
6607 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006608 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006609 return;
6610 }
6611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006613# ifdef FEAT_MZSCHEME
6614 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6615 {
6616 int towait = p_mzq;
6617
Bram Moolenaar0f873732019-12-05 20:28:46 +01006618 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006619 while (msec > 0)
6620 {
6621 mzvim_check_threads();
6622 if (msec < towait)
6623 towait = msec;
6624 Sleep(towait);
6625 msec -= towait;
6626 }
6627 }
6628 else
6629# endif
6630 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006632 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#endif
6634}
6635
6636
6637/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006638 * This version of remove is not scared by a readonly (backup) file.
6639 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 * Return 0 for success, -1 for failure.
6641 */
6642 int
6643mch_remove(char_u *name)
6644{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006645 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 int n;
6647
Bram Moolenaar203258c2016-01-17 22:15:16 +01006648 /*
6649 * On Windows, deleting a directory's symbolic link is done by
6650 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6651 */
6652 if (mch_isdir(name) && mch_is_symbolic_link(name))
6653 return mch_rmdir(name);
6654
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006655 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6656
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006657 wn = enc_to_utf16(name, NULL);
6658 if (wn == NULL)
6659 return -1;
6660
6661 n = DeleteFileW(wn) ? 0 : -1;
6662 vim_free(wn);
6663 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
6666
6667/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006668 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 */
6670 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006671mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006673#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6674# ifdef VIMDLL
6675 if (!gui.in_use)
6676# endif
6677 if (g_fCtrlCPressed || g_fCBrkPressed)
6678 {
6679 ctrl_break_was_pressed = g_fCBrkPressed;
6680 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6681 got_int = TRUE;
6682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683#endif
6684}
6685
Bram Moolenaar0f873732019-12-05 20:28:46 +01006686// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006687#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006688
6689/*
6690 * How much main memory in KiB that can be used by VIM.
6691 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006692 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006693mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006694{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006695 MEMORYSTATUSEX ms;
6696
Bram Moolenaar0f873732019-12-05 20:28:46 +01006697 // Need to use GlobalMemoryStatusEx() when there is more memory than
6698 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006699 ms.dwLength = sizeof(MEMORYSTATUSEX);
6700 GlobalMemoryStatusEx(&ms);
6701 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006704 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006705 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006706 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006707 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006708 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006709 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006710 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006711 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006712 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006716 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6718 * file whose short file name is "FOO.BAR" (its long file name will
6719 * be correct: "foo.bar~"). Because a file can be accessed by
6720 * either its SFN or its LFN, "foo.bar" has effectively been
6721 * renamed to "foo.bar", which is not at all what was wanted. This
6722 * seems to happen only when renaming files with three-character
6723 * extensions by appending a suffix that does not include ".".
6724 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6725 *
6726 * There is another problem, which isn't really a bug but isn't right either:
6727 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6728 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6729 * service pack 6. Doesn't seem to happen on Windows 98.
6730 *
6731 * Like rename(), returns 0 upon success, non-zero upon failure.
6732 * Should probably set errno appropriately when errors occur.
6733 */
6734 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006735mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006737 WCHAR *p;
6738 int i;
6739 WCHAR szTempFile[_MAX_PATH + 1];
6740 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006743 // No need to play tricks unless the file name contains a "~" as the
6744 // seventh character.
6745 p = wold;
6746 for (i = 0; wold[i] != NUL; ++i)
6747 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6748 && wold[i + 1] != 0)
6749 p = wold + i + 1;
6750 if ((int)(wold + i - p) < 8 || p[6] != '~')
6751 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006753 // Get base path of new file name. Undocumented feature: If pszNewFile is
6754 // a directory, no error is returned and pszFilePart will be NULL.
6755 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006757 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006759 // Get (and create) a unique temporary file name in directory of new file
6760 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 return -2;
6762
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006763 // blow the temp file away
6764 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 return -3;
6766
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006767 // rename old file to the temp file
6768 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 return -4;
6770
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006771 // now create an empty file called pszOldFile; this prevents the operating
6772 // system using pszOldFile as an alias (SFN) if we're renaming within the
6773 // same directory. For example, we're editing a file called
6774 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6775 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6776 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6777 // cause all sorts of problems later in buf_write(). So, we create an
6778 // empty file called filena~1.txt and the system will have to find some
6779 // other SFN for filena~1.txt~, such as filena~2.txt
6780 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6782 return -5;
6783 if (!CloseHandle(hf))
6784 return -6;
6785
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006786 // rename the temp file to the new file
6787 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006789 // Renaming failed. Rename the file back to its old name, so that it
6790 // looks like nothing happened.
6791 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 return -7;
6793 }
6794
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006795 // Seems to be left around on Novell filesystems
6796 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006798 // finally, remove the empty old file
6799 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 return -8;
6801
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006802 return 0;
6803}
6804
6805
6806/*
6807 * Converts the filenames to UTF-16, then call mch_wrename().
6808 * Like rename(), returns 0 upon success, non-zero upon failure.
6809 */
6810 int
6811mch_rename(
6812 const char *pszOldFile,
6813 const char *pszNewFile)
6814{
6815 WCHAR *wold = NULL;
6816 WCHAR *wnew = NULL;
6817 int retval = -1;
6818
6819 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6820 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6821 if (wold != NULL && wnew != NULL)
6822 retval = mch_wrename(wold, wnew);
6823 vim_free(wold);
6824 vim_free(wnew);
6825 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826}
6827
6828/*
6829 * Get the default shell for the current hardware platform
6830 */
6831 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006832default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006834 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835}
6836
6837/*
6838 * mch_access() extends access() to do more detailed check on network drives.
6839 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6840 */
6841 int
6842mch_access(char *n, int p)
6843{
6844 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006845 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006846 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006848 wn = enc_to_utf16((char_u *)n, NULL);
6849 if (wn == NULL)
6850 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006852 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 if (p & R_OK)
6857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // Read check is performed by seeing if we can do a find file on
6859 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006860 int i;
6861 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006863 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6864 TempNameW[i] = wn[i];
6865 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6866 TempNameW[i++] = '\\';
6867 TempNameW[i++] = '*';
6868 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006870 hFile = FindFirstFileW(TempNameW, &d);
6871 if (hFile == INVALID_HANDLE_VALUE)
6872 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
6877 if (p & W_OK)
6878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006879 // Trying to create a temporary file in the directory should catch
6880 // directories on read-only network shares. However, in
6881 // directories whose ACL allows writes but denies deletes will end
6882 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006883 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6884 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006885 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006886 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 }
6889 else
6890 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006891 // Don't consider a file read-only if another process has opened it.
6892 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6893
Bram Moolenaar0f873732019-12-05 20:28:46 +01006894 // Trying to open the file for the required access does ACL, read-only
6895 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006896 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6897 | ((p & R_OK) ? GENERIC_READ : 0);
6898
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006899 hFile = CreateFileW(wn, access_mode, share_mode,
6900 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (hFile == INVALID_HANDLE_VALUE)
6902 goto getout;
6903 CloseHandle(hFile);
6904 }
6905
Bram Moolenaar0f873732019-12-05 20:28:46 +01006906 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 return retval;
6910}
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006913 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 */
6915 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006916mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
6918 WCHAR *wn;
6919 int f;
6920
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006921 wn = enc_to_utf16((char_u *)name, NULL);
6922 if (wn == NULL)
6923 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006925 f = _wopen(wn, flags, mode);
6926 vim_free(wn);
6927 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006931 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 */
6933 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006934mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 WCHAR *wn, *wm;
6937 FILE *f = NULL;
6938
Bram Moolenaara12a1612019-01-24 16:39:02 +01006939#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006940 // Work around an annoying assertion in the Microsoft debug CRT
6941 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006942 char newMode = mode[strlen(mode) - 1];
6943 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006945 _get_fmode(&oldMode);
6946 if (newMode == 't')
6947 _set_fmode(_O_TEXT);
6948 else if (newMode == 'b')
6949 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006950#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006951 wn = enc_to_utf16((char_u *)name, NULL);
6952 wm = enc_to_utf16((char_u *)mode, NULL);
6953 if (wn != NULL && wm != NULL)
6954 f = _wfopen(wn, wm);
6955 vim_free(wn);
6956 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006957
Bram Moolenaara12a1612019-01-24 16:39:02 +01006958#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006959 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006960#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006961 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964/*
6965 * SUB STREAM (aka info stream) handling:
6966 *
6967 * NTFS can have sub streams for each file. Normal contents of file is
6968 * stored in the main stream, and extra contents (author information and
6969 * title and so on) can be stored in sub stream. After Windows 2000, user
6970 * can access and store those informations in sub streams via explorer's
6971 * property menuitem in right click menu. Those informations in sub streams
6972 * were lost when copying only the main stream. So we have to copy sub
6973 * streams.
6974 *
6975 * Incomplete explanation:
6976 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6977 * More useful info and an example:
6978 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6979 */
6980
6981/*
6982 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6983 * and write to stream "substream" of file "to".
6984 * Errors are ignored.
6985 */
6986 static void
6987copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6988{
6989 HANDLE hTo;
6990 WCHAR *to_name;
6991
6992 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6993 wcscpy(to_name, to);
6994 wcscat(to_name, substream);
6995
6996 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6997 FILE_ATTRIBUTE_NORMAL, NULL);
6998 if (hTo != INVALID_HANDLE_VALUE)
6999 {
7000 long done;
7001 DWORD todo;
7002 DWORD readcnt, written;
7003 char buf[4096];
7004
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 for (done = 0; done < len; done += written)
7007 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007008 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007009 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
7010 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
7012 FALSE, FALSE, context)
7013 || readcnt != todo
7014 || !WriteFile(hTo, buf, todo, &written, NULL)
7015 || written != todo)
7016 break;
7017 }
7018 CloseHandle(hTo);
7019 }
7020
7021 free(to_name);
7022}
7023
7024/*
7025 * Copy info streams from file "from" to file "to".
7026 */
7027 static void
7028copy_infostreams(char_u *from, char_u *to)
7029{
7030 WCHAR *fromw;
7031 WCHAR *tow;
7032 HANDLE sh;
7033 WIN32_STREAM_ID sid;
7034 int headersize;
7035 WCHAR streamname[_MAX_PATH];
7036 DWORD readcount;
7037 void *context = NULL;
7038 DWORD lo, hi;
7039 int len;
7040
Bram Moolenaar0f873732019-12-05 20:28:46 +01007041 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007042 fromw = enc_to_utf16(from, NULL);
7043 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (fromw != NULL && tow != NULL)
7045 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007046 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
7048 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
7049 if (sh != INVALID_HANDLE_VALUE)
7050 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 // Use BackupRead() to find the info streams. Repeat until we
7052 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 for (;;)
7054 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007055 // Get the header to find the length of the stream name. If
7056 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
7060 &readcount, FALSE, FALSE, &context)
7061 || readcount == 0)
7062 break;
7063
Bram Moolenaar0f873732019-12-05 20:28:46 +01007064 // We only deal with streams that have a name. The normal
7065 // file data appears to be without a name, even though docs
7066 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (sid.dwStreamNameSize > 0)
7068 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007069 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (!BackupRead(sh, (LPBYTE)streamname,
7071 sid.dwStreamNameSize,
7072 &readcount, FALSE, FALSE, &context))
7073 break;
7074
Bram Moolenaar0f873732019-12-05 20:28:46 +01007075 // Copy an info stream with a name ":anything:$DATA".
7076 // Skip "::$DATA", it has no stream name (examples suggest
7077 // it might be used for the normal file contents).
7078 // Note that BackupRead() counts bytes, but the name is in
7079 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 len = readcount / sizeof(WCHAR);
7081 streamname[len] = 0;
7082 if (len > 7 && wcsicmp(streamname + len - 6,
7083 L":$DATA") == 0)
7084 {
7085 streamname[len - 6] = 0;
7086 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007087 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089 }
7090
Bram Moolenaar0f873732019-12-05 20:28:46 +01007091 // Advance to the next stream. We might try seeking too far,
7092 // but BackupSeek() doesn't skip over stream borders, thus
7093 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 &lo, &hi, &context);
7096 }
7097
Bram Moolenaar0f873732019-12-05 20:28:46 +01007098 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
7100
7101 CloseHandle(sh);
7102 }
7103 }
7104 vim_free(fromw);
7105 vim_free(tow);
7106}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108/*
7109 * Copy file attributes from file "from" to file "to".
7110 * For Windows NT and later we copy info streams.
7111 * Always returns zero, errors are ignored.
7112 */
7113 int
7114mch_copy_file_attribute(char_u *from, char_u *to)
7115{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007116 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007117 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 return 0;
7119}
7120
7121#if defined(MYRESETSTKOFLW) || defined(PROTO)
7122/*
7123 * Recreate a destroyed stack guard page in win32.
7124 * Written by Benjamin Peterson.
7125 */
7126
Bram Moolenaar0f873732019-12-05 20:28:46 +01007127// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007128# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129
7130/*
7131 * This function does the same thing as _resetstkoflw(), which is only
7132 * available in DevStudio .net and later.
7133 * Returns 0 for failure, 1 for success.
7134 */
7135 int
7136myresetstkoflw(void)
7137{
7138 BYTE *pStackPtr;
7139 BYTE *pGuardPage;
7140 BYTE *pStackBase;
7141 BYTE *pLowestPossiblePage;
7142 MEMORY_BASIC_INFORMATION mbi;
7143 SYSTEM_INFO si;
7144 DWORD nPageSize;
7145 DWORD dummy;
7146
Bram Moolenaar0f873732019-12-05 20:28:46 +01007147 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 GetSystemInfo(&si);
7149 nPageSize = si.dwPageSize;
7150
Bram Moolenaar0f873732019-12-05 20:28:46 +01007151 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 pStackPtr = (BYTE*)_alloca(1);
7153
Bram Moolenaar0f873732019-12-05 20:28:46 +01007154 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
7156 return 0;
7157 pStackBase = (BYTE*)mbi.AllocationBase;
7158
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007159 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007161 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007164 // We want the first committed page in the stack Start at the stack
7165 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 BYTE *pBlock = pStackBase;
7167
Bram Moolenaara466c992005-07-09 21:03:22 +00007168 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
7170 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
7171 return 0;
7172
7173 pBlock += mbi.RegionSize;
7174
7175 if (mbi.State & MEM_COMMIT)
7176 break;
7177 }
7178
Bram Moolenaar0f873732019-12-05 20:28:46 +01007179 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (mbi.Protect & PAGE_GUARD)
7181 return 1;
7182
Bram Moolenaar0f873732019-12-05 20:28:46 +01007183 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
7185 pGuardPage = pLowestPossiblePage;
7186 else
7187 pGuardPage = (BYTE*)mbi.BaseAddress;
7188
Bram Moolenaar0f873732019-12-05 20:28:46 +01007189 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7191 return 0;
7192
Bram Moolenaar0f873732019-12-05 20:28:46 +01007193 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7195 &dummy))
7196 return 0;
7197 }
7198
7199 return 1;
7200}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007203
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007204/*
7205 * The command line arguments in UCS2
7206 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007207static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007208static LPWSTR *ArglistW = NULL;
7209static int global_argc = 0;
7210static char **global_argv;
7211
Bram Moolenaar0f873732019-12-05 20:28:46 +01007212static int used_file_argc = 0; // last argument in global_argv[] used
7213 // for the argument list.
7214static int *used_file_indexes = NULL; // indexes in global_argv[] for
7215 // command line arguments added to
7216 // the argument list
7217static int used_file_count = 0; // nr of entries in used_file_indexes
7218static int used_file_literal = FALSE; // take file names literally
7219static int used_file_full_path = FALSE; // file name was full path
7220static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007221static int used_alist_count = 0;
7222
7223
7224/*
7225 * Get the command line arguments. Unicode version.
7226 * Returns argc. Zero when something fails.
7227 */
7228 int
7229get_cmd_argsW(char ***argvp)
7230{
7231 char **argv = NULL;
7232 int argc = 0;
7233 int i;
7234
Bram Moolenaar14993322014-09-09 12:25:33 +02007235 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007236 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7237 if (ArglistW != NULL)
7238 {
7239 argv = malloc((nArgsW + 1) * sizeof(char *));
7240 if (argv != NULL)
7241 {
7242 argc = nArgsW;
7243 argv[argc] = NULL;
7244 for (i = 0; i < argc; ++i)
7245 {
7246 int len;
7247
Bram Moolenaar0f873732019-12-05 20:28:46 +01007248 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007249 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007251 (LPSTR *)&argv[i], &len, 0, 0);
7252 if (argv[i] == NULL)
7253 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007254 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007255 while (i > 0)
7256 free(argv[--i]);
7257 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007258 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007259 argc = 0;
7260 }
7261 }
7262 }
7263 }
7264
7265 global_argc = argc;
7266 global_argv = argv;
7267 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007268 {
7269 if (used_file_indexes != NULL)
7270 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007271 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007272 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007273
7274 if (argvp != NULL)
7275 *argvp = argv;
7276 return argc;
7277}
7278
7279 void
7280free_cmd_argsW(void)
7281{
7282 if (ArglistW != NULL)
7283 {
7284 GlobalFree(ArglistW);
7285 ArglistW = NULL;
7286 }
7287}
7288
7289/*
7290 * Remember "name" is an argument that was added to the argument list.
7291 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7292 * is called.
7293 */
7294 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007295used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007296{
7297 int i;
7298
7299 if (used_file_indexes == NULL)
7300 return;
7301 for (i = used_file_argc + 1; i < global_argc; ++i)
7302 if (STRCMP(global_argv[i], name) == 0)
7303 {
7304 used_file_argc = i;
7305 used_file_indexes[used_file_count++] = i;
7306 break;
7307 }
7308 used_file_literal = literal;
7309 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007310 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007311}
7312
7313/*
7314 * Remember the length of the argument list as it was. If it changes then we
7315 * leave it alone when 'encoding' is set.
7316 */
7317 void
7318set_alist_count(void)
7319{
7320 used_alist_count = GARGCOUNT;
7321}
7322
7323/*
7324 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7325 * has been changed while starting up. Use the UCS-2 command line arguments
7326 * and convert them to 'encoding'.
7327 */
7328 void
7329fix_arg_enc(void)
7330{
7331 int i;
7332 int idx;
7333 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007334 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007335
Bram Moolenaar0f873732019-12-05 20:28:46 +01007336 // Safety checks:
7337 // - if argument count differs between the wide and non-wide argument
7338 // list, something must be wrong.
7339 // - the file name arguments must have been located.
7340 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007341 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007342 || ArglistW == NULL
7343 || used_file_indexes == NULL
7344 || used_file_count == 0
7345 || used_alist_count != GARGCOUNT)
7346 return;
7347
Bram Moolenaar0f873732019-12-05 20:28:46 +01007348 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007349 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007350 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007351 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007352 for (i = 0; i < GARGCOUNT; ++i)
7353 fnum_list[i] = GARGLIST[i].ae_fnum;
7354
Bram Moolenaar0f873732019-12-05 20:28:46 +01007355 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007356 alist_clear(&global_alist);
7357 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007358 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007359
7360 for (i = 0; i < used_file_count; ++i)
7361 {
7362 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007363 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007364 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007365 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007366 int literal = used_file_literal;
7367
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007368#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007369 // When using diff mode may need to concatenate file name to
7370 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007371 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7372 && !mch_isdir(alist_name(&GARGLIST[0])))
7373 {
7374 char_u *r;
7375
7376 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7377 if (r != NULL)
7378 {
7379 vim_free(str);
7380 str = r;
7381 }
7382 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007383#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007384 // Re-use the old buffer by renaming it. When not using literal
7385 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007386 if (used_file_literal)
7387 buf_set_name(fnum_list[i], str);
7388
Bram Moolenaar0f873732019-12-05 20:28:46 +01007389 // Check backtick literal. backtick literal is already expanded in
7390 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007391 if (literal == FALSE)
7392 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007393 size_t len = STRLEN(str);
7394
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007395 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7396 literal = TRUE;
7397 }
7398 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007399 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007400 }
7401
7402 if (!used_file_literal)
7403 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007404 // Now expand wildcards in the arguments.
7405 // Temporarily add '(' and ')' to 'isfname'. These are valid
7406 // filename characters but are excluded from 'isfname' to make
7407 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7408 // Also, unset wildignore to not be influenced by this option.
7409 // The arguments specified in command-line should be kept even if
7410 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007411 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007412 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007413 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007414 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007415 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007416 }
7417
Bram Moolenaar0f873732019-12-05 20:28:46 +01007418 // If wildcard expansion failed, we are editing the first file of the
7419 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007420 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7421 {
7422 do_cmdline_cmd((char_u *)":rewind");
7423 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007424 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007425 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007426
7427 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007428}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007429
7430 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007431mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007432{
7433 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007434 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007435
Bram Moolenaar964b3742019-05-24 18:54:09 +02007436 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007437 if (envbuf == NULL)
7438 return -1;
7439
7440 sprintf((char *)envbuf, "%s=%s", var, value);
7441
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007442 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007443
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007444 vim_free(envbuf);
7445 if (p == NULL)
7446 return -1;
7447 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007448#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007449 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007450#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007451 // Unlike Un*x systems, we can free the string for _wputenv().
7452 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007453
7454 return 0;
7455}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007456
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007457/*
7458 * Support for 256 colors and 24-bit colors was added in Windows 10
7459 * version 1703 (Creators update).
7460 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007461#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7462
7463/*
7464 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007465 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007466 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007467#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007468
7469/*
7470 * ConPTY differences between versions, need different logic.
7471 * version 1903 (May 2019 update).
7472 */
7473#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7474
7475/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007476 * version 1909 (November 2019 update).
7477 */
7478#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7479
7480/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007481 * Stay ahead of the next update, and when it's done, fix this.
7482 * version ? (2020 update, temporarily use the build number of insider preview)
7483 */
7484#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
7485
7486/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007487 * Confirm until this version. Also the logic changes.
7488 * insider preview.
7489 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007490#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007491
7492/*
7493 * Not stable now.
7494 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007495#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007496
7497 static void
7498vtp_flag_init(void)
7499{
7500 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007501#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007502 DWORD mode;
7503 HANDLE out;
7504
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007505# ifdef VIMDLL
7506 if (!gui.in_use)
7507# endif
7508 {
7509 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007510
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007511 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7512 GetConsoleMode(out, &mode);
7513 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7514 if (SetConsoleMode(out, mode) == 0)
7515 vtp_working = 0;
7516 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007517#endif
7518
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007519 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007520 conpty_working = 1;
7521 if (ver >= CONPTY_STABLE_BUILD)
7522 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007523
Bram Moolenaar57da6982019-09-13 22:30:11 +02007524 if (ver <= CONPTY_INSIDER_BUILD)
7525 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007526 if (ver <= CONPTY_1909_BUILD)
7527 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007528 if (ver <= CONPTY_1903_BUILD)
7529 conpty_type = 2;
7530 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7531 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007532
7533 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
7534 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007535}
7536
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007537#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007538
7539 static void
7540vtp_init(void)
7541{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007542 HMODULE hKerneldll;
7543 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007544# ifdef FEAT_TERMGUICOLORS
7545 COLORREF fg, bg;
7546# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007547
Bram Moolenaar0f873732019-12-05 20:28:46 +01007548 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007549 hKerneldll = GetModuleHandle("kernel32.dll");
7550 if (hKerneldll != NULL)
7551 {
7552 pGetConsoleScreenBufferInfoEx =
7553 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7554 hKerneldll, "GetConsoleScreenBufferInfoEx");
7555 pSetConsoleScreenBufferInfoEx =
7556 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7557 hKerneldll, "SetConsoleScreenBufferInfoEx");
7558 if (pGetConsoleScreenBufferInfoEx != NULL
7559 && pSetConsoleScreenBufferInfoEx != NULL)
7560 has_csbiex = TRUE;
7561 }
7562
7563 csbi.cbSize = sizeof(csbi);
7564 if (has_csbiex)
7565 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007566 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7567 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007568 store_console_bg_rgb = save_console_bg_rgb;
7569 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007570
7571# ifdef FEAT_TERMGUICOLORS
7572 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7573 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7574 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7575 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7576 default_console_color_bg = bg;
7577 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007578# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007579
7580 set_console_color_rgb();
7581}
7582
7583 static void
7584vtp_exit(void)
7585{
Bram Moolenaardf543822020-01-30 11:53:59 +01007586 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007587}
7588
7589 static int
7590vtp_printf(
7591 char *format,
7592 ...)
7593{
7594 char_u buf[100];
7595 va_list list;
7596 DWORD result;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007597 int len;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007598
7599 va_start(list, format);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007600 len = vim_vsnprintf((char *)buf, 100, (char *)format, list);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007601 va_end(list);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007602 WriteConsoleA(g_hConOut, buf, (DWORD)len, &result, NULL);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007603 return (int)result;
7604}
7605
7606 static void
7607vtp_sgr_bulk(
7608 int arg)
7609{
7610 int args[1];
7611
7612 args[0] = arg;
7613 vtp_sgr_bulks(1, args);
7614}
7615
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007616#define FAST256(x) \
7617 if ((*p-- = "0123456789"[(n = x % 10)]) \
7618 && x >= 10 && (*p-- = "0123456789"[((m = x % 100) - n) / 10]) \
7619 && x >= 100 && (*p-- = "012"[((x & 0xff) - m) / 100]));
7620
7621#define FAST256CASE(x) \
7622 case x: \
7623 FAST256(newargs[x - 1]);
7624
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007625 static void
7626vtp_sgr_bulks(
7627 int argc,
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007628 int *args)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007629{
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007630#define MAXSGR 16
7631#define SGRBUFSIZE 2 + 4 * MAXSGR + 1 // '\033[' + SGR + 'm'
7632 char_u buf[SGRBUFSIZE];
7633 char_u *p;
7634 int in, out;
7635 int newargs[16];
7636 static int sgrfgr = -1, sgrfgg, sgrfgb;
7637 static int sgrbgr = -1, sgrbgg, sgrbgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007638
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007639 if (argc == 0)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007640 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007641 sgrfgr = sgrbgr = -1;
7642 vtp_printf("033[m");
7643 return;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007644 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007645
7646 in = out = 0;
7647 while (in < argc)
7648 {
7649 int s = args[in];
7650 int copylen = 1;
7651
7652 if (s == 38)
7653 {
7654 if (argc - in >= 5 && args[in + 1] == 2)
7655 {
7656 if (sgrfgr == args[in + 2] && sgrfgg == args[in + 3]
7657 && sgrfgb == args[in + 4])
7658 {
7659 in += 5;
7660 copylen = 0;
7661 }
7662 else
7663 {
7664 sgrfgr = args[in + 2];
7665 sgrfgg = args[in + 3];
7666 sgrfgb = args[in + 4];
7667 copylen = 5;
7668 }
7669 }
7670 else if (argc - in >= 3 && args[in + 1] == 5)
7671 {
7672 sgrfgr = -1;
7673 copylen = 3;
7674 }
7675 }
7676 else if (s == 48)
7677 {
7678 if (argc - in >= 5 && args[in + 1] == 2)
7679 {
7680 if (sgrbgr == args[in + 2] && sgrbgg == args[in + 3]
7681 && sgrbgb == args[in + 4])
7682 {
7683 in += 5;
7684 copylen = 0;
7685 }
7686 else
7687 {
7688 sgrbgr = args[in + 2];
7689 sgrbgg = args[in + 3];
7690 sgrbgb = args[in + 4];
7691 copylen = 5;
7692 }
7693 }
7694 else if (argc - in >= 3 && args[in + 1] == 5)
7695 {
7696 sgrbgr = -1;
7697 copylen = 3;
7698 }
7699 }
7700 else if (30 <= s && s <= 39)
7701 sgrfgr = -1;
7702 else if (90 <= s && s <= 97)
7703 sgrfgr = -1;
7704 else if (40 <= s && s <= 49)
7705 sgrbgr = -1;
7706 else if (100 <= s && s <= 107)
7707 sgrbgr = -1;
7708 else if (s == 0)
7709 sgrfgr = sgrbgr = -1;
7710
7711 while (copylen--)
7712 newargs[out++] = args[in++];
7713 }
7714
7715 p = &buf[sizeof(buf) - 1];
7716 *p-- = 'm';
7717
7718 switch (out)
7719 {
7720 int n, m;
7721 DWORD r;
7722
7723 FAST256CASE(16);
7724 *p-- = ';';
7725 FAST256CASE(15);
7726 *p-- = ';';
7727 FAST256CASE(14);
7728 *p-- = ';';
7729 FAST256CASE(13);
7730 *p-- = ';';
7731 FAST256CASE(12);
7732 *p-- = ';';
7733 FAST256CASE(11);
7734 *p-- = ';';
7735 FAST256CASE(10);
7736 *p-- = ';';
7737 FAST256CASE(9);
7738 *p-- = ';';
7739 FAST256CASE(8);
7740 *p-- = ';';
7741 FAST256CASE(7);
7742 *p-- = ';';
7743 FAST256CASE(6);
7744 *p-- = ';';
7745 FAST256CASE(5);
7746 *p-- = ';';
7747 FAST256CASE(4);
7748 *p-- = ';';
7749 FAST256CASE(3);
7750 *p-- = ';';
7751 FAST256CASE(2);
7752 *p-- = ';';
7753 FAST256CASE(1);
7754 *p-- = '[';
7755 *p = '\033';
7756 WriteConsoleA(g_hConOut, p, (DWORD)(&buf[SGRBUFSIZE] - p), &r, NULL);
7757 default:
7758 break;
7759 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007760}
7761
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007762# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007763 static int
7764ctermtoxterm(
7765 int cterm)
7766{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007767 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007768
7769 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7770 return (((int)r << 16) | ((int)g << 8) | (int)b);
7771}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007772# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007773
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007774 static void
7775set_console_color_rgb(void)
7776{
7777# ifdef FEAT_TERMGUICOLORS
7778 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007779 guicolor_T fg, bg;
7780 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007781
7782 if (!USE_VTP)
7783 return;
7784
Bram Moolenaara050b942019-12-02 21:35:31 +01007785 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7786
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007787 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7788 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7789
7790 csbi.cbSize = sizeof(csbi);
7791 if (has_csbiex)
7792 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7793
7794 csbi.cbSize = sizeof(csbi);
7795 csbi.srWindow.Right += 1;
7796 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007797 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7798 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007799 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7800 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007801 if (has_csbiex)
7802 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7803# endif
7804}
7805
Bram Moolenaara050b942019-12-02 21:35:31 +01007806# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7807 void
7808get_default_console_color(
7809 int *cterm_fg,
7810 int *cterm_bg,
7811 guicolor_T *gui_fg,
7812 guicolor_T *gui_bg)
7813{
7814 int id;
7815 guicolor_T guifg = INVALCOLOR;
7816 guicolor_T guibg = INVALCOLOR;
7817 int ctermfg = 0;
7818 int ctermbg = 0;
7819
7820 id = syn_name2id((char_u *)"Normal");
7821 if (id > 0 && p_tgc)
7822 syn_id2colors(id, &guifg, &guibg);
7823 if (guifg == INVALCOLOR)
7824 {
7825 ctermfg = -1;
7826 if (id > 0)
7827 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7828 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7829 : default_console_color_fg;
7830 cterm_normal_fg_gui_color = guifg;
7831 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7832 }
7833 if (guibg == INVALCOLOR)
7834 {
7835 ctermbg = -1;
7836 if (id > 0)
7837 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7838 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7839 : default_console_color_bg;
7840 cterm_normal_bg_gui_color = guibg;
7841 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7842 }
7843
7844 *cterm_fg = ctermfg;
7845 *cterm_bg = ctermbg;
7846 *gui_fg = guifg;
7847 *gui_bg = guibg;
7848}
7849# endif
7850
Bram Moolenaardf543822020-01-30 11:53:59 +01007851/*
7852 * Set the console colors to the original colors or the last set colors.
7853 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007854 static void
7855reset_console_color_rgb(void)
7856{
7857# ifdef FEAT_TERMGUICOLORS
7858 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7859
7860 csbi.cbSize = sizeof(csbi);
7861 if (has_csbiex)
7862 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7863
7864 csbi.cbSize = sizeof(csbi);
7865 csbi.srWindow.Right += 1;
7866 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007867 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7868 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7869 if (has_csbiex)
7870 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7871# endif
7872}
7873
7874/*
7875 * Set the console colors to the original colors.
7876 */
7877 static void
7878restore_console_color_rgb(void)
7879{
7880# ifdef FEAT_TERMGUICOLORS
7881 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7882
7883 csbi.cbSize = sizeof(csbi);
7884 if (has_csbiex)
7885 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7886
7887 csbi.cbSize = sizeof(csbi);
7888 csbi.srWindow.Right += 1;
7889 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007890 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7891 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007892 if (has_csbiex)
7893 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7894# endif
7895}
7896
7897 void
7898control_console_color_rgb(void)
7899{
7900 if (USE_VTP)
7901 set_console_color_rgb();
7902 else
7903 reset_console_color_rgb();
7904}
7905
7906 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007907use_vtp(void)
7908{
7909 return USE_VTP;
7910}
7911
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007912 int
7913is_term_win32(void)
7914{
7915 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7916}
7917
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007918 int
7919has_vtp_working(void)
7920{
7921 return vtp_working;
7922}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007923
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007924#endif
7925
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007926 int
7927has_conpty_working(void)
7928{
7929 return conpty_working;
7930}
7931
7932 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007933get_conpty_type(void)
7934{
7935 return conpty_type;
7936}
7937
7938 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007939is_conpty_stable(void)
7940{
7941 return conpty_stable;
7942}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007943
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007944 int
7945get_conpty_fix_type(void)
7946{
7947 return conpty_fix_type;
7948}
7949
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007950#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007951 void
7952resize_console_buf(void)
7953{
7954 CONSOLE_SCREEN_BUFFER_INFO csbi;
7955 COORD coord;
7956 SMALL_RECT newsize;
7957
7958 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7959 {
7960 coord.X = SRWIDTH(csbi.srWindow);
7961 coord.Y = SRHEIGHT(csbi.srWindow);
7962 SetConsoleScreenBufferSize(g_hConOut, coord);
7963
7964 newsize.Left = 0;
7965 newsize.Top = 0;
7966 newsize.Right = coord.X - 1;
7967 newsize.Bottom = coord.Y - 1;
7968 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7969
7970 SetConsoleScreenBufferSize(g_hConOut, coord);
7971 }
7972}
7973#endif