blob: 7c2051f7f3971756dd4ae10332a847a0139dbbf0 [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>
Bram Moolenaare7bebc42021-02-01 20:50:37 +010036# include <winternl.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39#undef chdir
40#ifdef __GNUC__
41# ifndef __MINGW32__
42# include <dirent.h>
43# endif
44#else
45# include <direct.h>
46#endif
47
Bram Moolenaar82881492012-11-20 16:53:39 +010048#ifndef PROTO
Bram Moolenaar4f974752019-02-17 17:44:42 +010049# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010050# include <shellapi.h>
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052#endif
53
Bram Moolenaarfb630902016-10-29 14:55:00 +020054#ifdef FEAT_JOB_CHANNEL
55# include <tlhelp32.h>
56#endif
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef __MINGW32__
59# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
60# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
61# endif
62# ifndef RIGHTMOST_BUTTON_PRESSED
63# define RIGHTMOST_BUTTON_PRESSED 0x0002
64# endif
65# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
66# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
67# endif
68# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
69# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
70# endif
71# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
72# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
73# endif
74
75/*
76 * EventFlags
77 */
78# ifndef MOUSE_MOVED
79# define MOUSE_MOVED 0x0001
80# endif
81# ifndef DOUBLE_CLICK
82# define DOUBLE_CLICK 0x0002
83# endif
84#endif
85
Bram Moolenaar0f873732019-12-05 20:28:46 +010086// Record all output and all keyboard & mouse input
87// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89#ifdef MCH_WRITE_DUMP
90FILE* fdDump = NULL;
91#endif
92
93/*
94 * When generating prototypes for Win32 on Unix, these lines make the syntax
95 * errors disappear. They do not need to be correct.
96 */
97#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010098# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000099typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000100typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101typedef int ACCESS_MASK;
102typedef int BOOL;
103typedef int COLORREF;
104typedef int CONSOLE_CURSOR_INFO;
105typedef int COORD;
106typedef int DWORD;
107typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100108typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109typedef int HDC;
110typedef int HFONT;
111typedef int HICON;
112typedef int HINSTANCE;
113typedef int HWND;
114typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200115typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116typedef int KEY_EVENT_RECORD;
117typedef int LOGFONT;
118typedef int LPBOOL;
119typedef int LPCTSTR;
120typedef int LPDWORD;
121typedef int LPSTR;
122typedef int LPTSTR;
123typedef int LPVOID;
124typedef int MOUSE_EVENT_RECORD;
125typedef int PACL;
126typedef int PDWORD;
127typedef int PHANDLE;
128typedef int PRINTDLG;
129typedef int PSECURITY_DESCRIPTOR;
130typedef int PSID;
131typedef int SECURITY_INFORMATION;
132typedef int SHORT;
133typedef int SMALL_RECT;
134typedef int TEXTMETRIC;
135typedef int TOKEN_INFORMATION_CLASS;
136typedef int TRUSTEE;
137typedef int WORD;
138typedef int WCHAR;
139typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100140typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200141typedef int SE_OBJECT_TYPE;
142typedef int PSNSECINFO;
143typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100144typedef int STARTUPINFO;
145typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200146typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100147# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
149
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200150#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100151// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
153static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
154
Bram Moolenaar0f873732019-12-05 20:28:46 +0100155// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100157static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaar0f873732019-12-05 20:28:46 +0100159// The attribute of the screen when the editor was started
160static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161static WORD g_attrCurrent;
162
Bram Moolenaar0f873732019-12-05 20:28:46 +0100163static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
164static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
165static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static void scroll(unsigned cLines);
168static void set_scroll_region(unsigned left, unsigned top,
169 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100170static void set_scroll_region_tb(unsigned top, unsigned bottom);
171static void set_scroll_region_lr(unsigned left, unsigned right);
172static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void delete_lines(unsigned cLines);
174static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static int s_cursor_visible = TRUE;
177static int did_create_conin = FALSE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200178#endif
179#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static int s_dont_use_vimrun = TRUE;
181static int need_vimrun_warning = FALSE;
182static char *vimrun_path = "vimrun ";
183#endif
184
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200185static int win32_getattrs(char_u *name);
186static int win32_setattrs(char_u *name, int attrs);
187static int win32_set_archive(char_u *name);
188
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100189static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200190static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100191static int conpty_stable = 0;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +0200192static int conpty_fix_type = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100193static void vtp_flag_init();
194
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200195#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100196static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100197static void vtp_init();
198static void vtp_exit();
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100199static void vtp_sgr_bulk(int arg);
200static void vtp_sgr_bulks(int argc, int *argv);
201
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200202static int wt_working = 0;
203static void wt_init();
204
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100205static guicolor_T save_console_bg_rgb;
206static guicolor_T save_console_fg_rgb;
Bram Moolenaardf543822020-01-30 11:53:59 +0100207static guicolor_T store_console_bg_rgb;
208static guicolor_T store_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100209
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200210static int g_color_index_bg = 0;
211static int g_color_index_fg = 7;
212
213# ifdef FEAT_TERMGUICOLORS
214static int default_console_color_bg = 0x000000; // black
215static int default_console_color_fg = 0xc0c0c0; // white
216# endif
217
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100218# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200219# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200220# define USE_WT (wt_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100221# else
222# define USE_VTP 0
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200223# define USE_WT 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100224# endif
225
226static void set_console_color_rgb(void);
227static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100228static void restore_console_color_rgb(void);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100229#endif
230
Bram Moolenaar0f873732019-12-05 20:28:46 +0100231// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100232#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
233# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
234#endif
235
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200236#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100237static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#endif
239
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200240static char_u *exe_path = NULL;
241
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100242static BOOL win8_or_later = FALSE;
243
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200244# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
245# define UChar UnicodeChar
246# else
247# define UChar uChar.UnicodeChar
248# endif
249
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200250#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100251// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100252typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
253{
254 ULONG cbSize;
255 COORD dwSize;
256 COORD dwCursorPosition;
257 WORD wAttributes;
258 SMALL_RECT srWindow;
259 COORD dwMaximumWindowSize;
260 WORD wPopupAttributes;
261 BOOL bFullscreenSupported;
262 COLORREF ColorTable[16];
263} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
264typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
265static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
266typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
267static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
268static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100269#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100270
271/*
272 * Get version number including build number
273 */
274typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100275#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100276 (((major) << 24) | ((minor) << 16) | (build))
277
278 static DWORD
279get_build_number(void)
280{
281 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
282 HMODULE hNtdll;
283 PfnRtlGetVersion pRtlGetVersion;
284 DWORD ver = MAKE_VER(0, 0, 0);
285
286 hNtdll = GetModuleHandle("ntdll.dll");
287 if (hNtdll != NULL)
288 {
289 pRtlGetVersion =
290 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
291 pRtlGetVersion(&osver);
292 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
293 min(osver.dwMinorVersion, 255),
294 min(osver.dwBuildNumber, 32767));
295 }
296 return ver;
297}
298
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200299#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200300 static BOOL
301is_ambiwidth_event(
302 INPUT_RECORD *ir)
303{
304 return ir->EventType == KEY_EVENT
305 && ir->Event.KeyEvent.bKeyDown
306 && ir->Event.KeyEvent.wRepeatCount == 1
307 && ir->Event.KeyEvent.wVirtualKeyCode == 0x12
308 && ir->Event.KeyEvent.wVirtualScanCode == 0x38
309 && ir->Event.KeyEvent.UChar == 0
310 && ir->Event.KeyEvent.dwControlKeyState == 2;
311}
312
313 static void
314make_ambiwidth_event(
315 INPUT_RECORD *down,
316 INPUT_RECORD *up)
317{
318 down->Event.KeyEvent.wVirtualKeyCode = 0;
319 down->Event.KeyEvent.wVirtualScanCode = 0;
320 down->Event.KeyEvent.UChar = up->Event.KeyEvent.UChar;
321 down->Event.KeyEvent.dwControlKeyState = 0;
322}
323
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100324/*
325 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100326 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100327 */
328 static BOOL
329read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100330 HANDLE hInput,
331 INPUT_RECORD *lpBuffer,
332 DWORD nLength,
333 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100334{
335 enum
336 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100337 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100338 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100339 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100340 static DWORD s_dwIndex = 0;
341 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100342 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100343 int head;
344 int tail;
345 int i;
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200346 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100347
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200348 if (nLength == -2)
349 return (s_dwMax > 0) ? TRUE : FALSE;
350
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100351 if (!win8_or_later)
352 {
353 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200354 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
355 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100356 }
357
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100358 if (s_dwMax == 0)
359 {
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200360 if (!USE_WT && nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200361 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200362 GetNumberOfConsoleInputEvents(hInput, &dwEvents);
363 if (dwEvents == 0 && nLength == -1)
364 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
365 ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents);
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100366 s_dwIndex = 0;
367 s_dwMax = dwEvents;
368 if (dwEvents == 0)
369 {
370 *lpEvents = 0;
371 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100372 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100373
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200374 for (i = s_dwIndex; i < (int)s_dwMax - 1; ++i)
375 if (is_ambiwidth_event(&s_irCache[i]))
376 make_ambiwidth_event(&s_irCache[i], &s_irCache[i + 1]);
377
Bram Moolenaardd415a62014-02-05 14:02:27 +0100378 if (s_dwMax > 1)
379 {
380 head = 0;
381 tail = s_dwMax - 1;
382 while (head != tail)
383 {
384 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
385 && s_irCache[head + 1].EventType
386 == WINDOW_BUFFER_SIZE_EVENT)
387 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100388 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100389 for (i = head; i < tail; ++i)
390 s_irCache[i] = s_irCache[i + 1];
391 --tail;
392 continue;
393 }
394 head++;
395 }
396 s_dwMax = tail + 1;
397 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100398 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100399
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200400 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
401 {
402 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
403 {
404 s_irPseudo = s_irCache[s_dwIndex];
405 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
406 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
407 *lpBuffer = s_irPseudo;
408 *lpEvents = 1;
409 return TRUE;
410 }
411 }
412
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100413 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200414 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100415 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100416 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100417 return TRUE;
418}
419
420/*
421 * Version of PeekConsoleInput() that works with IME.
422 */
423 static BOOL
424peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100425 HANDLE hInput,
426 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200427 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100428 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100429{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100430 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100431}
432
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100433# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200434 static DWORD
435msg_wait_for_multiple_objects(
436 DWORD nCount,
437 LPHANDLE pHandles,
438 BOOL fWaitAll,
439 DWORD dwMilliseconds,
440 DWORD dwWakeMask)
441{
442 if (read_console_input(NULL, NULL, -2, NULL))
443 return WAIT_OBJECT_0;
444 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
445 dwMilliseconds, dwWakeMask);
446}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100447# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200448
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100449# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200450 static DWORD
451wait_for_single_object(
452 HANDLE hHandle,
453 DWORD dwMilliseconds)
454{
455 if (read_console_input(NULL, NULL, -2, NULL))
456 return WAIT_OBJECT_0;
457 return WaitForSingleObject(hHandle, dwMilliseconds);
458}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100459# endif
460#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 static void
463get_exe_name(void)
464{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100465 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
466 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100467#define MAX_ENV_PATH_LEN 8192
468 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200469 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471 if (exe_name == NULL)
472 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100473 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100474 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (*temp != NUL)
476 exe_name = FullName_save((char_u *)temp, FALSE);
477 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000478
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000480 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200481 exe_path = vim_strnsave(exe_name, gettail_sep(exe_name) - exe_name);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200482 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // Append our starting directory to $PATH, so that when doing
485 // "!xxd" it's found in our starting directory. Needed because
486 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200487 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100488 if (p == NULL
489 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200490 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100491 if (p == NULL || *p == NUL)
492 temp[0] = NUL;
493 else
494 {
495 STRCPY(temp, p);
496 STRCAT(temp, ";");
497 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200498 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100499 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200500 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200505/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100506 * Unescape characters in "p" that appear in "escaped".
507 */
508 static void
509unescape_shellxquote(char_u *p, char_u *escaped)
510{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100511 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100512 int n;
513
514 while (*p != NUL)
515 {
516 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
517 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100518 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100519 p += n;
520 l -= n;
521 }
522}
523
524/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200525 * Load library "name".
526 */
527 HINSTANCE
528vimLoadLib(char *name)
529{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200530 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200531
Bram Moolenaar0f873732019-12-05 20:28:46 +0100532 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
533 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200534 if (exe_path == NULL)
535 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200536 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200537 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200538 WCHAR old_dirw[MAXPATHL];
539
540 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
541 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100542 // Change directory to where the executable is, both to make
543 // sure we find a .dll there and to avoid looking for a .dll
544 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100545 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200546 dll = LoadLibrary(name);
547 SetCurrentDirectoryW(old_dirw);
548 return dll;
549 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200550 }
551 return dll;
552}
553
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200554#if defined(VIMDLL) || defined(PROTO)
555/*
556 * Check if the current executable file is for the GUI subsystem.
557 */
558 int
559mch_is_gui_executable(void)
560{
561 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
562 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
563 PIMAGE_NT_HEADERS pPE;
564
565 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
566 return FALSE;
567 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
568 if (pPE->Signature != IMAGE_NT_SIGNATURE)
569 return FALSE;
570 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
571 return TRUE;
572 return FALSE;
573}
574#endif
575
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100576#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
577/*
578 * Get related information about 'funcname' which is imported by 'hInst'.
579 * If 'info' is 0, return the function address.
580 * If 'info' is 1, return the module name which the function is imported from.
581 */
582 static void *
583get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
584{
585 PBYTE pImage = (PBYTE)hInst;
586 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
587 PIMAGE_NT_HEADERS pPE;
588 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100589 PIMAGE_THUNK_DATA pIAT; // Import Address Table
590 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100591 PIMAGE_IMPORT_BY_NAME pImpName;
592
593 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
594 return NULL;
595 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
596 if (pPE->Signature != IMAGE_NT_SIGNATURE)
597 return NULL;
598 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
599 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
600 .VirtualAddress);
601 for (; pImpDesc->FirstThunk; ++pImpDesc)
602 {
603 if (!pImpDesc->OriginalFirstThunk)
604 continue;
605 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
606 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
607 for (; pIAT->u1.Function; ++pIAT, ++pINT)
608 {
609 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
610 continue;
611 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
612 + (UINT_PTR)(pINT->u1.AddressOfData));
613 if (strcmp((char *)pImpName->Name, funcname) == 0)
614 {
615 switch (info)
616 {
617 case 0:
618 return (void *)pIAT->u1.Function;
619 case 1:
620 return (void *)(pImage + pImpDesc->Name);
621 default:
622 return NULL;
623 }
624 }
625 }
626 }
627 return NULL;
628}
629
630/*
631 * Get the module handle which 'funcname' in 'hInst' is imported from.
632 */
633 HINSTANCE
634find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
635{
636 char *modulename;
637
638 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
639 if (modulename != NULL)
640 return GetModuleHandleA(modulename);
641 return NULL;
642}
643
644/*
645 * Get the address of 'funcname' which is imported by 'hInst' DLL.
646 */
647 void *
648get_dll_import_func(HINSTANCE hInst, const char *funcname)
649{
650 return get_imported_func_info(hInst, funcname, 0);
651}
652#endif
653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
655# ifndef GETTEXT_DLL
656# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200657# define GETTEXT_DLL_ALT1 "libintl-8.dll"
658# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100660// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000661static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200662static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000663static char *null_libintl_textdomain(const char *);
664static char *null_libintl_bindtextdomain(const char *, const char *);
665static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100666static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200668static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000669char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200670char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
671 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000672char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
673char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000675char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
676 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100677int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100680dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 int i;
683 static struct
684 {
685 char *name;
686 FARPROC *ptr;
687 } libintl_entry[] =
688 {
689 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200690 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
692 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
693 {NULL, NULL}
694 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100695 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar7554c542018-10-06 15:03:15 +0200697 // No need to initialize twice.
698 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200700 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100701 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100702# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100703 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200704 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100705# endif
706# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200707 if (!hLibintlDLL)
708 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100709# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100710 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200712 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200714 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200716 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200718 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 for (i = 0; libintl_entry[i].name != NULL
721 && libintl_entry[i].ptr != NULL; ++i)
722 {
723 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
724 libintl_entry[i].name)) == NULL)
725 {
726 dyn_libintl_end();
727 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000728 {
729 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100730 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000731 verbose_leave();
732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return 0;
734 }
735 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000736
Bram Moolenaar0f873732019-12-05 20:28:46 +0100737 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000738 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000739 "bind_textdomain_codeset");
740 if (dyn_libintl_bind_textdomain_codeset == NULL)
741 dyn_libintl_bind_textdomain_codeset =
742 null_libintl_bind_textdomain_codeset;
743
Bram Moolenaar0f873732019-12-05 20:28:46 +0100744 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100745 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
746 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100747 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100748 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
749 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 return 1;
752}
753
754 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100755dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 if (hLibintlDLL)
758 FreeLibrary(hLibintlDLL);
759 hLibintlDLL = NULL;
760 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200761 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 dyn_libintl_textdomain = null_libintl_textdomain;
763 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000764 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100765 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766}
767
768 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000769null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 return (char*)msgid;
772}
773
774 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200775null_libintl_ngettext(
776 const char *msgid,
777 const char *msgid_plural,
778 unsigned long n)
779{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200780 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200781}
782
Bram Moolenaaree695f72016-08-03 22:08:45 +0200783 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100784null_libintl_bindtextdomain(
785 const char *domainname UNUSED,
786 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 return NULL;
789}
790
791 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100792null_libintl_bind_textdomain_codeset(
793 const char *domainname UNUSED,
794 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000795{
796 return NULL;
797}
798
799 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 return NULL;
803}
804
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200805 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100806null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100807{
808 return 0;
809}
810
Bram Moolenaar0f873732019-12-05 20:28:46 +0100811#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar0f873732019-12-05 20:28:46 +0100813// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815#ifndef VER_PLATFORM_WIN32_WINDOWS
816# define VER_PLATFORM_WIN32_WINDOWS 1
817#endif
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100820# ifndef PROTO
821# include <aclapi.h>
822# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200823# ifndef PROTECTED_DACL_SECURITY_INFORMATION
824# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
827
Bram Moolenaar27515922013-06-29 15:36:26 +0200828#ifdef HAVE_ACL
829/*
830 * Enables or disables the specified privilege.
831 */
832 static BOOL
833win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
834{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100835 BOOL bResult;
836 LUID luid;
837 HANDLE hToken;
838 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200839
840 if (!OpenProcessToken(GetCurrentProcess(),
841 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
842 return FALSE;
843
844 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
845 {
846 CloseHandle(hToken);
847 return FALSE;
848 }
849
Bram Moolenaar45500912014-07-09 20:51:07 +0200850 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200851 tokenPrivileges.Privileges[0].Luid = luid;
852 tokenPrivileges.Privileges[0].Attributes = bEnable ?
853 SE_PRIVILEGE_ENABLED : 0;
854
855 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
856 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
857
858 CloseHandle(hToken);
859
860 return bResult && GetLastError() == ERROR_SUCCESS;
861}
862#endif
863
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100864#ifdef _MSC_VER
865// Suppress the deprecation warning for using GetVersionEx().
866// It is needed for implementing "windowsversion()".
867# pragma warning(push)
868# pragma warning(disable: 4996)
869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870/*
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200871 * Set "win8_or_later" and fill in "windowsVersion" if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
873 void
874PlatformId(void)
875{
876 static int done = FALSE;
877
878 if (!done)
879 {
880 OSVERSIONINFO ovi;
881
882 ovi.dwOSVersionInfoSize = sizeof(ovi);
883 GetVersionEx(&ovi);
884
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200885#ifdef FEAT_EVAL
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100886 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
887 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200888#endif
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100889 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
890 || ovi.dwMajorVersion > 6)
891 win8_or_later = TRUE;
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100894 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200895 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#endif
897 done = TRUE;
898 }
899}
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100900#ifdef _MSC_VER
901# pragma warning(pop)
902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200904#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100906# define SHIFT (SHIFT_PRESSED)
907# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
908# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
909# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911
Bram Moolenaar0f873732019-12-05 20:28:46 +0100912// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
913// We map function keys to their ANSI terminal equivalents, as produced
914// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
915// ANSI key with a value >= '\300' is nonstandard, but provided anyway
916// so that the user can have access to all SHIFT-, CTRL-, and ALT-
917// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000919static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 WORD wVirtKey;
922 BOOL fAnsiKey;
923 int chAlone;
924 int chShift;
925 int chCtrl;
926 int chAlt;
927} VirtKeyMap[] =
928{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200929// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
931
932 { VK_F1, TRUE, ';', 'T', '^', 'h', },
933 { VK_F2, TRUE, '<', 'U', '_', 'i', },
934 { VK_F3, TRUE, '=', 'V', '`', 'j', },
935 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
936 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
937 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
938 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
939 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
940 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
941 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200942 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
943 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200945 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
946 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
947 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
948 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
949 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
950 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
951 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
952 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
953 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
954 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100955 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200957 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100959# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200960 // Most people don't have F13-F20, but what the hell...
961 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
962 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
963 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
964 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
965 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
966 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
967 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
968 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100969# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200970 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
971 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
972 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
973 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200975 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
976 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
977 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
978 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
979 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
980 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
981 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
982 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
983 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
984 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100985 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986};
987
988
Bram Moolenaar0f873732019-12-05 20:28:46 +0100989/*
990 * The return code indicates key code size.
991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000994 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 UINT uMods = pker->dwControlKeyState;
997 static int s_iIsDead = 0;
998 static WORD awAnsiCode[2];
999 static BYTE abKeystate[256];
1000
1001
1002 if (s_iIsDead == 2)
1003 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001004 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 s_iIsDead = 0;
1006 return 1;
1007 }
1008
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001009 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return 1;
1011
Bram Moolenaara80faa82020-04-12 19:37:17 +02001012 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar0f873732019-12-05 20:28:46 +01001014 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001015 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 if (uMods & SHIFT_PRESSED)
1018 abKeystate[VK_SHIFT] = 0x80;
1019 if (uMods & CAPSLOCK_ON)
1020 abKeystate[VK_CAPITAL] = 1;
1021
1022 if ((uMods & ALT_GR) == ALT_GR)
1023 {
1024 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
1025 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
1026 }
1027
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001028 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
1029 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001032 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 return s_iIsDead;
1035}
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037static BOOL g_fJustGotFocus = FALSE;
1038
1039/*
1040 * Decode a KEY_EVENT into one or two keystrokes
1041 */
1042 static BOOL
1043decode_key_event(
1044 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001045 WCHAR *pch,
1046 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001048 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 int i;
1051 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1052
1053 *pch = *pch2 = NUL;
1054 g_fJustGotFocus = FALSE;
1055
Bram Moolenaar0f873732019-12-05 20:28:46 +01001056 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (!pker->bKeyDown)
1058 return FALSE;
1059
Bram Moolenaar0f873732019-12-05 20:28:46 +01001060 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 switch (pker->wVirtualKeyCode)
1062 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001063 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 case VK_SHIFT:
1065 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001066 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return FALSE;
1068
1069 default:
1070 break;
1071 }
1072
Bram Moolenaar0f873732019-12-05 20:28:46 +01001073 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001074 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001076 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (pker->wVirtualKeyCode == '6')
1078 {
1079 *pch = Ctrl_HAT;
1080 return TRUE;
1081 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001082 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 else if (pker->wVirtualKeyCode == '2')
1084 {
1085 *pch = NUL;
1086 return TRUE;
1087 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001088 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 else if (pker->wVirtualKeyCode == 0xBD)
1090 {
1091 *pch = Ctrl__;
1092 return TRUE;
1093 }
1094 }
1095
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1098 {
1099 *pch = K_NUL;
1100 *pch2 = '\017';
1101 return TRUE;
1102 }
1103
1104 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1105 {
1106 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1107 {
1108 if (nModifs == 0)
1109 *pch = VirtKeyMap[i].chAlone;
1110 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1111 *pch = VirtKeyMap[i].chShift;
1112 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1113 *pch = VirtKeyMap[i].chCtrl;
1114 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1115 *pch = VirtKeyMap[i].chAlt;
1116
1117 if (*pch != 0)
1118 {
1119 if (VirtKeyMap[i].fAnsiKey)
1120 {
1121 *pch2 = *pch;
1122 *pch = K_NUL;
1123 }
1124
1125 return TRUE;
1126 }
1127 }
1128 }
1129
1130 i = win32_kbd_patch_key(pker);
1131
1132 if (i < 0)
1133 *pch = NUL;
1134 else
1135 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001136 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 if (pmodifiers != NULL)
1139 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001140 // Pass on the ALT key as a modifier, but only when not combined
1141 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1143 *pmodifiers |= MOD_MASK_ALT;
1144
Bram Moolenaar0f873732019-12-05 20:28:46 +01001145 // Pass on SHIFT only for special keys, because we don't know when
1146 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1148 *pmodifiers |= MOD_MASK_SHIFT;
1149
Bram Moolenaar0f873732019-12-05 20:28:46 +01001150 // Pass on CTRL only for non-special keys, because we don't know
1151 // when it's already included with the character. And not when
1152 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1154 && *pch >= 0x20 && *pch < 0x80)
1155 *pmodifiers |= MOD_MASK_CTRL;
1156 }
1157 }
1158
1159 return (*pch != NUL);
1160}
1161
Bram Moolenaar0f873732019-12-05 20:28:46 +01001162#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
1166 * For the GUI the mouse handling is in gui_w32.c.
1167 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001168#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001170mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001173#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001174static int g_fMouseAvail = FALSE; // mouse present
1175static int g_fMouseActive = FALSE; // mouse enabled
1176static int g_nMouseClick = -1; // mouse status
1177static int g_xMouse; // mouse x coordinate
1178static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
1180/*
1181 * Enable or disable mouse input
1182 */
1183 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001184mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 DWORD cmodein;
1187
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001188# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001189 if (gui.in_use)
1190 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (!g_fMouseAvail)
1193 return;
1194
1195 g_fMouseActive = on;
1196 GetConsoleMode(g_hConIn, &cmodein);
1197
1198 if (g_fMouseActive)
1199 cmodein |= ENABLE_MOUSE_INPUT;
1200 else
1201 cmodein &= ~ENABLE_MOUSE_INPUT;
1202
1203 SetConsoleMode(g_hConIn, cmodein);
1204}
1205
Bram Moolenaar157d8132018-03-06 17:09:20 +01001206
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001207# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001208/*
1209 * Called when 'balloonevalterm' changed.
1210 */
1211 void
1212mch_bevalterm_changed(void)
1213{
1214 mch_setmouse(g_fMouseActive);
1215}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001216# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/*
1219 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1220 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1221 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1222 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1223 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1224 * and we return the mouse position in g_xMouse and g_yMouse.
1225 *
1226 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1227 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1228 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1229 *
1230 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1231 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1232 *
1233 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1234 * moves, even if it stays within the same character cell. We ignore
1235 * all MOUSE_MOVED messages if the position hasn't really changed, and
1236 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1237 * we're only interested in MOUSE_DRAG).
1238 *
1239 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1240 * 2-button mouses by pressing the left & right buttons simultaneously.
1241 * In practice, it's almost impossible to click both at the same time,
1242 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1243 * in such cases, if the user is clicking quickly.
1244 */
1245 static BOOL
1246decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001247 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 static int s_nOldButton = -1;
1250 static int s_nOldMouseClick = -1;
1251 static int s_xOldMouse = -1;
1252 static int s_yOldMouse = -1;
1253 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001254# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 static int s_cClicks = 1;
1258 static BOOL s_fReleased = TRUE;
1259 static DWORD s_dwLastClickTime = 0;
1260 static BOOL s_fNextIsMiddle = FALSE;
1261
Bram Moolenaar0f873732019-12-05 20:28:46 +01001262 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1265 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1266 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1267 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1268
1269 int nButton;
1270
1271 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1272 cButtons = 2;
1273
1274 if (!g_fMouseAvail || !g_fMouseActive)
1275 {
1276 g_nMouseClick = -1;
1277 return FALSE;
1278 }
1279
Bram Moolenaar0f873732019-12-05 20:28:46 +01001280 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (g_fJustGotFocus)
1282 {
1283 g_fJustGotFocus = FALSE;
1284 return FALSE;
1285 }
1286
Bram Moolenaar0f873732019-12-05 20:28:46 +01001287 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (g_nMouseClick != -1)
1289 return TRUE;
1290
1291 nButton = -1;
1292 g_xMouse = pmer->dwMousePosition.X;
1293 g_yMouse = pmer->dwMousePosition.Y;
1294
1295 if (pmer->dwEventFlags == MOUSE_MOVED)
1296 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001297 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1298 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1300 return FALSE;
1301 }
1302
Bram Moolenaar0f873732019-12-05 20:28:46 +01001303 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1305 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001306 nButton = MOUSE_RELEASE;
1307
Bram Moolenaar0f873732019-12-05 20:28:46 +01001308 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001310 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001311# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001312 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001313 if (p_bevalterm)
1314 nButton = MOUSE_DRAG;
1315 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001316# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001317 return FALSE;
1318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 s_fReleased = TRUE;
1321 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001322 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001324 // on a 2-button mouse, hold down left and right buttons
1325 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1328 {
1329 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1330
Bram Moolenaar0f873732019-12-05 20:28:46 +01001331 // if either left or right button only is pressed, see if the
1332 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (dwLR == LEFT || dwLR == RIGHT)
1334 {
1335 for (;;)
1336 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001337 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1339 != WAIT_OBJECT_0)
1340 break;
1341 else
1342 {
1343 DWORD cRecords = 0;
1344 INPUT_RECORD ir;
1345 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1346
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001347 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1350 || !(pmer2->dwButtonState & LEFT_RIGHT))
1351 break;
1352 else
1353 {
1354 if (pmer2->dwEventFlags != MOUSE_MOVED)
1355 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001356 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 return decode_mouse_event(pmer2);
1359 }
1360 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1361 s_yOldMouse == pmer2->dwMousePosition.Y)
1362 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001363 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001364 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar0f873732019-12-05 20:28:46 +01001366 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001367 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1370 break;
1371 }
1372 else
1373 break;
1374 }
1375 }
1376 }
1377 }
1378 }
1379
1380 if (s_fNextIsMiddle)
1381 {
1382 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1383 ? MOUSE_DRAG : MOUSE_MIDDLE;
1384 s_fNextIsMiddle = FALSE;
1385 }
1386 else if (cButtons == 2 &&
1387 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1388 {
1389 nButton = MOUSE_MIDDLE;
1390
1391 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1392 {
1393 s_fNextIsMiddle = TRUE;
1394 nButton = MOUSE_RELEASE;
1395 }
1396 }
1397 else if ((pmer->dwButtonState & LEFT) == LEFT)
1398 nButton = MOUSE_LEFT;
1399 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1400 nButton = MOUSE_MIDDLE;
1401 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1402 nButton = MOUSE_RIGHT;
1403
1404 if (! s_fReleased && ! s_fNextIsMiddle
1405 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1406 return FALSE;
1407
1408 s_fReleased = s_fNextIsMiddle;
1409 }
1410
1411 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1412 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001413 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (nButton != -1 && nButton != MOUSE_RELEASE)
1415 {
1416 DWORD dwCurrentTime = GetTickCount();
1417
1418 if (s_xOldMouse != g_xMouse
1419 || s_yOldMouse != g_yMouse
1420 || s_nOldButton != nButton
1421 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001422# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1426 {
1427 s_cClicks = 1;
1428 }
1429 else if (++s_cClicks > 4)
1430 {
1431 s_cClicks = 1;
1432 }
1433
1434 s_dwLastClickTime = dwCurrentTime;
1435 }
1436 }
1437 else if (pmer->dwEventFlags == MOUSE_MOVED)
1438 {
1439 if (nButton != -1 && nButton != MOUSE_RELEASE)
1440 nButton = MOUSE_DRAG;
1441
1442 s_cClicks = 1;
1443 }
1444
1445 if (nButton == -1)
1446 return FALSE;
1447
1448 if (nButton != MOUSE_RELEASE)
1449 s_nOldButton = nButton;
1450
1451 g_nMouseClick = nButton;
1452
1453 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1454 g_nMouseClick |= MOUSE_SHIFT;
1455 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1456 g_nMouseClick |= MOUSE_CTRL;
1457 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1458 g_nMouseClick |= MOUSE_ALT;
1459
1460 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1461 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1462
Bram Moolenaar0f873732019-12-05 20:28:46 +01001463 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (s_xOldMouse == g_xMouse
1465 && s_yOldMouse == g_yMouse
1466 && s_nOldMouseClick == g_nMouseClick)
1467 {
1468 g_nMouseClick = -1;
1469 return FALSE;
1470 }
1471
1472 s_xOldMouse = g_xMouse;
1473 s_yOldMouse = g_yMouse;
1474 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001475# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 s_nOldMouseClick = g_nMouseClick;
1479
1480 return TRUE;
1481}
1482
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001483#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485
1486#ifdef MCH_CURSOR_SHAPE
1487/*
1488 * Set the shape of the cursor.
1489 * 'thickness' can be from 1 (thin) to 99 (block)
1490 */
1491 static void
1492mch_set_cursor_shape(int thickness)
1493{
1494 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1495 ConsoleCursorInfo.dwSize = thickness;
1496 ConsoleCursorInfo.bVisible = s_cursor_visible;
1497
1498 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1499 if (s_cursor_visible)
1500 SetConsoleCursorPosition(g_hConOut, g_coord);
1501}
1502
1503 void
1504mch_update_cursor(void)
1505{
1506 int idx;
1507 int thickness;
1508
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001509# ifdef VIMDLL
1510 if (gui.in_use)
1511 return;
1512# endif
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /*
1515 * How the cursor is drawn depends on the current mode.
1516 */
1517 idx = get_shape_idx(FALSE);
1518
1519 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001520 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 else
1522 thickness = shape_table[idx].percentage;
1523 mch_set_cursor_shape(thickness);
1524}
1525#endif
1526
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001527#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Handle FOCUS_EVENT.
1530 */
1531 static void
1532handle_focus_event(INPUT_RECORD ir)
1533{
1534 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1535 ui_focus_change((int)g_fJustGotFocus);
1536}
1537
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001538static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
1541 * Wait until console input from keyboard or mouse is available,
1542 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001543 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * Return TRUE if something is available FALSE if not.
1545 */
1546 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001547WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 DWORD dwNow = 0, dwEndTime = 0;
1550 INPUT_RECORD ir;
1551 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001552 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001553# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001554 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
1557 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001558 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 dwEndTime = GetTickCount() + msec;
1560 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001561 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 dwEndTime = INFINITE;
1563
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001564 // We need to loop until the end of the time period, because
1565 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 for (;;)
1567 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001568 // Only process messages when waiting.
1569 if (msec != 0)
1570 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001571# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001572 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001573# endif
1574# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001575 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001576# endif
1577# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001578 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001579# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001580 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001581
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001582 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001583# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001584 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 )
1587 return TRUE;
1588
1589 if (msec > 0)
1590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001591 // If the specified wait time has passed, return. Beware that
1592 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001594 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 break;
1596 }
1597 if (msec != 0)
1598 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001599 DWORD dwWaitTime = dwEndTime - dwNow;
1600
Bram Moolenaarc478ee32020-12-01 21:27:51 +01001601 // Don't wait for more than 11 msec to avoid dropping characters,
1602 // check channel while waiting for input and handle a callback from
1603 // 'balloonexpr'.
1604 if (dwWaitTime > 11)
1605 dwWaitTime = 11;
1606
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001607# ifdef FEAT_MZSCHEME
Bram Moolenaarc478ee32020-12-01 21:27:51 +01001608 if (mzthreads_allowed() && p_mzq > 0 && (long)dwWaitTime > p_mzq)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001609 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001610# endif
1611# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001612 // When waiting very briefly don't trigger timers.
1613 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001614 {
1615 long due_time;
1616
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001617 // Trigger timers and then get the time in msec until the next
1618 // one is due. Wait up to that time.
1619 due_time = check_due_timer();
1620 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001621 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001622 // timer may have used feedkeys().
1623 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001624 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001625 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1626 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001627 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001628# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001629 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001630# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001631 // Wait for either an event on the console input or a
1632 // message in the client-server window.
1633 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1634 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001635# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001636 wait_for_single_object(g_hConIn, dwWaitTime)
1637 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001638# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001639 )
1640 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642
1643 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001644 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001646# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (State & CMDLINE && msg_row == Rows - 1)
1648 {
1649 CONSOLE_SCREEN_BUFFER_INFO csbi;
1650
1651 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1652 {
1653 if (csbi.dwCursorPosition.Y != msg_row)
1654 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001655 // The screen is now messed up, must redraw the
1656 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 redraw_all_later(CLEAR);
1658 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1659 redrawcmd();
1660 }
1661 }
1662 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 if (cRecords > 0)
1666 {
1667 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1668 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001669# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001670 // Windows IME sends two '\n's with only one 'ENTER'. First:
1671 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001672 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1674 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001675 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 continue;
1677 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1680 NULL, FALSE))
1681 return TRUE;
1682 }
1683
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001684 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 if (ir.EventType == FOCUS_EVENT)
1687 handle_focus_event(ir);
1688 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001689 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001690 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1691
Bram Moolenaar36698e32019-12-11 22:57:40 +01001692 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001693 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001694 if (dwSize.X != Columns || dwSize.Y != Rows)
1695 {
1696 CONSOLE_SCREEN_BUFFER_INFO csbi;
1697 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001698 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001699 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001700 if (dwSize.X != Columns || dwSize.Y != Rows)
1701 {
1702 ResizeConBuf(g_hConOut, dwSize);
1703 shell_resized();
1704 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001705 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 else if (ir.EventType == MOUSE_EVENT
1708 && decode_mouse_event(&ir.Event.MouseEvent))
1709 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else if (msec == 0)
1712 break;
1713 }
1714
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001715# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001716 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (input_available())
1718 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001719# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 return FALSE;
1722}
1723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724/*
1725 * return non-zero if a character is available
1726 */
1727 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001728mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001730# ifdef VIMDLL
1731 if (gui.in_use)
1732 return TRUE;
1733# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001734 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001736
1737# if defined(FEAT_TERMINAL) || defined(PROTO)
1738/*
1739 * Check for any pending input or messages.
1740 */
1741 int
1742mch_check_messages(void)
1743{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001744# ifdef VIMDLL
1745 if (gui.in_use)
1746 return TRUE;
1747# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001748 return WaitForChar(0L, TRUE);
1749}
1750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752/*
1753 * Create the console input. Used when reading stdin doesn't work.
1754 */
1755 static void
1756create_conin(void)
1757{
1758 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1759 FILE_SHARE_READ|FILE_SHARE_WRITE,
1760 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001761 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 did_create_conin = TRUE;
1763}
1764
1765/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001766 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001768 static WCHAR
1769tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001771 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 for (;;)
1774 {
1775 INPUT_RECORD ir;
1776 DWORD cRecords = 0;
1777
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001778# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001779 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (input_available())
1781 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (g_nMouseClick != -1)
1783 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001784# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001785 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 if (did_create_conin)
1788 read_error_exit();
1789 create_conin();
1790 continue;
1791 }
1792
1793 if (ir.EventType == KEY_EVENT)
1794 {
1795 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1796 pmodifiers, TRUE))
1797 return ch;
1798 }
1799 else if (ir.EventType == FOCUS_EVENT)
1800 handle_focus_event(ir);
1801 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1802 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 else if (ir.EventType == MOUSE_EVENT)
1804 {
1805 if (decode_mouse_event(&ir.Event.MouseEvent))
1806 return 0;
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001810#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812
1813/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001814 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 * Get one or more characters from the keyboard or the mouse.
1816 * If time == 0, do not wait for characters.
1817 * If time == n, wait a short time for characters.
1818 * If time == -1, wait forever for characters.
1819 * Returns the number of characters read into buf.
1820 */
1821 int
1822mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001823 char_u *buf UNUSED,
1824 int maxlen UNUSED,
1825 long time UNUSED,
1826 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001828#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830 int len;
1831 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001832# ifdef VIMDLL
1833// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1834# define TYPEAHEADSPACE 6
1835# else
1836# define TYPEAHEADSPACE 0
1837# endif
1838# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001839 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 static int typeaheadlen = 0;
1841
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001842# ifdef VIMDLL
1843 if (gui.in_use)
1844 return 0;
1845# endif
1846
Bram Moolenaar0f873732019-12-05 20:28:46 +01001847 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (typeaheadlen > 0)
1849 goto theend;
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (time >= 0)
1852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001853 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001856 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001858 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar3918c952005-03-15 22:34:55 +00001860 /*
1861 * If there is no character available within 2 seconds (default)
1862 * write the autoscript file to disk. Or cause the CursorHold event
1863 * to be triggered.
1864 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001865 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001867 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001869 buf[0] = K_SPECIAL;
1870 buf[1] = KS_EXTRA;
1871 buf[2] = (int)KE_CURSORHOLD;
1872 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001874 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 }
1877
1878 /*
1879 * Try to read as many characters as there are, until the buffer is full.
1880 */
1881
Bram Moolenaar0f873732019-12-05 20:28:46 +01001882 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 g_fCBrkPressed = FALSE;
1884
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001885# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (fdDump)
1887 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaar0f873732019-12-05 20:28:46 +01001890 // Keep looping until there is something in the typeahead buffer and more
1891 // to get and still room in the buffer (up to two bytes for a char and
1892 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001893 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001894 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 if (typebuf_changed(tb_change_cnt))
1897 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001898 // "buf" may be invalid now if a client put something in the
1899 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 typeaheadlen = 0;
1901 break;
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (g_nMouseClick != -1)
1904 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001905# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (fdDump)
1907 fprintf(fdDump, "{%02x @ %d, %d}",
1908 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 typeahead[typeaheadlen++] = ESC + 128;
1911 typeahead[typeaheadlen++] = 'M';
1912 typeahead[typeaheadlen++] = g_nMouseClick;
1913 typeahead[typeaheadlen++] = g_xMouse + '!';
1914 typeahead[typeaheadlen++] = g_yMouse + '!';
1915 g_nMouseClick = -1;
1916 }
1917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001919 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 int modifiers = 0;
1921
1922 c = tgetch(&modifiers, &ch2);
1923
1924 if (typebuf_changed(tb_change_cnt))
1925 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001926 // "buf" may be invalid now if a client put something in the
1927 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 typeaheadlen = 0;
1929 break;
1930 }
1931
1932 if (c == Ctrl_C && ctrl_c_interrupts)
1933 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001934# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 got_int = TRUE;
1938 }
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 int n = 1;
1943
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001944 if (ch2 == NUL)
1945 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001946 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001947 char_u *p;
1948 WCHAR ch[2];
1949
1950 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001951 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001952 {
1953 ch[1] = tgetch(&modifiers, &ch2);
1954 n++;
1955 }
1956 p = utf16_to_enc(ch, &n);
1957 if (p != NULL)
1958 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001959 for (i = 0, j = 0; i < n; i++)
1960 {
1961 typeahead[typeaheadlen + j++] = p[i];
1962# ifdef VIMDLL
1963 if (p[i] == CSI)
1964 {
1965 typeahead[typeaheadlen + j++] = KS_EXTRA;
1966 typeahead[typeaheadlen + j++] = KE_CSI;
1967 }
1968# endif
1969 }
1970 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001971 vim_free(p);
1972 }
1973 }
1974 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001975 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001976 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001977# ifdef VIMDLL
1978 if (c == CSI)
1979 {
1980 typeahead[typeaheadlen + 1] = KS_EXTRA;
1981 typeahead[typeaheadlen + 2] = KE_CSI;
1982 n = 3;
1983 }
1984# endif
1985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (ch2 != NUL)
1987 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001988 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001989 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001990 switch (ch2)
1991 {
1992 case (WCHAR)'\324': // SHIFT+Insert
1993 case (WCHAR)'\325': // CTRL+Insert
1994 case (WCHAR)'\327': // SHIFT+Delete
1995 case (WCHAR)'\330': // CTRL+Delete
1996 typeahead[typeaheadlen + n] = (char_u)ch2;
1997 n++;
1998 break;
1999
2000 default:
2001 typeahead[typeaheadlen + n] = 3;
2002 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2003 n += 2;
2004 break;
2005 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002006 }
2007 else
2008 {
2009 typeahead[typeaheadlen + n] = 3;
2010 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2011 n += 2;
2012 }
Bram Moolenaar45500912014-07-09 20:51:07 +02002013 }
2014
Bram Moolenaar0f873732019-12-05 20:28:46 +01002015 // Use the ALT key to set the 8th bit of the character
2016 // when it's one byte, the 8th bit isn't set yet and not
2017 // using a double-byte encoding (would become a lead
2018 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if ((modifiers & MOD_MASK_ALT)
2020 && n == 1
2021 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 )
2024 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00002025 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
2026 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 modifiers &= ~MOD_MASK_ALT;
2028 }
2029
2030 if (modifiers != 0)
2031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002032 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 mch_memmove(typeahead + typeaheadlen + 3,
2034 typeahead + typeaheadlen, n);
2035 typeahead[typeaheadlen++] = K_SPECIAL;
2036 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2037 typeahead[typeaheadlen++] = modifiers;
2038 }
2039
2040 typeaheadlen += n;
2041
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002042# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (fdDump)
2044 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 }
2048 }
2049
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002050# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (fdDump)
2052 {
2053 fputs("]\n", fdDump);
2054 fflush(fdDump);
2055 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002059 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 len = 0;
2061 while (len < maxlen && typeaheadlen > 0)
2062 {
2063 buf[len++] = typeahead[0];
2064 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2065 }
Bram Moolenaar7ca86fe2020-09-03 19:25:11 +02002066# ifdef FEAT_JOB_CHANNEL
2067 if (len > 0)
2068 {
2069 buf[len] = NUL;
2070 ch_log(NULL, "raw key input: \"%s\"", buf);
2071 }
2072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 return len;
2074
Bram Moolenaar0f873732019-12-05 20:28:46 +01002075#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002077#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078}
2079
Bram Moolenaar82881492012-11-20 16:53:39 +01002080#ifndef PROTO
2081# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002082# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
2085
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002086/*
Bram Moolenaar95da1362020-05-30 18:37:55 +02002087 * Return TRUE if "name" is an executable file, FALSE if not or it doesn't exist.
Bram Moolenaar43663192017-03-05 14:29:12 +01002088 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2089 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002090 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 static int
Bram Moolenaar95da1362020-05-30 18:37:55 +02002093executable_file(char *name, char_u **path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
Bram Moolenaar95da1362020-05-30 18:37:55 +02002095 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002096 {
Bram Moolenaar95da1362020-05-30 18:37:55 +02002097 if (path != NULL)
2098 *path = FullName_save((char_u *)name, FALSE);
2099 return TRUE;
2100 }
2101 return FALSE;
2102}
2103
2104/*
2105 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2106 * If "use_path" is FALSE: Return TRUE if "name" exists.
2107 * If "use_pathext" is TRUE search "name" with extensions in $PATHEXT.
2108 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2109 * the allocated memory.
2110 */
2111 static int
2112executable_exists(char *name, char_u **path, int use_path, int use_pathext)
2113{
2114 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
2115 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
2116 // UTF-8.
2117 char_u buf[_MAX_PATH * 3];
2118 size_t len = STRLEN(name);
2119 size_t tmplen;
2120 char_u *p, *e, *e2;
2121 char_u *pathbuf = NULL;
2122 char_u *pathext = NULL;
2123 char_u *pathextbuf = NULL;
2124 int noext = FALSE;
2125 int retval = FALSE;
2126
2127 if (len >= sizeof(buf)) // safety check
Bram Moolenaar43663192017-03-05 14:29:12 +01002128 return FALSE;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002129
2130 // Using the name directly when a Unix-shell like 'shell'.
2131 if (strstr((char *)gettail(p_sh), "sh") != NULL)
2132 noext = TRUE;
2133
2134 if (use_pathext)
2135 {
2136 pathext = mch_getenv("PATHEXT");
2137 if (pathext == NULL)
2138 pathext = (char_u *)".com;.exe;.bat;.cmd";
2139
2140 if (noext == FALSE)
2141 {
2142 /*
2143 * Loop over all extensions in $PATHEXT.
2144 * Check "name" ends with extension.
2145 */
2146 p = pathext;
2147 while (*p)
2148 {
2149 if (p[0] == ';'
2150 || (p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2151 {
2152 // Skip empty or single ".".
2153 ++p;
2154 continue;
2155 }
2156 e = vim_strchr(p, ';');
2157 if (e == NULL)
2158 e = p + STRLEN(p);
2159 tmplen = e - p;
2160
2161 if (_strnicoll(name + len - tmplen, (char *)p, tmplen) == 0)
2162 {
2163 noext = TRUE;
2164 break;
2165 }
2166
2167 p = e;
2168 }
2169 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002170 }
2171
Bram Moolenaar95da1362020-05-30 18:37:55 +02002172 // Prepend single "." to pathext, it's means no extension added.
2173 if (pathext == NULL)
2174 pathext = (char_u *)".";
2175 else if (noext == TRUE)
2176 {
2177 if (pathextbuf == NULL)
2178 pathextbuf = alloc(STRLEN(pathext) + 3);
2179 if (pathextbuf == NULL)
2180 {
2181 retval = FALSE;
2182 goto theend;
2183 }
2184 STRCPY(pathextbuf, ".;");
2185 STRCAT(pathextbuf, pathext);
2186 pathext = pathextbuf;
2187 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002188
Bram Moolenaar95da1362020-05-30 18:37:55 +02002189 // Use $PATH when "use_path" is TRUE and "name" is basename.
2190 if (use_path && gettail((char_u *)name) == (char_u *)name)
2191 {
2192 p = mch_getenv("PATH");
2193 if (p != NULL)
2194 {
2195 pathbuf = alloc(STRLEN(p) + 3);
2196 if (pathbuf == NULL)
2197 {
2198 retval = FALSE;
2199 goto theend;
2200 }
2201 STRCPY(pathbuf, ".;");
2202 STRCAT(pathbuf, p);
2203 }
2204 }
2205
2206 /*
2207 * Walk through all entries in $PATH to check if "name" exists there and
2208 * is an executable file.
2209 */
2210 p = (pathbuf != NULL) ? pathbuf : (char_u *)".";
2211 while (*p)
2212 {
2213 if (*p == ';') // Skip empty entry
2214 {
2215 ++p;
2216 continue;
2217 }
2218 e = vim_strchr(p, ';');
2219 if (e == NULL)
2220 e = p + STRLEN(p);
2221
2222 if (e - p + len + 2 > sizeof(buf))
2223 {
2224 retval = FALSE;
2225 goto theend;
2226 }
2227 // A single "." that means current dir.
2228 if (e - p == 1 && *p == '.')
2229 STRCPY(buf, name);
2230 else
2231 {
2232 vim_strncpy(buf, p, e - p);
2233 add_pathsep(buf);
2234 STRCAT(buf, name);
2235 }
2236 tmplen = STRLEN(buf);
2237
2238 /*
2239 * Loop over all extensions in $PATHEXT.
2240 * Check "name" with extension added.
2241 */
2242 p = pathext;
2243 while (*p)
2244 {
2245 if (*p == ';')
2246 {
2247 // Skip empty entry
2248 ++p;
2249 continue;
2250 }
2251 e2 = vim_strchr(p, (int)';');
2252 if (e2 == NULL)
2253 e2 = p + STRLEN(p);
2254
2255 if (!(p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2256 {
2257 // Not a single "." that means no extension is added.
2258 if (e2 - p + tmplen + 1 > sizeof(buf))
2259 {
2260 retval = FALSE;
2261 goto theend;
2262 }
2263 vim_strncpy(buf + tmplen, p, e2 - p);
2264 }
2265 if (executable_file((char *)buf, path))
2266 {
2267 retval = TRUE;
2268 goto theend;
2269 }
2270
2271 p = e2;
2272 }
2273
2274 p = e;
2275 }
2276
2277theend:
2278 free(pathextbuf);
2279 free(pathbuf);
2280 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281}
2282
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002283#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2284 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002285/*
2286 * Bad parameter handler.
2287 *
2288 * Certain MS CRT functions will intentionally crash when passed invalid
2289 * parameters to highlight possible security holes. Setting this function as
2290 * the bad parameter handler will prevent the crash.
2291 *
2292 * In debug builds the parameters contain CRT information that might help track
2293 * down the source of a problem, but in non-debug builds the arguments are all
2294 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2295 * worth allowing these to make debugging of issues easier.
2296 */
2297 static void
2298bad_param_handler(const wchar_t *expression,
2299 const wchar_t *function,
2300 const wchar_t *file,
2301 unsigned int line,
2302 uintptr_t pReserved)
2303{
2304}
2305
2306# define SET_INVALID_PARAM_HANDLER \
2307 ((void)_set_invalid_parameter_handler(bad_param_handler))
2308#else
2309# define SET_INVALID_PARAM_HANDLER
2310#endif
2311
Bram Moolenaar4f974752019-02-17 17:44:42 +01002312#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
2314/*
2315 * GUI version of mch_init().
2316 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002317 static void
2318mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002320# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar0f873732019-12-05 20:28:46 +01002324 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002325 SET_INVALID_PARAM_HANDLER;
2326
Bram Moolenaar0f873732019-12-05 20:28:46 +01002327 // Let critical errors result in a failure, not in a dialog box. Required
2328 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 SetErrorMode(SEM_FAILCRITICALERRORS);
2330
Bram Moolenaar0f873732019-12-05 20:28:46 +01002331 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
Bram Moolenaar0f873732019-12-05 20:28:46 +01002333 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 Rows = 25;
2335 Columns = 80;
2336
Bram Moolenaar0f873732019-12-05 20:28:46 +01002337 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 char_u vimrun_location[_MAX_PATH + 4];
2340
Bram Moolenaar0f873732019-12-05 20:28:46 +01002341 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 STRCPY(vimrun_location, exe_name);
2343 STRCPY(gettail(vimrun_location), "vimrun.exe");
2344 if (mch_getperm(vimrun_location) >= 0)
2345 {
2346 if (*skiptowhite(vimrun_location) != NUL)
2347 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 mch_memmove(vimrun_location + 1, vimrun_location,
2350 STRLEN(vimrun_location) + 1);
2351 *vimrun_location = '"';
2352 STRCPY(gettail(vimrun_location), "vimrun\" ");
2353 }
2354 else
2355 STRCPY(gettail(vimrun_location), "vimrun ");
2356
2357 vimrun_path = (char *)vim_strsave(vimrun_location);
2358 s_dont_use_vimrun = FALSE;
2359 }
Bram Moolenaar95da1362020-05-30 18:37:55 +02002360 else if (executable_exists("vimrun.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 s_dont_use_vimrun = FALSE;
2362
Bram Moolenaar0f873732019-12-05 20:28:46 +01002363 // Don't give the warning for a missing vimrun.exe right now, but only
2364 // when vimrun was supposed to be used. Don't bother people that do
2365 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (s_dont_use_vimrun)
2367 need_vimrun_warning = TRUE;
2368 }
2369
2370 /*
2371 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2372 * Otherwise the default "findstr /n" is used.
2373 */
Bram Moolenaar95da1362020-05-30 18:37:55 +02002374 if (!executable_exists("findstr.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2376
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002377# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002378 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002379# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002380
2381 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382}
2383
2384
Bram Moolenaar0f873732019-12-05 20:28:46 +01002385#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002386
2387#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002389# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2390# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392/*
2393 * ClearConsoleBuffer()
2394 * Description:
2395 * Clears the entire contents of the console screen buffer, using the
2396 * specified attribute.
2397 * Returns:
2398 * TRUE on success
2399 */
2400 static BOOL
2401ClearConsoleBuffer(WORD wAttribute)
2402{
2403 CONSOLE_SCREEN_BUFFER_INFO csbi;
2404 COORD coord;
2405 DWORD NumCells, dummy;
2406
2407 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2408 return FALSE;
2409
2410 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2411 coord.X = 0;
2412 coord.Y = 0;
2413 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2414 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2417 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
2420 return TRUE;
2421}
2422
2423/*
2424 * FitConsoleWindow()
2425 * Description:
2426 * Checks if the console window will fit within given buffer dimensions.
2427 * Also, if requested, will shrink the window to fit.
2428 * Returns:
2429 * TRUE on success
2430 */
2431 static BOOL
2432FitConsoleWindow(
2433 COORD dwBufferSize,
2434 BOOL WantAdjust)
2435{
2436 CONSOLE_SCREEN_BUFFER_INFO csbi;
2437 COORD dwWindowSize;
2438 BOOL NeedAdjust = FALSE;
2439
2440 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2441 {
2442 /*
2443 * A buffer resize will fail if the current console window does
2444 * not lie completely within that buffer. To avoid this, we might
2445 * have to move and possibly shrink the window.
2446 */
2447 if (csbi.srWindow.Right >= dwBufferSize.X)
2448 {
2449 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2450 if (dwWindowSize.X > dwBufferSize.X)
2451 dwWindowSize.X = dwBufferSize.X;
2452 csbi.srWindow.Right = dwBufferSize.X - 1;
2453 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2454 NeedAdjust = TRUE;
2455 }
2456 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2457 {
2458 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2459 if (dwWindowSize.Y > dwBufferSize.Y)
2460 dwWindowSize.Y = dwBufferSize.Y;
2461 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2462 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2463 NeedAdjust = TRUE;
2464 }
2465 if (NeedAdjust && WantAdjust)
2466 {
2467 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2468 return FALSE;
2469 }
2470 return TRUE;
2471 }
2472
2473 return FALSE;
2474}
2475
2476typedef struct ConsoleBufferStruct
2477{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002478 BOOL IsValid;
2479 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002480 PCHAR_INFO Buffer;
2481 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002482 PSMALL_RECT Regions;
2483 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484} ConsoleBuffer;
2485
2486/*
2487 * SaveConsoleBuffer()
2488 * Description:
2489 * Saves important information about the console buffer, including the
2490 * actual buffer contents. The saved information is suitable for later
2491 * restoration by RestoreConsoleBuffer().
2492 * Returns:
2493 * TRUE if all information was saved; FALSE otherwise
2494 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2495 */
2496 static BOOL
2497SaveConsoleBuffer(
2498 ConsoleBuffer *cb)
2499{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002500 DWORD NumCells;
2501 COORD BufferCoord;
2502 SMALL_RECT ReadRegion;
2503 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002504 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (cb == NULL)
2507 return FALSE;
2508
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002509 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 cb->IsValid = FALSE;
2512 return FALSE;
2513 }
2514 cb->IsValid = TRUE;
2515
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002516 /*
2517 * Allocate a buffer large enough to hold the entire console screen
2518 * buffer. If this ConsoleBuffer structure has already been initialized
2519 * with a buffer of the correct size, then just use that one.
2520 */
2521 if (!cb->IsValid || cb->Buffer == NULL ||
2522 cb->BufferSize.X != cb->Info.dwSize.X ||
2523 cb->BufferSize.Y != cb->Info.dwSize.Y)
2524 {
2525 cb->BufferSize.X = cb->Info.dwSize.X;
2526 cb->BufferSize.Y = cb->Info.dwSize.Y;
2527 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2528 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002529 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002530 if (cb->Buffer == NULL)
2531 return FALSE;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002535 * We will now copy the console screen buffer into our buffer.
2536 * ReadConsoleOutput() seems to be limited as far as how much you
2537 * can read at a time. Empirically, this number seems to be about
2538 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2539 * in chunks until it is all copied. The chunks will all have the
2540 * same horizontal characteristics, so initialize them now. The
2541 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002543 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002544 ReadRegion.Left = 0;
2545 ReadRegion.Right = cb->Info.dwSize.X - 1;
2546 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002547
2548 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2549 if (cb->Regions == NULL || numregions != cb->NumRegions)
2550 {
2551 cb->NumRegions = numregions;
2552 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002553 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002554 if (cb->Regions == NULL)
2555 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002556 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002557 return FALSE;
2558 }
2559 }
2560
2561 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002563 /*
2564 * Read into position (0, Y) in our buffer.
2565 */
2566 BufferCoord.Y = Y;
2567 /*
2568 * Read the region whose top left corner is (0, Y) and whose bottom
2569 * right corner is (width - 1, Y + Y_incr - 1). This should define
2570 * a region of size width by Y_incr. Don't worry if this region is
2571 * too large for the remaining buffer; it will be cropped.
2572 */
2573 ReadRegion.Top = Y;
2574 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002575 if (!ReadConsoleOutputW(g_hConOut, // output handle
2576 cb->Buffer, // our buffer
2577 cb->BufferSize, // dimensions of our buffer
2578 BufferCoord, // offset in our buffer
2579 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002580 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002581 VIM_CLEAR(cb->Buffer);
2582 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002583 return FALSE;
2584 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002585 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587
2588 return TRUE;
2589}
2590
2591/*
2592 * RestoreConsoleBuffer()
2593 * Description:
2594 * Restores important information about the console buffer, including the
2595 * actual buffer contents, if desired. The information to restore is in
2596 * the same format used by SaveConsoleBuffer().
2597 * Returns:
2598 * TRUE on success
2599 */
2600 static BOOL
2601RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002602 ConsoleBuffer *cb,
2603 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002605 COORD BufferCoord;
2606 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002607 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (cb == NULL || !cb->IsValid)
2610 return FALSE;
2611
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002612 /*
2613 * Before restoring the buffer contents, clear the current buffer, and
2614 * restore the cursor position and window information. Doing this now
2615 * prevents old buffer contents from "flashing" onto the screen.
2616 */
2617 if (RestoreScreen)
2618 ClearConsoleBuffer(cb->Info.wAttributes);
2619
2620 FitConsoleWindow(cb->Info.dwSize, TRUE);
2621 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2622 return FALSE;
2623 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2624 return FALSE;
2625
2626 if (!RestoreScreen)
2627 {
2628 /*
2629 * No need to restore the screen buffer contents, so we're done.
2630 */
2631 return TRUE;
2632 }
2633
2634 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2635 return FALSE;
2636 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2637 return FALSE;
2638
2639 /*
2640 * Restore the screen buffer contents.
2641 */
2642 if (cb->Buffer != NULL)
2643 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002644 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002645 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002646 BufferCoord.X = cb->Regions[i].Left;
2647 BufferCoord.Y = cb->Regions[i].Top;
2648 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002649 if (!WriteConsoleOutputW(g_hConOut, // output handle
2650 cb->Buffer, // our buffer
2651 cb->BufferSize, // dimensions of our buffer
2652 BufferCoord, // offset in our buffer
2653 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002654 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002655 }
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 return TRUE;
2659}
2660
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002661# define FEAT_RESTORE_ORIG_SCREEN
2662# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002663static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665static ConsoleBuffer g_cbNonTermcap = { 0 };
2666static ConsoleBuffer g_cbTermcap = { 0 };
2667
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002668# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002670HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671static HICON g_hOrigIconSmall = NULL;
2672static HICON g_hOrigIcon = NULL;
2673static HICON g_hVimIcon = NULL;
2674static BOOL g_fCanChangeIcon = FALSE;
2675
Bram Moolenaar0f873732019-12-05 20:28:46 +01002676// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002677# ifndef ICON_SMALL
2678# define ICON_SMALL 0
2679# endif
2680# ifndef ICON_BIG
2681# define ICON_BIG 1
2682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * GetConsoleIcon()
2685 * Description:
2686 * Attempts to retrieve the small icon and/or the big icon currently in
2687 * use by a given window.
2688 * Returns:
2689 * TRUE on success
2690 */
2691 static BOOL
2692GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002693 HWND hWnd,
2694 HICON *phIconSmall,
2695 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 if (hWnd == NULL)
2698 return FALSE;
2699
2700 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002701 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2702 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002704 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2705 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return TRUE;
2707}
2708
2709/*
2710 * SetConsoleIcon()
2711 * Description:
2712 * Attempts to change the small icon and/or the big icon currently in
2713 * use by a given window.
2714 * Returns:
2715 * TRUE on success
2716 */
2717 static BOOL
2718SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002719 HWND hWnd,
2720 HICON hIconSmall,
2721 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (hWnd == NULL)
2724 return FALSE;
2725
2726 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002727 SendMessage(hWnd, WM_SETICON,
2728 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002730 SendMessage(hWnd, WM_SETICON,
2731 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 return TRUE;
2733}
2734
2735/*
2736 * SaveConsoleTitleAndIcon()
2737 * Description:
2738 * Saves the current console window title in g_szOrigTitle, for later
2739 * restoration. Also, attempts to obtain a handle to the console window,
2740 * and use it to save the small and big icons currently in use by the
2741 * console window. This is not always possible on some versions of Windows;
2742 * nor is it possible when running Vim remotely using Telnet (since the
2743 * console window the user sees is owned by a remote process).
2744 */
2745 static void
2746SaveConsoleTitleAndIcon(void)
2747{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2750 return;
2751
2752 /*
2753 * Obtain a handle to the console window using GetConsoleWindow() from
2754 * KERNEL32.DLL; we need to handle in order to change the window icon.
2755 * This function only exists on NT-based Windows, starting with Windows
2756 * 2000. On older operating systems, we can't change the window icon
2757 * anyway.
2758 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002759 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (g_hWnd == NULL)
2761 return;
2762
Bram Moolenaar0f873732019-12-05 20:28:46 +01002763 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2765 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2766 return;
2767
Bram Moolenaar0f873732019-12-05 20:28:46 +01002768 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002769 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002770 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (g_hVimIcon != NULL)
2772 g_fCanChangeIcon = TRUE;
2773}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776static int g_fWindInitCalled = FALSE;
2777static int g_fTermcapMode = FALSE;
2778static CONSOLE_CURSOR_INFO g_cci;
2779static DWORD g_cmodein = 0;
2780static DWORD g_cmodeout = 0;
2781
2782/*
2783 * non-GUI version of mch_init().
2784 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002785 static void
2786mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002788# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002789 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002790# endif
2791# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar0f873732019-12-05 20:28:46 +01002795 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002796 SET_INVALID_PARAM_HANDLER;
2797
Bram Moolenaar0f873732019-12-05 20:28:46 +01002798 // Let critical errors result in a failure, not in a dialog box. Required
2799 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 SetErrorMode(SEM_FAILCRITICALERRORS);
2801
Bram Moolenaar0f873732019-12-05 20:28:46 +01002802 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 out_flush();
2804
Bram Moolenaar0f873732019-12-05 20:28:46 +01002805 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (read_cmd_fd == 0)
2807 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2808 else
2809 create_conin();
2810 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2811
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002812# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002813 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002814 SaveConsoleBuffer(&g_cbOrig);
2815 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002816# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002817 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002818 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2819 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (cterm_normal_fg_color == 0)
2822 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2823 if (cterm_normal_bg_color == 0)
2824 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2825
Bram Moolenaarbdace832019-03-02 10:13:42 +01002826 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002827 g_color_index_fg = g_attrDefault & 0xf;
2828 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2829
Bram Moolenaar0f873732019-12-05 20:28:46 +01002830 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 update_tcap(g_attrCurrent);
2832
2833 GetConsoleCursorInfo(g_hConOut, &g_cci);
2834 GetConsoleMode(g_hConIn, &g_cmodein);
2835 GetConsoleMode(g_hConOut, &g_cmodeout);
2836
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002837# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 SaveConsoleTitleAndIcon();
2839 /*
2840 * Set both the small and big icons of the console window to Vim's icon.
2841 * Note that Vim presently only has one size of icon (32x32), but it
2842 * automatically gets scaled down to 16x16 when setting the small icon.
2843 */
2844 if (g_fCanChangeIcon)
2845 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 ui_get_shellsize();
2849
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002850# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 fdDump = fopen("dump", "wt");
2852
2853 if (fdDump)
2854 {
2855 time_t t;
2856
2857 time(&t);
2858 fputs(ctime(&t), fdDump);
2859 fflush(fdDump);
2860 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 g_fWindInitCalled = TRUE;
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002867# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002868 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002869# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002870
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002871 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002872 vtp_init();
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002873 wt_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874}
2875
2876/*
2877 * non-GUI version of mch_exit().
2878 * Shut down and exit with status `r'
2879 * Careful: mch_exit() may be called before mch_init()!
2880 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002881 static void
2882mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002884 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002886 vtp_exit();
2887
Bram Moolenaar955f1982017-02-05 15:10:51 +01002888 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (g_fWindInitCalled)
2890 settmode(TMODE_COOK);
2891
Bram Moolenaar0f873732019-12-05 20:28:46 +01002892 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 if (g_fWindInitCalled)
2895 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002896# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002897 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /*
2899 * Restore both the small and big icons of the console window to
2900 * what they were at startup. Don't do this when the window is
2901 * closed, Vim would hang here.
2902 */
2903 if (g_fCanChangeIcon && !g_fForceExit)
2904 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002907# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (fdDump)
2909 {
2910 time_t t;
2911
2912 time(&t);
2913 fputs(ctime(&t), fdDump);
2914 fclose(fdDump);
2915 }
2916 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919
2920 SetConsoleCursorInfo(g_hConOut, &g_cci);
2921 SetConsoleMode(g_hConIn, g_cmodein);
2922 SetConsoleMode(g_hConOut, g_cmodeout);
2923
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002924# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 exit(r);
2929}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002930#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002932 void
2933mch_init(void)
2934{
2935#ifdef VIMDLL
2936 if (gui.starting)
2937 mch_init_g();
2938 else
2939 mch_init_c();
2940#elif defined(FEAT_GUI_MSWIN)
2941 mch_init_g();
2942#else
2943 mch_init_c();
2944#endif
2945}
2946
2947 void
2948mch_exit(int r)
2949{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002950#ifdef FEAT_NETBEANS_INTG
2951 netbeans_send_disconnect();
2952#endif
2953
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002954#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002955 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002956 mch_exit_g(r);
2957 else
2958 mch_exit_c(r);
2959#elif defined(FEAT_GUI_MSWIN)
2960 mch_exit_g(r);
2961#else
2962 mch_exit_c(r);
2963#endif
2964}
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966/*
2967 * Do we have an interactive window?
2968 */
2969 int
2970mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002971 int argc UNUSED,
2972 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
2974 get_exe_name();
2975
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002976#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002977 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002979# ifdef VIMDLL
2980 if (gui.in_use)
2981 return OK;
2982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (isatty(1))
2984 return OK;
2985 return FAIL;
2986#endif
2987}
2988
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002989/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002990 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 * When "len" is > 0, also expand short to long filenames.
2992 */
2993 void
2994fname_case(
2995 char_u *name,
2996 int len)
2997{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002998 int flen;
2999 WCHAR *p;
3000 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00003002 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003003 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 return;
3005
3006 slash_adjust(name);
3007
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003008 p = enc_to_utf16(name, NULL);
3009 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003010 return;
3011
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003012 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003014 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003016 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003018 if (len > 0 || flen >= (int)STRLEN(q))
3019 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
3020 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003023 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024}
3025
3026
3027/*
3028 * Insert user name in s[len].
3029 */
3030 int
3031mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003032 char_u *s,
3033 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003035 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003036 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003038 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003039 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003040 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003041
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003042 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003043 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003044 vim_strncpy(s, p, len - 1);
3045 vim_free(p);
3046 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003047 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 s[0] = NUL;
3050 return FAIL;
3051}
3052
3053
3054/*
3055 * Insert host name in s[len].
3056 */
3057 void
3058mch_get_host_name(
3059 char_u *s,
3060 int len)
3061{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003062 WCHAR wszHostName[256 + 1];
3063 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003065 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003066 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003067 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003068
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003069 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003070 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003071 vim_strncpy(s, p, len - 1);
3072 vim_free(p);
3073 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003074 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
3078
3079/*
3080 * return process ID
3081 */
3082 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003083mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 return (long)GetCurrentProcessId();
3086}
3087
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003088/*
3089 * return TRUE if process "pid" is still running
3090 */
3091 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02003092mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003093{
3094 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
3095 DWORD status = 0;
3096 int ret = FALSE;
3097
3098 if (hProcess == NULL)
3099 return FALSE; // might not have access
3100 if (GetExitCodeProcess(hProcess, &status) )
3101 ret = status == STILL_ACTIVE;
3102 CloseHandle(hProcess);
3103 return ret;
3104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106/*
3107 * Get name of current directory into buffer 'buf' of length 'len' bytes.
3108 * Return OK for success, FAIL for failure.
3109 */
3110 int
3111mch_dirname(
3112 char_u *buf,
3113 int len)
3114{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003115 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 /*
3118 * Originally this was:
3119 * return (getcwd(buf, len) != NULL ? OK : FAIL);
3120 * But the Win32s known bug list says that getcwd() doesn't work
3121 * so use the Win32 system call instead. <Negri>
3122 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003123 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003125 WCHAR wcbuf[_MAX_PATH + 1];
3126 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003128 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003130 p = utf16_to_enc(wcbuf, NULL);
3131 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02003132 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003133 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003135 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
3137 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003138 if (p == NULL)
3139 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003140
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003141 if (p != NULL)
3142 {
3143 vim_strncpy(buf, p, len - 1);
3144 vim_free(p);
3145 return OK;
3146 }
3147 }
3148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149}
3150
3151/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01003152 * Get file permissions for "name".
3153 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
3155 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003156mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003158 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01003159 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003161 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01003162 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163}
3164
3165
3166/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003167 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003168 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003169 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 */
3171 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003172mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003174 long n;
3175 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003176
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003177 p = enc_to_utf16(name, NULL);
3178 if (p == NULL)
3179 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003181 n = _wchmod(p, perm);
3182 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003183 if (n == -1)
3184 return FAIL;
3185
3186 win32_set_archive(name);
3187
3188 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189}
3190
3191/*
3192 * Set hidden flag for "name".
3193 */
3194 void
3195mch_hide(char_u *name)
3196{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003197 int attrs = win32_getattrs(name);
3198 if (attrs == -1)
3199 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003201 attrs |= FILE_ATTRIBUTE_HIDDEN;
3202 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203}
3204
3205/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003206 * Return TRUE if file "name" exists and is hidden.
3207 */
3208 int
3209mch_ishidden(char_u *name)
3210{
3211 int f = win32_getattrs(name);
3212
3213 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003214 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003215
3216 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3217}
3218
3219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 * return TRUE if "name" is a directory
3221 * return FALSE if "name" is not a directory or upon error
3222 */
3223 int
3224mch_isdir(char_u *name)
3225{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003226 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003229 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3232}
3233
3234/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003235 * return TRUE if "name" is a directory, NOT a symlink to a directory
3236 * return FALSE if "name" is not a directory
3237 * return FALSE for error
3238 */
3239 int
3240mch_isrealdir(char_u *name)
3241{
3242 return mch_isdir(name) && !mch_is_symbolic_link(name);
3243}
3244
3245/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003246 * Create directory "name".
3247 * Return 0 on success, -1 on error.
3248 */
3249 int
3250mch_mkdir(char_u *name)
3251{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003252 WCHAR *p;
3253 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003254
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003255 p = enc_to_utf16(name, NULL);
3256 if (p == NULL)
3257 return -1;
3258 retval = _wmkdir(p);
3259 vim_free(p);
3260 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003261}
3262
3263/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003264 * Delete directory "name".
3265 * Return 0 on success, -1 on error.
3266 */
3267 int
3268mch_rmdir(char_u *name)
3269{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003270 WCHAR *p;
3271 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003272
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003273 p = enc_to_utf16(name, NULL);
3274 if (p == NULL)
3275 return -1;
3276 retval = _wrmdir(p);
3277 vim_free(p);
3278 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003279}
3280
3281/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003282 * Return TRUE if file "fname" has more than one link.
3283 */
3284 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003285mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003286{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003287 BY_HANDLE_FILE_INFORMATION info;
3288
3289 return win32_fileinfo(fname, &info) == FILEINFO_OK
3290 && info.nNumberOfLinks > 1;
3291}
3292
3293/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003294 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003295 */
3296 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003297mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003298{
3299 HANDLE hFind;
3300 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003301 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003302 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003303 WIN32_FIND_DATAW findDataW;
3304
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003305 wn = enc_to_utf16(name, NULL);
3306 if (wn == NULL)
3307 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003308
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003309 hFind = FindFirstFileW(wn, &findDataW);
3310 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003311 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003312 {
3313 fileFlags = findDataW.dwFileAttributes;
3314 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003315 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003316 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003317
3318 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003319 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3320 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003321 res = TRUE;
3322
3323 return res;
3324}
3325
3326/*
3327 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3328 * link.
3329 */
3330 int
3331mch_is_linked(char_u *fname)
3332{
3333 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3334 return TRUE;
3335 return FALSE;
3336}
3337
3338/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003339 * Get the by-handle-file-information for "fname".
3340 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003341 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003342 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3343 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3344 */
3345 int
3346win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3347{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003348 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003349 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003350 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003351
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003352 wn = enc_to_utf16(fname, NULL);
3353 if (wn == NULL)
3354 return FILEINFO_ENC_FAIL;
3355
3356 hFile = CreateFileW(wn, // file name
3357 GENERIC_READ, // access mode
3358 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3359 NULL, // security descriptor
3360 OPEN_EXISTING, // creation disposition
3361 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3362 NULL); // handle to template file
3363 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003364
3365 if (hFile != INVALID_HANDLE_VALUE)
3366 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003367 if (GetFileInformationByHandle(hFile, info) != 0)
3368 res = FILEINFO_OK;
3369 else
3370 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003371 CloseHandle(hFile);
3372 }
3373
Bram Moolenaar03f48552006-02-28 23:52:23 +00003374 return res;
3375}
3376
3377/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003378 * get file attributes for `name'
3379 * -1 : error
3380 * else FILE_ATTRIBUTE_* defined in winnt.h
3381 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003382 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003383win32_getattrs(char_u *name)
3384{
3385 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003386 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003388 p = enc_to_utf16(name, NULL);
3389 if (p == NULL)
3390 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003392 attr = GetFileAttributesW(p);
3393 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003394
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003395 return attr;
3396}
3397
3398/*
3399 * set file attributes for `name' to `attrs'
3400 *
3401 * return -1 for failure, 0 otherwise
3402 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003403 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003404win32_setattrs(char_u *name, int attrs)
3405{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003406 int res;
3407 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003409 p = enc_to_utf16(name, NULL);
3410 if (p == NULL)
3411 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003412
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003413 res = SetFileAttributesW(p, attrs);
3414 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003415
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003416 return res ? 0 : -1;
3417}
3418
3419/*
3420 * Set archive flag for "name".
3421 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003422 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003423win32_set_archive(char_u *name)
3424{
3425 int attrs = win32_getattrs(name);
3426 if (attrs == -1)
3427 return -1;
3428
3429 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3430 return win32_setattrs(name, attrs);
3431}
3432
3433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * Return TRUE if file or directory "name" is writable (not readonly).
3435 * Strange semantics of Win32: a readonly directory is writable, but you can't
3436 * delete a file. Let's say this means it is writable.
3437 */
3438 int
3439mch_writable(char_u *name)
3440{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003441 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003443 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3444 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003448 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003449 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003450 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3451 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003454mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar95da1362020-05-30 18:37:55 +02003456 return executable_exists((char *)name, path, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459/*
3460 * Check what "name" is:
3461 * NODE_NORMAL: file or directory (or doesn't exist)
3462 * NODE_WRITABLE: writable device, socket, fifo, etc.
3463 * NODE_OTHER: non-writable things
3464 */
3465 int
3466mch_nodetype(char_u *name)
3467{
3468 HANDLE hFile;
3469 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003470 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar0f873732019-12-05 20:28:46 +01003472 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3473 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3474 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003475 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3476 return NODE_WRITABLE;
3477
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003478 wn = enc_to_utf16(name, NULL);
3479 if (wn == NULL)
3480 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003481
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003482 hFile = CreateFileW(wn, // file name
3483 GENERIC_WRITE, // access mode
3484 0, // share mode
3485 NULL, // security descriptor
3486 OPEN_EXISTING, // creation disposition
3487 0, // file attributes
3488 NULL); // handle to template file
3489 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (hFile == INVALID_HANDLE_VALUE)
3491 return NODE_NORMAL;
3492
3493 type = GetFileType(hFile);
3494 CloseHandle(hFile);
3495 if (type == FILE_TYPE_CHAR)
3496 return NODE_WRITABLE;
3497 if (type == FILE_TYPE_DISK)
3498 return NODE_NORMAL;
3499 return NODE_OTHER;
3500}
3501
3502#ifdef HAVE_ACL
3503struct my_acl
3504{
3505 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3506 PSID pSidOwner;
3507 PSID pSidGroup;
3508 PACL pDacl;
3509 PACL pSacl;
3510};
3511#endif
3512
3513/*
3514 * Return a pointer to the ACL of file "fname" in allocated memory.
3515 * Return NULL if the ACL is not available for whatever reason.
3516 */
3517 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003518mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520#ifndef HAVE_ACL
3521 return (vim_acl_T)NULL;
3522#else
3523 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003524 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003526 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003527 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003529 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003530
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003531 wn = enc_to_utf16(fname, NULL);
3532 if (wn == NULL)
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01003533 {
3534 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003535 return NULL;
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01003536 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003537
3538 // Try to retrieve the entire security descriptor.
3539 err = GetNamedSecurityInfoW(
3540 wn, // Abstract filename
3541 SE_FILE_OBJECT, // File Object
3542 OWNER_SECURITY_INFORMATION |
3543 GROUP_SECURITY_INFORMATION |
3544 DACL_SECURITY_INFORMATION |
3545 SACL_SECURITY_INFORMATION,
3546 &p->pSidOwner, // Ownership information.
3547 &p->pSidGroup, // Group membership.
3548 &p->pDacl, // Discretionary information.
3549 &p->pSacl, // For auditing purposes.
3550 &p->pSecurityDescriptor);
3551 if (err == ERROR_ACCESS_DENIED ||
3552 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003553 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003554 // Retrieve only DACL.
3555 (void)GetNamedSecurityInfoW(
3556 wn,
3557 SE_FILE_OBJECT,
3558 DACL_SECURITY_INFORMATION,
3559 NULL,
3560 NULL,
3561 &p->pDacl,
3562 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003563 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003564 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003565 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003566 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003567 mch_free_acl((vim_acl_T)p);
3568 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003570 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572
3573 return (vim_acl_T)p;
3574#endif
3575}
3576
Bram Moolenaar27515922013-06-29 15:36:26 +02003577#ifdef HAVE_ACL
3578/*
3579 * Check if "acl" contains inherited ACE.
3580 */
3581 static BOOL
3582is_acl_inherited(PACL acl)
3583{
3584 DWORD i;
3585 ACL_SIZE_INFORMATION acl_info;
3586 PACCESS_ALLOWED_ACE ace;
3587
3588 acl_info.AceCount = 0;
3589 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3590 for (i = 0; i < acl_info.AceCount; i++)
3591 {
3592 GetAce(acl, i, (LPVOID *)&ace);
3593 if (ace->Header.AceFlags & INHERITED_ACE)
3594 return TRUE;
3595 }
3596 return FALSE;
3597}
3598#endif
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600/*
3601 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3602 * Errors are ignored.
3603 * This must only be called with "acl" equal to what mch_get_acl() returned.
3604 */
3605 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003606mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608#ifdef HAVE_ACL
3609 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003610 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003611 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003613 if (p == NULL)
3614 return;
3615
3616 wn = enc_to_utf16(fname, NULL);
3617 if (wn == NULL)
3618 return;
3619
3620 // Set security flags
3621 if (p->pSidOwner)
3622 sec_info |= OWNER_SECURITY_INFORMATION;
3623 if (p->pSidGroup)
3624 sec_info |= GROUP_SECURITY_INFORMATION;
3625 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003626 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003627 sec_info |= DACL_SECURITY_INFORMATION;
3628 // Do not inherit its parent's DACL.
3629 // If the DACL is inherited, Cygwin permissions would be changed.
3630 if (!is_acl_inherited(p->pDacl))
3631 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003632 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003633 if (p->pSacl)
3634 sec_info |= SACL_SECURITY_INFORMATION;
3635
3636 (void)SetNamedSecurityInfoW(
3637 wn, // Abstract filename
3638 SE_FILE_OBJECT, // File Object
3639 sec_info,
3640 p->pSidOwner, // Ownership information.
3641 p->pSidGroup, // Group membership.
3642 p->pDacl, // Discretionary information.
3643 p->pSacl // For auditing purposes.
3644 );
3645 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#endif
3647}
3648
3649 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003650mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652#ifdef HAVE_ACL
3653 struct my_acl *p = (struct my_acl *)acl;
3654
3655 if (p != NULL)
3656 {
3657 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3658 vim_free(p);
3659 }
3660#endif
3661}
3662
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003663#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665/*
3666 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3667 */
3668 static BOOL WINAPI
3669handler_routine(
3670 DWORD dwCtrlType)
3671{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003672 INPUT_RECORD ir;
3673 DWORD out;
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 switch (dwCtrlType)
3676 {
3677 case CTRL_C_EVENT:
3678 if (ctrl_c_interrupts)
3679 g_fCtrlCPressed = TRUE;
3680 return TRUE;
3681
3682 case CTRL_BREAK_EVENT:
3683 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003684 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003685 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003686 ir.EventType = KEY_EVENT;
3687 ir.Event.KeyEvent.bKeyDown = TRUE;
3688 ir.Event.KeyEvent.wRepeatCount = 1;
3689 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3690 ir.Event.KeyEvent.wVirtualScanCode = 0;
3691 ir.Event.KeyEvent.dwControlKeyState = 0;
3692 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3693 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return TRUE;
3695
Bram Moolenaar0f873732019-12-05 20:28:46 +01003696 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 case CTRL_CLOSE_EVENT:
3698 case CTRL_LOGOFF_EVENT:
3699 case CTRL_SHUTDOWN_EVENT:
3700 windgoto((int)Rows - 1, 0);
3701 g_fForceExit = TRUE;
3702
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003703 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 (dwCtrlType == CTRL_CLOSE_EVENT
3705 ? _("close")
3706 : dwCtrlType == CTRL_LOGOFF_EVENT
3707 ? _("logoff")
3708 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003709# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar0f873732019-12-05 20:28:46 +01003713 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar0f873732019-12-05 20:28:46 +01003715 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 default:
3718 return FALSE;
3719 }
3720}
3721
3722
3723/*
3724 * set the tty in (raw) ? "raw" : "cooked" mode
3725 */
3726 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003727mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 DWORD cmodein;
3730 DWORD cmodeout;
3731 BOOL bEnableHandler;
3732
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003733# ifdef VIMDLL
3734 if (gui.in_use)
3735 return;
3736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 GetConsoleMode(g_hConIn, &cmodein);
3738 GetConsoleMode(g_hConOut, &cmodeout);
3739 if (tmode == TMODE_RAW)
3740 {
3741 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3742 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (g_fMouseActive)
3744 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003745 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003746# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003747 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3748 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003749 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003750# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003751 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003752# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003753 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 bEnableHandler = TRUE;
3755 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003756 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3759 ENABLE_ECHO_INPUT);
3760 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3761 bEnableHandler = FALSE;
3762 }
3763 SetConsoleMode(g_hConIn, cmodein);
3764 SetConsoleMode(g_hConOut, cmodeout);
3765 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3766
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003767# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (fdDump)
3769 {
3770 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3771 tmode == TMODE_RAW ? "raw" :
3772 tmode == TMODE_COOK ? "cooked" : "normal",
3773 cmodein, cmodeout);
3774 fflush(fdDump);
3775 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777}
3778
3779
3780/*
3781 * Get the size of the current window in `Rows' and `Columns'
3782 * Return OK when size could be determined, FAIL otherwise.
3783 */
3784 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003785mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 CONSOLE_SCREEN_BUFFER_INFO csbi;
3788
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003789# ifdef VIMDLL
3790 if (gui.in_use)
3791 return OK;
3792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3794 {
3795 /*
3796 * For some reason, we are trying to get the screen dimensions
3797 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3798 * variables are really intended to mean the size of Vim screen
3799 * while in termcap mode.
3800 */
3801 Rows = g_cbTermcap.Info.dwSize.Y;
3802 Columns = g_cbTermcap.Info.dwSize.X;
3803 }
3804 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3805 {
3806 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3807 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3808 }
3809 else
3810 {
3811 Rows = 25;
3812 Columns = 80;
3813 }
3814 return OK;
3815}
3816
3817/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003818 * Resize console buffer to 'COORD'
3819 */
3820 static void
3821ResizeConBuf(
3822 HANDLE hConsole,
3823 COORD coordScreen)
3824{
3825 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3826 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003827# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003828 if (fdDump)
3829 {
3830 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3831 GetLastError());
3832 fflush(fdDump);
3833 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003834# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003835 }
3836}
3837
3838/*
3839 * Resize console window size to 'srWindowRect'
3840 */
3841 static void
3842ResizeWindow(
3843 HANDLE hConsole,
3844 SMALL_RECT srWindowRect)
3845{
3846 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3847 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003848# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003849 if (fdDump)
3850 {
3851 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3852 GetLastError());
3853 fflush(fdDump);
3854 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003855# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003856 }
3857}
3858
3859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 * Set a console window to `xSize' * `ySize'
3861 */
3862 static void
3863ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003864 HANDLE hConsole,
3865 int xSize,
3866 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003868 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3869 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003871 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003872 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003874# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (fdDump)
3876 {
3877 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3878 fflush(fdDump);
3879 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar0f873732019-12-05 20:28:46 +01003882 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 coordScreen = GetLargestConsoleWindowSize(hConsole);
3884
Bram Moolenaar0f873732019-12-05 20:28:46 +01003885 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3887 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3888 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3889
3890 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3891 {
3892 int sx, sy;
3893
3894 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3895 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3896 if (sy < ySize || sx < xSize)
3897 {
3898 /*
3899 * Increasing number of lines/columns, do buffer first.
3900 * Use the maximal size in x and y direction.
3901 */
3902 if (sy < ySize)
3903 coordScreen.Y = ySize;
3904 else
3905 coordScreen.Y = sy;
3906 if (sx < xSize)
3907 coordScreen.X = xSize;
3908 else
3909 coordScreen.X = sx;
3910 SetConsoleScreenBufferSize(hConsole, coordScreen);
3911 }
3912 }
3913
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003914 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 coordScreen.X = xSize;
3916 coordScreen.Y = ySize;
3917
Bram Moolenaar2551c032018-08-23 22:38:31 +02003918 // In the new console call API, only the first time in reverse order
3919 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003921 ResizeWindow(hConsole, srWindowRect);
3922 ResizeConBuf(hConsole, coordScreen);
3923 }
3924 else
3925 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003926 // Workaround for a Windows 10 bug
3927 cursor.X = srWindowRect.Left;
3928 cursor.Y = srWindowRect.Top;
3929 SetConsoleCursorPosition(hConsole, cursor);
3930
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003931 ResizeConBuf(hConsole, coordScreen);
3932 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003933 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935}
3936
3937
3938/*
3939 * Set the console window to `Rows' * `Columns'
3940 */
3941 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003942mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 COORD coordScreen;
3945
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003946# ifdef VIMDLL
3947 if (gui.in_use)
3948 return;
3949# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003950 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (suppress_winsize != 0)
3952 {
3953 suppress_winsize = 2;
3954 return;
3955 }
3956
3957 if (term_console)
3958 {
3959 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3960
Bram Moolenaar0f873732019-12-05 20:28:46 +01003961 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (Rows > coordScreen.Y)
3963 Rows = coordScreen.Y;
3964 if (Columns > coordScreen.X)
3965 Columns = coordScreen.X;
3966
3967 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3968 }
3969}
3970
3971/*
3972 * Rows and/or Columns has changed.
3973 */
3974 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003975mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003977# ifdef VIMDLL
3978 if (gui.in_use)
3979 return;
3980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3982}
3983
3984
3985/*
3986 * Called when started up, to set the winsize that was delayed.
3987 */
3988 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003989mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 if (suppress_winsize == 2)
3992 {
3993 suppress_winsize = 0;
3994 mch_set_shellsize();
3995 shell_resized();
3996 }
3997 suppress_winsize = 0;
3998}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003999#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004001 static BOOL
4002vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01004003 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004004 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01004005 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004006 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004007 PROCESS_INFORMATION *pi,
4008 LPVOID *env,
4009 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004010{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004011 BOOL ret = FALSE;
4012 WCHAR *wcmd, *wcwd = NULL;
4013
4014 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4015 if (wcmd == NULL)
4016 return FALSE;
4017 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004018 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004019 wcwd = enc_to_utf16((char_u *)cwd, NULL);
4020 if (wcwd == NULL)
4021 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004022 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004023
4024 ret = CreateProcessW(
4025 NULL, // Executable name
4026 wcmd, // Command to execute
4027 NULL, // Process security attributes
4028 NULL, // Thread security attributes
4029 inherit_handles, // Inherit handles
4030 flags, // Creation flags
4031 env, // Environment
4032 wcwd, // Current directory
4033 (LPSTARTUPINFOW)si, // Startup information
4034 pi); // Process information
4035theend:
4036 vim_free(wcmd);
4037 vim_free(wcwd);
4038 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004042 static HINSTANCE
4043vim_shell_execute(
4044 char *cmd,
4045 INT n_show_cmd)
4046{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004047 HINSTANCE ret;
4048 WCHAR *wcmd;
4049
4050 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4051 if (wcmd == NULL)
4052 return (HINSTANCE) 0;
4053
4054 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
4055 vim_free(wcmd);
4056 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004057}
4058
4059
Bram Moolenaar4f974752019-02-17 17:44:42 +01004060#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062/*
4063 * Specialised version of system() for Win32 GUI mode.
4064 * This version proceeds as follows:
4065 * 1. Create a console window for use by the subprocess
4066 * 2. Run the subprocess (it gets the allocated console by default)
4067 * 3. Wait for the subprocess to terminate and get its exit code
4068 * 4. Prompt the user to press a key to close the console window
4069 */
4070 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004071mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 STARTUPINFO si;
4074 PROCESS_INFORMATION pi;
4075 DWORD ret = 0;
4076 HWND hwnd = GetFocus();
4077
4078 si.cb = sizeof(si);
4079 si.lpReserved = NULL;
4080 si.lpDesktop = NULL;
4081 si.lpTitle = NULL;
4082 si.dwFlags = STARTF_USESHOWWINDOW;
4083 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004084 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004085 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004087 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004088 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 else
4090 si.wShowWindow = SW_SHOWNORMAL;
4091 si.cbReserved2 = 0;
4092 si.lpReserved2 = NULL;
4093
Bram Moolenaar0f873732019-12-05 20:28:46 +01004094 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004095 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004096 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
4097 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar0f873732019-12-05 20:28:46 +01004099 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004101# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 int delay = 1;
4103
Bram Moolenaar0f873732019-12-05 20:28:46 +01004104 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 for (;;)
4106 {
4107 MSG msg;
4108
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004109 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
4111 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004112 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02004113 delay = 1;
4114 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4117 break;
4118
Bram Moolenaar0f873732019-12-05 20:28:46 +01004119 // We start waiting for a very short time and then increase it, so
4120 // that we respond quickly when the process is quick, and don't
4121 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (delay < 50)
4123 delay += 10;
4124 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004125# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar0f873732019-12-05 20:28:46 +01004129 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 GetExitCodeProcess(pi.hProcess, &ret);
4131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaar0f873732019-12-05 20:28:46 +01004133 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 CloseHandle(pi.hThread);
4135 CloseHandle(pi.hProcess);
4136
Bram Moolenaar0f873732019-12-05 20:28:46 +01004137 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4139
4140 return ret;
4141}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004142
4143/*
4144 * Thread launched by the gui to send the current buffer data to the
4145 * process. This way avoid to hang up vim totally if the children
4146 * process take a long time to process the lines.
4147 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004148 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004149sub_process_writer(LPVOID param)
4150{
4151 HANDLE g_hChildStd_IN_Wr = param;
4152 linenr_T lnum = curbuf->b_op_start.lnum;
4153 DWORD len = 0;
4154 DWORD l;
4155 char_u *lp = ml_get(lnum);
4156 char_u *s;
4157 int written = 0;
4158
4159 for (;;)
4160 {
4161 l = (DWORD)STRLEN(lp + written);
4162 if (l == 0)
4163 len = 0;
4164 else if (lp[written] == NL)
4165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004166 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004167 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4168 }
4169 else
4170 {
4171 s = vim_strchr(lp + written, NL);
4172 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4173 s == NULL ? l : (DWORD)(s - (lp + written)),
4174 &len, NULL);
4175 }
4176 if (len == (int)l)
4177 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004178 // Finished a line, add a NL, unless this line should not have
4179 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004180 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004181 || (!curbuf->b_p_bin
4182 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004183 || (lnum != curbuf->b_no_eol_lnum
4184 && (lnum != curbuf->b_ml.ml_line_count
4185 || curbuf->b_p_eol)))
4186 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004187 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4188 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004189 }
4190
4191 ++lnum;
4192 if (lnum > curbuf->b_op_end.lnum)
4193 break;
4194
4195 lp = ml_get(lnum);
4196 written = 0;
4197 }
4198 else if (len > 0)
4199 written += len;
4200 }
4201
Bram Moolenaar0f873732019-12-05 20:28:46 +01004202 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004203 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004204 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004205}
4206
4207
Bram Moolenaar0f873732019-12-05 20:28:46 +01004208# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004209
4210/*
4211 * This function read from the children's stdout and write the
4212 * data on screen or in the buffer accordingly.
4213 */
4214 static void
4215dump_pipe(int options,
4216 HANDLE g_hChildStd_OUT_Rd,
4217 garray_T *ga,
4218 char_u buffer[],
4219 DWORD *buffer_off)
4220{
4221 DWORD availableBytes = 0;
4222 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004223 int ret;
4224 DWORD len;
4225 DWORD toRead;
4226 int repeatCount;
4227
Bram Moolenaar0f873732019-12-05 20:28:46 +01004228 // we query the pipe to see if there is any data to read
4229 // to avoid to perform a blocking read
4230 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4231 NULL, // optional buffer
4232 0, // buffer size
4233 NULL, // number of read bytes
4234 &availableBytes, // available bytes total
4235 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004236
4237 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004238 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004239 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004240 {
4241 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004242 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004243 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004244 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004245
Bram Moolenaar0f873732019-12-05 20:28:46 +01004246 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004247 if (len == 0)
4248 break;
4249
4250 availableBytes -= len;
4251
4252 if (options & SHELL_READ)
4253 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004254 // Do NUL -> NL translation, append NL separated
4255 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004256 for (i = 0; i < len; ++i)
4257 {
4258 if (buffer[i] == NL)
4259 append_ga_line(ga);
4260 else if (buffer[i] == NUL)
4261 ga_append(ga, NL);
4262 else
4263 ga_append(ga, buffer[i]);
4264 }
4265 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004266 else if (has_mbyte)
4267 {
4268 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004269 int c;
4270 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004271
4272 len += *buffer_off;
4273 buffer[len] = NUL;
4274
Bram Moolenaar0f873732019-12-05 20:28:46 +01004275 // Check if the last character in buffer[] is
4276 // incomplete, keep these bytes for the next
4277 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004278 for (p = buffer; p < buffer + len; p += l)
4279 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004280 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004281 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004282 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004283 else if (MB_BYTE2LEN(*p) != l)
4284 break;
4285 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004286 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004287 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004288 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004289 if (len >= 12)
4290 ++p;
4291 else
4292 {
4293 *buffer_off = len;
4294 return;
4295 }
4296 }
4297 c = *p;
4298 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004299 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004300 if (p < buffer + len)
4301 {
4302 *p = c;
4303 *buffer_off = (DWORD)((buffer + len) - p);
4304 mch_memmove(buffer, p, *buffer_off);
4305 return;
4306 }
4307 *buffer_off = 0;
4308 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004309 else
4310 {
4311 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004312 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004313 }
4314
4315 windgoto(msg_row, msg_col);
4316 cursor_on();
4317 out_flush();
4318 }
4319}
4320
4321/*
4322 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4323 * for communication and doesn't open any new window.
4324 */
4325 static int
4326mch_system_piped(char *cmd, int options)
4327{
4328 STARTUPINFO si;
4329 PROCESS_INFORMATION pi;
4330 DWORD ret = 0;
4331
4332 HANDLE g_hChildStd_IN_Rd = NULL;
4333 HANDLE g_hChildStd_IN_Wr = NULL;
4334 HANDLE g_hChildStd_OUT_Rd = NULL;
4335 HANDLE g_hChildStd_OUT_Wr = NULL;
4336
Bram Moolenaar0f873732019-12-05 20:28:46 +01004337 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004338 DWORD len;
4339
Bram Moolenaar0f873732019-12-05 20:28:46 +01004340 // buffer used to receive keys
4341 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4342 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004343
4344 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004345 int noread_cnt = 0;
4346 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004347 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004348 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004349 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004350
4351 SECURITY_ATTRIBUTES saAttr;
4352
Bram Moolenaar0f873732019-12-05 20:28:46 +01004353 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004354 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4355 saAttr.bInheritHandle = TRUE;
4356 saAttr.lpSecurityDescriptor = NULL;
4357
4358 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004359 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004360 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004361 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004362 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004363 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004364 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004365 {
4366 CloseHandle(g_hChildStd_IN_Rd);
4367 CloseHandle(g_hChildStd_IN_Wr);
4368 CloseHandle(g_hChildStd_OUT_Rd);
4369 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004370 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004371 }
4372
4373 si.cb = sizeof(si);
4374 si.lpReserved = NULL;
4375 si.lpDesktop = NULL;
4376 si.lpTitle = NULL;
4377 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4378
Bram Moolenaar0f873732019-12-05 20:28:46 +01004379 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004380 si.hStdError = g_hChildStd_OUT_Wr;
4381 si.hStdOutput = g_hChildStd_OUT_Wr;
4382 si.hStdInput = g_hChildStd_IN_Rd;
4383 si.wShowWindow = SW_HIDE;
4384 si.cbReserved2 = 0;
4385 si.lpReserved2 = NULL;
4386
4387 if (options & SHELL_READ)
4388 ga_init2(&ga, 1, BUFLEN);
4389
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004390 if (cmd != NULL)
4391 {
4392 p = (char *)vim_strsave((char_u *)cmd);
4393 if (p != NULL)
4394 unescape_shellxquote((char_u *)p, p_sxe);
4395 else
4396 p = cmd;
4397 }
4398
Bram Moolenaar0f873732019-12-05 20:28:46 +01004399 // Now, run the command.
4400 // About "Inherit handles" being TRUE: this command can be litigious,
4401 // handle inheritance was deactivated for pending temp file, but, if we
4402 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004403 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4404 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004405
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004406 if (p != cmd)
4407 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004408
Bram Moolenaar0f873732019-12-05 20:28:46 +01004409 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004410 CloseHandle(g_hChildStd_IN_Rd);
4411 CloseHandle(g_hChildStd_OUT_Wr);
4412
4413 if (options & SHELL_WRITE)
4414 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004415 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004416 _beginthreadex(NULL, // security attributes
4417 0, // default stack size
4418 sub_process_writer, // function to be executed
4419 g_hChildStd_IN_Wr, // parameter
4420 0, // creation flag, start immediately
4421 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004422 CloseHandle(thread);
4423 g_hChildStd_IN_Wr = NULL;
4424 }
4425
Bram Moolenaar0f873732019-12-05 20:28:46 +01004426 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004427 for (;;)
4428 {
4429 MSG msg;
4430
Bram Moolenaar175d0702013-12-11 18:36:33 +01004431 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004432 {
4433 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004434 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004435 }
4436
Bram Moolenaar0f873732019-12-05 20:28:46 +01004437 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004438 if ((options & (SHELL_READ|SHELL_WRITE))
4439# ifdef FEAT_GUI
4440 || gui.in_use
4441# endif
4442 )
4443 {
4444 len = 0;
4445 if (!(options & SHELL_EXPAND)
4446 && ((options &
4447 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4448 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4449# ifdef FEAT_GUI
4450 || gui.in_use
4451# endif
4452 )
4453 && (ta_len > 0 || noread_cnt > 4))
4454 {
4455 if (ta_len == 0)
4456 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004457 // Get extra characters when we don't have any. Reset the
4458 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004459 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004460 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4461 }
4462 if (ta_len > 0 || len > 0)
4463 {
4464 /*
4465 * For pipes: Check for CTRL-C: send interrupt signal to
4466 * child. Check for CTRL-D: EOF, close pipe to child.
4467 */
4468 if (len == 1 && cmd != NULL)
4469 {
4470 if (ta_buf[ta_len] == Ctrl_C)
4471 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004472 // Learn what exit code is expected, for
4473 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004474 TerminateProcess(pi.hProcess, 9);
4475 }
4476 if (ta_buf[ta_len] == Ctrl_D)
4477 {
4478 CloseHandle(g_hChildStd_IN_Wr);
4479 g_hChildStd_IN_Wr = NULL;
4480 }
4481 }
4482
Bram Moolenaarf4140482020-02-15 23:06:45 +01004483 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004484
4485 /*
4486 * For pipes: echo the typed characters. For a pty this
4487 * does not seem to work.
4488 */
4489 for (i = ta_len; i < ta_len + len; ++i)
4490 {
4491 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4492 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004493 else if (has_mbyte)
4494 {
4495 int l = (*mb_ptr2len)(ta_buf + i);
4496
4497 msg_outtrans_len(ta_buf + i, l);
4498 i += l - 1;
4499 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004500 else
4501 msg_outtrans_len(ta_buf + i, 1);
4502 }
4503 windgoto(msg_row, msg_col);
4504 out_flush();
4505
4506 ta_len += len;
4507
4508 /*
4509 * Write the characters to the child, unless EOF has been
4510 * typed for pipes. Write one character at a time, to
4511 * avoid losing too much typeahead. When writing buffer
4512 * lines, drop the typed characters (only check for
4513 * CTRL-C).
4514 */
4515 if (options & SHELL_WRITE)
4516 ta_len = 0;
4517 else if (g_hChildStd_IN_Wr != NULL)
4518 {
4519 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4520 1, &len, NULL);
4521 // if we are typing in, we want to keep things reactive
4522 delay = 1;
4523 if (len > 0)
4524 {
4525 ta_len -= len;
4526 mch_memmove(ta_buf, ta_buf + len, ta_len);
4527 }
4528 }
4529 }
4530 }
4531 }
4532
4533 if (ta_len)
4534 ui_inchar_undo(ta_buf, ta_len);
4535
4536 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4537 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004538 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004539 break;
4540 }
4541
4542 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004543 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004544
Bram Moolenaar0f873732019-12-05 20:28:46 +01004545 // We start waiting for a very short time and then increase it, so
4546 // that we respond quickly when the process is quick, and don't
4547 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004548 if (delay < 50)
4549 delay += 10;
4550 }
4551
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004553 CloseHandle(g_hChildStd_OUT_Rd);
4554 if (g_hChildStd_IN_Wr != NULL)
4555 CloseHandle(g_hChildStd_IN_Wr);
4556
4557 WaitForSingleObject(pi.hProcess, INFINITE);
4558
Bram Moolenaar0f873732019-12-05 20:28:46 +01004559 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004560 GetExitCodeProcess(pi.hProcess, &ret);
4561
4562 if (options & SHELL_READ)
4563 {
4564 if (ga.ga_len > 0)
4565 {
4566 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004567 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004568 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4569 }
4570 else
4571 curbuf->b_no_eol_lnum = 0;
4572 ga_clear(&ga);
4573 }
4574
Bram Moolenaar0f873732019-12-05 20:28:46 +01004575 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004576 CloseHandle(pi.hThread);
4577 CloseHandle(pi.hProcess);
4578
4579 return ret;
4580}
4581
4582 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004583mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004584{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004585 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004586 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004587 return mch_system_piped(cmd, options);
4588 else
4589 return mch_system_classic(cmd, options);
4590}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004593#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004594 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004595mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004596{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004597 int ret;
4598 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004599 char_u *buf;
4600 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004601
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004602 // If the command starts and ends with double quotes, enclose the command
4603 // in parentheses.
4604 len = STRLEN(cmd);
4605 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4606 {
4607 len += 3;
4608 buf = alloc(len);
4609 if (buf == NULL)
4610 return -1;
4611 vim_snprintf((char *)buf, len, "(%s)", cmd);
4612 wcmd = enc_to_utf16(buf, NULL);
4613 free(buf);
4614 }
4615 else
4616 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4617
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004618 if (wcmd == NULL)
4619 return -1;
4620
4621 ret = _wsystem(wcmd);
4622 vim_free(wcmd);
4623 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626#endif
4627
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004628 static int
4629mch_system(char *cmd, int options)
4630{
4631#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004632 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004633 return mch_system_g(cmd, options);
4634 else
4635 return mch_system_c(cmd, options);
4636#elif defined(FEAT_GUI_MSWIN)
4637 return mch_system_g(cmd, options);
4638#else
4639 return mch_system_c(cmd, options);
4640#endif
4641}
4642
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004643#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4644/*
4645 * Use a terminal window to run a shell command in.
4646 */
4647 static int
4648mch_call_shell_terminal(
4649 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004651{
4652 jobopt_T opt;
4653 char_u *newcmd = NULL;
4654 typval_T argvar[2];
4655 long_u cmdlen;
4656 int retval = -1;
4657 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004658 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004659 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004660 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004661
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004662 if (cmd == NULL)
4663 cmdlen = STRLEN(p_sh) + 1;
4664 else
4665 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004666 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004667 if (newcmd == NULL)
4668 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004669 if (cmd == NULL)
4670 {
4671 STRCPY(newcmd, p_sh);
4672 ch_log(NULL, "starting terminal to run a shell");
4673 }
4674 else
4675 {
4676 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4677 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4678 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004679
4680 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004681
4682 argvar[0].v_type = VAR_STRING;
4683 argvar[0].vval.v_string = newcmd;
4684 argvar[1].v_type = VAR_UNKNOWN;
4685 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004686 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004687 {
4688 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004689 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004690 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004691
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004692 job = term_getjob(buf->b_term);
4693 ++job->jv_refcount;
4694
Bram Moolenaar0f873732019-12-05 20:28:46 +01004695 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004696 aucmd_prepbuf(&aco, buf);
4697
4698 clear_oparg(&oa);
4699 while (term_use_loop())
4700 {
4701 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 // If terminal_loop() returns OK we got a key that is handled
4704 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004705 if (terminal_loop(TRUE) == OK)
4706 normal_cmd(&oa, TRUE);
4707 }
4708 else
4709 normal_cmd(&oa, TRUE);
4710 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004711 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004712 ch_log(NULL, "system command finished");
4713
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004714 job_unref(job);
4715
Bram Moolenaar0f873732019-12-05 20:28:46 +01004716 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004717 aucmd_restbuf(&aco);
4718
4719 wait_return(TRUE);
4720 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4721
4722 vim_free(newcmd);
4723 return retval;
4724}
4725#endif
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727/*
4728 * Either execute a command by calling the shell or start a new shell
4729 */
4730 int
4731mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004732 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 int x = 0;
4736 int tmode = cur_tmode;
4737#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004738 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004739
Bram Moolenaar0f873732019-12-05 20:28:46 +01004740 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004741 if (GetConsoleTitleW(szShellTitle,
4742 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004743 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004744 if (cmd == NULL)
4745 wcscat(szShellTitle, L" :sh");
4746 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004747 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004748 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004749
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004750 if (wn != NULL)
4751 {
4752 wcscat(szShellTitle, L" - !");
4753 if ((wcslen(szShellTitle) + wcslen(wn) <
4754 sizeof(szShellTitle)/sizeof(WCHAR)))
4755 wcscat(szShellTitle, wn);
4756 SetConsoleTitleW(szShellTitle);
4757 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004758 }
4759 }
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762
4763 out_flush();
4764
4765#ifdef MCH_WRITE_DUMP
4766 if (fdDump)
4767 {
4768 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4769 fflush(fdDump);
4770 }
4771#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004772#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004773 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004774 if (
4775# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004776 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004777# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004778 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004779 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4780 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004781 char_u *cmdbase = cmd;
4782
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004783 if (cmdbase != NULL)
4784 // Skip a leading quote and (.
4785 while (*cmdbase == '"' || *cmdbase == '(')
4786 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004787
4788 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004789 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4790 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004791 {
4792 // Use a terminal window to run the command in.
4793 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004794# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004795 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004796# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004797 return x;
4798 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004799 }
4800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Catch all deadly signals while running the external command, because a
4804 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4805 */
4806 signal(SIGINT, SIG_IGN);
4807#if defined(__GNUC__) && !defined(__MINGW32__)
4808 signal(SIGKILL, SIG_IGN);
4809#else
4810 signal(SIGBREAK, SIG_IGN);
4811#endif
4812 signal(SIGILL, SIG_IGN);
4813 signal(SIGFPE, SIG_IGN);
4814 signal(SIGSEGV, SIG_IGN);
4815 signal(SIGTERM, SIG_IGN);
4816 signal(SIGABRT, SIG_IGN);
4817
4818 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004819 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 if (cmd == NULL)
4822 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004823 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 else
4826 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004827 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004828 char_u *newcmd = NULL;
4829 char_u *cmdbase = cmd;
4830 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004831
Bram Moolenaar0f873732019-12-05 20:28:46 +01004832 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004833 if (*cmdbase == '"' )
4834 ++cmdbase;
4835 if (*cmdbase == '(')
4836 ++cmdbase;
4837
Bram Moolenaar1c465442017-03-12 20:10:05 +01004838 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004839 {
4840 STARTUPINFO si;
4841 PROCESS_INFORMATION pi;
4842 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004843 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004844 char_u *p;
4845
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004846 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004847 si.cb = sizeof(si);
4848 si.lpReserved = NULL;
4849 si.lpDesktop = NULL;
4850 si.lpTitle = NULL;
4851 si.dwFlags = 0;
4852 si.cbReserved2 = 0;
4853 si.lpReserved2 = NULL;
4854
4855 cmdbase = skipwhite(cmdbase + 5);
4856 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004857 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004858 {
4859 cmdbase = skipwhite(cmdbase + 4);
4860 si.dwFlags = STARTF_USESHOWWINDOW;
4861 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004862 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004863 }
4864 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004865 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004866 {
4867 cmdbase = skipwhite(cmdbase + 2);
4868 flags = CREATE_NO_WINDOW;
4869 si.dwFlags = STARTF_USESTDHANDLES;
4870 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004871 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004872 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004873 NULL, // Security att.
4874 OPEN_EXISTING, // Open flags
4875 FILE_ATTRIBUTE_NORMAL, // File att.
4876 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004877 si.hStdOutput = si.hStdInput;
4878 si.hStdError = si.hStdInput;
4879 }
4880
Bram Moolenaar0f873732019-12-05 20:28:46 +01004881 // Remove a trailing ", ) and )" if they have a match
4882 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004883 if (cmdbase > cmd)
4884 {
4885 p = cmdbase + STRLEN(cmdbase);
4886 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4887 *--p = NUL;
4888 if (p > cmdbase && p[-1] == ')'
4889 && (*cmd =='(' || cmd[1] == '('))
4890 *--p = NUL;
4891 }
4892
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004893 newcmd = cmdbase;
4894 unescape_shellxquote(cmdbase, p_sxe);
4895
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004896 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004897 * If creating new console, arguments are passed to the
4898 * 'cmd.exe' as-is. If it's not, arguments are not treated
4899 * correctly for current 'cmd.exe'. So unescape characters in
4900 * shellxescape except '|' for avoiding to be treated as
4901 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004902 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004903 if (flags != CREATE_NEW_CONSOLE)
4904 {
4905 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004906 char_u *cmd_shell = mch_getenv("COMSPEC");
4907
4908 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004909 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004910
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004911 subcmd = vim_strsave_escaped_ext(cmdbase,
4912 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004913 if (subcmd != NULL)
4914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004915 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004916 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004917 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004918 if (newcmd != NULL)
4919 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004920 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004921 else
4922 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004923 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004924 }
4925 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004926
4927 /*
4928 * Now, start the command as a process, so that it doesn't
4929 * inherit our handles which causes unpleasant dangling swap
4930 * files if we exit before the spawned process
4931 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004932 if (vim_create_process((char *)newcmd, FALSE, flags,
4933 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004934 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004935 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4936 > (HINSTANCE)32)
4937 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004938 else
4939 {
4940 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004941#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004942# ifdef VIMDLL
4943 if (gui.in_use)
4944# endif
4945 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004946#endif
4947 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004948
4949 if (newcmd != cmdbase)
4950 vim_free(newcmd);
4951
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004952 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004953 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004954 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004955 CloseHandle(si.hStdInput);
4956 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004957 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004958 CloseHandle(pi.hThread);
4959 CloseHandle(pi.hProcess);
4960 }
4961 else
4962 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004963 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004964#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004965 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004966 (!s_dont_use_vimrun && p_stmp ?
4967 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4968 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004970 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004971
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004972 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004973 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004975#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004976 if (
4977# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004978 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004979# endif
4980 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004982 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4983 "External commands will not pause after completion.\n"
4984 "See :help win32-vimrun for more information.");
4985 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004986 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4987 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004988
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004989 if (wmsg != NULL && wtitle != NULL)
4990 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4991 vim_free(wmsg);
4992 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 need_vimrun_warning = FALSE;
4994 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004995 if (
4996# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004997 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004998# endif
4999 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005000 // Use vimrun to execute the command. It opens a console
5001 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005002 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 vimrun_path,
5004 (msg_silent != 0 || (options & SHELL_DOOUT))
5005 ? "-s " : "",
5006 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005007 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005008# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005009 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005010# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02005011 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005012 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005013 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
5014 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005015 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005017 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00005018 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005020 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 }
5024
5025 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005026 {
5027 // The shell may have messed with the mode, always set it.
5028 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005029 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
Bram Moolenaar0f873732019-12-05 20:28:46 +01005032 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01005034#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005035 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005036 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037#endif
5038 )
5039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005040 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 msg_putchar('\n');
5042 }
5043#ifdef FEAT_TITLE
5044 resettitle();
5045#endif
5046
5047 signal(SIGINT, SIG_DFL);
5048#if defined(__GNUC__) && !defined(__MINGW32__)
5049 signal(SIGKILL, SIG_DFL);
5050#else
5051 signal(SIGBREAK, SIG_DFL);
5052#endif
5053 signal(SIGILL, SIG_DFL);
5054 signal(SIGFPE, SIG_DFL);
5055 signal(SIGSEGV, SIG_DFL);
5056 signal(SIGTERM, SIG_DFL);
5057 signal(SIGABRT, SIG_DFL);
5058
5059 return x;
5060}
5061
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005062#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005063 static HANDLE
5064job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01005065 char_u *fname,
5066 DWORD dwDesiredAccess,
5067 DWORD dwShareMode,
5068 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
5069 DWORD dwCreationDisposition,
5070 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005071{
5072 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005073 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005075 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005076 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005077 return INVALID_HANDLE_VALUE;
5078
5079 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
5080 lpSecurityAttributes, dwCreationDisposition,
5081 dwFlagsAndAttributes, NULL);
5082 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005083 return h;
5084}
5085
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005086/*
5087 * Turn the dictionary "env" into a NUL separated list that can be used as the
5088 * environment argument of vim_create_process().
5089 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005090 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005091win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005092{
5093 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005094 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005095 LPVOID base = GetEnvironmentStringsW();
5096
Bram Moolenaar0f873732019-12-05 20:28:46 +01005097 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005098 if (ga_grow(gap, 1) == FAIL)
5099 return;
5100
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005101 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005102 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005103 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005104 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005105 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005106 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005107 typval_T *item = &dict_lookup(hi)->di_tv;
5108 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005110 --todo;
5111 if (wkey != NULL && wval != NULL)
5112 {
5113 size_t n;
5114 size_t lkey = wcslen(wkey);
5115 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02005116
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005117 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
5118 continue;
5119 for (n = 0; n < lkey; n++)
5120 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
5121 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
5122 for (n = 0; n < lval; n++)
5123 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
5124 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5125 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01005126 vim_free(wkey);
5127 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005128 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005129 }
5130 }
5131
Bram Moolenaar355757a2020-02-10 22:06:32 +01005132 if (base)
5133 {
5134 WCHAR *p = (WCHAR*) base;
5135
5136 // for last \0
5137 if (ga_grow(gap, 1) == FAIL)
5138 return;
5139
5140 while (*p != 0 || *(p + 1) != 0)
5141 {
5142 if (ga_grow(gap, 1) == OK)
5143 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5144 p++;
5145 }
5146 FreeEnvironmentStrings(base);
5147 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5148 }
5149
Bram Moolenaar493359e2018-06-12 20:25:52 +02005150# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005151 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005152# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005153 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005154 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005155# endif
5156# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005157 char_u *version = get_vim_var_str(VV_VERSION);
5158 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005159# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005160 // size of "VIM_SERVERNAME=" and value,
5161 // plus "VIM_TERMINAL=" and value,
5162 // plus two terminating NULs
5163 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005164# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005165 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005166# endif
5167# ifdef FEAT_TERMINAL
5168 + 13 + version_len + 2
5169# endif
5170 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005171
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005172 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005173 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005174# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005175 for (n = 0; n < 15; n++)
5176 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5177 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005178 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005179 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5180 (WCHAR)servername[n];
5181 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005182# endif
5183# ifdef FEAT_TERMINAL
5184 if (is_terminal)
5185 {
5186 for (n = 0; n < 13; n++)
5187 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5188 (WCHAR)"VIM_TERMINAL="[n];
5189 for (n = 0; n < version_len; n++)
5190 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5191 (WCHAR)version[n];
5192 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5193 }
5194# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005195 }
5196 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005197# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005198}
5199
Bram Moolenaarb091f302019-01-19 14:37:00 +01005200/*
5201 * Create a pair of pipes.
5202 * Return TRUE for success, FALSE for failure.
5203 */
5204 static BOOL
5205create_pipe_pair(HANDLE handles[2])
5206{
5207 static LONG s;
5208 char name[64];
5209 SECURITY_ATTRIBUTES sa;
5210
5211 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5212 GetCurrentProcessId(),
5213 InterlockedIncrement(&s));
5214
5215 // Create named pipe. Max size of named pipe is 65535.
5216 handles[1] = CreateNamedPipe(
5217 name,
5218 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5219 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005220 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005221
5222 if (handles[1] == INVALID_HANDLE_VALUE)
5223 return FALSE;
5224
5225 sa.nLength = sizeof(sa);
5226 sa.bInheritHandle = TRUE;
5227 sa.lpSecurityDescriptor = NULL;
5228
5229 handles[0] = CreateFile(name,
5230 FILE_GENERIC_READ,
5231 FILE_SHARE_READ, &sa,
5232 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5233
5234 if (handles[0] == INVALID_HANDLE_VALUE)
5235 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005236 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005237 return FALSE;
5238 }
5239
5240 return TRUE;
5241}
5242
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005243 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005244mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005245{
5246 STARTUPINFO si;
5247 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005248 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005249 SECURITY_ATTRIBUTES saAttr;
5250 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005251 HANDLE ifd[2];
5252 HANDLE ofd[2];
5253 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005254 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005255
5256 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5257 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5258 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5259 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5260 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5261 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5262 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5263
5264 if (use_out_for_err && use_null_for_out)
5265 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005266
5267 ifd[0] = INVALID_HANDLE_VALUE;
5268 ifd[1] = INVALID_HANDLE_VALUE;
5269 ofd[0] = INVALID_HANDLE_VALUE;
5270 ofd[1] = INVALID_HANDLE_VALUE;
5271 efd[0] = INVALID_HANDLE_VALUE;
5272 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005273 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005274
Bram Moolenaar14207f42016-10-27 21:13:10 +02005275 jo = CreateJobObject(NULL, NULL);
5276 if (jo == NULL)
5277 {
5278 job->jv_status = JOB_FAILED;
5279 goto failed;
5280 }
5281
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005282 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005283 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005284
Bram Moolenaar76467df2016-02-12 19:30:26 +01005285 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005286 ZeroMemory(&si, sizeof(si));
5287 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005288 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005289 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005290
Bram Moolenaard8070362016-02-15 21:56:54 +01005291 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5292 saAttr.bInheritHandle = TRUE;
5293 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005294
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005295 if (use_file_for_in)
5296 {
5297 char_u *fname = options->jo_io_name[PART_IN];
5298
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005299 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5300 FILE_SHARE_READ | FILE_SHARE_WRITE,
5301 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5302 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005304 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005305 goto failed;
5306 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005307 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005308 else if (!use_null_for_in
5309 && (!create_pipe_pair(ifd)
5310 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005311 goto failed;
5312
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005313 if (use_file_for_out)
5314 {
5315 char_u *fname = options->jo_io_name[PART_OUT];
5316
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005317 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5318 FILE_SHARE_READ | FILE_SHARE_WRITE,
5319 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5320 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005322 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005323 goto failed;
5324 }
5325 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005326 else if (!use_null_for_out &&
5327 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005328 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005329 goto failed;
5330
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005331 if (use_file_for_err)
5332 {
5333 char_u *fname = options->jo_io_name[PART_ERR];
5334
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005335 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5336 FILE_SHARE_READ | FILE_SHARE_WRITE,
5337 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5338 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005340 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005341 goto failed;
5342 }
5343 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005344 else if (!use_out_for_err && !use_null_for_err &&
5345 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005346 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005347 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005348
Bram Moolenaard8070362016-02-15 21:56:54 +01005349 si.dwFlags |= STARTF_USESTDHANDLES;
5350 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005351 si.hStdOutput = ofd[1];
5352 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5353
5354 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5355 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005356 if (options->jo_set & JO_CHANNEL)
5357 {
5358 channel = options->jo_channel;
5359 if (channel != NULL)
5360 ++channel->ch_refcount;
5361 }
5362 else
5363 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005364 if (channel == NULL)
5365 goto failed;
5366 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005367
5368 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005369 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005370 CREATE_DEFAULT_ERROR_MODE |
5371 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005372 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005373 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005374 &si, &pi,
5375 ga.ga_data,
5376 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005377 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005378 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005379 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005380 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005381 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005382
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005383 ga_clear(&ga);
5384
Bram Moolenaar14207f42016-10-27 21:13:10 +02005385 if (!AssignProcessToJobObject(jo, pi.hProcess))
5386 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005387 // if failing, switch the way to terminate
5388 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005389 CloseHandle(jo);
5390 jo = NULL;
5391 }
5392 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005393 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005394 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005395 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005396 job->jv_status = JOB_STARTED;
5397
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005398 CloseHandle(ifd[0]);
5399 CloseHandle(ofd[1]);
5400 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005401 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005402
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005403 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005404 if (channel != NULL)
5405 {
5406 channel_set_pipes(channel,
5407 use_file_for_in || use_null_for_in
5408 ? INVALID_FD : (sock_T)ifd[1],
5409 use_file_for_out || use_null_for_out
5410 ? INVALID_FD : (sock_T)ofd[0],
5411 use_out_for_err || use_file_for_err || use_null_for_err
5412 ? INVALID_FD : (sock_T)efd[0]);
5413 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005414 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005415 return;
5416
5417failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005418 CloseHandle(ifd[0]);
5419 CloseHandle(ofd[0]);
5420 CloseHandle(efd[0]);
5421 CloseHandle(ifd[1]);
5422 CloseHandle(ofd[1]);
5423 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005424 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005425 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005426}
5427
5428 char *
5429mch_job_status(job_T *job)
5430{
5431 DWORD dwExitCode = 0;
5432
Bram Moolenaar76467df2016-02-12 19:30:26 +01005433 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5434 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005435 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005436 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005437 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005438 {
5439 ch_log(job->jv_channel, "Job ended");
5440 job->jv_status = JOB_ENDED;
5441 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005442 return "dead";
5443 }
5444 return "run";
5445}
5446
Bram Moolenaar97792de2016-10-15 18:36:49 +02005447 job_T *
5448mch_detect_ended_job(job_T *job_list)
5449{
5450 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5451 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5452 job_T *job = job_list;
5453
5454 while (job != NULL)
5455 {
5456 DWORD n;
5457 DWORD result;
5458
5459 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5460 && job != NULL; job = job->jv_next)
5461 {
5462 if (job->jv_status == JOB_STARTED)
5463 {
5464 jobHandles[n] = job->jv_proc_info.hProcess;
5465 jobArray[n] = job;
5466 ++n;
5467 }
5468 }
5469 if (n == 0)
5470 continue;
5471 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5472 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5473 {
5474 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5475
5476 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5477 return wait_job;
5478 }
5479 }
5480 return NULL;
5481}
5482
Bram Moolenaarfb630902016-10-29 14:55:00 +02005483 static BOOL
5484terminate_all(HANDLE process, int code)
5485{
5486 PROCESSENTRY32 pe;
5487 HANDLE h = INVALID_HANDLE_VALUE;
5488 DWORD pid = GetProcessId(process);
5489
5490 if (pid != 0)
5491 {
5492 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5493 if (h != INVALID_HANDLE_VALUE)
5494 {
5495 pe.dwSize = sizeof(PROCESSENTRY32);
5496 if (!Process32First(h, &pe))
5497 goto theend;
5498
5499 do
5500 {
5501 if (pe.th32ParentProcessID == pid)
5502 {
5503 HANDLE ph = OpenProcess(
5504 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5505 if (ph != NULL)
5506 {
5507 terminate_all(ph, code);
5508 CloseHandle(ph);
5509 }
5510 }
5511 } while (Process32Next(h, &pe));
5512
5513 CloseHandle(h);
5514 }
5515 }
5516
5517theend:
5518 return TerminateProcess(process, code);
5519}
5520
5521/*
5522 * Send a (deadly) signal to "job".
5523 * Return FAIL if it didn't work.
5524 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005525 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005526mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005527{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005528 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005529
Bram Moolenaar923d9262016-02-25 20:56:01 +01005530 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005531 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005532 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005533 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005534 {
5535 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5536 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005537 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005538 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005539 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005540 }
5541
5542 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5543 return FAIL;
5544 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005545 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5546 job->jv_proc_info.dwProcessId)
5547 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005548 FreeConsole();
5549 return ret;
5550}
5551
5552/*
5553 * Clear the data related to "job".
5554 */
5555 void
5556mch_clear_job(job_T *job)
5557{
5558 if (job->jv_status != JOB_FAILED)
5559 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005560 if (job->jv_job_object != NULL)
5561 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005562 CloseHandle(job->jv_proc_info.hProcess);
5563 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005564}
5565#endif
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005568#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570/*
5571 * Start termcap mode
5572 */
5573 static void
5574termcap_mode_start(void)
5575{
5576 DWORD cmodein;
5577
5578 if (g_fTermcapMode)
5579 return;
5580
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005581 if (!p_rs && USE_VTP)
5582 vtp_printf("\033[?1049h");
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 SaveConsoleBuffer(&g_cbNonTermcap);
5585
5586 if (g_cbTermcap.IsValid)
5587 {
5588 /*
5589 * We've been in termcap mode before. Restore certain screen
5590 * characteristics, including the buffer size and the window
5591 * size. Since we will be redrawing the screen, we don't need
5592 * to restore the actual contents of the buffer.
5593 */
5594 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005595 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5597 Rows = g_cbTermcap.Info.dwSize.Y;
5598 Columns = g_cbTermcap.Info.dwSize.X;
5599 }
5600 else
5601 {
5602 /*
5603 * This is our first time entering termcap mode. Clear the console
5604 * screen buffer, and resize the buffer to match the current window
5605 * size. We will use this as the size of our editing environment.
5606 */
5607 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005608 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5610 }
5611
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005612# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (g_fMouseActive)
5618 cmodein |= ENABLE_MOUSE_INPUT;
5619 else
5620 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 cmodein |= ENABLE_WINDOW_INPUT;
5622 SetConsoleMode(g_hConIn, cmodein);
5623
5624 redraw_later_clear();
5625 g_fTermcapMode = TRUE;
5626}
5627
5628
5629/*
5630 * End termcap mode
5631 */
5632 static void
5633termcap_mode_end(void)
5634{
5635 DWORD cmodein;
5636 ConsoleBuffer *cb;
5637 COORD coord;
5638 DWORD dwDummy;
5639
5640 if (!g_fTermcapMode)
5641 return;
5642
5643 SaveConsoleBuffer(&g_cbTermcap);
5644
5645 GetConsoleMode(g_hConIn, &cmodein);
5646 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5647 SetConsoleMode(g_hConIn, cmodein);
5648
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005649# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005650 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005651# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005653# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005654 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005655 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 SetConsoleCursorInfo(g_hConOut, &g_cci);
5657
5658 if (p_rs || exiting)
5659 {
5660 /*
5661 * Clear anything that happens to be on the current line.
5662 */
5663 coord.X = 0;
5664 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5665 FillConsoleOutputCharacter(g_hConOut, ' ',
5666 cb->Info.dwSize.X, coord, &dwDummy);
5667 /*
5668 * The following is just for aesthetics. If we are exiting without
5669 * restoring the screen, then we want to have a prompt string
5670 * appear at the bottom line. However, the command interpreter
5671 * seems to always advance the cursor one line before displaying
5672 * the prompt string, which causes the screen to scroll. To
5673 * counter this, move the cursor up one line before exiting.
5674 */
5675 if (exiting && !p_rs)
5676 coord.Y--;
5677 /*
5678 * Position the cursor at the leftmost column of the desired row.
5679 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005680 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005683 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005684 vtp_printf("\033[?1049l");
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 g_fTermcapMode = FALSE;
5687}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005688#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005691#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 void
5693mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005694 char_u *s UNUSED,
5695 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005697 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698}
5699
5700#else
5701
5702/*
5703 * clear `n' chars, starting from `coord'
5704 */
5705 static void
5706clear_chars(
5707 COORD coord,
5708 DWORD n)
5709{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005710 if (!USE_VTP)
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02005711 {
5712 DWORD dwDummy;
5713
5714 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
5715 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord,
5716 &dwDummy);
5717 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005718 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005719 {
5720 set_console_color_rgb();
5721 gotoxy(coord.X + 1, coord.Y + 1);
5722 vtp_printf("\033[%dX", n);
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726
5727/*
5728 * Clear the screen
5729 */
5730 static void
5731clear_screen(void)
5732{
5733 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005734
5735 if (!USE_VTP)
5736 clear_chars(g_coord, Rows * Columns);
5737 else
5738 {
5739 set_console_color_rgb();
5740 gotoxy(1, 1);
5741 vtp_printf("\033[2J");
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743}
5744
5745
5746/*
5747 * Clear to end of display
5748 */
5749 static void
5750clear_to_end_of_display(void)
5751{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005752 COORD save = g_coord;
5753
5754 if (!USE_VTP)
5755 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005757 else
5758 {
5759 set_console_color_rgb();
5760 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5761 vtp_printf("\033[0J");
5762
5763 gotoxy(save.X + 1, save.Y + 1);
5764 g_coord = save;
5765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766}
5767
5768
5769/*
5770 * Clear to end of line
5771 */
5772 static void
5773clear_to_end_of_line(void)
5774{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005775 COORD save = g_coord;
5776
5777 if (!USE_VTP)
5778 clear_chars(g_coord, Columns - g_coord.X);
5779 else
5780 {
5781 set_console_color_rgb();
5782 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5783 vtp_printf("\033[0K");
5784
5785 gotoxy(save.X + 1, save.Y + 1);
5786 g_coord = save;
5787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788}
5789
5790
5791/*
5792 * Scroll the scroll region up by `cLines' lines
5793 */
5794 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005795scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 COORD oldcoord = g_coord;
5798
5799 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5800 delete_lines(cLines);
5801
5802 g_coord = oldcoord;
5803}
5804
5805
5806/*
5807 * Set the scroll region
5808 */
5809 static void
5810set_scroll_region(
5811 unsigned left,
5812 unsigned top,
5813 unsigned right,
5814 unsigned bottom)
5815{
5816 if (left >= right
5817 || top >= bottom
5818 || right > (unsigned) Columns - 1
5819 || bottom > (unsigned) Rows - 1)
5820 return;
5821
5822 g_srScrollRegion.Left = left;
5823 g_srScrollRegion.Top = top;
5824 g_srScrollRegion.Right = right;
5825 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005826}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005827
Bram Moolenaar6982f422019-02-16 16:48:01 +01005828 static void
5829set_scroll_region_tb(
5830 unsigned top,
5831 unsigned bottom)
5832{
5833 if (top >= bottom || bottom > (unsigned)Rows - 1)
5834 return;
5835
5836 g_srScrollRegion.Top = top;
5837 g_srScrollRegion.Bottom = bottom;
5838}
5839
5840 static void
5841set_scroll_region_lr(
5842 unsigned left,
5843 unsigned right)
5844{
5845 if (left >= right || right > (unsigned)Columns - 1)
5846 return;
5847
5848 g_srScrollRegion.Left = left;
5849 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850}
5851
5852
5853/*
5854 * Insert `cLines' lines at the current cursor position
5855 */
5856 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005857insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005859 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 COORD dest;
5861 CHAR_INFO fill;
5862
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005863 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5864
Bram Moolenaar6982f422019-02-16 16:48:01 +01005865 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 dest.Y = g_coord.Y + cLines;
5867
Bram Moolenaar6982f422019-02-16 16:48:01 +01005868 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 source.Top = g_coord.Y;
5870 source.Right = g_srScrollRegion.Right;
5871 source.Bottom = g_srScrollRegion.Bottom - cLines;
5872
Bram Moolenaar6982f422019-02-16 16:48:01 +01005873 clip.Left = g_srScrollRegion.Left;
5874 clip.Top = g_coord.Y;
5875 clip.Right = g_srScrollRegion.Right;
5876 clip.Bottom = g_srScrollRegion.Bottom;
5877
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005878 fill.Char.AsciiChar = ' ';
5879 if (!USE_VTP)
5880 fill.Attributes = g_attrCurrent;
5881 else
5882 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005884 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005885
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005886 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5887
Bram Moolenaar6982f422019-02-16 16:48:01 +01005888 // Here we have to deal with a win32 console flake: If the scroll
5889 // region looks like abc and we scroll c to a and fill with d we get
5890 // cbd... if we scroll block c one line at a time to a, we get cdd...
5891 // vim expects cdd consistently... So we have to deal with that
5892 // here... (this also occurs scrolling the same way in the other
5893 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 if (source.Bottom < dest.Y)
5896 {
5897 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005898 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar6982f422019-02-16 16:48:01 +01005900 coord.X = source.Left;
5901 for (i = clip.Top; i < dest.Y; ++i)
5902 {
5903 coord.Y = i;
5904 clear_chars(coord, source.Right - source.Left + 1);
5905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02005907
5908 if (USE_WT)
5909 {
5910 COORD coord;
5911 int i;
5912
5913 coord.X = source.Left;
5914 for (i = source.Top; i < dest.Y; ++i)
5915 {
5916 coord.Y = i;
5917 clear_chars(coord, source.Right - source.Left + 1);
5918 }
5919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920}
5921
5922
5923/*
5924 * Delete `cLines' lines at the current cursor position
5925 */
5926 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005927delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005929 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 COORD dest;
5931 CHAR_INFO fill;
5932 int nb;
5933
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005934 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5935
Bram Moolenaar6982f422019-02-16 16:48:01 +01005936 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 dest.Y = g_coord.Y;
5938
Bram Moolenaar6982f422019-02-16 16:48:01 +01005939 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 source.Top = g_coord.Y + cLines;
5941 source.Right = g_srScrollRegion.Right;
5942 source.Bottom = g_srScrollRegion.Bottom;
5943
Bram Moolenaar6982f422019-02-16 16:48:01 +01005944 clip.Left = g_srScrollRegion.Left;
5945 clip.Top = g_coord.Y;
5946 clip.Right = g_srScrollRegion.Right;
5947 clip.Bottom = g_srScrollRegion.Bottom;
5948
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005949 fill.Char.AsciiChar = ' ';
5950 if (!USE_VTP)
5951 fill.Attributes = g_attrCurrent;
5952 else
5953 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005955 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005956
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005957 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5958
Bram Moolenaar6982f422019-02-16 16:48:01 +01005959 // Here we have to deal with a win32 console flake; See insert_lines()
5960 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
5962 nb = dest.Y + (source.Bottom - source.Top) + 1;
5963
5964 if (nb < source.Top)
5965 {
5966 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005967 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
Bram Moolenaar6982f422019-02-16 16:48:01 +01005969 coord.X = source.Left;
5970 for (i = nb; i < clip.Bottom; ++i)
5971 {
5972 coord.Y = i;
5973 clear_chars(coord, source.Right - source.Left + 1);
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02005976
5977 if (USE_WT)
5978 {
5979 COORD coord;
5980 int i;
5981
5982 coord.X = source.Left;
5983 for (i = nb; i <= source.Bottom; ++i)
5984 {
5985 coord.Y = i;
5986 clear_chars(coord, source.Right - source.Left + 1);
5987 }
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989}
5990
5991
5992/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005993 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 */
5995 static void
5996gotoxy(
5997 unsigned x,
5998 unsigned y)
5999{
6000 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
6001 return;
6002
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006003 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02006004 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01006005 // There are reports of double-width characters not displayed
6006 // correctly. This workaround should fix it, similar to how it's done
6007 // for VTP.
6008 g_coord.X = 0;
6009 SetConsoleCursorPosition(g_hConOut, g_coord);
6010
Bram Moolenaar2313b612019-09-27 14:14:32 +02006011 // external cursor coords are 1-based; internal are 0-based
6012 g_coord.X = x - 1;
6013 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006014 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006015 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006016 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02006017 {
6018 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02006019 // destruction. Insider build bug. Always enabled because it's cheap
6020 // and avoids mistakes with recognizing the build.
6021 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006022
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006023 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006024
6025 g_coord.X = x - 1;
6026 g_coord.Y = y - 1;
6027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028}
6029
6030
6031/*
6032 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006033 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
6035 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006036textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006038 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
6040 SetConsoleTextAttribute(g_hConOut, wAttr);
6041}
6042
6043
6044 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006045textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006047 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006049 if (!USE_VTP)
6050 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6051 else
6052 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053}
6054
6055
6056 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006057textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006059 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006061 if (!USE_VTP)
6062 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6063 else
6064 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065}
6066
6067
6068/*
6069 * restore the default text attribute (whatever we started with)
6070 */
6071 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006072normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006074 if (!USE_VTP)
6075 textattr(g_attrDefault);
6076 else
6077 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078}
6079
6080
6081static WORD g_attrPreStandout = 0;
6082
6083/*
6084 * Make the text standout, by brightening it
6085 */
6086 static void
6087standout(void)
6088{
6089 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
6092}
6093
6094
6095/*
6096 * Turn off standout mode
6097 */
6098 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006099standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006103
6104 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105}
6106
6107
6108/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00006109 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 */
6111 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006112mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 char_u *p;
6115 int n;
6116
6117 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
6118 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006119 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006120# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006121 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006122# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006123 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 p = T_ME + 2;
6126 n = getdigits(&p);
6127 if (*p == 'm' && n > 0)
6128 {
6129 cterm_normal_fg_color = (n & 0xf) + 1;
6130 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
6131 }
6132 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006133# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006134 cterm_normal_fg_gui_color = INVALCOLOR;
6135 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137}
6138
6139
6140/*
6141 * visual bell: flash the screen
6142 */
6143 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006144visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 COORD coordOrigin = {0, 0};
6147 WORD attrFlash = ~g_attrCurrent & 0xff;
6148
6149 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006150 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 if (oldattrs == NULL)
6153 return;
6154 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
6155 coordOrigin, &dwDummy);
6156 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6157 coordOrigin, &dwDummy);
6158
Bram Moolenaar0f873732019-12-05 20:28:46 +01006159 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006160 if (!USE_VTP)
6161 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 coordOrigin, &dwDummy);
6163 vim_free(oldattrs);
6164}
6165
6166
6167/*
6168 * Make the cursor visible or invisible
6169 */
6170 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006171cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006174
6175 if (USE_VTP)
6176 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6177
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006178# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181}
6182
6183
6184/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006185 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006186 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006188 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006190 char_u *pchBuf,
6191 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006193 COORD coord = g_coord;
6194 DWORD written;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006195 DWORD n, cchwritten;
6196 static DWORD cells;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006197 static WCHAR *unicodebuf = NULL;
6198 static int unibuflen = 0;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006199 static int length;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006200 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006201 static WCHAR *utf8spbuf = NULL;
6202 static int utf8splength;
6203 static DWORD utf8spcells;
6204 static WCHAR **utf8usingbuf = &unicodebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006206 if (cbToWrite != 1 || *pchBuf != ' ' || !enc_utf8)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006207 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006208 utf8usingbuf = &unicodebuf;
6209 do
6210 {
6211 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6212 unicodebuf, unibuflen);
6213 if (length && length <= unibuflen)
6214 break;
6215 vim_free(unicodebuf);
6216 unicodebuf = length ? LALLOC_MULT(WCHAR, length) : NULL;
6217 unibuflen = unibuflen ? 0 : length;
6218 } while(1);
6219 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006220 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006221 else // cbToWrite == 1 && *pchBuf == ' ' && enc_utf8
6222 {
6223 if (utf8usingbuf != &utf8spbuf)
6224 {
6225 if (utf8spbuf == NULL)
6226 {
6227 cells = mb_string2cells((char_u *)" ", 1);
6228 length = MultiByteToWideChar(CP_UTF8, 0, " ", 1, NULL, 0);
6229 utf8spbuf = LALLOC_MULT(WCHAR, length);
6230 if (utf8spbuf != NULL)
6231 {
6232 MultiByteToWideChar(CP_UTF8, 0, " ", 1, utf8spbuf, length);
6233 utf8usingbuf = &utf8spbuf;
6234 utf8splength = length;
6235 utf8spcells = cells;
6236 }
6237 }
6238 else
6239 {
6240 utf8usingbuf = &utf8spbuf;
6241 length = utf8splength;
6242 cells = utf8spcells;
6243 }
6244 }
6245 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006246
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006247 if (!USE_VTP)
6248 {
6249 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6250 coord, &written);
6251 // When writing fails or didn't write a single character, pretend one
6252 // character was written, otherwise we get stuck.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006253 if (WriteConsoleOutputCharacterW(g_hConOut, *utf8usingbuf, length,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006254 coord, &cchwritten) == 0
6255 || cchwritten == 0 || cchwritten == (DWORD)-1)
6256 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006257 }
6258 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006259 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006260 if (WriteConsoleW(g_hConOut, *utf8usingbuf, length, &cchwritten,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006261 NULL) == 0 || cchwritten == 0)
6262 cchwritten = 1;
6263 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006264
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006265 if (cchwritten == length)
6266 {
6267 written = cbToWrite;
6268 g_coord.X += (SHORT)cells;
6269 }
6270 else
6271 {
6272 char_u *p = pchBuf;
6273 for (n = 0; n < cchwritten; n++)
6274 MB_CPTR_ADV(p);
6275 written = p - pchBuf;
6276 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 while (g_coord.X > g_srScrollRegion.Right)
6280 {
6281 g_coord.X -= (SHORT) Columns;
6282 if (g_coord.Y < g_srScrollRegion.Bottom)
6283 g_coord.Y++;
6284 }
6285
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006286 // Cursor under VTP is always in the correct position, no need to reset.
6287 if (!USE_VTP)
6288 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290 return written;
6291}
6292
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006293 static char_u *
6294get_seq(
6295 int *args,
6296 int *count,
6297 char_u *head)
6298{
6299 int argc;
6300 char_u *p;
6301
6302 if (head == NULL || *head != '\033')
6303 return NULL;
6304
6305 argc = 0;
6306 p = head;
6307 ++p;
6308 do
6309 {
6310 ++p;
6311 args[argc] = getdigits(&p);
6312 argc += (argc < 15) ? 1 : 0;
6313 } while (*p == ';');
6314 *count = argc;
6315
6316 return p;
6317}
6318
6319 static char_u *
6320get_sgr(
6321 int *args,
6322 int *count,
6323 char_u *head)
6324{
6325 char_u *p = get_seq(args, count, head);
6326
6327 return (p && *p == 'm') ? ++p : NULL;
6328}
6329
6330/*
6331 * Pointer to next if SGR (^[[n;2;*;*;*m), NULL otherwise.
6332 */
6333 static char_u *
6334sgrn2(
6335 char_u *head,
6336 int n)
6337{
6338 int argc;
6339 int args[16];
6340 char_u *p = get_sgr(args, &argc, head);
6341
6342 return p && argc == 5 && args[0] == n && args[1] == 2 ? p : NULL;
6343}
6344
6345/*
6346 * Pointer to next if SGR(^[[nm)<space>ESC, NULL otherwise.
6347 */
6348 static char_u *
6349sgrnc(
6350 char_u *head,
6351 int n)
6352{
6353 int argc;
6354 int args[16];
6355 char_u *p = get_sgr(args, &argc, head);
6356
6357 return p && argc == 1 && args[0] == n && (p = skipwhite(p)) && *p == '\033'
6358 ? p : NULL;
6359}
6360
6361 static char_u *
6362skipblank(char_u *q)
6363{
6364 char_u *p = q;
6365
6366 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
6367 ++p;
6368 return p;
6369}
6370
6371/*
6372 * Pointer to the next if any whitespace that may follow SGR is ESC, otherwise
6373 * NULL.
6374 */
6375 static char_u *
6376sgrn2c(
6377 char_u *head,
6378 int n)
6379{
6380 char_u *p = sgrn2(head, n);
6381
6382 return p && *p != NUL && (p = skipblank(p)) && *p == '\033' ? p : NULL;
6383}
6384
6385/*
6386 * If there is only a newline between the sequence immediately following it,
6387 * a pointer to the character following the newline is returned.
6388 * Otherwise NULL.
6389 */
6390 static char_u *
6391sgrn2cn(
6392 char_u *head,
6393 int n)
6394{
6395 char_u *p = sgrn2(head, n);
6396
6397 return p && p[0] == 0x0a && p[1] == '\033' ? ++p : NULL;
6398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400/*
6401 * mch_write(): write the output buffer to the screen, translating ESC
6402 * sequences into calls to console output routines.
6403 */
6404 void
6405mch_write(
6406 char_u *s,
6407 int len)
6408{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006409# ifdef VIMDLL
6410 if (gui.in_use)
6411 return;
6412# endif
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 s[len] = NUL;
6415
6416 if (!term_console)
6417 {
6418 write(1, s, (unsigned)len);
6419 return;
6420 }
6421
Bram Moolenaar0f873732019-12-05 20:28:46 +01006422 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 while (len--)
6424 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006425 int prefix = -1;
6426 char_u ch;
6427
6428 // While processing a sequence, on rare occasions it seems that another
6429 // sequence may be inserted asynchronously.
6430 if (len < 0)
6431 {
6432 redraw_all_later(CLEAR);
6433 return;
6434 }
6435
6436 while((ch = s[++prefix]))
6437 if (ch <= 0x1e && !(ch != '\n' && ch != '\r' && ch != '\b'
6438 && ch != '\a' && ch != '\033'))
6439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
6441 if (p_wd)
6442 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006443 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 if (prefix != 0)
6445 prefix = 1;
6446 }
6447
6448 if (prefix != 0)
6449 {
6450 DWORD nWritten;
6451
6452 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006453# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (fdDump)
6455 {
6456 fputc('>', fdDump);
6457 fwrite(s, sizeof(char_u), nWritten, fdDump);
6458 fputs("<\n", fdDump);
6459 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 len -= (nWritten - 1);
6462 s += nWritten;
6463 }
6464 else if (s[0] == '\n')
6465 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006466 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (g_coord.Y == g_srScrollRegion.Bottom)
6468 {
6469 scroll(1);
6470 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6471 }
6472 else
6473 {
6474 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6475 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006476# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (fdDump)
6478 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 s++;
6481 }
6482 else if (s[0] == '\r')
6483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006484 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (fdDump)
6488 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 s++;
6491 }
6492 else if (s[0] == '\b')
6493 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006494 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (g_coord.X > g_srScrollRegion.Left)
6496 g_coord.X--;
6497 else if (g_coord.Y > g_srScrollRegion.Top)
6498 {
6499 g_coord.X = g_srScrollRegion.Right;
6500 g_coord.Y--;
6501 }
6502 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006503# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if (fdDump)
6505 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 s++;
6508 }
6509 else if (s[0] == '\a')
6510 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006511 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006513# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (fdDump)
6515 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 s++;
6518 }
6519 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6520 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006521# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006522 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006523# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006524 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006525 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006526 char_u *sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
6528 switch (s[2])
6529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 case '0': case '1': case '2': case '3': case '4':
6531 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006532 if (*(p = get_seq(args, &argc, s)) != 'm')
6533 goto notsgr;
6534
6535 p = s;
6536
6537 // Handling frequent optional sequences. Output to the screen
6538 // takes too long, so do not output as much as possible.
6539
6540 // If resetFG,FG,BG,<cr>,BG,FG are connected, the preceding
6541 // resetFG,FG,BG are omitted.
6542 if (sgrn2(sgrn2(sgrn2cn(sgrn2(sgrnc(p, 39), 38), 48), 48), 38))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006543 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006544 p = sgrn2(sgrn2(sgrnc(p, 39), 38), 48);
6545 len = len + 1 - (int)(p - s);
6546 s = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 break;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006550 // If FG,BG,BG,FG of SGR are connected, the first FG can be
6551 // omitted.
6552 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 48), 38))
6553 p = sp;
6554
6555 // If FG,BG,FG,BG of SGR are connected, the first FG can be
6556 // omitted.
6557 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 38), 48))
6558 p = sp;
6559
6560 // If BG,BG of SGR are connected, the first BG can be omitted.
6561 if (sgrn2((sp = sgrn2(p, 48)), 48))
6562 p = sp;
6563
6564 // If restoreFG and FG are connected, the restoreFG can be
6565 // omitted.
6566 if (sgrn2((sp = sgrnc(p, 39)), 38))
6567 p = sp;
6568
6569 p = get_seq(args, &argc, p);
6570
6571notsgr:
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006572 arg1 = args[0];
6573 arg2 = args[1];
6574 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006576 if (argc == 1 && args[0] == 0)
6577 normvideo();
6578 else if (argc == 1)
6579 {
6580 if (USE_VTP)
6581 textcolor((WORD) arg1);
6582 else
6583 textattr((WORD) arg1);
6584 }
6585 else if (USE_VTP)
6586 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006588 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006590 gotoxy(arg2, arg1);
6591 }
6592 else if (argc == 2 && *p == 'r')
6593 {
6594 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6595 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006596 else if (argc == 2 && *p == 'R')
6597 {
6598 set_scroll_region_tb(arg1, arg2);
6599 }
6600 else if (argc == 2 && *p == 'V')
6601 {
6602 set_scroll_region_lr(arg1, arg2);
6603 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006604 else if (argc == 1 && *p == 'A')
6605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 gotoxy(g_coord.X + 1,
6607 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6608 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006609 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
6611 textbackground((WORD) arg1);
6612 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006613 else if (argc == 1 && *p == 'C')
6614 {
6615 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6616 g_coord.Y + 1);
6617 }
6618 else if (argc == 1 && *p == 'f')
6619 {
6620 textcolor((WORD) arg1);
6621 }
6622 else if (argc == 1 && *p == 'H')
6623 {
6624 gotoxy(1, arg1);
6625 }
6626 else if (argc == 1 && *p == 'L')
6627 {
6628 insert_lines(arg1);
6629 }
6630 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
6632 delete_lines(arg1);
6633 }
6634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006635 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 s = p + 1;
6637 break;
6638
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 gotoxy(g_coord.X + 1,
6641 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6642 goto got3;
6643
6644 case 'B':
6645 visual_bell();
6646 goto got3;
6647
6648 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6650 g_coord.Y + 1);
6651 goto got3;
6652
6653 case 'E':
6654 termcap_mode_end();
6655 goto got3;
6656
6657 case 'F':
6658 standout();
6659 goto got3;
6660
6661 case 'f':
6662 standend();
6663 goto got3;
6664
6665 case 'H':
6666 gotoxy(1, 1);
6667 goto got3;
6668
6669 case 'j':
6670 clear_to_end_of_display();
6671 goto got3;
6672
6673 case 'J':
6674 clear_screen();
6675 goto got3;
6676
6677 case 'K':
6678 clear_to_end_of_line();
6679 goto got3;
6680
6681 case 'L':
6682 insert_lines(1);
6683 goto got3;
6684
6685 case 'M':
6686 delete_lines(1);
6687 goto got3;
6688
6689 case 'S':
6690 termcap_mode_start();
6691 goto got3;
6692
6693 case 'V':
6694 cursor_visible(TRUE);
6695 goto got3;
6696
6697 case 'v':
6698 cursor_visible(FALSE);
6699 goto got3;
6700
6701 got3:
6702 s += 3;
6703 len -= 2;
6704 }
6705
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006706# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (fdDump)
6708 {
6709 fputs("ESC | ", fdDump);
6710 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6711 fputc('\n', fdDump);
6712 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715 else
6716 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 DWORD nWritten;
6719
6720 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006721# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (fdDump)
6723 {
6724 fputc('>', fdDump);
6725 fwrite(s, sizeof(char_u), nWritten, fdDump);
6726 fputs("<\n", fdDump);
6727 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 len -= (nWritten - 1);
6731 s += nWritten;
6732 }
6733 }
6734
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006735# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (fdDump)
6737 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
Bram Moolenaar0f873732019-12-05 20:28:46 +01006741#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743
6744/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006745 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 */
6747 void
6748mch_delay(
6749 long msec,
Bram Moolenaar0981c872020-08-23 14:28:37 +02006750 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006752#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006753 Sleep((int)msec); // never wait for input
6754#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006755# ifdef VIMDLL
6756 if (gui.in_use)
6757 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006758 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006759 return;
6760 }
6761# endif
Bram Moolenaar0981c872020-08-23 14:28:37 +02006762 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006763# ifdef FEAT_MZSCHEME
6764 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6765 {
6766 int towait = p_mzq;
6767
Bram Moolenaar0f873732019-12-05 20:28:46 +01006768 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006769 while (msec > 0)
6770 {
6771 mzvim_check_threads();
6772 if (msec < towait)
6773 towait = msec;
6774 Sleep(towait);
6775 msec -= towait;
6776 }
6777 }
6778 else
6779# endif
6780 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006782 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783#endif
6784}
6785
6786
6787/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006788 * This version of remove is not scared by a readonly (backup) file.
6789 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 * Return 0 for success, -1 for failure.
6791 */
6792 int
6793mch_remove(char_u *name)
6794{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006795 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int n;
6797
Bram Moolenaar203258c2016-01-17 22:15:16 +01006798 /*
6799 * On Windows, deleting a directory's symbolic link is done by
6800 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6801 */
6802 if (mch_isdir(name) && mch_is_symbolic_link(name))
6803 return mch_rmdir(name);
6804
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006805 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6806
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006807 wn = enc_to_utf16(name, NULL);
6808 if (wn == NULL)
6809 return -1;
6810
6811 n = DeleteFileW(wn) ? 0 : -1;
6812 vim_free(wn);
6813 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816
6817/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006818 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 */
6820 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006821mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006823#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6824# ifdef VIMDLL
6825 if (!gui.in_use)
6826# endif
6827 if (g_fCtrlCPressed || g_fCBrkPressed)
6828 {
6829 ctrl_break_was_pressed = g_fCBrkPressed;
6830 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6831 got_int = TRUE;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#endif
6834}
6835
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006837#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006838
6839/*
6840 * How much main memory in KiB that can be used by VIM.
6841 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006842 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006843mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006844{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006845 MEMORYSTATUSEX ms;
6846
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 // Need to use GlobalMemoryStatusEx() when there is more memory than
6848 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006849 ms.dwLength = sizeof(MEMORYSTATUSEX);
6850 GlobalMemoryStatusEx(&ms);
6851 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006852 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006854 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006855 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006856 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006859 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006860 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006861 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006862 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006866 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6868 * file whose short file name is "FOO.BAR" (its long file name will
6869 * be correct: "foo.bar~"). Because a file can be accessed by
6870 * either its SFN or its LFN, "foo.bar" has effectively been
6871 * renamed to "foo.bar", which is not at all what was wanted. This
6872 * seems to happen only when renaming files with three-character
6873 * extensions by appending a suffix that does not include ".".
6874 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6875 *
6876 * There is another problem, which isn't really a bug but isn't right either:
6877 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6878 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6879 * service pack 6. Doesn't seem to happen on Windows 98.
6880 *
6881 * Like rename(), returns 0 upon success, non-zero upon failure.
6882 * Should probably set errno appropriately when errors occur.
6883 */
6884 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006885mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006887 WCHAR *p;
6888 int i;
6889 WCHAR szTempFile[_MAX_PATH + 1];
6890 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006893 // No need to play tricks unless the file name contains a "~" as the
6894 // seventh character.
6895 p = wold;
6896 for (i = 0; wold[i] != NUL; ++i)
6897 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6898 && wold[i + 1] != 0)
6899 p = wold + i + 1;
6900 if ((int)(wold + i - p) < 8 || p[6] != '~')
6901 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006903 // Get base path of new file name. Undocumented feature: If pszNewFile is
6904 // a directory, no error is returned and pszFilePart will be NULL.
6905 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006907 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006909 // Get (and create) a unique temporary file name in directory of new file
6910 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 return -2;
6912
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006913 // blow the temp file away
6914 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 return -3;
6916
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006917 // rename old file to the temp file
6918 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 return -4;
6920
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006921 // now create an empty file called pszOldFile; this prevents the operating
6922 // system using pszOldFile as an alias (SFN) if we're renaming within the
6923 // same directory. For example, we're editing a file called
6924 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6925 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6926 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6927 // cause all sorts of problems later in buf_write(). So, we create an
6928 // empty file called filena~1.txt and the system will have to find some
6929 // other SFN for filena~1.txt~, such as filena~2.txt
6930 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6932 return -5;
6933 if (!CloseHandle(hf))
6934 return -6;
6935
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006936 // rename the temp file to the new file
6937 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006939 // Renaming failed. Rename the file back to its old name, so that it
6940 // looks like nothing happened.
6941 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 return -7;
6943 }
6944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006945 // Seems to be left around on Novell filesystems
6946 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006948 // finally, remove the empty old file
6949 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 return -8;
6951
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006952 return 0;
6953}
6954
6955
6956/*
6957 * Converts the filenames to UTF-16, then call mch_wrename().
6958 * Like rename(), returns 0 upon success, non-zero upon failure.
6959 */
6960 int
6961mch_rename(
6962 const char *pszOldFile,
6963 const char *pszNewFile)
6964{
6965 WCHAR *wold = NULL;
6966 WCHAR *wnew = NULL;
6967 int retval = -1;
6968
6969 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6970 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6971 if (wold != NULL && wnew != NULL)
6972 retval = mch_wrename(wold, wnew);
6973 vim_free(wold);
6974 vim_free(wnew);
6975 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976}
6977
6978/*
6979 * Get the default shell for the current hardware platform
6980 */
6981 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006982default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006984 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985}
6986
6987/*
6988 * mch_access() extends access() to do more detailed check on network drives.
6989 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6990 */
6991 int
6992mch_access(char *n, int p)
6993{
6994 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006996 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006998 wn = enc_to_utf16((char_u *)n, NULL);
6999 if (wn == NULL)
7000 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01007002 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 if (p & R_OK)
7007 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007008 // Read check is performed by seeing if we can do a find file on
7009 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007010 int i;
7011 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007013 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
7014 TempNameW[i] = wn[i];
7015 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
7016 TempNameW[i++] = '\\';
7017 TempNameW[i++] = '*';
7018 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007020 hFile = FindFirstFileW(TempNameW, &d);
7021 if (hFile == INVALID_HANDLE_VALUE)
7022 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026
7027 if (p & W_OK)
7028 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007029 // Trying to create a temporary file in the directory should catch
7030 // directories on read-only network shares. However, in
7031 // directories whose ACL allows writes but denies deletes will end
7032 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007033 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
7034 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007035 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007036 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038 }
7039 else
7040 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007041 // Don't consider a file read-only if another process has opened it.
7042 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
7043
Bram Moolenaar0f873732019-12-05 20:28:46 +01007044 // Trying to open the file for the required access does ACL, read-only
7045 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007046 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
7047 | ((p & R_OK) ? GENERIC_READ : 0);
7048
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007049 hFile = CreateFileW(wn, access_mode, share_mode,
7050 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (hFile == INVALID_HANDLE_VALUE)
7052 goto getout;
7053 CloseHandle(hFile);
7054 }
7055
Bram Moolenaar0f873732019-12-05 20:28:46 +01007056 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 return retval;
7060}
7061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007063 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 */
7065 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007066mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 WCHAR *wn;
7069 int f;
7070
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007071 wn = enc_to_utf16((char_u *)name, NULL);
7072 if (wn == NULL)
7073 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007075 f = _wopen(wn, flags, mode);
7076 vim_free(wn);
7077 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078}
7079
7080/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007081 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 */
7083 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007084mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086 WCHAR *wn, *wm;
7087 FILE *f = NULL;
7088
Bram Moolenaara12a1612019-01-24 16:39:02 +01007089#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01007090 // Work around an annoying assertion in the Microsoft debug CRT
7091 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007092 char newMode = mode[strlen(mode) - 1];
7093 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007094
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007095 _get_fmode(&oldMode);
7096 if (newMode == 't')
7097 _set_fmode(_O_TEXT);
7098 else if (newMode == 'b')
7099 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007100#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007101 wn = enc_to_utf16((char_u *)name, NULL);
7102 wm = enc_to_utf16((char_u *)mode, NULL);
7103 if (wn != NULL && wm != NULL)
7104 f = _wfopen(wn, wm);
7105 vim_free(wn);
7106 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007107
Bram Moolenaara12a1612019-01-24 16:39:02 +01007108#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007109 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007110#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007111 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114/*
7115 * SUB STREAM (aka info stream) handling:
7116 *
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007117 * NTFS can have sub streams for each file. The normal contents of a file is
7118 * stored in the main stream, and extra contents (author information, title and
7119 * so on) can be stored in a sub stream. After Windows 2000, the user can
7120 * access and store this information in sub streams via an explorer's property
7121 * menu item in the right click menu. This information in sub streams was lost
7122 * when copying only the main stream. Therefore we have to copy sub streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 *
7124 * Incomplete explanation:
7125 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
7126 * More useful info and an example:
7127 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
7128 */
7129
7130/*
7131 * Copy info stream data "substream". Read from the file with BackupRead(sh)
7132 * and write to stream "substream" of file "to".
7133 * Errors are ignored.
7134 */
7135 static void
7136copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
7137{
7138 HANDLE hTo;
7139 WCHAR *to_name;
7140
7141 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
7142 wcscpy(to_name, to);
7143 wcscat(to_name, substream);
7144
7145 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
7146 FILE_ATTRIBUTE_NORMAL, NULL);
7147 if (hTo != INVALID_HANDLE_VALUE)
7148 {
7149 long done;
7150 DWORD todo;
7151 DWORD readcnt, written;
7152 char buf[4096];
7153
Bram Moolenaar0f873732019-12-05 20:28:46 +01007154 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 for (done = 0; done < len; done += written)
7156 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007157 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007158 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
7159 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
7161 FALSE, FALSE, context)
7162 || readcnt != todo
7163 || !WriteFile(hTo, buf, todo, &written, NULL)
7164 || written != todo)
7165 break;
7166 }
7167 CloseHandle(hTo);
7168 }
7169
7170 free(to_name);
7171}
7172
7173/*
7174 * Copy info streams from file "from" to file "to".
7175 */
7176 static void
7177copy_infostreams(char_u *from, char_u *to)
7178{
7179 WCHAR *fromw;
7180 WCHAR *tow;
7181 HANDLE sh;
7182 WIN32_STREAM_ID sid;
7183 int headersize;
7184 WCHAR streamname[_MAX_PATH];
7185 DWORD readcount;
7186 void *context = NULL;
7187 DWORD lo, hi;
7188 int len;
7189
Bram Moolenaar0f873732019-12-05 20:28:46 +01007190 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007191 fromw = enc_to_utf16(from, NULL);
7192 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (fromw != NULL && tow != NULL)
7194 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007195 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
7197 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
7198 if (sh != INVALID_HANDLE_VALUE)
7199 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007200 // Use BackupRead() to find the info streams. Repeat until we
7201 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 for (;;)
7203 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007204 // Get the header to find the length of the stream name. If
7205 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007207 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
7209 &readcount, FALSE, FALSE, &context)
7210 || readcount == 0)
7211 break;
7212
Bram Moolenaar0f873732019-12-05 20:28:46 +01007213 // We only deal with streams that have a name. The normal
7214 // file data appears to be without a name, even though docs
7215 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (sid.dwStreamNameSize > 0)
7217 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007218 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (!BackupRead(sh, (LPBYTE)streamname,
7220 sid.dwStreamNameSize,
7221 &readcount, FALSE, FALSE, &context))
7222 break;
7223
Bram Moolenaar0f873732019-12-05 20:28:46 +01007224 // Copy an info stream with a name ":anything:$DATA".
7225 // Skip "::$DATA", it has no stream name (examples suggest
7226 // it might be used for the normal file contents).
7227 // Note that BackupRead() counts bytes, but the name is in
7228 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 len = readcount / sizeof(WCHAR);
7230 streamname[len] = 0;
7231 if (len > 7 && wcsicmp(streamname + len - 6,
7232 L":$DATA") == 0)
7233 {
7234 streamname[len - 6] = 0;
7235 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007236 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238 }
7239
Bram Moolenaar0f873732019-12-05 20:28:46 +01007240 // Advance to the next stream. We might try seeking too far,
7241 // but BackupSeek() doesn't skip over stream borders, thus
7242 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007243 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 &lo, &hi, &context);
7245 }
7246
Bram Moolenaar0f873732019-12-05 20:28:46 +01007247 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
7249
7250 CloseHandle(sh);
7251 }
7252 }
7253 vim_free(fromw);
7254 vim_free(tow);
7255}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257/*
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007258 * ntdll.dll definitions
7259 */
7260#define FileEaInformation 7
7261#ifndef STATUS_SUCCESS
7262# define STATUS_SUCCESS ((NTSTATUS) 0x00000000L)
7263#endif
7264
7265typedef struct _FILE_FULL_EA_INFORMATION_ {
7266 ULONG NextEntryOffset;
7267 UCHAR Flags;
7268 UCHAR EaNameLength;
7269 USHORT EaValueLength;
7270 CHAR EaName[1];
7271} FILE_FULL_EA_INFORMATION_, *PFILE_FULL_EA_INFORMATION_;
7272
7273typedef struct _FILE_EA_INFORMATION_ {
7274 ULONG EaSize;
7275} FILE_EA_INFORMATION_, *PFILE_EA_INFORMATION_;
7276
7277typedef NTSTATUS (NTAPI *PfnNtOpenFile)(
7278 PHANDLE FileHandle,
7279 ACCESS_MASK DesiredAccess,
7280 POBJECT_ATTRIBUTES ObjectAttributes,
7281 PIO_STATUS_BLOCK IoStatusBlock,
7282 ULONG ShareAccess,
7283 ULONG OpenOptions);
7284typedef NTSTATUS (NTAPI *PfnNtClose)(
7285 HANDLE Handle);
7286typedef NTSTATUS (NTAPI *PfnNtSetEaFile)(
7287 HANDLE FileHandle,
7288 PIO_STATUS_BLOCK IoStatusBlock,
7289 PVOID Buffer,
7290 ULONG Length);
7291typedef NTSTATUS (NTAPI *PfnNtQueryEaFile)(
7292 HANDLE FileHandle,
7293 PIO_STATUS_BLOCK IoStatusBlock,
7294 PVOID Buffer,
7295 ULONG Length,
7296 BOOLEAN ReturnSingleEntry,
7297 PVOID EaList,
7298 ULONG EaListLength,
7299 PULONG EaIndex,
7300 BOOLEAN RestartScan);
7301typedef NTSTATUS (NTAPI *PfnNtQueryInformationFile)(
7302 HANDLE FileHandle,
7303 PIO_STATUS_BLOCK IoStatusBlock,
7304 PVOID FileInformation,
7305 ULONG Length,
7306 FILE_INFORMATION_CLASS FileInformationClass);
7307typedef VOID (NTAPI *PfnRtlInitUnicodeString)(
7308 PUNICODE_STRING DestinationString,
7309 PCWSTR SourceString);
7310
7311PfnNtOpenFile pNtOpenFile = NULL;
7312PfnNtClose pNtClose = NULL;
7313PfnNtSetEaFile pNtSetEaFile = NULL;
7314PfnNtQueryEaFile pNtQueryEaFile = NULL;
7315PfnNtQueryInformationFile pNtQueryInformationFile = NULL;
7316PfnRtlInitUnicodeString pRtlInitUnicodeString = NULL;
7317
7318/*
7319 * Load ntdll.dll functions.
7320 */
7321 static BOOL
7322load_ntdll(void)
7323{
7324 static int loaded = -1;
7325
7326 if (loaded == -1)
7327 {
7328 HMODULE hNtdll = GetModuleHandle("ntdll.dll");
7329 if (hNtdll != NULL)
7330 {
7331 pNtOpenFile = (PfnNtOpenFile) GetProcAddress(hNtdll, "NtOpenFile");
7332 pNtClose = (PfnNtClose) GetProcAddress(hNtdll, "NtClose");
7333 pNtSetEaFile = (PfnNtSetEaFile)
7334 GetProcAddress(hNtdll, "NtSetEaFile");
7335 pNtQueryEaFile = (PfnNtQueryEaFile)
7336 GetProcAddress(hNtdll, "NtQueryEaFile");
7337 pNtQueryInformationFile = (PfnNtQueryInformationFile)
7338 GetProcAddress(hNtdll, "NtQueryInformationFile");
7339 pRtlInitUnicodeString = (PfnRtlInitUnicodeString)
7340 GetProcAddress(hNtdll, "RtlInitUnicodeString");
7341 }
7342 if (pNtOpenFile == NULL
7343 || pNtClose == NULL
7344 || pNtSetEaFile == NULL
7345 || pNtQueryEaFile == NULL
7346 || pNtQueryInformationFile == NULL
7347 || pRtlInitUnicodeString == NULL)
7348 loaded = FALSE;
7349 else
7350 loaded = TRUE;
7351 }
7352 return (BOOL) loaded;
7353}
7354
7355/*
7356 * Copy extended attributes (EA) from file "from" to file "to".
7357 */
7358 static void
7359copy_extattr(char_u *from, char_u *to)
7360{
7361 char_u *fromf = NULL;
7362 char_u *tof = NULL;
7363 WCHAR *fromw = NULL;
7364 WCHAR *tow = NULL;
7365 UNICODE_STRING u;
7366 HANDLE h;
7367 OBJECT_ATTRIBUTES oa;
7368 IO_STATUS_BLOCK iosb;
7369 FILE_EA_INFORMATION_ eainfo = {0};
7370 void *ea = NULL;
7371
7372 if (!load_ntdll())
7373 return;
7374
7375 // Convert the file names to the fully qualified object names.
7376 fromf = alloc(STRLEN(from) + 5);
7377 tof = alloc(STRLEN(to) + 5);
7378 if (fromf == NULL || tof == NULL)
7379 goto theend;
7380 STRCPY(fromf, "\\??\\");
7381 STRCAT(fromf, from);
7382 STRCPY(tof, "\\??\\");
7383 STRCAT(tof, to);
7384
7385 // Convert the names to wide characters.
7386 fromw = enc_to_utf16(fromf, NULL);
7387 tow = enc_to_utf16(tof, NULL);
7388 if (fromw == NULL || tow == NULL)
7389 goto theend;
7390
7391 // Get the EA.
7392 pRtlInitUnicodeString(&u, fromw);
7393 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
7394 if (pNtOpenFile(&h, FILE_READ_EA, &oa, &iosb, 0,
7395 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
7396 goto theend;
7397 pNtQueryInformationFile(h, &iosb, &eainfo, sizeof(eainfo),
7398 FileEaInformation);
7399 if (eainfo.EaSize != 0)
7400 {
7401 ea = alloc(eainfo.EaSize);
7402 if (ea != NULL)
7403 {
7404 if (pNtQueryEaFile(h, &iosb, ea, eainfo.EaSize, FALSE,
7405 NULL, 0, NULL, TRUE) != STATUS_SUCCESS)
7406 {
7407 vim_free(ea);
7408 ea = NULL;
7409 }
7410 }
7411 }
7412 pNtClose(h);
7413
7414 // Set the EA.
7415 if (ea != NULL)
7416 {
7417 pRtlInitUnicodeString(&u, tow);
7418 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
7419 if (pNtOpenFile(&h, FILE_WRITE_EA, &oa, &iosb, 0,
7420 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
7421 goto theend;
7422
7423 pNtSetEaFile(h, &iosb, ea, eainfo.EaSize);
7424 pNtClose(h);
7425 }
7426
7427theend:
7428 vim_free(fromf);
7429 vim_free(tof);
7430 vim_free(fromw);
7431 vim_free(tow);
7432 vim_free(ea);
7433}
7434
7435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 * Copy file attributes from file "from" to file "to".
7437 * For Windows NT and later we copy info streams.
7438 * Always returns zero, errors are ignored.
7439 */
7440 int
7441mch_copy_file_attribute(char_u *from, char_u *to)
7442{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007443 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007444 copy_infostreams(from, to);
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007445 copy_extattr(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 return 0;
7447}
7448
7449#if defined(MYRESETSTKOFLW) || defined(PROTO)
7450/*
7451 * Recreate a destroyed stack guard page in win32.
7452 * Written by Benjamin Peterson.
7453 */
7454
Bram Moolenaar0f873732019-12-05 20:28:46 +01007455// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007456# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458/*
7459 * This function does the same thing as _resetstkoflw(), which is only
7460 * available in DevStudio .net and later.
7461 * Returns 0 for failure, 1 for success.
7462 */
7463 int
7464myresetstkoflw(void)
7465{
7466 BYTE *pStackPtr;
7467 BYTE *pGuardPage;
7468 BYTE *pStackBase;
7469 BYTE *pLowestPossiblePage;
7470 MEMORY_BASIC_INFORMATION mbi;
7471 SYSTEM_INFO si;
7472 DWORD nPageSize;
7473 DWORD dummy;
7474
Bram Moolenaar0f873732019-12-05 20:28:46 +01007475 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 GetSystemInfo(&si);
7477 nPageSize = si.dwPageSize;
7478
Bram Moolenaar0f873732019-12-05 20:28:46 +01007479 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 pStackPtr = (BYTE*)_alloca(1);
7481
Bram Moolenaar0f873732019-12-05 20:28:46 +01007482 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
7484 return 0;
7485 pStackBase = (BYTE*)mbi.AllocationBase;
7486
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007487 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01007488 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007489 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007492 // We want the first committed page in the stack Start at the stack
7493 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 BYTE *pBlock = pStackBase;
7495
Bram Moolenaara466c992005-07-09 21:03:22 +00007496 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
7498 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
7499 return 0;
7500
7501 pBlock += mbi.RegionSize;
7502
7503 if (mbi.State & MEM_COMMIT)
7504 break;
7505 }
7506
Bram Moolenaar0f873732019-12-05 20:28:46 +01007507 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 if (mbi.Protect & PAGE_GUARD)
7509 return 1;
7510
Bram Moolenaar0f873732019-12-05 20:28:46 +01007511 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
7513 pGuardPage = pLowestPossiblePage;
7514 else
7515 pGuardPage = (BYTE*)mbi.BaseAddress;
7516
Bram Moolenaar0f873732019-12-05 20:28:46 +01007517 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7519 return 0;
7520
Bram Moolenaar0f873732019-12-05 20:28:46 +01007521 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7523 &dummy))
7524 return 0;
7525 }
7526
7527 return 1;
7528}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007531
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007532/*
7533 * The command line arguments in UCS2
7534 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007535static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007536static LPWSTR *ArglistW = NULL;
7537static int global_argc = 0;
7538static char **global_argv;
7539
Bram Moolenaar0f873732019-12-05 20:28:46 +01007540static int used_file_argc = 0; // last argument in global_argv[] used
7541 // for the argument list.
7542static int *used_file_indexes = NULL; // indexes in global_argv[] for
7543 // command line arguments added to
7544 // the argument list
7545static int used_file_count = 0; // nr of entries in used_file_indexes
7546static int used_file_literal = FALSE; // take file names literally
7547static int used_file_full_path = FALSE; // file name was full path
7548static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007549static int used_alist_count = 0;
7550
7551
7552/*
7553 * Get the command line arguments. Unicode version.
7554 * Returns argc. Zero when something fails.
7555 */
7556 int
7557get_cmd_argsW(char ***argvp)
7558{
7559 char **argv = NULL;
7560 int argc = 0;
7561 int i;
7562
Bram Moolenaar14993322014-09-09 12:25:33 +02007563 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007564 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7565 if (ArglistW != NULL)
7566 {
7567 argv = malloc((nArgsW + 1) * sizeof(char *));
7568 if (argv != NULL)
7569 {
7570 argc = nArgsW;
7571 argv[argc] = NULL;
7572 for (i = 0; i < argc; ++i)
7573 {
7574 int len;
7575
Bram Moolenaar0f873732019-12-05 20:28:46 +01007576 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007577 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007578 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007579 (LPSTR *)&argv[i], &len, 0, 0);
7580 if (argv[i] == NULL)
7581 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007582 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007583 while (i > 0)
7584 free(argv[--i]);
7585 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007586 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007587 argc = 0;
7588 }
7589 }
7590 }
7591 }
7592
7593 global_argc = argc;
7594 global_argv = argv;
7595 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007596 {
7597 if (used_file_indexes != NULL)
7598 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007599 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007600 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007601
7602 if (argvp != NULL)
7603 *argvp = argv;
7604 return argc;
7605}
7606
7607 void
7608free_cmd_argsW(void)
7609{
7610 if (ArglistW != NULL)
7611 {
7612 GlobalFree(ArglistW);
7613 ArglistW = NULL;
7614 }
7615}
7616
7617/*
7618 * Remember "name" is an argument that was added to the argument list.
7619 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7620 * is called.
7621 */
7622 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007623used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007624{
7625 int i;
7626
7627 if (used_file_indexes == NULL)
7628 return;
7629 for (i = used_file_argc + 1; i < global_argc; ++i)
7630 if (STRCMP(global_argv[i], name) == 0)
7631 {
7632 used_file_argc = i;
7633 used_file_indexes[used_file_count++] = i;
7634 break;
7635 }
7636 used_file_literal = literal;
7637 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007638 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007639}
7640
7641/*
7642 * Remember the length of the argument list as it was. If it changes then we
7643 * leave it alone when 'encoding' is set.
7644 */
7645 void
7646set_alist_count(void)
7647{
7648 used_alist_count = GARGCOUNT;
7649}
7650
7651/*
7652 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7653 * has been changed while starting up. Use the UCS-2 command line arguments
7654 * and convert them to 'encoding'.
7655 */
7656 void
7657fix_arg_enc(void)
7658{
7659 int i;
7660 int idx;
7661 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007662 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007663
Bram Moolenaar0f873732019-12-05 20:28:46 +01007664 // Safety checks:
7665 // - if argument count differs between the wide and non-wide argument
7666 // list, something must be wrong.
7667 // - the file name arguments must have been located.
7668 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007669 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007670 || ArglistW == NULL
7671 || used_file_indexes == NULL
7672 || used_file_count == 0
7673 || used_alist_count != GARGCOUNT)
7674 return;
7675
Bram Moolenaar0f873732019-12-05 20:28:46 +01007676 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007677 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007678 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007679 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007680 for (i = 0; i < GARGCOUNT; ++i)
7681 fnum_list[i] = GARGLIST[i].ae_fnum;
7682
Bram Moolenaar0f873732019-12-05 20:28:46 +01007683 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007684 alist_clear(&global_alist);
7685 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007686 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007687
7688 for (i = 0; i < used_file_count; ++i)
7689 {
7690 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007691 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007692 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007693 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007694 int literal = used_file_literal;
7695
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007696#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007697 // When using diff mode may need to concatenate file name to
7698 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007699 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7700 && !mch_isdir(alist_name(&GARGLIST[0])))
7701 {
7702 char_u *r;
7703
7704 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7705 if (r != NULL)
7706 {
7707 vim_free(str);
7708 str = r;
7709 }
7710 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007711#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007712 // Re-use the old buffer by renaming it. When not using literal
7713 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007714 if (used_file_literal)
7715 buf_set_name(fnum_list[i], str);
7716
Bram Moolenaar0f873732019-12-05 20:28:46 +01007717 // Check backtick literal. backtick literal is already expanded in
7718 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007719 if (literal == FALSE)
7720 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007721 size_t len = STRLEN(str);
7722
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007723 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7724 literal = TRUE;
7725 }
7726 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007727 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007728 }
7729
7730 if (!used_file_literal)
7731 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007732 // Now expand wildcards in the arguments.
7733 // Temporarily add '(' and ')' to 'isfname'. These are valid
7734 // filename characters but are excluded from 'isfname' to make
7735 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7736 // Also, unset wildignore to not be influenced by this option.
7737 // The arguments specified in command-line should be kept even if
7738 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007739 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007740 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007741 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007742 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007743 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007744 }
7745
Bram Moolenaar0f873732019-12-05 20:28:46 +01007746 // If wildcard expansion failed, we are editing the first file of the
7747 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007748 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7749 {
7750 do_cmdline_cmd((char_u *)":rewind");
7751 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007752 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007753 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007754
7755 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007756}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007757
7758 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007759mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007760{
7761 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007762 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007763
Bram Moolenaar964b3742019-05-24 18:54:09 +02007764 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007765 if (envbuf == NULL)
7766 return -1;
7767
7768 sprintf((char *)envbuf, "%s=%s", var, value);
7769
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007770 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007771
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007772 vim_free(envbuf);
7773 if (p == NULL)
7774 return -1;
7775 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007776#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007777 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007778#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007779 // Unlike Un*x systems, we can free the string for _wputenv().
7780 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007781
7782 return 0;
7783}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007784
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007785/*
7786 * Support for 256 colors and 24-bit colors was added in Windows 10
7787 * version 1703 (Creators update).
7788 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007789#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7790
7791/*
7792 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007793 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007794 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007795#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007796
7797/*
7798 * ConPTY differences between versions, need different logic.
7799 * version 1903 (May 2019 update).
7800 */
7801#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7802
7803/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007804 * version 1909 (November 2019 update).
7805 */
7806#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7807
7808/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007809 * Stay ahead of the next update, and when it's done, fix this.
7810 * version ? (2020 update, temporarily use the build number of insider preview)
7811 */
7812#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
7813
7814/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007815 * Confirm until this version. Also the logic changes.
7816 * insider preview.
7817 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007818#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007819
7820/*
7821 * Not stable now.
7822 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007823#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007824
7825 static void
7826vtp_flag_init(void)
7827{
7828 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007829#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007830 DWORD mode;
7831 HANDLE out;
7832
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007833# ifdef VIMDLL
7834 if (!gui.in_use)
7835# endif
7836 {
7837 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007838
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007839 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7840 GetConsoleMode(out, &mode);
7841 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7842 if (SetConsoleMode(out, mode) == 0)
7843 vtp_working = 0;
7844 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007845#endif
7846
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007847 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007848 conpty_working = 1;
7849 if (ver >= CONPTY_STABLE_BUILD)
7850 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007851
Bram Moolenaar57da6982019-09-13 22:30:11 +02007852 if (ver <= CONPTY_INSIDER_BUILD)
7853 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007854 if (ver <= CONPTY_1909_BUILD)
7855 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007856 if (ver <= CONPTY_1903_BUILD)
7857 conpty_type = 2;
7858 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7859 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007860
7861 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
7862 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007863}
7864
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007865#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007866
7867 static void
7868vtp_init(void)
7869{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007870 HMODULE hKerneldll;
7871 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007872# ifdef FEAT_TERMGUICOLORS
7873 COLORREF fg, bg;
7874# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007875
Bram Moolenaar0f873732019-12-05 20:28:46 +01007876 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007877 hKerneldll = GetModuleHandle("kernel32.dll");
7878 if (hKerneldll != NULL)
7879 {
7880 pGetConsoleScreenBufferInfoEx =
7881 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7882 hKerneldll, "GetConsoleScreenBufferInfoEx");
7883 pSetConsoleScreenBufferInfoEx =
7884 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7885 hKerneldll, "SetConsoleScreenBufferInfoEx");
7886 if (pGetConsoleScreenBufferInfoEx != NULL
7887 && pSetConsoleScreenBufferInfoEx != NULL)
7888 has_csbiex = TRUE;
7889 }
7890
7891 csbi.cbSize = sizeof(csbi);
7892 if (has_csbiex)
7893 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007894 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7895 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007896 store_console_bg_rgb = save_console_bg_rgb;
7897 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007898
7899# ifdef FEAT_TERMGUICOLORS
7900 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7901 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7902 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7903 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7904 default_console_color_bg = bg;
7905 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007906# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007907
7908 set_console_color_rgb();
7909}
7910
7911 static void
7912vtp_exit(void)
7913{
Bram Moolenaardf543822020-01-30 11:53:59 +01007914 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007915}
7916
Bram Moolenaar06b7b582020-05-30 17:49:25 +02007917 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007918vtp_printf(
7919 char *format,
7920 ...)
7921{
7922 char_u buf[100];
7923 va_list list;
7924 DWORD result;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007925 int len;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007926
7927 va_start(list, format);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007928 len = vim_vsnprintf((char *)buf, 100, (char *)format, list);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007929 va_end(list);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007930 WriteConsoleA(g_hConOut, buf, (DWORD)len, &result, NULL);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007931 return (int)result;
7932}
7933
7934 static void
7935vtp_sgr_bulk(
7936 int arg)
7937{
7938 int args[1];
7939
7940 args[0] = arg;
7941 vtp_sgr_bulks(1, args);
7942}
7943
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007944#define FAST256(x) \
7945 if ((*p-- = "0123456789"[(n = x % 10)]) \
7946 && x >= 10 && (*p-- = "0123456789"[((m = x % 100) - n) / 10]) \
7947 && x >= 100 && (*p-- = "012"[((x & 0xff) - m) / 100]));
7948
7949#define FAST256CASE(x) \
7950 case x: \
7951 FAST256(newargs[x - 1]);
7952
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007953 static void
7954vtp_sgr_bulks(
7955 int argc,
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007956 int *args)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007957{
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007958#define MAXSGR 16
7959#define SGRBUFSIZE 2 + 4 * MAXSGR + 1 // '\033[' + SGR + 'm'
7960 char_u buf[SGRBUFSIZE];
7961 char_u *p;
7962 int in, out;
7963 int newargs[16];
7964 static int sgrfgr = -1, sgrfgg, sgrfgb;
7965 static int sgrbgr = -1, sgrbgg, sgrbgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007966
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007967 if (argc == 0)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007968 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007969 sgrfgr = sgrbgr = -1;
7970 vtp_printf("033[m");
7971 return;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007972 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007973
7974 in = out = 0;
7975 while (in < argc)
7976 {
7977 int s = args[in];
7978 int copylen = 1;
7979
7980 if (s == 38)
7981 {
7982 if (argc - in >= 5 && args[in + 1] == 2)
7983 {
7984 if (sgrfgr == args[in + 2] && sgrfgg == args[in + 3]
7985 && sgrfgb == args[in + 4])
7986 {
7987 in += 5;
7988 copylen = 0;
7989 }
7990 else
7991 {
7992 sgrfgr = args[in + 2];
7993 sgrfgg = args[in + 3];
7994 sgrfgb = args[in + 4];
7995 copylen = 5;
7996 }
7997 }
7998 else if (argc - in >= 3 && args[in + 1] == 5)
7999 {
8000 sgrfgr = -1;
8001 copylen = 3;
8002 }
8003 }
8004 else if (s == 48)
8005 {
8006 if (argc - in >= 5 && args[in + 1] == 2)
8007 {
8008 if (sgrbgr == args[in + 2] && sgrbgg == args[in + 3]
8009 && sgrbgb == args[in + 4])
8010 {
8011 in += 5;
8012 copylen = 0;
8013 }
8014 else
8015 {
8016 sgrbgr = args[in + 2];
8017 sgrbgg = args[in + 3];
8018 sgrbgb = args[in + 4];
8019 copylen = 5;
8020 }
8021 }
8022 else if (argc - in >= 3 && args[in + 1] == 5)
8023 {
8024 sgrbgr = -1;
8025 copylen = 3;
8026 }
8027 }
8028 else if (30 <= s && s <= 39)
8029 sgrfgr = -1;
8030 else if (90 <= s && s <= 97)
8031 sgrfgr = -1;
8032 else if (40 <= s && s <= 49)
8033 sgrbgr = -1;
8034 else if (100 <= s && s <= 107)
8035 sgrbgr = -1;
8036 else if (s == 0)
8037 sgrfgr = sgrbgr = -1;
8038
8039 while (copylen--)
8040 newargs[out++] = args[in++];
8041 }
8042
8043 p = &buf[sizeof(buf) - 1];
8044 *p-- = 'm';
8045
8046 switch (out)
8047 {
8048 int n, m;
8049 DWORD r;
8050
8051 FAST256CASE(16);
8052 *p-- = ';';
8053 FAST256CASE(15);
8054 *p-- = ';';
8055 FAST256CASE(14);
8056 *p-- = ';';
8057 FAST256CASE(13);
8058 *p-- = ';';
8059 FAST256CASE(12);
8060 *p-- = ';';
8061 FAST256CASE(11);
8062 *p-- = ';';
8063 FAST256CASE(10);
8064 *p-- = ';';
8065 FAST256CASE(9);
8066 *p-- = ';';
8067 FAST256CASE(8);
8068 *p-- = ';';
8069 FAST256CASE(7);
8070 *p-- = ';';
8071 FAST256CASE(6);
8072 *p-- = ';';
8073 FAST256CASE(5);
8074 *p-- = ';';
8075 FAST256CASE(4);
8076 *p-- = ';';
8077 FAST256CASE(3);
8078 *p-- = ';';
8079 FAST256CASE(2);
8080 *p-- = ';';
8081 FAST256CASE(1);
8082 *p-- = '[';
8083 *p = '\033';
8084 WriteConsoleA(g_hConOut, p, (DWORD)(&buf[SGRBUFSIZE] - p), &r, NULL);
8085 default:
8086 break;
8087 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008088}
8089
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008090 static void
8091wt_init(void)
8092{
8093 wt_working = (mch_getenv("WT_SESSION") != NULL);
8094}
8095
8096 int
8097use_wt(void)
8098{
8099 return USE_WT;
8100}
8101
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008102# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008103 static int
8104ctermtoxterm(
8105 int cterm)
8106{
Bram Moolenaar9894e392018-05-05 14:29:06 +02008107 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008108
8109 cterm_color2rgb(cterm, &r, &g, &b, &idx);
8110 return (((int)r << 16) | ((int)g << 8) | (int)b);
8111}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008112# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008113
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008114 static void
8115set_console_color_rgb(void)
8116{
8117# ifdef FEAT_TERMGUICOLORS
8118 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01008119 guicolor_T fg, bg;
8120 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008121
8122 if (!USE_VTP)
8123 return;
8124
Bram Moolenaara050b942019-12-02 21:35:31 +01008125 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
8126
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008127 if (USE_WT)
8128 {
8129 term_fg_rgb_color(fg);
8130 term_bg_rgb_color(bg);
8131 return;
8132 }
8133
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008134 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
8135 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
8136
8137 csbi.cbSize = sizeof(csbi);
8138 if (has_csbiex)
8139 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8140
8141 csbi.cbSize = sizeof(csbi);
8142 csbi.srWindow.Right += 1;
8143 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008144 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
8145 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008146 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
8147 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008148 if (has_csbiex)
8149 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8150# endif
8151}
8152
Bram Moolenaara050b942019-12-02 21:35:31 +01008153# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
8154 void
8155get_default_console_color(
8156 int *cterm_fg,
8157 int *cterm_bg,
8158 guicolor_T *gui_fg,
8159 guicolor_T *gui_bg)
8160{
8161 int id;
8162 guicolor_T guifg = INVALCOLOR;
8163 guicolor_T guibg = INVALCOLOR;
8164 int ctermfg = 0;
8165 int ctermbg = 0;
8166
8167 id = syn_name2id((char_u *)"Normal");
8168 if (id > 0 && p_tgc)
8169 syn_id2colors(id, &guifg, &guibg);
8170 if (guifg == INVALCOLOR)
8171 {
8172 ctermfg = -1;
8173 if (id > 0)
8174 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
8175 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
8176 : default_console_color_fg;
8177 cterm_normal_fg_gui_color = guifg;
8178 ctermfg = ctermfg < 0 ? 0 : ctermfg;
8179 }
8180 if (guibg == INVALCOLOR)
8181 {
8182 ctermbg = -1;
8183 if (id > 0)
8184 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
8185 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
8186 : default_console_color_bg;
8187 cterm_normal_bg_gui_color = guibg;
8188 ctermbg = ctermbg < 0 ? 0 : ctermbg;
8189 }
8190
8191 *cterm_fg = ctermfg;
8192 *cterm_bg = ctermbg;
8193 *gui_fg = guifg;
8194 *gui_bg = guibg;
8195}
8196# endif
8197
Bram Moolenaardf543822020-01-30 11:53:59 +01008198/*
8199 * Set the console colors to the original colors or the last set colors.
8200 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008201 static void
8202reset_console_color_rgb(void)
8203{
8204# ifdef FEAT_TERMGUICOLORS
8205 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8206
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008207 if (USE_WT)
8208 return;
8209
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008210 csbi.cbSize = sizeof(csbi);
8211 if (has_csbiex)
8212 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8213
8214 csbi.cbSize = sizeof(csbi);
8215 csbi.srWindow.Right += 1;
8216 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008217 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
8218 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
8219 if (has_csbiex)
8220 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8221# endif
8222}
8223
8224/*
8225 * Set the console colors to the original colors.
8226 */
8227 static void
8228restore_console_color_rgb(void)
8229{
8230# ifdef FEAT_TERMGUICOLORS
8231 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8232
8233 csbi.cbSize = sizeof(csbi);
8234 if (has_csbiex)
8235 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8236
8237 csbi.cbSize = sizeof(csbi);
8238 csbi.srWindow.Right += 1;
8239 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008240 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
8241 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008242 if (has_csbiex)
8243 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8244# endif
8245}
8246
8247 void
8248control_console_color_rgb(void)
8249{
8250 if (USE_VTP)
8251 set_console_color_rgb();
8252 else
8253 reset_console_color_rgb();
8254}
8255
8256 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008257use_vtp(void)
8258{
8259 return USE_VTP;
8260}
8261
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008262 int
8263is_term_win32(void)
8264{
8265 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
8266}
8267
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008268 int
8269has_vtp_working(void)
8270{
8271 return vtp_working;
8272}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008273
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01008274#endif
8275
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008276 int
8277has_conpty_working(void)
8278{
8279 return conpty_working;
8280}
8281
8282 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02008283get_conpty_type(void)
8284{
8285 return conpty_type;
8286}
8287
8288 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008289is_conpty_stable(void)
8290{
8291 return conpty_stable;
8292}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008293
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008294 int
8295get_conpty_fix_type(void)
8296{
8297 return conpty_fix_type;
8298}
8299
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008300#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008301 void
8302resize_console_buf(void)
8303{
8304 CONSOLE_SCREEN_BUFFER_INFO csbi;
8305 COORD coord;
8306 SMALL_RECT newsize;
8307
8308 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
8309 {
8310 coord.X = SRWIDTH(csbi.srWindow);
8311 coord.Y = SRHEIGHT(csbi.srWindow);
8312 SetConsoleScreenBufferSize(g_hConOut, coord);
8313
8314 newsize.Left = 0;
8315 newsize.Top = 0;
8316 newsize.Right = coord.X - 1;
8317 newsize.Bottom = coord.Y - 1;
8318 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
8319
8320 SetConsoleScreenBufferSize(g_hConOut, coord);
8321 }
8322}
8323#endif