blob: c5bbc2bf2bb0098c34b56a83992f707bd94c014c [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 Moolenaar3d0e7a92021-05-01 17:46:03 +0200532 // No need to load any library when registering OLE.
533 if (found_register_arg)
534 return dll;
535
Bram Moolenaar0f873732019-12-05 20:28:46 +0100536 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
537 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200538 if (exe_path == NULL)
539 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200540 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200541 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200542 WCHAR old_dirw[MAXPATHL];
543
544 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
545 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100546 // Change directory to where the executable is, both to make
547 // sure we find a .dll there and to avoid looking for a .dll
548 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100549 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200550 dll = LoadLibrary(name);
551 SetCurrentDirectoryW(old_dirw);
552 return dll;
553 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200554 }
555 return dll;
556}
557
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200558#if defined(VIMDLL) || defined(PROTO)
559/*
560 * Check if the current executable file is for the GUI subsystem.
561 */
562 int
563mch_is_gui_executable(void)
564{
565 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
566 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
567 PIMAGE_NT_HEADERS pPE;
568
569 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
570 return FALSE;
571 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
572 if (pPE->Signature != IMAGE_NT_SIGNATURE)
573 return FALSE;
574 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
575 return TRUE;
576 return FALSE;
577}
578#endif
579
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100580#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
581/*
582 * Get related information about 'funcname' which is imported by 'hInst'.
583 * If 'info' is 0, return the function address.
584 * If 'info' is 1, return the module name which the function is imported from.
585 */
586 static void *
587get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
588{
589 PBYTE pImage = (PBYTE)hInst;
590 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
591 PIMAGE_NT_HEADERS pPE;
592 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100593 PIMAGE_THUNK_DATA pIAT; // Import Address Table
594 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100595 PIMAGE_IMPORT_BY_NAME pImpName;
596
597 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
598 return NULL;
599 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
600 if (pPE->Signature != IMAGE_NT_SIGNATURE)
601 return NULL;
602 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
603 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
604 .VirtualAddress);
605 for (; pImpDesc->FirstThunk; ++pImpDesc)
606 {
607 if (!pImpDesc->OriginalFirstThunk)
608 continue;
609 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
610 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
611 for (; pIAT->u1.Function; ++pIAT, ++pINT)
612 {
613 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
614 continue;
615 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
616 + (UINT_PTR)(pINT->u1.AddressOfData));
617 if (strcmp((char *)pImpName->Name, funcname) == 0)
618 {
619 switch (info)
620 {
621 case 0:
622 return (void *)pIAT->u1.Function;
623 case 1:
624 return (void *)(pImage + pImpDesc->Name);
625 default:
626 return NULL;
627 }
628 }
629 }
630 }
631 return NULL;
632}
633
634/*
635 * Get the module handle which 'funcname' in 'hInst' is imported from.
636 */
637 HINSTANCE
638find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
639{
640 char *modulename;
641
642 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
643 if (modulename != NULL)
644 return GetModuleHandleA(modulename);
645 return NULL;
646}
647
648/*
649 * Get the address of 'funcname' which is imported by 'hInst' DLL.
650 */
651 void *
652get_dll_import_func(HINSTANCE hInst, const char *funcname)
653{
654 return get_imported_func_info(hInst, funcname, 0);
655}
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
659# ifndef GETTEXT_DLL
660# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200661# define GETTEXT_DLL_ALT1 "libintl-8.dll"
662# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100664// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000665static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200666static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000667static char *null_libintl_textdomain(const char *);
668static char *null_libintl_bindtextdomain(const char *, const char *);
669static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100670static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200672static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000673char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200674char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
675 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000676char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
677char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000679char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
680 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100681int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100684dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 int i;
687 static struct
688 {
689 char *name;
690 FARPROC *ptr;
691 } libintl_entry[] =
692 {
693 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200694 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
696 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
697 {NULL, NULL}
698 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100699 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
Bram Moolenaar7554c542018-10-06 15:03:15 +0200701 // No need to initialize twice.
702 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200704 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100705 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100706# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100707 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200708 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100709# endif
710# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200711 if (!hLibintlDLL)
712 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100713# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100714 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200716 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200718 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100719 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200720 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200722 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724 for (i = 0; libintl_entry[i].name != NULL
725 && libintl_entry[i].ptr != NULL; ++i)
726 {
727 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
728 libintl_entry[i].name)) == NULL)
729 {
730 dyn_libintl_end();
731 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000732 {
733 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000735 verbose_leave();
736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 return 0;
738 }
739 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000740
Bram Moolenaar0f873732019-12-05 20:28:46 +0100741 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000743 "bind_textdomain_codeset");
744 if (dyn_libintl_bind_textdomain_codeset == NULL)
745 dyn_libintl_bind_textdomain_codeset =
746 null_libintl_bind_textdomain_codeset;
747
Bram Moolenaar0f873732019-12-05 20:28:46 +0100748 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100749 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
750 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100751 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100752 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
753 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 return 1;
756}
757
758 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100759dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 if (hLibintlDLL)
762 FreeLibrary(hLibintlDLL);
763 hLibintlDLL = NULL;
764 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200765 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 dyn_libintl_textdomain = null_libintl_textdomain;
767 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000768 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100769 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
772 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000773null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 return (char*)msgid;
776}
777
778 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200779null_libintl_ngettext(
780 const char *msgid,
781 const char *msgid_plural,
782 unsigned long n)
783{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200784 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200785}
786
Bram Moolenaaree695f72016-08-03 22:08:45 +0200787 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100788null_libintl_bindtextdomain(
789 const char *domainname UNUSED,
790 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 return NULL;
793}
794
795 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100796null_libintl_bind_textdomain_codeset(
797 const char *domainname UNUSED,
798 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000799{
800 return NULL;
801}
802
803 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100804null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 return NULL;
807}
808
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200809 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100810null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100811{
812 return 0;
813}
814
Bram Moolenaar0f873732019-12-05 20:28:46 +0100815#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar0f873732019-12-05 20:28:46 +0100817// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819#ifndef VER_PLATFORM_WIN32_WINDOWS
820# define VER_PLATFORM_WIN32_WINDOWS 1
821#endif
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100824# ifndef PROTO
825# include <aclapi.h>
826# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200827# ifndef PROTECTED_DACL_SECURITY_INFORMATION
828# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#endif
831
Bram Moolenaar27515922013-06-29 15:36:26 +0200832#ifdef HAVE_ACL
833/*
834 * Enables or disables the specified privilege.
835 */
836 static BOOL
837win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
838{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100839 BOOL bResult;
840 LUID luid;
841 HANDLE hToken;
842 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200843
844 if (!OpenProcessToken(GetCurrentProcess(),
845 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
846 return FALSE;
847
848 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
849 {
850 CloseHandle(hToken);
851 return FALSE;
852 }
853
Bram Moolenaar45500912014-07-09 20:51:07 +0200854 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200855 tokenPrivileges.Privileges[0].Luid = luid;
856 tokenPrivileges.Privileges[0].Attributes = bEnable ?
857 SE_PRIVILEGE_ENABLED : 0;
858
859 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
860 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
861
862 CloseHandle(hToken);
863
864 return bResult && GetLastError() == ERROR_SUCCESS;
865}
866#endif
867
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100868#ifdef _MSC_VER
869// Suppress the deprecation warning for using GetVersionEx().
870// It is needed for implementing "windowsversion()".
871# pragma warning(push)
872# pragma warning(disable: 4996)
873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874/*
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200875 * Set "win8_or_later" and fill in "windowsVersion" if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 */
877 void
878PlatformId(void)
879{
880 static int done = FALSE;
881
882 if (!done)
883 {
884 OSVERSIONINFO ovi;
885
886 ovi.dwOSVersionInfoSize = sizeof(ovi);
887 GetVersionEx(&ovi);
888
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200889#ifdef FEAT_EVAL
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100890 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
891 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200892#endif
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100893 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
894 || ovi.dwMajorVersion > 6)
895 win8_or_later = TRUE;
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100898 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200899 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901 done = TRUE;
902 }
903}
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100904#ifdef _MSC_VER
905# pragma warning(pop)
906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200908#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100910# define SHIFT (SHIFT_PRESSED)
911# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
912# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
913# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915
Bram Moolenaar0f873732019-12-05 20:28:46 +0100916// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
917// We map function keys to their ANSI terminal equivalents, as produced
918// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
919// ANSI key with a value >= '\300' is nonstandard, but provided anyway
920// so that the user can have access to all SHIFT-, CTRL-, and ALT-
921// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000923static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
925 WORD wVirtKey;
926 BOOL fAnsiKey;
927 int chAlone;
928 int chShift;
929 int chCtrl;
930 int chAlt;
931} VirtKeyMap[] =
932{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200933// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
935
936 { VK_F1, TRUE, ';', 'T', '^', 'h', },
937 { VK_F2, TRUE, '<', 'U', '_', 'i', },
938 { VK_F3, TRUE, '=', 'V', '`', 'j', },
939 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
940 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
941 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
942 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
943 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
944 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
945 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200946 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
947 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200949 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
950 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
951 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
952 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
953 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
954 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
955 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
956 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
957 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
958 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100959 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200961 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100963# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200964 // Most people don't have F13-F20, but what the hell...
965 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
966 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
967 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
968 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
969 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
970 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
971 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
972 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100973# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200974 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
975 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
976 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
977 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200979 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
980 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
981 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
982 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
983 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
984 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
985 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
986 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
987 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
988 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100989 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990};
991
992
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993/*
994 * The return code indicates key code size.
995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000998 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
1000 UINT uMods = pker->dwControlKeyState;
1001 static int s_iIsDead = 0;
1002 static WORD awAnsiCode[2];
1003 static BYTE abKeystate[256];
1004
1005
1006 if (s_iIsDead == 2)
1007 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001008 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 s_iIsDead = 0;
1010 return 1;
1011 }
1012
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001013 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 return 1;
1015
Bram Moolenaara80faa82020-04-12 19:37:17 +02001016 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar0f873732019-12-05 20:28:46 +01001018 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001019 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021 if (uMods & SHIFT_PRESSED)
1022 abKeystate[VK_SHIFT] = 0x80;
1023 if (uMods & CAPSLOCK_ON)
1024 abKeystate[VK_CAPITAL] = 1;
1025
1026 if ((uMods & ALT_GR) == ALT_GR)
1027 {
1028 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
1029 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
1030 }
1031
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001032 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
1033 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001036 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 return s_iIsDead;
1039}
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041static BOOL g_fJustGotFocus = FALSE;
1042
1043/*
1044 * Decode a KEY_EVENT into one or two keystrokes
1045 */
1046 static BOOL
1047decode_key_event(
1048 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001049 WCHAR *pch,
1050 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001052 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 int i;
1055 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1056
1057 *pch = *pch2 = NUL;
1058 g_fJustGotFocus = FALSE;
1059
Bram Moolenaar0f873732019-12-05 20:28:46 +01001060 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (!pker->bKeyDown)
1062 return FALSE;
1063
Bram Moolenaar0f873732019-12-05 20:28:46 +01001064 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 switch (pker->wVirtualKeyCode)
1066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001067 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case VK_SHIFT:
1069 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001070 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 return FALSE;
1072
1073 default:
1074 break;
1075 }
1076
Bram Moolenaar0f873732019-12-05 20:28:46 +01001077 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001078 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001080 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (pker->wVirtualKeyCode == '6')
1082 {
1083 *pch = Ctrl_HAT;
1084 return TRUE;
1085 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else if (pker->wVirtualKeyCode == '2')
1088 {
1089 *pch = NUL;
1090 return TRUE;
1091 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001092 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else if (pker->wVirtualKeyCode == 0xBD)
1094 {
1095 *pch = Ctrl__;
1096 return TRUE;
1097 }
1098 }
1099
Bram Moolenaar0f873732019-12-05 20:28:46 +01001100 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1102 {
1103 *pch = K_NUL;
1104 *pch2 = '\017';
1105 return TRUE;
1106 }
1107
1108 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1109 {
1110 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1111 {
1112 if (nModifs == 0)
1113 *pch = VirtKeyMap[i].chAlone;
1114 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1115 *pch = VirtKeyMap[i].chShift;
1116 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1117 *pch = VirtKeyMap[i].chCtrl;
1118 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1119 *pch = VirtKeyMap[i].chAlt;
1120
1121 if (*pch != 0)
1122 {
1123 if (VirtKeyMap[i].fAnsiKey)
1124 {
1125 *pch2 = *pch;
1126 *pch = K_NUL;
1127 }
1128
1129 return TRUE;
1130 }
1131 }
1132 }
1133
1134 i = win32_kbd_patch_key(pker);
1135
1136 if (i < 0)
1137 *pch = NUL;
1138 else
1139 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001140 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142 if (pmodifiers != NULL)
1143 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001144 // Pass on the ALT key as a modifier, but only when not combined
1145 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1147 *pmodifiers |= MOD_MASK_ALT;
1148
Bram Moolenaar0f873732019-12-05 20:28:46 +01001149 // Pass on SHIFT only for special keys, because we don't know when
1150 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1152 *pmodifiers |= MOD_MASK_SHIFT;
1153
Bram Moolenaar0f873732019-12-05 20:28:46 +01001154 // Pass on CTRL only for non-special keys, because we don't know
1155 // when it's already included with the character. And not when
1156 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1158 && *pch >= 0x20 && *pch < 0x80)
1159 *pmodifiers |= MOD_MASK_CTRL;
1160 }
1161 }
1162
1163 return (*pch != NUL);
1164}
1165
Bram Moolenaar0f873732019-12-05 20:28:46 +01001166#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169/*
1170 * For the GUI the mouse handling is in gui_w32.c.
1171 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001172#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001174mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
1176}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001177#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001178static int g_fMouseAvail = FALSE; // mouse present
1179static int g_fMouseActive = FALSE; // mouse enabled
1180static int g_nMouseClick = -1; // mouse status
1181static int g_xMouse; // mouse x coordinate
1182static int g_yMouse; // mouse y coordinate
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001183static DWORD g_cmodein = 0; // Original console input mode
1184static DWORD g_cmodeout = 0; // Original console output mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
1186/*
1187 * Enable or disable mouse input
1188 */
1189 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001190mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
1192 DWORD cmodein;
1193
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001194# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001195 if (gui.in_use)
1196 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (!g_fMouseAvail)
1199 return;
1200
1201 g_fMouseActive = on;
1202 GetConsoleMode(g_hConIn, &cmodein);
1203
1204 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001207 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 cmodein &= ~ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001212 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
1213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001215 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216}
1217
Bram Moolenaar157d8132018-03-06 17:09:20 +01001218
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001219# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001220/*
1221 * Called when 'balloonevalterm' changed.
1222 */
1223 void
1224mch_bevalterm_changed(void)
1225{
1226 mch_setmouse(g_fMouseActive);
1227}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001228# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230/*
1231 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1232 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1233 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1234 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1235 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1236 * and we return the mouse position in g_xMouse and g_yMouse.
1237 *
1238 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1239 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1240 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1241 *
1242 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1243 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1244 *
1245 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1246 * moves, even if it stays within the same character cell. We ignore
1247 * all MOUSE_MOVED messages if the position hasn't really changed, and
1248 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1249 * we're only interested in MOUSE_DRAG).
1250 *
1251 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1252 * 2-button mouses by pressing the left & right buttons simultaneously.
1253 * In practice, it's almost impossible to click both at the same time,
1254 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1255 * in such cases, if the user is clicking quickly.
1256 */
1257 static BOOL
1258decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001259 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 static int s_nOldButton = -1;
1262 static int s_nOldMouseClick = -1;
1263 static int s_xOldMouse = -1;
1264 static int s_yOldMouse = -1;
1265 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001266# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 static int s_cClicks = 1;
1270 static BOOL s_fReleased = TRUE;
1271 static DWORD s_dwLastClickTime = 0;
1272 static BOOL s_fNextIsMiddle = FALSE;
1273
Bram Moolenaar0f873732019-12-05 20:28:46 +01001274 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1277 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1278 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1279 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1280
1281 int nButton;
1282
1283 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1284 cButtons = 2;
1285
1286 if (!g_fMouseAvail || !g_fMouseActive)
1287 {
1288 g_nMouseClick = -1;
1289 return FALSE;
1290 }
1291
Bram Moolenaar0f873732019-12-05 20:28:46 +01001292 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (g_fJustGotFocus)
1294 {
1295 g_fJustGotFocus = FALSE;
1296 return FALSE;
1297 }
1298
Bram Moolenaar0f873732019-12-05 20:28:46 +01001299 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (g_nMouseClick != -1)
1301 return TRUE;
1302
1303 nButton = -1;
1304 g_xMouse = pmer->dwMousePosition.X;
1305 g_yMouse = pmer->dwMousePosition.Y;
1306
1307 if (pmer->dwEventFlags == MOUSE_MOVED)
1308 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001309 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1310 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1312 return FALSE;
1313 }
1314
Bram Moolenaar0f873732019-12-05 20:28:46 +01001315 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1317 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001318 nButton = MOUSE_RELEASE;
1319
Bram Moolenaar0f873732019-12-05 20:28:46 +01001320 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001322 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001323# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001324 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001325 if (p_bevalterm)
1326 nButton = MOUSE_DRAG;
1327 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001328# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001329 return FALSE;
1330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 s_fReleased = TRUE;
1333 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001334 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001336 // on a 2-button mouse, hold down left and right buttons
1337 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1340 {
1341 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1342
Bram Moolenaar0f873732019-12-05 20:28:46 +01001343 // if either left or right button only is pressed, see if the
1344 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (dwLR == LEFT || dwLR == RIGHT)
1346 {
1347 for (;;)
1348 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001349 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1351 != WAIT_OBJECT_0)
1352 break;
1353 else
1354 {
1355 DWORD cRecords = 0;
1356 INPUT_RECORD ir;
1357 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1358
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001359 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1362 || !(pmer2->dwButtonState & LEFT_RIGHT))
1363 break;
1364 else
1365 {
1366 if (pmer2->dwEventFlags != MOUSE_MOVED)
1367 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001368 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 return decode_mouse_event(pmer2);
1371 }
1372 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1373 s_yOldMouse == pmer2->dwMousePosition.Y)
1374 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001375 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001376 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaar0f873732019-12-05 20:28:46 +01001378 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001379 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1382 break;
1383 }
1384 else
1385 break;
1386 }
1387 }
1388 }
1389 }
1390 }
1391
1392 if (s_fNextIsMiddle)
1393 {
1394 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1395 ? MOUSE_DRAG : MOUSE_MIDDLE;
1396 s_fNextIsMiddle = FALSE;
1397 }
1398 else if (cButtons == 2 &&
1399 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1400 {
1401 nButton = MOUSE_MIDDLE;
1402
1403 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1404 {
1405 s_fNextIsMiddle = TRUE;
1406 nButton = MOUSE_RELEASE;
1407 }
1408 }
1409 else if ((pmer->dwButtonState & LEFT) == LEFT)
1410 nButton = MOUSE_LEFT;
1411 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1412 nButton = MOUSE_MIDDLE;
1413 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1414 nButton = MOUSE_RIGHT;
1415
1416 if (! s_fReleased && ! s_fNextIsMiddle
1417 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1418 return FALSE;
1419
1420 s_fReleased = s_fNextIsMiddle;
1421 }
1422
1423 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1424 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001425 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (nButton != -1 && nButton != MOUSE_RELEASE)
1427 {
1428 DWORD dwCurrentTime = GetTickCount();
1429
1430 if (s_xOldMouse != g_xMouse
1431 || s_yOldMouse != g_yMouse
1432 || s_nOldButton != nButton
1433 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001434# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1438 {
1439 s_cClicks = 1;
1440 }
1441 else if (++s_cClicks > 4)
1442 {
1443 s_cClicks = 1;
1444 }
1445
1446 s_dwLastClickTime = dwCurrentTime;
1447 }
1448 }
1449 else if (pmer->dwEventFlags == MOUSE_MOVED)
1450 {
1451 if (nButton != -1 && nButton != MOUSE_RELEASE)
1452 nButton = MOUSE_DRAG;
1453
1454 s_cClicks = 1;
1455 }
1456
1457 if (nButton == -1)
1458 return FALSE;
1459
1460 if (nButton != MOUSE_RELEASE)
1461 s_nOldButton = nButton;
1462
1463 g_nMouseClick = nButton;
1464
1465 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1466 g_nMouseClick |= MOUSE_SHIFT;
1467 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1468 g_nMouseClick |= MOUSE_CTRL;
1469 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1470 g_nMouseClick |= MOUSE_ALT;
1471
1472 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1473 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1474
Bram Moolenaar0f873732019-12-05 20:28:46 +01001475 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (s_xOldMouse == g_xMouse
1477 && s_yOldMouse == g_yMouse
1478 && s_nOldMouseClick == g_nMouseClick)
1479 {
1480 g_nMouseClick = -1;
1481 return FALSE;
1482 }
1483
1484 s_xOldMouse = g_xMouse;
1485 s_yOldMouse = g_yMouse;
1486 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001487# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 s_nOldMouseClick = g_nMouseClick;
1491
1492 return TRUE;
1493}
1494
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001495#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497
1498#ifdef MCH_CURSOR_SHAPE
1499/*
1500 * Set the shape of the cursor.
1501 * 'thickness' can be from 1 (thin) to 99 (block)
1502 */
1503 static void
1504mch_set_cursor_shape(int thickness)
1505{
1506 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1507 ConsoleCursorInfo.dwSize = thickness;
1508 ConsoleCursorInfo.bVisible = s_cursor_visible;
1509
1510 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1511 if (s_cursor_visible)
1512 SetConsoleCursorPosition(g_hConOut, g_coord);
1513}
1514
1515 void
1516mch_update_cursor(void)
1517{
1518 int idx;
1519 int thickness;
1520
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001521# ifdef VIMDLL
1522 if (gui.in_use)
1523 return;
1524# endif
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /*
1527 * How the cursor is drawn depends on the current mode.
1528 */
1529 idx = get_shape_idx(FALSE);
1530
1531 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001532 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 else
1534 thickness = shape_table[idx].percentage;
1535 mch_set_cursor_shape(thickness);
1536}
1537#endif
1538
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001539#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
1541 * Handle FOCUS_EVENT.
1542 */
1543 static void
1544handle_focus_event(INPUT_RECORD ir)
1545{
1546 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1547 ui_focus_change((int)g_fJustGotFocus);
1548}
1549
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001550static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
1553 * Wait until console input from keyboard or mouse is available,
1554 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001555 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 * Return TRUE if something is available FALSE if not.
1557 */
1558 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001559WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 DWORD dwNow = 0, dwEndTime = 0;
1562 INPUT_RECORD ir;
1563 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001564 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001565# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001566 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
1569 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001570 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 dwEndTime = GetTickCount() + msec;
1572 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001573 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 dwEndTime = INFINITE;
1575
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001576 // We need to loop until the end of the time period, because
1577 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 for (;;)
1579 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001580 // Only process messages when waiting.
1581 if (msec != 0)
1582 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001583# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001584 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001585# endif
1586# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001587 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001588# endif
1589# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001590 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001591# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001592 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001593
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001594 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001595# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001596 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 )
1599 return TRUE;
1600
1601 if (msec > 0)
1602 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001603 // If the specified wait time has passed, return. Beware that
1604 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001606 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 break;
1608 }
1609 if (msec != 0)
1610 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001611 DWORD dwWaitTime = dwEndTime - dwNow;
1612
Bram Moolenaarc478ee32020-12-01 21:27:51 +01001613 // Don't wait for more than 11 msec to avoid dropping characters,
1614 // check channel while waiting for input and handle a callback from
1615 // 'balloonexpr'.
1616 if (dwWaitTime > 11)
1617 dwWaitTime = 11;
1618
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001619# ifdef FEAT_MZSCHEME
Bram Moolenaarc478ee32020-12-01 21:27:51 +01001620 if (mzthreads_allowed() && p_mzq > 0 && (long)dwWaitTime > p_mzq)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001621 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001622# endif
1623# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001624 // When waiting very briefly don't trigger timers.
1625 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001626 {
1627 long due_time;
1628
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001629 // Trigger timers and then get the time in msec until the next
1630 // one is due. Wait up to that time.
1631 due_time = check_due_timer();
1632 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001633 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001634 // timer may have used feedkeys().
1635 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001636 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001637 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1638 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001639 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001640# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001641 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001642# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001643 // Wait for either an event on the console input or a
1644 // message in the client-server window.
1645 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1646 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001647# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001648 wait_for_single_object(g_hConIn, dwWaitTime)
1649 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001650# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001651 )
1652 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654
1655 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001656 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001658# ifdef FEAT_MBYTE_IME
matveyt4eb19142021-05-20 11:54:10 +02001659 // May have to redraw if the cursor ends up in the wrong place.
1660 // Only when not peeking.
1661 if (State & CMDLINE && msg_row == Rows - 1 && msec != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 CONSOLE_SCREEN_BUFFER_INFO csbi;
1664
1665 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1666 {
1667 if (csbi.dwCursorPosition.Y != msg_row)
1668 {
matveyte08795e2021-05-07 15:00:17 +02001669 // The screen is now messed up, must redraw the command
1670 // line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 redraw_all_later(CLEAR);
matveyte08795e2021-05-07 15:00:17 +02001672 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 redrawcmd();
1674 }
1675 }
1676 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 if (cRecords > 0)
1680 {
1681 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1682 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001683# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001684 // Windows IME sends two '\n's with only one 'ENTER'. First:
1685 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001686 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1688 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001689 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 continue;
1691 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1694 NULL, FALSE))
1695 return TRUE;
1696 }
1697
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001698 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700 if (ir.EventType == FOCUS_EVENT)
1701 handle_focus_event(ir);
1702 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001703 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001704 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1705
Bram Moolenaar36698e32019-12-11 22:57:40 +01001706 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001707 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001708 if (dwSize.X != Columns || dwSize.Y != Rows)
1709 {
1710 CONSOLE_SCREEN_BUFFER_INFO csbi;
1711 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001712 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001713 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001714 if (dwSize.X != Columns || dwSize.Y != Rows)
1715 {
1716 ResizeConBuf(g_hConOut, dwSize);
1717 shell_resized();
1718 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001719 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else if (ir.EventType == MOUSE_EVENT
1722 && decode_mouse_event(&ir.Event.MouseEvent))
1723 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725 else if (msec == 0)
1726 break;
1727 }
1728
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001729# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001730 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (input_available())
1732 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001733# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001734
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return FALSE;
1736}
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738/*
1739 * return non-zero if a character is available
1740 */
1741 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001742mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
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, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001750
1751# if defined(FEAT_TERMINAL) || defined(PROTO)
1752/*
1753 * Check for any pending input or messages.
1754 */
1755 int
1756mch_check_messages(void)
1757{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001758# ifdef VIMDLL
1759 if (gui.in_use)
1760 return TRUE;
1761# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001762 return WaitForChar(0L, TRUE);
1763}
1764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766/*
1767 * Create the console input. Used when reading stdin doesn't work.
1768 */
1769 static void
1770create_conin(void)
1771{
1772 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1773 FILE_SHARE_READ|FILE_SHARE_WRITE,
1774 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001775 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 did_create_conin = TRUE;
1777}
1778
1779/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001780 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001782 static WCHAR
1783tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001785 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 for (;;)
1788 {
1789 INPUT_RECORD ir;
1790 DWORD cRecords = 0;
1791
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001792# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001793 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (input_available())
1795 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (g_nMouseClick != -1)
1797 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001798# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001799 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801 if (did_create_conin)
1802 read_error_exit();
1803 create_conin();
1804 continue;
1805 }
1806
1807 if (ir.EventType == KEY_EVENT)
1808 {
1809 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1810 pmodifiers, TRUE))
1811 return ch;
1812 }
1813 else if (ir.EventType == FOCUS_EVENT)
1814 handle_focus_event(ir);
1815 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1816 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 else if (ir.EventType == MOUSE_EVENT)
1818 {
1819 if (decode_mouse_event(&ir.Event.MouseEvent))
1820 return 0;
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001824#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826
1827/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001828 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 * Get one or more characters from the keyboard or the mouse.
1830 * If time == 0, do not wait for characters.
1831 * If time == n, wait a short time for characters.
1832 * If time == -1, wait forever for characters.
1833 * Returns the number of characters read into buf.
1834 */
1835 int
1836mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001837 char_u *buf UNUSED,
1838 int maxlen UNUSED,
1839 long time UNUSED,
1840 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001842#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 int len;
1845 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001846# ifdef VIMDLL
1847// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1848# define TYPEAHEADSPACE 6
1849# else
1850# define TYPEAHEADSPACE 0
1851# endif
1852# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001853 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 static int typeaheadlen = 0;
1855
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001856# ifdef VIMDLL
1857 if (gui.in_use)
1858 return 0;
1859# endif
1860
Bram Moolenaar0f873732019-12-05 20:28:46 +01001861 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (typeaheadlen > 0)
1863 goto theend;
1864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (time >= 0)
1866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001867 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001870 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001872 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar3918c952005-03-15 22:34:55 +00001874 /*
1875 * If there is no character available within 2 seconds (default)
1876 * write the autoscript file to disk. Or cause the CursorHold event
1877 * to be triggered.
1878 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001879 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001881 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001883 buf[0] = K_SPECIAL;
1884 buf[1] = KS_EXTRA;
1885 buf[2] = (int)KE_CURSORHOLD;
1886 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001888 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 }
1891
1892 /*
1893 * Try to read as many characters as there are, until the buffer is full.
1894 */
1895
Bram Moolenaar0f873732019-12-05 20:28:46 +01001896 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 g_fCBrkPressed = FALSE;
1898
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001899# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (fdDump)
1901 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaar0f873732019-12-05 20:28:46 +01001904 // Keep looping until there is something in the typeahead buffer and more
1905 // to get and still room in the buffer (up to two bytes for a char and
1906 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001907 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001908 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 if (typebuf_changed(tb_change_cnt))
1911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001912 // "buf" may be invalid now if a client put something in the
1913 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 typeaheadlen = 0;
1915 break;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (g_nMouseClick != -1)
1918 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001919# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (fdDump)
1921 fprintf(fdDump, "{%02x @ %d, %d}",
1922 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 typeahead[typeaheadlen++] = ESC + 128;
1925 typeahead[typeaheadlen++] = 'M';
1926 typeahead[typeaheadlen++] = g_nMouseClick;
1927 typeahead[typeaheadlen++] = g_xMouse + '!';
1928 typeahead[typeaheadlen++] = g_yMouse + '!';
1929 g_nMouseClick = -1;
1930 }
1931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001933 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 int modifiers = 0;
1935
1936 c = tgetch(&modifiers, &ch2);
1937
1938 if (typebuf_changed(tb_change_cnt))
1939 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001940 // "buf" may be invalid now if a client put something in the
1941 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 typeaheadlen = 0;
1943 break;
1944 }
1945
1946 if (c == Ctrl_C && ctrl_c_interrupts)
1947 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001948# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 got_int = TRUE;
1952 }
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 int n = 1;
1957
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001958 if (ch2 == NUL)
1959 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001960 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001961 char_u *p;
1962 WCHAR ch[2];
1963
1964 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001965 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001966 {
1967 ch[1] = tgetch(&modifiers, &ch2);
1968 n++;
1969 }
1970 p = utf16_to_enc(ch, &n);
1971 if (p != NULL)
1972 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001973 for (i = 0, j = 0; i < n; i++)
1974 {
1975 typeahead[typeaheadlen + j++] = p[i];
1976# ifdef VIMDLL
1977 if (p[i] == CSI)
1978 {
1979 typeahead[typeaheadlen + j++] = KS_EXTRA;
1980 typeahead[typeaheadlen + j++] = KE_CSI;
1981 }
1982# endif
1983 }
1984 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001985 vim_free(p);
1986 }
1987 }
1988 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001989 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001990 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001991# ifdef VIMDLL
1992 if (c == CSI)
1993 {
1994 typeahead[typeaheadlen + 1] = KS_EXTRA;
1995 typeahead[typeaheadlen + 2] = KE_CSI;
1996 n = 3;
1997 }
1998# endif
1999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (ch2 != NUL)
2001 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02002002 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002003 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02002004 switch (ch2)
2005 {
2006 case (WCHAR)'\324': // SHIFT+Insert
2007 case (WCHAR)'\325': // CTRL+Insert
2008 case (WCHAR)'\327': // SHIFT+Delete
2009 case (WCHAR)'\330': // CTRL+Delete
2010 typeahead[typeaheadlen + n] = (char_u)ch2;
2011 n++;
2012 break;
2013
2014 default:
2015 typeahead[typeaheadlen + n] = 3;
2016 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2017 n += 2;
2018 break;
2019 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002020 }
2021 else
2022 {
2023 typeahead[typeaheadlen + n] = 3;
2024 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2025 n += 2;
2026 }
Bram Moolenaar45500912014-07-09 20:51:07 +02002027 }
2028
Bram Moolenaar0f873732019-12-05 20:28:46 +01002029 // Use the ALT key to set the 8th bit of the character
2030 // when it's one byte, the 8th bit isn't set yet and not
2031 // using a double-byte encoding (would become a lead
2032 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if ((modifiers & MOD_MASK_ALT)
2034 && n == 1
2035 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 )
2038 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00002039 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
2040 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 modifiers &= ~MOD_MASK_ALT;
2042 }
2043
2044 if (modifiers != 0)
2045 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002046 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 mch_memmove(typeahead + typeaheadlen + 3,
2048 typeahead + typeaheadlen, n);
2049 typeahead[typeaheadlen++] = K_SPECIAL;
2050 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2051 typeahead[typeaheadlen++] = modifiers;
2052 }
2053
2054 typeaheadlen += n;
2055
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002056# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (fdDump)
2058 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 }
2062 }
2063
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002064# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (fdDump)
2066 {
2067 fputs("]\n", fdDump);
2068 fflush(fdDump);
2069 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002073 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 len = 0;
2075 while (len < maxlen && typeaheadlen > 0)
2076 {
2077 buf[len++] = typeahead[0];
2078 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2079 }
Bram Moolenaar7ca86fe2020-09-03 19:25:11 +02002080# ifdef FEAT_JOB_CHANNEL
2081 if (len > 0)
2082 {
2083 buf[len] = NUL;
2084 ch_log(NULL, "raw key input: \"%s\"", buf);
2085 }
2086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 return len;
2088
Bram Moolenaar0f873732019-12-05 20:28:46 +01002089#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002091#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092}
2093
Bram Moolenaar82881492012-11-20 16:53:39 +01002094#ifndef PROTO
2095# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002096# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#endif
2099
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002100/*
Bram Moolenaar95da1362020-05-30 18:37:55 +02002101 * Return TRUE if "name" is an executable file, FALSE if not or it doesn't exist.
Bram Moolenaar43663192017-03-05 14:29:12 +01002102 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2103 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002104 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 static int
Bram Moolenaar95da1362020-05-30 18:37:55 +02002107executable_file(char *name, char_u **path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
Bram Moolenaar95da1362020-05-30 18:37:55 +02002109 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002110 {
Bram Moolenaar95da1362020-05-30 18:37:55 +02002111 if (path != NULL)
2112 *path = FullName_save((char_u *)name, FALSE);
2113 return TRUE;
2114 }
2115 return FALSE;
2116}
2117
2118/*
2119 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2120 * If "use_path" is FALSE: Return TRUE if "name" exists.
2121 * If "use_pathext" is TRUE search "name" with extensions in $PATHEXT.
2122 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2123 * the allocated memory.
2124 */
2125 static int
2126executable_exists(char *name, char_u **path, int use_path, int use_pathext)
2127{
2128 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
2129 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
2130 // UTF-8.
2131 char_u buf[_MAX_PATH * 3];
2132 size_t len = STRLEN(name);
2133 size_t tmplen;
2134 char_u *p, *e, *e2;
2135 char_u *pathbuf = NULL;
2136 char_u *pathext = NULL;
2137 char_u *pathextbuf = NULL;
2138 int noext = FALSE;
2139 int retval = FALSE;
2140
2141 if (len >= sizeof(buf)) // safety check
Bram Moolenaar43663192017-03-05 14:29:12 +01002142 return FALSE;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002143
2144 // Using the name directly when a Unix-shell like 'shell'.
2145 if (strstr((char *)gettail(p_sh), "sh") != NULL)
2146 noext = TRUE;
2147
2148 if (use_pathext)
2149 {
2150 pathext = mch_getenv("PATHEXT");
2151 if (pathext == NULL)
2152 pathext = (char_u *)".com;.exe;.bat;.cmd";
2153
2154 if (noext == FALSE)
2155 {
2156 /*
2157 * Loop over all extensions in $PATHEXT.
2158 * Check "name" ends with extension.
2159 */
2160 p = pathext;
2161 while (*p)
2162 {
2163 if (p[0] == ';'
2164 || (p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2165 {
2166 // Skip empty or single ".".
2167 ++p;
2168 continue;
2169 }
2170 e = vim_strchr(p, ';');
2171 if (e == NULL)
2172 e = p + STRLEN(p);
2173 tmplen = e - p;
2174
2175 if (_strnicoll(name + len - tmplen, (char *)p, tmplen) == 0)
2176 {
2177 noext = TRUE;
2178 break;
2179 }
2180
2181 p = e;
2182 }
2183 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002184 }
2185
Bram Moolenaar95da1362020-05-30 18:37:55 +02002186 // Prepend single "." to pathext, it's means no extension added.
2187 if (pathext == NULL)
2188 pathext = (char_u *)".";
2189 else if (noext == TRUE)
2190 {
2191 if (pathextbuf == NULL)
2192 pathextbuf = alloc(STRLEN(pathext) + 3);
2193 if (pathextbuf == NULL)
2194 {
2195 retval = FALSE;
2196 goto theend;
2197 }
2198 STRCPY(pathextbuf, ".;");
2199 STRCAT(pathextbuf, pathext);
2200 pathext = pathextbuf;
2201 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002202
Bram Moolenaar95da1362020-05-30 18:37:55 +02002203 // Use $PATH when "use_path" is TRUE and "name" is basename.
2204 if (use_path && gettail((char_u *)name) == (char_u *)name)
2205 {
2206 p = mch_getenv("PATH");
2207 if (p != NULL)
2208 {
2209 pathbuf = alloc(STRLEN(p) + 3);
2210 if (pathbuf == NULL)
2211 {
2212 retval = FALSE;
2213 goto theend;
2214 }
2215 STRCPY(pathbuf, ".;");
2216 STRCAT(pathbuf, p);
2217 }
2218 }
2219
2220 /*
2221 * Walk through all entries in $PATH to check if "name" exists there and
2222 * is an executable file.
2223 */
2224 p = (pathbuf != NULL) ? pathbuf : (char_u *)".";
2225 while (*p)
2226 {
2227 if (*p == ';') // Skip empty entry
2228 {
2229 ++p;
2230 continue;
2231 }
2232 e = vim_strchr(p, ';');
2233 if (e == NULL)
2234 e = p + STRLEN(p);
2235
2236 if (e - p + len + 2 > sizeof(buf))
2237 {
2238 retval = FALSE;
2239 goto theend;
2240 }
2241 // A single "." that means current dir.
2242 if (e - p == 1 && *p == '.')
2243 STRCPY(buf, name);
2244 else
2245 {
2246 vim_strncpy(buf, p, e - p);
2247 add_pathsep(buf);
2248 STRCAT(buf, name);
2249 }
2250 tmplen = STRLEN(buf);
2251
2252 /*
2253 * Loop over all extensions in $PATHEXT.
2254 * Check "name" with extension added.
2255 */
2256 p = pathext;
2257 while (*p)
2258 {
2259 if (*p == ';')
2260 {
2261 // Skip empty entry
2262 ++p;
2263 continue;
2264 }
2265 e2 = vim_strchr(p, (int)';');
2266 if (e2 == NULL)
2267 e2 = p + STRLEN(p);
2268
2269 if (!(p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2270 {
2271 // Not a single "." that means no extension is added.
2272 if (e2 - p + tmplen + 1 > sizeof(buf))
2273 {
2274 retval = FALSE;
2275 goto theend;
2276 }
2277 vim_strncpy(buf + tmplen, p, e2 - p);
2278 }
2279 if (executable_file((char *)buf, path))
2280 {
2281 retval = TRUE;
2282 goto theend;
2283 }
2284
2285 p = e2;
2286 }
2287
2288 p = e;
2289 }
2290
2291theend:
2292 free(pathextbuf);
2293 free(pathbuf);
2294 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295}
2296
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002297#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2298 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002299/*
2300 * Bad parameter handler.
2301 *
2302 * Certain MS CRT functions will intentionally crash when passed invalid
2303 * parameters to highlight possible security holes. Setting this function as
2304 * the bad parameter handler will prevent the crash.
2305 *
2306 * In debug builds the parameters contain CRT information that might help track
2307 * down the source of a problem, but in non-debug builds the arguments are all
2308 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2309 * worth allowing these to make debugging of issues easier.
2310 */
2311 static void
2312bad_param_handler(const wchar_t *expression,
2313 const wchar_t *function,
2314 const wchar_t *file,
2315 unsigned int line,
2316 uintptr_t pReserved)
2317{
2318}
2319
2320# define SET_INVALID_PARAM_HANDLER \
2321 ((void)_set_invalid_parameter_handler(bad_param_handler))
2322#else
2323# define SET_INVALID_PARAM_HANDLER
2324#endif
2325
Bram Moolenaar4f974752019-02-17 17:44:42 +01002326#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328/*
2329 * GUI version of mch_init().
2330 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002331 static void
2332mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002334# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaar0f873732019-12-05 20:28:46 +01002338 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002339 SET_INVALID_PARAM_HANDLER;
2340
Bram Moolenaar0f873732019-12-05 20:28:46 +01002341 // Let critical errors result in a failure, not in a dialog box. Required
2342 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 SetErrorMode(SEM_FAILCRITICALERRORS);
2344
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 Rows = 25;
2349 Columns = 80;
2350
Bram Moolenaar0f873732019-12-05 20:28:46 +01002351 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 char_u vimrun_location[_MAX_PATH + 4];
2354
Bram Moolenaar0f873732019-12-05 20:28:46 +01002355 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 STRCPY(vimrun_location, exe_name);
2357 STRCPY(gettail(vimrun_location), "vimrun.exe");
2358 if (mch_getperm(vimrun_location) >= 0)
2359 {
2360 if (*skiptowhite(vimrun_location) != NUL)
2361 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002362 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 mch_memmove(vimrun_location + 1, vimrun_location,
2364 STRLEN(vimrun_location) + 1);
2365 *vimrun_location = '"';
2366 STRCPY(gettail(vimrun_location), "vimrun\" ");
2367 }
2368 else
2369 STRCPY(gettail(vimrun_location), "vimrun ");
2370
2371 vimrun_path = (char *)vim_strsave(vimrun_location);
2372 s_dont_use_vimrun = FALSE;
2373 }
Bram Moolenaar95da1362020-05-30 18:37:55 +02002374 else if (executable_exists("vimrun.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 s_dont_use_vimrun = FALSE;
2376
Bram Moolenaar0f873732019-12-05 20:28:46 +01002377 // Don't give the warning for a missing vimrun.exe right now, but only
2378 // when vimrun was supposed to be used. Don't bother people that do
2379 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (s_dont_use_vimrun)
2381 need_vimrun_warning = TRUE;
2382 }
2383
2384 /*
2385 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2386 * Otherwise the default "findstr /n" is used.
2387 */
Bram Moolenaar95da1362020-05-30 18:37:55 +02002388 if (!executable_exists("findstr.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2390
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002391# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002392 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002393# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002394
2395 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396}
2397
2398
Bram Moolenaar0f873732019-12-05 20:28:46 +01002399#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002400
2401#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002403# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2404# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406/*
2407 * ClearConsoleBuffer()
2408 * Description:
2409 * Clears the entire contents of the console screen buffer, using the
2410 * specified attribute.
2411 * Returns:
2412 * TRUE on success
2413 */
2414 static BOOL
2415ClearConsoleBuffer(WORD wAttribute)
2416{
2417 CONSOLE_SCREEN_BUFFER_INFO csbi;
2418 COORD coord;
2419 DWORD NumCells, dummy;
2420
2421 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2422 return FALSE;
2423
2424 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2425 coord.X = 0;
2426 coord.Y = 0;
2427 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2428 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2431 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 return TRUE;
2435}
2436
2437/*
2438 * FitConsoleWindow()
2439 * Description:
2440 * Checks if the console window will fit within given buffer dimensions.
2441 * Also, if requested, will shrink the window to fit.
2442 * Returns:
2443 * TRUE on success
2444 */
2445 static BOOL
2446FitConsoleWindow(
2447 COORD dwBufferSize,
2448 BOOL WantAdjust)
2449{
2450 CONSOLE_SCREEN_BUFFER_INFO csbi;
2451 COORD dwWindowSize;
2452 BOOL NeedAdjust = FALSE;
2453
2454 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2455 {
2456 /*
2457 * A buffer resize will fail if the current console window does
2458 * not lie completely within that buffer. To avoid this, we might
2459 * have to move and possibly shrink the window.
2460 */
2461 if (csbi.srWindow.Right >= dwBufferSize.X)
2462 {
2463 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2464 if (dwWindowSize.X > dwBufferSize.X)
2465 dwWindowSize.X = dwBufferSize.X;
2466 csbi.srWindow.Right = dwBufferSize.X - 1;
2467 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2468 NeedAdjust = TRUE;
2469 }
2470 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2471 {
2472 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2473 if (dwWindowSize.Y > dwBufferSize.Y)
2474 dwWindowSize.Y = dwBufferSize.Y;
2475 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2476 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2477 NeedAdjust = TRUE;
2478 }
2479 if (NeedAdjust && WantAdjust)
2480 {
2481 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2482 return FALSE;
2483 }
2484 return TRUE;
2485 }
2486
2487 return FALSE;
2488}
2489
2490typedef struct ConsoleBufferStruct
2491{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002492 BOOL IsValid;
2493 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002494 PCHAR_INFO Buffer;
2495 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002496 PSMALL_RECT Regions;
2497 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498} ConsoleBuffer;
2499
2500/*
2501 * SaveConsoleBuffer()
2502 * Description:
2503 * Saves important information about the console buffer, including the
2504 * actual buffer contents. The saved information is suitable for later
2505 * restoration by RestoreConsoleBuffer().
2506 * Returns:
2507 * TRUE if all information was saved; FALSE otherwise
2508 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2509 */
2510 static BOOL
2511SaveConsoleBuffer(
2512 ConsoleBuffer *cb)
2513{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002514 DWORD NumCells;
2515 COORD BufferCoord;
2516 SMALL_RECT ReadRegion;
2517 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002518 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (cb == NULL)
2521 return FALSE;
2522
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002523 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 cb->IsValid = FALSE;
2526 return FALSE;
2527 }
2528 cb->IsValid = TRUE;
2529
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002530 /*
2531 * Allocate a buffer large enough to hold the entire console screen
2532 * buffer. If this ConsoleBuffer structure has already been initialized
2533 * with a buffer of the correct size, then just use that one.
2534 */
2535 if (!cb->IsValid || cb->Buffer == NULL ||
2536 cb->BufferSize.X != cb->Info.dwSize.X ||
2537 cb->BufferSize.Y != cb->Info.dwSize.Y)
2538 {
2539 cb->BufferSize.X = cb->Info.dwSize.X;
2540 cb->BufferSize.Y = cb->Info.dwSize.Y;
2541 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2542 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002543 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002544 if (cb->Buffer == NULL)
2545 return FALSE;
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002549 * We will now copy the console screen buffer into our buffer.
2550 * ReadConsoleOutput() seems to be limited as far as how much you
2551 * can read at a time. Empirically, this number seems to be about
2552 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2553 * in chunks until it is all copied. The chunks will all have the
2554 * same horizontal characteristics, so initialize them now. The
2555 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002557 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002558 ReadRegion.Left = 0;
2559 ReadRegion.Right = cb->Info.dwSize.X - 1;
2560 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002561
2562 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2563 if (cb->Regions == NULL || numregions != cb->NumRegions)
2564 {
2565 cb->NumRegions = numregions;
2566 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002567 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002568 if (cb->Regions == NULL)
2569 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002570 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002571 return FALSE;
2572 }
2573 }
2574
2575 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002577 /*
2578 * Read into position (0, Y) in our buffer.
2579 */
2580 BufferCoord.Y = Y;
2581 /*
2582 * Read the region whose top left corner is (0, Y) and whose bottom
2583 * right corner is (width - 1, Y + Y_incr - 1). This should define
2584 * a region of size width by Y_incr. Don't worry if this region is
2585 * too large for the remaining buffer; it will be cropped.
2586 */
2587 ReadRegion.Top = Y;
2588 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002589 if (!ReadConsoleOutputW(g_hConOut, // output handle
2590 cb->Buffer, // our buffer
2591 cb->BufferSize, // dimensions of our buffer
2592 BufferCoord, // offset in our buffer
2593 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002594 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002595 VIM_CLEAR(cb->Buffer);
2596 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002597 return FALSE;
2598 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002599 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
2602 return TRUE;
2603}
2604
2605/*
2606 * RestoreConsoleBuffer()
2607 * Description:
2608 * Restores important information about the console buffer, including the
2609 * actual buffer contents, if desired. The information to restore is in
2610 * the same format used by SaveConsoleBuffer().
2611 * Returns:
2612 * TRUE on success
2613 */
2614 static BOOL
2615RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002616 ConsoleBuffer *cb,
2617 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002619 COORD BufferCoord;
2620 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002621 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 if (cb == NULL || !cb->IsValid)
2624 return FALSE;
2625
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002626 /*
2627 * Before restoring the buffer contents, clear the current buffer, and
2628 * restore the cursor position and window information. Doing this now
2629 * prevents old buffer contents from "flashing" onto the screen.
2630 */
2631 if (RestoreScreen)
2632 ClearConsoleBuffer(cb->Info.wAttributes);
2633
2634 FitConsoleWindow(cb->Info.dwSize, TRUE);
2635 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2636 return FALSE;
2637 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2638 return FALSE;
2639
2640 if (!RestoreScreen)
2641 {
2642 /*
2643 * No need to restore the screen buffer contents, so we're done.
2644 */
2645 return TRUE;
2646 }
2647
2648 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2649 return FALSE;
2650 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2651 return FALSE;
2652
2653 /*
2654 * Restore the screen buffer contents.
2655 */
2656 if (cb->Buffer != NULL)
2657 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002658 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002659 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002660 BufferCoord.X = cb->Regions[i].Left;
2661 BufferCoord.Y = cb->Regions[i].Top;
2662 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002663 if (!WriteConsoleOutputW(g_hConOut, // output handle
2664 cb->Buffer, // our buffer
2665 cb->BufferSize, // dimensions of our buffer
2666 BufferCoord, // offset in our buffer
2667 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002668 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002669 }
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 return TRUE;
2673}
2674
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002675# define FEAT_RESTORE_ORIG_SCREEN
2676# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002677static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679static ConsoleBuffer g_cbNonTermcap = { 0 };
2680static ConsoleBuffer g_cbTermcap = { 0 };
2681
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002682# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002684HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685static HICON g_hOrigIconSmall = NULL;
2686static HICON g_hOrigIcon = NULL;
2687static HICON g_hVimIcon = NULL;
2688static BOOL g_fCanChangeIcon = FALSE;
2689
Bram Moolenaar0f873732019-12-05 20:28:46 +01002690// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002691# ifndef ICON_SMALL
2692# define ICON_SMALL 0
2693# endif
2694# ifndef ICON_BIG
2695# define ICON_BIG 1
2696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * GetConsoleIcon()
2699 * Description:
2700 * Attempts to retrieve the small icon and/or the big icon currently in
2701 * use by a given window.
2702 * Returns:
2703 * TRUE on success
2704 */
2705 static BOOL
2706GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002707 HWND hWnd,
2708 HICON *phIconSmall,
2709 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711 if (hWnd == NULL)
2712 return FALSE;
2713
2714 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002715 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2716 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002718 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2719 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return TRUE;
2721}
2722
2723/*
2724 * SetConsoleIcon()
2725 * Description:
2726 * Attempts to change the small icon and/or the big icon currently in
2727 * use by a given window.
2728 * Returns:
2729 * TRUE on success
2730 */
2731 static BOOL
2732SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002733 HWND hWnd,
2734 HICON hIconSmall,
2735 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (hWnd == NULL)
2738 return FALSE;
2739
2740 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002741 SendMessage(hWnd, WM_SETICON,
2742 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002744 SendMessage(hWnd, WM_SETICON,
2745 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return TRUE;
2747}
2748
2749/*
2750 * SaveConsoleTitleAndIcon()
2751 * Description:
2752 * Saves the current console window title in g_szOrigTitle, for later
2753 * restoration. Also, attempts to obtain a handle to the console window,
2754 * and use it to save the small and big icons currently in use by the
2755 * console window. This is not always possible on some versions of Windows;
2756 * nor is it possible when running Vim remotely using Telnet (since the
2757 * console window the user sees is owned by a remote process).
2758 */
2759 static void
2760SaveConsoleTitleAndIcon(void)
2761{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002762 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2764 return;
2765
2766 /*
2767 * Obtain a handle to the console window using GetConsoleWindow() from
2768 * KERNEL32.DLL; we need to handle in order to change the window icon.
2769 * This function only exists on NT-based Windows, starting with Windows
2770 * 2000. On older operating systems, we can't change the window icon
2771 * anyway.
2772 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002773 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (g_hWnd == NULL)
2775 return;
2776
Bram Moolenaar0f873732019-12-05 20:28:46 +01002777 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2779 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2780 return;
2781
Bram Moolenaar0f873732019-12-05 20:28:46 +01002782 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002783 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002784 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (g_hVimIcon != NULL)
2786 g_fCanChangeIcon = TRUE;
2787}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790static int g_fWindInitCalled = FALSE;
2791static int g_fTermcapMode = FALSE;
2792static CONSOLE_CURSOR_INFO g_cci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794/*
2795 * non-GUI version of mch_init().
2796 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002797 static void
2798mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002800# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002801 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002802# endif
2803# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
Bram Moolenaar0f873732019-12-05 20:28:46 +01002807 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002808 SET_INVALID_PARAM_HANDLER;
2809
Bram Moolenaar0f873732019-12-05 20:28:46 +01002810 // Let critical errors result in a failure, not in a dialog box. Required
2811 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 SetErrorMode(SEM_FAILCRITICALERRORS);
2813
Bram Moolenaar0f873732019-12-05 20:28:46 +01002814 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 out_flush();
2816
Bram Moolenaar0f873732019-12-05 20:28:46 +01002817 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (read_cmd_fd == 0)
2819 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2820 else
2821 create_conin();
2822 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2823
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002824# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002825 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002826 SaveConsoleBuffer(&g_cbOrig);
2827 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002828# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002829 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002830 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2831 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (cterm_normal_fg_color == 0)
2834 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2835 if (cterm_normal_bg_color == 0)
2836 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2837
Bram Moolenaarbdace832019-03-02 10:13:42 +01002838 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002839 g_color_index_fg = g_attrDefault & 0xf;
2840 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2841
Bram Moolenaar0f873732019-12-05 20:28:46 +01002842 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 update_tcap(g_attrCurrent);
2844
2845 GetConsoleCursorInfo(g_hConOut, &g_cci);
2846 GetConsoleMode(g_hConIn, &g_cmodein);
2847 GetConsoleMode(g_hConOut, &g_cmodeout);
2848
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002849# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 SaveConsoleTitleAndIcon();
2851 /*
2852 * Set both the small and big icons of the console window to Vim's icon.
2853 * Note that Vim presently only has one size of icon (32x32), but it
2854 * automatically gets scaled down to 16x16 when setting the small icon.
2855 */
2856 if (g_fCanChangeIcon)
2857 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
2860 ui_get_shellsize();
2861
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002862# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 fdDump = fopen("dump", "wt");
2864
2865 if (fdDump)
2866 {
2867 time_t t;
2868
2869 time(&t);
2870 fputs(ctime(&t), fdDump);
2871 fflush(fdDump);
2872 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 g_fWindInitCalled = TRUE;
2876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002879# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002880 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002881# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002882
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002883 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002884 vtp_init();
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002885 wt_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886}
2887
2888/*
2889 * non-GUI version of mch_exit().
2890 * Shut down and exit with status `r'
2891 * Careful: mch_exit() may be called before mch_init()!
2892 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002893 static void
2894mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002896 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002898 vtp_exit();
2899
Bram Moolenaar955f1982017-02-05 15:10:51 +01002900 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (g_fWindInitCalled)
2902 settmode(TMODE_COOK);
2903
Bram Moolenaar0f873732019-12-05 20:28:46 +01002904 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 if (g_fWindInitCalled)
2907 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002908# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002909 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 /*
2911 * Restore both the small and big icons of the console window to
2912 * what they were at startup. Don't do this when the window is
2913 * closed, Vim would hang here.
2914 */
2915 if (g_fCanChangeIcon && !g_fForceExit)
2916 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002919# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (fdDump)
2921 {
2922 time_t t;
2923
2924 time(&t);
2925 fputs(ctime(&t), fdDump);
2926 fclose(fdDump);
2927 }
2928 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931
2932 SetConsoleCursorInfo(g_hConOut, &g_cci);
Wez Furlong6ef5ab52021-05-30 19:29:41 +02002933 SetConsoleMode(g_hConIn, g_cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 SetConsoleMode(g_hConOut, g_cmodeout);
2935
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002936# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 exit(r);
2941}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002942#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002944 void
2945mch_init(void)
2946{
2947#ifdef VIMDLL
2948 if (gui.starting)
2949 mch_init_g();
2950 else
2951 mch_init_c();
2952#elif defined(FEAT_GUI_MSWIN)
2953 mch_init_g();
2954#else
2955 mch_init_c();
2956#endif
2957}
2958
2959 void
2960mch_exit(int r)
2961{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002962#ifdef FEAT_NETBEANS_INTG
2963 netbeans_send_disconnect();
2964#endif
2965
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002966#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002967 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002968 mch_exit_g(r);
2969 else
2970 mch_exit_c(r);
2971#elif defined(FEAT_GUI_MSWIN)
2972 mch_exit_g(r);
2973#else
2974 mch_exit_c(r);
2975#endif
2976}
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978/*
2979 * Do we have an interactive window?
2980 */
2981 int
2982mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002983 int argc UNUSED,
2984 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 get_exe_name();
2987
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002988#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002989 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002991# ifdef VIMDLL
2992 if (gui.in_use)
2993 return OK;
2994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (isatty(1))
2996 return OK;
2997 return FAIL;
2998#endif
2999}
3000
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003001/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003002 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 * When "len" is > 0, also expand short to long filenames.
3004 */
3005 void
3006fname_case(
3007 char_u *name,
3008 int len)
3009{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003010 int flen;
3011 WCHAR *p;
3012 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00003014 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003015 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 return;
3017
3018 slash_adjust(name);
3019
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003020 p = enc_to_utf16(name, NULL);
3021 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003022 return;
3023
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003024 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003026 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003028 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003030 if (len > 0 || flen >= (int)STRLEN(q))
3031 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
3032 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003035 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036}
3037
3038
3039/*
3040 * Insert user name in s[len].
3041 */
3042 int
3043mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003044 char_u *s,
3045 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003047 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003048 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003050 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003051 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003052 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003053
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003054 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003055 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003056 vim_strncpy(s, p, len - 1);
3057 vim_free(p);
3058 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003059 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 s[0] = NUL;
3062 return FAIL;
3063}
3064
3065
3066/*
3067 * Insert host name in s[len].
3068 */
3069 void
3070mch_get_host_name(
3071 char_u *s,
3072 int len)
3073{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003074 WCHAR wszHostName[256 + 1];
3075 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003077 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003078 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003079 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003080
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003081 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003082 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003083 vim_strncpy(s, p, len - 1);
3084 vim_free(p);
3085 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003086 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090
3091/*
3092 * return process ID
3093 */
3094 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003095mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 return (long)GetCurrentProcessId();
3098}
3099
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003100/*
3101 * return TRUE if process "pid" is still running
3102 */
3103 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02003104mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003105{
3106 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
3107 DWORD status = 0;
3108 int ret = FALSE;
3109
3110 if (hProcess == NULL)
3111 return FALSE; // might not have access
3112 if (GetExitCodeProcess(hProcess, &status) )
3113 ret = status == STILL_ACTIVE;
3114 CloseHandle(hProcess);
3115 return ret;
3116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118/*
3119 * Get name of current directory into buffer 'buf' of length 'len' bytes.
3120 * Return OK for success, FAIL for failure.
3121 */
3122 int
3123mch_dirname(
3124 char_u *buf,
3125 int len)
3126{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003127 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 /*
3130 * Originally this was:
3131 * return (getcwd(buf, len) != NULL ? OK : FAIL);
3132 * But the Win32s known bug list says that getcwd() doesn't work
3133 * so use the Win32 system call instead. <Negri>
3134 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003135 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003137 WCHAR wcbuf[_MAX_PATH + 1];
3138 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003140 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003142 p = utf16_to_enc(wcbuf, NULL);
3143 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02003144 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003145 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003147 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003150 if (p == NULL)
3151 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003152
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003153 if (p != NULL)
3154 {
3155 vim_strncpy(buf, p, len - 1);
3156 vim_free(p);
3157 return OK;
3158 }
3159 }
3160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161}
3162
3163/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01003164 * Get file permissions for "name".
3165 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
3167 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003168mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003170 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01003171 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003173 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01003174 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176
3177
3178/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003179 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003180 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003181 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 */
3183 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003184mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003186 long n;
3187 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003188
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003189 p = enc_to_utf16(name, NULL);
3190 if (p == NULL)
3191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003193 n = _wchmod(p, perm);
3194 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003195 if (n == -1)
3196 return FAIL;
3197
3198 win32_set_archive(name);
3199
3200 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
3204 * Set hidden flag for "name".
3205 */
3206 void
3207mch_hide(char_u *name)
3208{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003209 int attrs = win32_getattrs(name);
3210 if (attrs == -1)
3211 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003213 attrs |= FILE_ATTRIBUTE_HIDDEN;
3214 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215}
3216
3217/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003218 * Return TRUE if file "name" exists and is hidden.
3219 */
3220 int
3221mch_ishidden(char_u *name)
3222{
3223 int f = win32_getattrs(name);
3224
3225 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003226 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003227
3228 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * return TRUE if "name" is a directory
3233 * return FALSE if "name" is not a directory or upon error
3234 */
3235 int
3236mch_isdir(char_u *name)
3237{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003238 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003241 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3244}
3245
3246/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003247 * return TRUE if "name" is a directory, NOT a symlink to a directory
3248 * return FALSE if "name" is not a directory
3249 * return FALSE for error
3250 */
3251 int
3252mch_isrealdir(char_u *name)
3253{
3254 return mch_isdir(name) && !mch_is_symbolic_link(name);
3255}
3256
3257/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003258 * Create directory "name".
3259 * Return 0 on success, -1 on error.
3260 */
3261 int
3262mch_mkdir(char_u *name)
3263{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003264 WCHAR *p;
3265 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003266
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003267 p = enc_to_utf16(name, NULL);
3268 if (p == NULL)
3269 return -1;
3270 retval = _wmkdir(p);
3271 vim_free(p);
3272 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003273}
3274
3275/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003276 * Delete directory "name".
3277 * Return 0 on success, -1 on error.
3278 */
3279 int
3280mch_rmdir(char_u *name)
3281{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003282 WCHAR *p;
3283 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003284
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003285 p = enc_to_utf16(name, NULL);
3286 if (p == NULL)
3287 return -1;
3288 retval = _wrmdir(p);
3289 vim_free(p);
3290 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003291}
3292
3293/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003294 * Return TRUE if file "fname" has more than one link.
3295 */
3296 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003297mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003298{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003299 BY_HANDLE_FILE_INFORMATION info;
3300
3301 return win32_fileinfo(fname, &info) == FILEINFO_OK
3302 && info.nNumberOfLinks > 1;
3303}
3304
3305/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003306 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003307 */
3308 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003309mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003310{
3311 HANDLE hFind;
3312 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003313 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003314 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003315 WIN32_FIND_DATAW findDataW;
3316
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003317 wn = enc_to_utf16(name, NULL);
3318 if (wn == NULL)
3319 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003320
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003321 hFind = FindFirstFileW(wn, &findDataW);
3322 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003323 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003324 {
3325 fileFlags = findDataW.dwFileAttributes;
3326 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003327 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003328 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003329
3330 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003331 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3332 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003333 res = TRUE;
3334
3335 return res;
3336}
3337
3338/*
3339 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3340 * link.
3341 */
3342 int
3343mch_is_linked(char_u *fname)
3344{
3345 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3346 return TRUE;
3347 return FALSE;
3348}
3349
3350/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003351 * Get the by-handle-file-information for "fname".
3352 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003353 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003354 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3355 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3356 */
3357 int
3358win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3359{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003360 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003361 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003362 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003363
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003364 wn = enc_to_utf16(fname, NULL);
3365 if (wn == NULL)
3366 return FILEINFO_ENC_FAIL;
3367
3368 hFile = CreateFileW(wn, // file name
3369 GENERIC_READ, // access mode
3370 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3371 NULL, // security descriptor
3372 OPEN_EXISTING, // creation disposition
3373 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3374 NULL); // handle to template file
3375 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003376
3377 if (hFile != INVALID_HANDLE_VALUE)
3378 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003379 if (GetFileInformationByHandle(hFile, info) != 0)
3380 res = FILEINFO_OK;
3381 else
3382 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003383 CloseHandle(hFile);
3384 }
3385
Bram Moolenaar03f48552006-02-28 23:52:23 +00003386 return res;
3387}
3388
3389/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003390 * get file attributes for `name'
3391 * -1 : error
3392 * else FILE_ATTRIBUTE_* defined in winnt.h
3393 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003394 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003395win32_getattrs(char_u *name)
3396{
3397 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003398 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003399
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003400 p = enc_to_utf16(name, NULL);
3401 if (p == NULL)
3402 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003403
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003404 attr = GetFileAttributesW(p);
3405 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003406
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003407 return attr;
3408}
3409
3410/*
3411 * set file attributes for `name' to `attrs'
3412 *
3413 * return -1 for failure, 0 otherwise
3414 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003415 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003416win32_setattrs(char_u *name, int attrs)
3417{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003418 int res;
3419 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003420
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003421 p = enc_to_utf16(name, NULL);
3422 if (p == NULL)
3423 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003424
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003425 res = SetFileAttributesW(p, attrs);
3426 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003427
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003428 return res ? 0 : -1;
3429}
3430
3431/*
3432 * Set archive flag for "name".
3433 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003434 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003435win32_set_archive(char_u *name)
3436{
3437 int attrs = win32_getattrs(name);
3438 if (attrs == -1)
3439 return -1;
3440
3441 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3442 return win32_setattrs(name, attrs);
3443}
3444
3445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 * Return TRUE if file or directory "name" is writable (not readonly).
3447 * Strange semantics of Win32: a readonly directory is writable, but you can't
3448 * delete a file. Let's say this means it is writable.
3449 */
3450 int
3451mch_writable(char_u *name)
3452{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003453 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003455 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3456 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003460 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003461 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003462 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3463 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
3465 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003466mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaar95da1362020-05-30 18:37:55 +02003468 return executable_exists((char *)name, path, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471/*
3472 * Check what "name" is:
3473 * NODE_NORMAL: file or directory (or doesn't exist)
3474 * NODE_WRITABLE: writable device, socket, fifo, etc.
3475 * NODE_OTHER: non-writable things
3476 */
3477 int
3478mch_nodetype(char_u *name)
3479{
3480 HANDLE hFile;
3481 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003482 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar0f873732019-12-05 20:28:46 +01003484 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3485 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3486 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003487 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3488 return NODE_WRITABLE;
3489
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003490 wn = enc_to_utf16(name, NULL);
3491 if (wn == NULL)
3492 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003493
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003494 hFile = CreateFileW(wn, // file name
3495 GENERIC_WRITE, // access mode
3496 0, // share mode
3497 NULL, // security descriptor
3498 OPEN_EXISTING, // creation disposition
3499 0, // file attributes
3500 NULL); // handle to template file
3501 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (hFile == INVALID_HANDLE_VALUE)
3503 return NODE_NORMAL;
3504
3505 type = GetFileType(hFile);
3506 CloseHandle(hFile);
3507 if (type == FILE_TYPE_CHAR)
3508 return NODE_WRITABLE;
3509 if (type == FILE_TYPE_DISK)
3510 return NODE_NORMAL;
3511 return NODE_OTHER;
3512}
3513
3514#ifdef HAVE_ACL
3515struct my_acl
3516{
3517 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3518 PSID pSidOwner;
3519 PSID pSidGroup;
3520 PACL pDacl;
3521 PACL pSacl;
3522};
3523#endif
3524
3525/*
3526 * Return a pointer to the ACL of file "fname" in allocated memory.
3527 * Return NULL if the ACL is not available for whatever reason.
3528 */
3529 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003530mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532#ifndef HAVE_ACL
3533 return (vim_acl_T)NULL;
3534#else
3535 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003536 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003538 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003539 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003541 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003542
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003543 wn = enc_to_utf16(fname, NULL);
3544 if (wn == NULL)
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01003545 {
3546 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003547 return NULL;
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01003548 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003549
3550 // Try to retrieve the entire security descriptor.
3551 err = GetNamedSecurityInfoW(
3552 wn, // Abstract filename
3553 SE_FILE_OBJECT, // File Object
3554 OWNER_SECURITY_INFORMATION |
3555 GROUP_SECURITY_INFORMATION |
3556 DACL_SECURITY_INFORMATION |
3557 SACL_SECURITY_INFORMATION,
3558 &p->pSidOwner, // Ownership information.
3559 &p->pSidGroup, // Group membership.
3560 &p->pDacl, // Discretionary information.
3561 &p->pSacl, // For auditing purposes.
3562 &p->pSecurityDescriptor);
3563 if (err == ERROR_ACCESS_DENIED ||
3564 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003565 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003566 // Retrieve only DACL.
3567 (void)GetNamedSecurityInfoW(
3568 wn,
3569 SE_FILE_OBJECT,
3570 DACL_SECURITY_INFORMATION,
3571 NULL,
3572 NULL,
3573 &p->pDacl,
3574 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003575 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003576 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003577 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003578 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003579 mch_free_acl((vim_acl_T)p);
3580 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003582 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584
3585 return (vim_acl_T)p;
3586#endif
3587}
3588
Bram Moolenaar27515922013-06-29 15:36:26 +02003589#ifdef HAVE_ACL
3590/*
3591 * Check if "acl" contains inherited ACE.
3592 */
3593 static BOOL
3594is_acl_inherited(PACL acl)
3595{
3596 DWORD i;
3597 ACL_SIZE_INFORMATION acl_info;
3598 PACCESS_ALLOWED_ACE ace;
3599
3600 acl_info.AceCount = 0;
3601 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3602 for (i = 0; i < acl_info.AceCount; i++)
3603 {
3604 GetAce(acl, i, (LPVOID *)&ace);
3605 if (ace->Header.AceFlags & INHERITED_ACE)
3606 return TRUE;
3607 }
3608 return FALSE;
3609}
3610#endif
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612/*
3613 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3614 * Errors are ignored.
3615 * This must only be called with "acl" equal to what mch_get_acl() returned.
3616 */
3617 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003618mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620#ifdef HAVE_ACL
3621 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003622 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003623 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003625 if (p == NULL)
3626 return;
3627
3628 wn = enc_to_utf16(fname, NULL);
3629 if (wn == NULL)
3630 return;
3631
3632 // Set security flags
3633 if (p->pSidOwner)
3634 sec_info |= OWNER_SECURITY_INFORMATION;
3635 if (p->pSidGroup)
3636 sec_info |= GROUP_SECURITY_INFORMATION;
3637 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003638 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003639 sec_info |= DACL_SECURITY_INFORMATION;
3640 // Do not inherit its parent's DACL.
3641 // If the DACL is inherited, Cygwin permissions would be changed.
3642 if (!is_acl_inherited(p->pDacl))
3643 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003644 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003645 if (p->pSacl)
3646 sec_info |= SACL_SECURITY_INFORMATION;
3647
3648 (void)SetNamedSecurityInfoW(
3649 wn, // Abstract filename
3650 SE_FILE_OBJECT, // File Object
3651 sec_info,
3652 p->pSidOwner, // Ownership information.
3653 p->pSidGroup, // Group membership.
3654 p->pDacl, // Discretionary information.
3655 p->pSacl // For auditing purposes.
3656 );
3657 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#endif
3659}
3660
3661 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003662mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664#ifdef HAVE_ACL
3665 struct my_acl *p = (struct my_acl *)acl;
3666
3667 if (p != NULL)
3668 {
3669 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3670 vim_free(p);
3671 }
3672#endif
3673}
3674
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003675#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677/*
3678 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3679 */
3680 static BOOL WINAPI
3681handler_routine(
3682 DWORD dwCtrlType)
3683{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003684 INPUT_RECORD ir;
3685 DWORD out;
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 switch (dwCtrlType)
3688 {
3689 case CTRL_C_EVENT:
3690 if (ctrl_c_interrupts)
3691 g_fCtrlCPressed = TRUE;
3692 return TRUE;
3693
3694 case CTRL_BREAK_EVENT:
3695 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003696 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003697 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003698 ir.EventType = KEY_EVENT;
3699 ir.Event.KeyEvent.bKeyDown = TRUE;
3700 ir.Event.KeyEvent.wRepeatCount = 1;
3701 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3702 ir.Event.KeyEvent.wVirtualScanCode = 0;
3703 ir.Event.KeyEvent.dwControlKeyState = 0;
3704 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3705 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return TRUE;
3707
Bram Moolenaar0f873732019-12-05 20:28:46 +01003708 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 case CTRL_CLOSE_EVENT:
3710 case CTRL_LOGOFF_EVENT:
3711 case CTRL_SHUTDOWN_EVENT:
3712 windgoto((int)Rows - 1, 0);
3713 g_fForceExit = TRUE;
3714
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003715 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 (dwCtrlType == CTRL_CLOSE_EVENT
3717 ? _("close")
3718 : dwCtrlType == CTRL_LOGOFF_EVENT
3719 ? _("logoff")
3720 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003721# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaar0f873732019-12-05 20:28:46 +01003727 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 default:
3730 return FALSE;
3731 }
3732}
3733
3734
3735/*
3736 * set the tty in (raw) ? "raw" : "cooked" mode
3737 */
3738 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003739mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 DWORD cmodein;
3742 DWORD cmodeout;
3743 BOOL bEnableHandler;
3744
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003745# ifdef VIMDLL
3746 if (gui.in_use)
3747 return;
3748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 GetConsoleMode(g_hConIn, &cmodein);
3750 GetConsoleMode(g_hConOut, &cmodeout);
3751 if (tmode == TMODE_RAW)
3752 {
3753 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3754 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02003756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02003758 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
3759 }
3760 else
3761 {
3762 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
3763 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003764 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003765# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003766 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3767 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003768 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003769# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003770 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003771# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003772 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 bEnableHandler = TRUE;
3774 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003775 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3778 ENABLE_ECHO_INPUT);
3779 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3780 bEnableHandler = FALSE;
3781 }
Wez Furlong6ef5ab52021-05-30 19:29:41 +02003782 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 SetConsoleMode(g_hConOut, cmodeout);
3784 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3785
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003786# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (fdDump)
3788 {
3789 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3790 tmode == TMODE_RAW ? "raw" :
3791 tmode == TMODE_COOK ? "cooked" : "normal",
3792 cmodein, cmodeout);
3793 fflush(fdDump);
3794 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796}
3797
3798
3799/*
3800 * Get the size of the current window in `Rows' and `Columns'
3801 * Return OK when size could be determined, FAIL otherwise.
3802 */
3803 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003804mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 CONSOLE_SCREEN_BUFFER_INFO csbi;
3807
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003808# ifdef VIMDLL
3809 if (gui.in_use)
3810 return OK;
3811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3813 {
3814 /*
3815 * For some reason, we are trying to get the screen dimensions
3816 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3817 * variables are really intended to mean the size of Vim screen
3818 * while in termcap mode.
3819 */
3820 Rows = g_cbTermcap.Info.dwSize.Y;
3821 Columns = g_cbTermcap.Info.dwSize.X;
3822 }
3823 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3824 {
3825 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3826 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3827 }
3828 else
3829 {
3830 Rows = 25;
3831 Columns = 80;
3832 }
3833 return OK;
3834}
3835
3836/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003837 * Resize console buffer to 'COORD'
3838 */
3839 static void
3840ResizeConBuf(
3841 HANDLE hConsole,
3842 COORD coordScreen)
3843{
3844 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3845 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003846# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003847 if (fdDump)
3848 {
3849 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3850 GetLastError());
3851 fflush(fdDump);
3852 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003853# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003854 }
3855}
3856
3857/*
3858 * Resize console window size to 'srWindowRect'
3859 */
3860 static void
3861ResizeWindow(
3862 HANDLE hConsole,
3863 SMALL_RECT srWindowRect)
3864{
3865 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3866 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003867# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003868 if (fdDump)
3869 {
3870 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3871 GetLastError());
3872 fflush(fdDump);
3873 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003874# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003875 }
3876}
3877
3878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * Set a console window to `xSize' * `ySize'
3880 */
3881 static void
3882ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003883 HANDLE hConsole,
3884 int xSize,
3885 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003887 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3888 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003890 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003891 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003893# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (fdDump)
3895 {
3896 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3897 fflush(fdDump);
3898 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaar0f873732019-12-05 20:28:46 +01003901 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 coordScreen = GetLargestConsoleWindowSize(hConsole);
3903
Bram Moolenaar0f873732019-12-05 20:28:46 +01003904 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3906 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3907 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3908
3909 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3910 {
3911 int sx, sy;
3912
3913 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3914 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3915 if (sy < ySize || sx < xSize)
3916 {
3917 /*
3918 * Increasing number of lines/columns, do buffer first.
3919 * Use the maximal size in x and y direction.
3920 */
3921 if (sy < ySize)
3922 coordScreen.Y = ySize;
3923 else
3924 coordScreen.Y = sy;
3925 if (sx < xSize)
3926 coordScreen.X = xSize;
3927 else
3928 coordScreen.X = sx;
3929 SetConsoleScreenBufferSize(hConsole, coordScreen);
3930 }
3931 }
3932
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003933 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 coordScreen.X = xSize;
3935 coordScreen.Y = ySize;
3936
Bram Moolenaar2551c032018-08-23 22:38:31 +02003937 // In the new console call API, only the first time in reverse order
3938 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003940 ResizeWindow(hConsole, srWindowRect);
3941 ResizeConBuf(hConsole, coordScreen);
3942 }
3943 else
3944 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003945 // Workaround for a Windows 10 bug
3946 cursor.X = srWindowRect.Left;
3947 cursor.Y = srWindowRect.Top;
3948 SetConsoleCursorPosition(hConsole, cursor);
3949
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003950 ResizeConBuf(hConsole, coordScreen);
3951 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003952 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954}
3955
3956
3957/*
3958 * Set the console window to `Rows' * `Columns'
3959 */
3960 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003961mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 COORD coordScreen;
3964
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003965# ifdef VIMDLL
3966 if (gui.in_use)
3967 return;
3968# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003969 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (suppress_winsize != 0)
3971 {
3972 suppress_winsize = 2;
3973 return;
3974 }
3975
3976 if (term_console)
3977 {
3978 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3979
Bram Moolenaar0f873732019-12-05 20:28:46 +01003980 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (Rows > coordScreen.Y)
3982 Rows = coordScreen.Y;
3983 if (Columns > coordScreen.X)
3984 Columns = coordScreen.X;
3985
3986 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3987 }
3988}
3989
3990/*
3991 * Rows and/or Columns has changed.
3992 */
3993 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003994mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003996# ifdef VIMDLL
3997 if (gui.in_use)
3998 return;
3999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 set_scroll_region(0, 0, Columns - 1, Rows - 1);
4001}
4002
4003
4004/*
4005 * Called when started up, to set the winsize that was delayed.
4006 */
4007 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004008mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 if (suppress_winsize == 2)
4011 {
4012 suppress_winsize = 0;
4013 mch_set_shellsize();
4014 shell_resized();
4015 }
4016 suppress_winsize = 0;
4017}
Bram Moolenaar0f873732019-12-05 20:28:46 +01004018#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004020 static BOOL
4021vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01004022 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004023 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01004024 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004025 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004026 PROCESS_INFORMATION *pi,
4027 LPVOID *env,
4028 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004029{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004030 BOOL ret = FALSE;
4031 WCHAR *wcmd, *wcwd = NULL;
4032
4033 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4034 if (wcmd == NULL)
4035 return FALSE;
4036 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004037 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004038 wcwd = enc_to_utf16((char_u *)cwd, NULL);
4039 if (wcwd == NULL)
4040 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004041 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004042
4043 ret = CreateProcessW(
4044 NULL, // Executable name
4045 wcmd, // Command to execute
4046 NULL, // Process security attributes
4047 NULL, // Thread security attributes
4048 inherit_handles, // Inherit handles
4049 flags, // Creation flags
4050 env, // Environment
4051 wcwd, // Current directory
4052 (LPSTARTUPINFOW)si, // Startup information
4053 pi); // Process information
4054theend:
4055 vim_free(wcmd);
4056 vim_free(wcwd);
4057 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004061 static HINSTANCE
4062vim_shell_execute(
4063 char *cmd,
4064 INT n_show_cmd)
4065{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004066 HINSTANCE ret;
4067 WCHAR *wcmd;
4068
4069 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4070 if (wcmd == NULL)
4071 return (HINSTANCE) 0;
4072
4073 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
4074 vim_free(wcmd);
4075 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004076}
4077
4078
Bram Moolenaar4f974752019-02-17 17:44:42 +01004079#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081/*
4082 * Specialised version of system() for Win32 GUI mode.
4083 * This version proceeds as follows:
4084 * 1. Create a console window for use by the subprocess
4085 * 2. Run the subprocess (it gets the allocated console by default)
4086 * 3. Wait for the subprocess to terminate and get its exit code
4087 * 4. Prompt the user to press a key to close the console window
4088 */
4089 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004090mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 STARTUPINFO si;
4093 PROCESS_INFORMATION pi;
4094 DWORD ret = 0;
4095 HWND hwnd = GetFocus();
4096
4097 si.cb = sizeof(si);
4098 si.lpReserved = NULL;
4099 si.lpDesktop = NULL;
4100 si.lpTitle = NULL;
4101 si.dwFlags = STARTF_USESHOWWINDOW;
4102 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004103 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004104 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004106 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004107 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 else
4109 si.wShowWindow = SW_SHOWNORMAL;
4110 si.cbReserved2 = 0;
4111 si.lpReserved2 = NULL;
4112
Bram Moolenaar0f873732019-12-05 20:28:46 +01004113 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004114 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004115 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
4116 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004120# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 int delay = 1;
4122
Bram Moolenaar0f873732019-12-05 20:28:46 +01004123 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 for (;;)
4125 {
4126 MSG msg;
4127
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004128 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004131 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02004132 delay = 1;
4133 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4136 break;
4137
Bram Moolenaar0f873732019-12-05 20:28:46 +01004138 // We start waiting for a very short time and then increase it, so
4139 // that we respond quickly when the process is quick, and don't
4140 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (delay < 50)
4142 delay += 10;
4143 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004144# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar0f873732019-12-05 20:28:46 +01004148 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 GetExitCodeProcess(pi.hProcess, &ret);
4150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Bram Moolenaar0f873732019-12-05 20:28:46 +01004152 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 CloseHandle(pi.hThread);
4154 CloseHandle(pi.hProcess);
4155
Bram Moolenaar0f873732019-12-05 20:28:46 +01004156 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4158
4159 return ret;
4160}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004161
4162/*
4163 * Thread launched by the gui to send the current buffer data to the
4164 * process. This way avoid to hang up vim totally if the children
4165 * process take a long time to process the lines.
4166 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004167 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004168sub_process_writer(LPVOID param)
4169{
4170 HANDLE g_hChildStd_IN_Wr = param;
4171 linenr_T lnum = curbuf->b_op_start.lnum;
4172 DWORD len = 0;
4173 DWORD l;
4174 char_u *lp = ml_get(lnum);
4175 char_u *s;
4176 int written = 0;
4177
4178 for (;;)
4179 {
4180 l = (DWORD)STRLEN(lp + written);
4181 if (l == 0)
4182 len = 0;
4183 else if (lp[written] == NL)
4184 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004185 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004186 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4187 }
4188 else
4189 {
4190 s = vim_strchr(lp + written, NL);
4191 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4192 s == NULL ? l : (DWORD)(s - (lp + written)),
4193 &len, NULL);
4194 }
4195 if (len == (int)l)
4196 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004197 // Finished a line, add a NL, unless this line should not have
4198 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004199 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004200 || (!curbuf->b_p_bin
4201 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004202 || (lnum != curbuf->b_no_eol_lnum
4203 && (lnum != curbuf->b_ml.ml_line_count
4204 || curbuf->b_p_eol)))
4205 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004206 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4207 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004208 }
4209
4210 ++lnum;
4211 if (lnum > curbuf->b_op_end.lnum)
4212 break;
4213
4214 lp = ml_get(lnum);
4215 written = 0;
4216 }
4217 else if (len > 0)
4218 written += len;
4219 }
4220
Bram Moolenaar0f873732019-12-05 20:28:46 +01004221 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004222 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004223 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004224}
4225
4226
Bram Moolenaar0f873732019-12-05 20:28:46 +01004227# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004228
4229/*
4230 * This function read from the children's stdout and write the
4231 * data on screen or in the buffer accordingly.
4232 */
4233 static void
4234dump_pipe(int options,
4235 HANDLE g_hChildStd_OUT_Rd,
4236 garray_T *ga,
4237 char_u buffer[],
4238 DWORD *buffer_off)
4239{
4240 DWORD availableBytes = 0;
4241 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004242 int ret;
4243 DWORD len;
4244 DWORD toRead;
4245 int repeatCount;
4246
Bram Moolenaar0f873732019-12-05 20:28:46 +01004247 // we query the pipe to see if there is any data to read
4248 // to avoid to perform a blocking read
4249 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4250 NULL, // optional buffer
4251 0, // buffer size
4252 NULL, // number of read bytes
4253 &availableBytes, // available bytes total
4254 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004255
4256 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004257 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004258 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004259 {
4260 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004261 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004262 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004263 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004264
Bram Moolenaar0f873732019-12-05 20:28:46 +01004265 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004266 if (len == 0)
4267 break;
4268
4269 availableBytes -= len;
4270
4271 if (options & SHELL_READ)
4272 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004273 // Do NUL -> NL translation, append NL separated
4274 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004275 for (i = 0; i < len; ++i)
4276 {
4277 if (buffer[i] == NL)
4278 append_ga_line(ga);
4279 else if (buffer[i] == NUL)
4280 ga_append(ga, NL);
4281 else
4282 ga_append(ga, buffer[i]);
4283 }
4284 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004285 else if (has_mbyte)
4286 {
4287 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004288 int c;
4289 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004290
4291 len += *buffer_off;
4292 buffer[len] = NUL;
4293
Bram Moolenaar0f873732019-12-05 20:28:46 +01004294 // Check if the last character in buffer[] is
4295 // incomplete, keep these bytes for the next
4296 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004297 for (p = buffer; p < buffer + len; p += l)
4298 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004299 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004300 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004301 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004302 else if (MB_BYTE2LEN(*p) != l)
4303 break;
4304 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004305 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004306 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004307 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004308 if (len >= 12)
4309 ++p;
4310 else
4311 {
4312 *buffer_off = len;
4313 return;
4314 }
4315 }
4316 c = *p;
4317 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004318 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004319 if (p < buffer + len)
4320 {
4321 *p = c;
4322 *buffer_off = (DWORD)((buffer + len) - p);
4323 mch_memmove(buffer, p, *buffer_off);
4324 return;
4325 }
4326 *buffer_off = 0;
4327 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004328 else
4329 {
4330 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004331 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004332 }
4333
4334 windgoto(msg_row, msg_col);
4335 cursor_on();
4336 out_flush();
4337 }
4338}
4339
4340/*
4341 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4342 * for communication and doesn't open any new window.
4343 */
4344 static int
4345mch_system_piped(char *cmd, int options)
4346{
4347 STARTUPINFO si;
4348 PROCESS_INFORMATION pi;
4349 DWORD ret = 0;
4350
4351 HANDLE g_hChildStd_IN_Rd = NULL;
4352 HANDLE g_hChildStd_IN_Wr = NULL;
4353 HANDLE g_hChildStd_OUT_Rd = NULL;
4354 HANDLE g_hChildStd_OUT_Wr = NULL;
4355
Bram Moolenaar0f873732019-12-05 20:28:46 +01004356 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004357 DWORD len;
4358
Bram Moolenaar0f873732019-12-05 20:28:46 +01004359 // buffer used to receive keys
4360 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4361 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004362
4363 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004364 int noread_cnt = 0;
4365 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004366 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004367 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004368 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004369
4370 SECURITY_ATTRIBUTES saAttr;
4371
Bram Moolenaar0f873732019-12-05 20:28:46 +01004372 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004373 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4374 saAttr.bInheritHandle = TRUE;
4375 saAttr.lpSecurityDescriptor = NULL;
4376
4377 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004378 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004379 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004380 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004381 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004382 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004383 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004384 {
4385 CloseHandle(g_hChildStd_IN_Rd);
4386 CloseHandle(g_hChildStd_IN_Wr);
4387 CloseHandle(g_hChildStd_OUT_Rd);
4388 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004389 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004390 }
4391
4392 si.cb = sizeof(si);
4393 si.lpReserved = NULL;
4394 si.lpDesktop = NULL;
4395 si.lpTitle = NULL;
4396 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4397
Bram Moolenaar0f873732019-12-05 20:28:46 +01004398 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004399 si.hStdError = g_hChildStd_OUT_Wr;
4400 si.hStdOutput = g_hChildStd_OUT_Wr;
4401 si.hStdInput = g_hChildStd_IN_Rd;
4402 si.wShowWindow = SW_HIDE;
4403 si.cbReserved2 = 0;
4404 si.lpReserved2 = NULL;
4405
4406 if (options & SHELL_READ)
4407 ga_init2(&ga, 1, BUFLEN);
4408
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004409 if (cmd != NULL)
4410 {
4411 p = (char *)vim_strsave((char_u *)cmd);
4412 if (p != NULL)
4413 unescape_shellxquote((char_u *)p, p_sxe);
4414 else
4415 p = cmd;
4416 }
4417
Bram Moolenaar0f873732019-12-05 20:28:46 +01004418 // Now, run the command.
4419 // About "Inherit handles" being TRUE: this command can be litigious,
4420 // handle inheritance was deactivated for pending temp file, but, if we
4421 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004422 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4423 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004424
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004425 if (p != cmd)
4426 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004427
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004429 CloseHandle(g_hChildStd_IN_Rd);
4430 CloseHandle(g_hChildStd_OUT_Wr);
4431
4432 if (options & SHELL_WRITE)
4433 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004434 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004435 _beginthreadex(NULL, // security attributes
4436 0, // default stack size
4437 sub_process_writer, // function to be executed
4438 g_hChildStd_IN_Wr, // parameter
4439 0, // creation flag, start immediately
4440 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004441 CloseHandle(thread);
4442 g_hChildStd_IN_Wr = NULL;
4443 }
4444
Bram Moolenaar0f873732019-12-05 20:28:46 +01004445 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004446 for (;;)
4447 {
4448 MSG msg;
4449
Bram Moolenaar175d0702013-12-11 18:36:33 +01004450 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004451 {
4452 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004453 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004454 }
4455
Bram Moolenaar0f873732019-12-05 20:28:46 +01004456 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004457 if ((options & (SHELL_READ|SHELL_WRITE))
4458# ifdef FEAT_GUI
4459 || gui.in_use
4460# endif
4461 )
4462 {
4463 len = 0;
4464 if (!(options & SHELL_EXPAND)
4465 && ((options &
4466 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4467 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4468# ifdef FEAT_GUI
4469 || gui.in_use
4470# endif
4471 )
4472 && (ta_len > 0 || noread_cnt > 4))
4473 {
4474 if (ta_len == 0)
4475 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004476 // Get extra characters when we don't have any. Reset the
4477 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004478 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004479 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4480 }
4481 if (ta_len > 0 || len > 0)
4482 {
4483 /*
4484 * For pipes: Check for CTRL-C: send interrupt signal to
4485 * child. Check for CTRL-D: EOF, close pipe to child.
4486 */
4487 if (len == 1 && cmd != NULL)
4488 {
4489 if (ta_buf[ta_len] == Ctrl_C)
4490 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004491 // Learn what exit code is expected, for
4492 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004493 TerminateProcess(pi.hProcess, 9);
4494 }
4495 if (ta_buf[ta_len] == Ctrl_D)
4496 {
4497 CloseHandle(g_hChildStd_IN_Wr);
4498 g_hChildStd_IN_Wr = NULL;
4499 }
4500 }
4501
Bram Moolenaarf4140482020-02-15 23:06:45 +01004502 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004503
4504 /*
4505 * For pipes: echo the typed characters. For a pty this
4506 * does not seem to work.
4507 */
4508 for (i = ta_len; i < ta_len + len; ++i)
4509 {
4510 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4511 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004512 else if (has_mbyte)
4513 {
4514 int l = (*mb_ptr2len)(ta_buf + i);
4515
4516 msg_outtrans_len(ta_buf + i, l);
4517 i += l - 1;
4518 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004519 else
4520 msg_outtrans_len(ta_buf + i, 1);
4521 }
4522 windgoto(msg_row, msg_col);
4523 out_flush();
4524
4525 ta_len += len;
4526
4527 /*
4528 * Write the characters to the child, unless EOF has been
4529 * typed for pipes. Write one character at a time, to
4530 * avoid losing too much typeahead. When writing buffer
4531 * lines, drop the typed characters (only check for
4532 * CTRL-C).
4533 */
4534 if (options & SHELL_WRITE)
4535 ta_len = 0;
4536 else if (g_hChildStd_IN_Wr != NULL)
4537 {
4538 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4539 1, &len, NULL);
4540 // if we are typing in, we want to keep things reactive
4541 delay = 1;
4542 if (len > 0)
4543 {
4544 ta_len -= len;
4545 mch_memmove(ta_buf, ta_buf + len, ta_len);
4546 }
4547 }
4548 }
4549 }
4550 }
4551
4552 if (ta_len)
4553 ui_inchar_undo(ta_buf, ta_len);
4554
4555 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4556 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004557 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004558 break;
4559 }
4560
4561 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004562 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004563
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 // We start waiting for a very short time and then increase it, so
4565 // that we respond quickly when the process is quick, and don't
4566 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004567 if (delay < 50)
4568 delay += 10;
4569 }
4570
Bram Moolenaar0f873732019-12-05 20:28:46 +01004571 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004572 CloseHandle(g_hChildStd_OUT_Rd);
4573 if (g_hChildStd_IN_Wr != NULL)
4574 CloseHandle(g_hChildStd_IN_Wr);
4575
4576 WaitForSingleObject(pi.hProcess, INFINITE);
4577
Bram Moolenaar0f873732019-12-05 20:28:46 +01004578 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004579 GetExitCodeProcess(pi.hProcess, &ret);
4580
4581 if (options & SHELL_READ)
4582 {
4583 if (ga.ga_len > 0)
4584 {
4585 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004586 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004587 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4588 }
4589 else
4590 curbuf->b_no_eol_lnum = 0;
4591 ga_clear(&ga);
4592 }
4593
Bram Moolenaar0f873732019-12-05 20:28:46 +01004594 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004595 CloseHandle(pi.hThread);
4596 CloseHandle(pi.hProcess);
4597
4598 return ret;
4599}
4600
4601 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004602mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004603{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004604 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004605 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004606 return mch_system_piped(cmd, options);
4607 else
4608 return mch_system_classic(cmd, options);
4609}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004612#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004613 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004614mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004615{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004616 int ret;
4617 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004618 char_u *buf;
4619 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004620
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004621 // If the command starts and ends with double quotes, enclose the command
4622 // in parentheses.
4623 len = STRLEN(cmd);
4624 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4625 {
4626 len += 3;
4627 buf = alloc(len);
4628 if (buf == NULL)
4629 return -1;
4630 vim_snprintf((char *)buf, len, "(%s)", cmd);
4631 wcmd = enc_to_utf16(buf, NULL);
4632 free(buf);
4633 }
4634 else
4635 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4636
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004637 if (wcmd == NULL)
4638 return -1;
4639
4640 ret = _wsystem(wcmd);
4641 vim_free(wcmd);
4642 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
4645#endif
4646
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004647 static int
4648mch_system(char *cmd, int options)
4649{
4650#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004651 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004652 return mch_system_g(cmd, options);
4653 else
4654 return mch_system_c(cmd, options);
4655#elif defined(FEAT_GUI_MSWIN)
4656 return mch_system_g(cmd, options);
4657#else
4658 return mch_system_c(cmd, options);
4659#endif
4660}
4661
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004662#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4663/*
4664 * Use a terminal window to run a shell command in.
4665 */
4666 static int
4667mch_call_shell_terminal(
4668 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004669 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004670{
4671 jobopt_T opt;
4672 char_u *newcmd = NULL;
4673 typval_T argvar[2];
4674 long_u cmdlen;
4675 int retval = -1;
4676 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004677 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004678 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004679 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004680
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004681 if (cmd == NULL)
4682 cmdlen = STRLEN(p_sh) + 1;
4683 else
4684 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004685 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004686 if (newcmd == NULL)
4687 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004688 if (cmd == NULL)
4689 {
4690 STRCPY(newcmd, p_sh);
4691 ch_log(NULL, "starting terminal to run a shell");
4692 }
4693 else
4694 {
4695 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4696 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4697 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004698
4699 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004700
4701 argvar[0].v_type = VAR_STRING;
4702 argvar[0].vval.v_string = newcmd;
4703 argvar[1].v_type = VAR_UNKNOWN;
4704 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004705 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004706 {
4707 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004708 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004709 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004710
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004711 job = term_getjob(buf->b_term);
4712 ++job->jv_refcount;
4713
Bram Moolenaar0f873732019-12-05 20:28:46 +01004714 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004715 aucmd_prepbuf(&aco, buf);
4716
4717 clear_oparg(&oa);
4718 while (term_use_loop())
4719 {
4720 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4721 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004722 // If terminal_loop() returns OK we got a key that is handled
4723 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004724 if (terminal_loop(TRUE) == OK)
4725 normal_cmd(&oa, TRUE);
4726 }
4727 else
4728 normal_cmd(&oa, TRUE);
4729 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004730 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004731 ch_log(NULL, "system command finished");
4732
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004733 job_unref(job);
4734
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004736 aucmd_restbuf(&aco);
4737
4738 wait_return(TRUE);
4739 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4740
4741 vim_free(newcmd);
4742 return retval;
4743}
4744#endif
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746/*
4747 * Either execute a command by calling the shell or start a new shell
4748 */
4749 int
4750mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004751 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004752 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 int x = 0;
4755 int tmode = cur_tmode;
4756#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004757 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004758
Bram Moolenaar0f873732019-12-05 20:28:46 +01004759 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004760 if (GetConsoleTitleW(szShellTitle,
4761 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004762 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004763 if (cmd == NULL)
4764 wcscat(szShellTitle, L" :sh");
4765 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004766 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004767 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004768
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004769 if (wn != NULL)
4770 {
4771 wcscat(szShellTitle, L" - !");
4772 if ((wcslen(szShellTitle) + wcslen(wn) <
4773 sizeof(szShellTitle)/sizeof(WCHAR)))
4774 wcscat(szShellTitle, wn);
4775 SetConsoleTitleW(szShellTitle);
4776 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004777 }
4778 }
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#endif
4781
4782 out_flush();
4783
4784#ifdef MCH_WRITE_DUMP
4785 if (fdDump)
4786 {
4787 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4788 fflush(fdDump);
4789 }
4790#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004791#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004792 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004793 if (
4794# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004795 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004796# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004797 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004798 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4799 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004800 char_u *cmdbase = cmd;
4801
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004802 if (cmdbase != NULL)
4803 // Skip a leading quote and (.
4804 while (*cmdbase == '"' || *cmdbase == '(')
4805 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004806
4807 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004808 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4809 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004810 {
4811 // Use a terminal window to run the command in.
4812 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004813# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004814 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004815# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004816 return x;
4817 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004818 }
4819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 /*
4822 * Catch all deadly signals while running the external command, because a
4823 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4824 */
4825 signal(SIGINT, SIG_IGN);
4826#if defined(__GNUC__) && !defined(__MINGW32__)
4827 signal(SIGKILL, SIG_IGN);
4828#else
4829 signal(SIGBREAK, SIG_IGN);
4830#endif
4831 signal(SIGILL, SIG_IGN);
4832 signal(SIGFPE, SIG_IGN);
4833 signal(SIGSEGV, SIG_IGN);
4834 signal(SIGTERM, SIG_IGN);
4835 signal(SIGABRT, SIG_IGN);
4836
4837 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004838 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 if (cmd == NULL)
4841 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004842 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004846 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004847 char_u *newcmd = NULL;
4848 char_u *cmdbase = cmd;
4849 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004850
Bram Moolenaar0f873732019-12-05 20:28:46 +01004851 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004852 if (*cmdbase == '"' )
4853 ++cmdbase;
4854 if (*cmdbase == '(')
4855 ++cmdbase;
4856
Bram Moolenaar1c465442017-03-12 20:10:05 +01004857 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004858 {
4859 STARTUPINFO si;
4860 PROCESS_INFORMATION pi;
4861 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004862 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004863 char_u *p;
4864
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004865 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004866 si.cb = sizeof(si);
4867 si.lpReserved = NULL;
4868 si.lpDesktop = NULL;
4869 si.lpTitle = NULL;
4870 si.dwFlags = 0;
4871 si.cbReserved2 = 0;
4872 si.lpReserved2 = NULL;
4873
4874 cmdbase = skipwhite(cmdbase + 5);
4875 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004876 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004877 {
4878 cmdbase = skipwhite(cmdbase + 4);
4879 si.dwFlags = STARTF_USESHOWWINDOW;
4880 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004881 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004882 }
4883 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004884 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004885 {
4886 cmdbase = skipwhite(cmdbase + 2);
4887 flags = CREATE_NO_WINDOW;
4888 si.dwFlags = STARTF_USESTDHANDLES;
4889 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004890 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004891 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004892 NULL, // Security att.
4893 OPEN_EXISTING, // Open flags
4894 FILE_ATTRIBUTE_NORMAL, // File att.
4895 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004896 si.hStdOutput = si.hStdInput;
4897 si.hStdError = si.hStdInput;
4898 }
4899
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900 // Remove a trailing ", ) and )" if they have a match
4901 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004902 if (cmdbase > cmd)
4903 {
4904 p = cmdbase + STRLEN(cmdbase);
4905 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4906 *--p = NUL;
4907 if (p > cmdbase && p[-1] == ')'
4908 && (*cmd =='(' || cmd[1] == '('))
4909 *--p = NUL;
4910 }
4911
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004912 newcmd = cmdbase;
4913 unescape_shellxquote(cmdbase, p_sxe);
4914
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004915 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004916 * If creating new console, arguments are passed to the
4917 * 'cmd.exe' as-is. If it's not, arguments are not treated
4918 * correctly for current 'cmd.exe'. So unescape characters in
4919 * shellxescape except '|' for avoiding to be treated as
4920 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004921 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004922 if (flags != CREATE_NEW_CONSOLE)
4923 {
4924 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004925 char_u *cmd_shell = mch_getenv("COMSPEC");
4926
4927 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004928 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004929
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004930 subcmd = vim_strsave_escaped_ext(cmdbase,
4931 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004932 if (subcmd != NULL)
4933 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004934 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004935 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004936 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004937 if (newcmd != NULL)
4938 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004939 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004940 else
4941 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004942 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004943 }
4944 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004945
4946 /*
4947 * Now, start the command as a process, so that it doesn't
4948 * inherit our handles which causes unpleasant dangling swap
4949 * files if we exit before the spawned process
4950 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004951 if (vim_create_process((char *)newcmd, FALSE, flags,
4952 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004953 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004954 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4955 > (HINSTANCE)32)
4956 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004957 else
4958 {
4959 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004960#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004961# ifdef VIMDLL
4962 if (gui.in_use)
4963# endif
4964 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004965#endif
4966 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004967
4968 if (newcmd != cmdbase)
4969 vim_free(newcmd);
4970
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004971 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004973 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004974 CloseHandle(si.hStdInput);
4975 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004976 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004977 CloseHandle(pi.hThread);
4978 CloseHandle(pi.hProcess);
4979 }
4980 else
4981 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004982 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004983#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004984 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004985 (!s_dont_use_vimrun && p_stmp ?
4986 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4987 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004989 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004990
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004991 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004992 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004994#if defined(FEAT_GUI_MSWIN)
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 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01005001 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
5002 "External commands will not pause after completion.\n"
5003 "See :help win32-vimrun for more information.");
5004 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005005 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
5006 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01005007
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005008 if (wmsg != NULL && wtitle != NULL)
5009 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
5010 vim_free(wmsg);
5011 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 need_vimrun_warning = FALSE;
5013 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005014 if (
5015# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005016 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005017# endif
5018 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005019 // Use vimrun to execute the command. It opens a console
5020 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005021 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 vimrun_path,
5023 (msg_silent != 0 || (options & SHELL_DOOUT))
5024 ? "-s " : "",
5025 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005026 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005027# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005028 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005029# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02005030 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005031 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005032 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
5033 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005036 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00005037 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005039 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 }
5043
5044 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005045 {
5046 // The shell may have messed with the mode, always set it.
5047 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005048 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
Bram Moolenaar0f873732019-12-05 20:28:46 +01005051 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01005053#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005054 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005055 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#endif
5057 )
5058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005059 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 msg_putchar('\n');
5061 }
5062#ifdef FEAT_TITLE
5063 resettitle();
5064#endif
5065
5066 signal(SIGINT, SIG_DFL);
5067#if defined(__GNUC__) && !defined(__MINGW32__)
5068 signal(SIGKILL, SIG_DFL);
5069#else
5070 signal(SIGBREAK, SIG_DFL);
5071#endif
5072 signal(SIGILL, SIG_DFL);
5073 signal(SIGFPE, SIG_DFL);
5074 signal(SIGSEGV, SIG_DFL);
5075 signal(SIGTERM, SIG_DFL);
5076 signal(SIGABRT, SIG_DFL);
5077
5078 return x;
5079}
5080
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005081#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005082 static HANDLE
5083job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01005084 char_u *fname,
5085 DWORD dwDesiredAccess,
5086 DWORD dwShareMode,
5087 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
5088 DWORD dwCreationDisposition,
5089 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005090{
5091 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005092 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005093
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005094 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005095 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005096 return INVALID_HANDLE_VALUE;
5097
5098 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
5099 lpSecurityAttributes, dwCreationDisposition,
5100 dwFlagsAndAttributes, NULL);
5101 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005102 return h;
5103}
5104
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005105/*
5106 * Turn the dictionary "env" into a NUL separated list that can be used as the
5107 * environment argument of vim_create_process().
5108 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005109 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005110win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005111{
5112 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005113 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005114 LPVOID base = GetEnvironmentStringsW();
5115
Bram Moolenaar0f873732019-12-05 20:28:46 +01005116 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005117 if (ga_grow(gap, 1) == FAIL)
5118 return;
5119
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005120 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005121 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005122 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005123 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005124 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005125 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005126 typval_T *item = &dict_lookup(hi)->di_tv;
5127 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005128 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005129 --todo;
5130 if (wkey != NULL && wval != NULL)
5131 {
5132 size_t n;
5133 size_t lkey = wcslen(wkey);
5134 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02005135
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005136 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
5137 continue;
5138 for (n = 0; n < lkey; n++)
5139 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
5140 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
5141 for (n = 0; n < lval; n++)
5142 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
5143 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5144 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01005145 vim_free(wkey);
5146 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005147 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005148 }
5149 }
5150
Bram Moolenaar355757a2020-02-10 22:06:32 +01005151 if (base)
5152 {
5153 WCHAR *p = (WCHAR*) base;
5154
5155 // for last \0
5156 if (ga_grow(gap, 1) == FAIL)
5157 return;
5158
5159 while (*p != 0 || *(p + 1) != 0)
5160 {
5161 if (ga_grow(gap, 1) == OK)
5162 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5163 p++;
5164 }
5165 FreeEnvironmentStrings(base);
5166 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5167 }
5168
Bram Moolenaar493359e2018-06-12 20:25:52 +02005169# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005170 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005171# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005172 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005173 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005174# endif
5175# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005176 char_u *version = get_vim_var_str(VV_VERSION);
5177 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005178# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005179 // size of "VIM_SERVERNAME=" and value,
5180 // plus "VIM_TERMINAL=" and value,
5181 // plus two terminating NULs
5182 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005183# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005184 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005185# endif
5186# ifdef FEAT_TERMINAL
5187 + 13 + version_len + 2
5188# endif
5189 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005190
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005191 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005192 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005193# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005194 for (n = 0; n < 15; n++)
5195 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5196 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005197 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005198 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5199 (WCHAR)servername[n];
5200 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005201# endif
5202# ifdef FEAT_TERMINAL
5203 if (is_terminal)
5204 {
5205 for (n = 0; n < 13; n++)
5206 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5207 (WCHAR)"VIM_TERMINAL="[n];
5208 for (n = 0; n < version_len; n++)
5209 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5210 (WCHAR)version[n];
5211 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5212 }
5213# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005214 }
5215 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005216# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005217}
5218
Bram Moolenaarb091f302019-01-19 14:37:00 +01005219/*
5220 * Create a pair of pipes.
5221 * Return TRUE for success, FALSE for failure.
5222 */
5223 static BOOL
5224create_pipe_pair(HANDLE handles[2])
5225{
5226 static LONG s;
5227 char name[64];
5228 SECURITY_ATTRIBUTES sa;
5229
5230 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5231 GetCurrentProcessId(),
5232 InterlockedIncrement(&s));
5233
5234 // Create named pipe. Max size of named pipe is 65535.
5235 handles[1] = CreateNamedPipe(
5236 name,
5237 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5238 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005239 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005240
5241 if (handles[1] == INVALID_HANDLE_VALUE)
5242 return FALSE;
5243
5244 sa.nLength = sizeof(sa);
5245 sa.bInheritHandle = TRUE;
5246 sa.lpSecurityDescriptor = NULL;
5247
5248 handles[0] = CreateFile(name,
5249 FILE_GENERIC_READ,
5250 FILE_SHARE_READ, &sa,
5251 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5252
5253 if (handles[0] == INVALID_HANDLE_VALUE)
5254 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005255 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005256 return FALSE;
5257 }
5258
5259 return TRUE;
5260}
5261
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005262 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005263mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005264{
5265 STARTUPINFO si;
5266 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005267 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005268 SECURITY_ATTRIBUTES saAttr;
5269 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005270 HANDLE ifd[2];
5271 HANDLE ofd[2];
5272 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005273 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005274
5275 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5276 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5277 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5278 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5279 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5280 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5281 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5282
5283 if (use_out_for_err && use_null_for_out)
5284 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005285
5286 ifd[0] = INVALID_HANDLE_VALUE;
5287 ifd[1] = INVALID_HANDLE_VALUE;
5288 ofd[0] = INVALID_HANDLE_VALUE;
5289 ofd[1] = INVALID_HANDLE_VALUE;
5290 efd[0] = INVALID_HANDLE_VALUE;
5291 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005292 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005293
Bram Moolenaar14207f42016-10-27 21:13:10 +02005294 jo = CreateJobObject(NULL, NULL);
5295 if (jo == NULL)
5296 {
5297 job->jv_status = JOB_FAILED;
5298 goto failed;
5299 }
5300
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005301 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005302 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005303
Bram Moolenaar76467df2016-02-12 19:30:26 +01005304 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005305 ZeroMemory(&si, sizeof(si));
5306 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005307 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005308 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005309
Bram Moolenaard8070362016-02-15 21:56:54 +01005310 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5311 saAttr.bInheritHandle = TRUE;
5312 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005313
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005314 if (use_file_for_in)
5315 {
5316 char_u *fname = options->jo_io_name[PART_IN];
5317
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005318 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5319 FILE_SHARE_READ | FILE_SHARE_WRITE,
5320 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5321 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005323 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005324 goto failed;
5325 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005326 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005327 else if (!use_null_for_in
5328 && (!create_pipe_pair(ifd)
5329 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005330 goto failed;
5331
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005332 if (use_file_for_out)
5333 {
5334 char_u *fname = options->jo_io_name[PART_OUT];
5335
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005336 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5337 FILE_SHARE_READ | FILE_SHARE_WRITE,
5338 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5339 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005341 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005342 goto failed;
5343 }
5344 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005345 else if (!use_null_for_out &&
5346 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005347 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005348 goto failed;
5349
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005350 if (use_file_for_err)
5351 {
5352 char_u *fname = options->jo_io_name[PART_ERR];
5353
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005354 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5355 FILE_SHARE_READ | FILE_SHARE_WRITE,
5356 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5357 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005359 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005360 goto failed;
5361 }
5362 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005363 else if (!use_out_for_err && !use_null_for_err &&
5364 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005365 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005366 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005367
Bram Moolenaard8070362016-02-15 21:56:54 +01005368 si.dwFlags |= STARTF_USESTDHANDLES;
5369 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005370 si.hStdOutput = ofd[1];
5371 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5372
5373 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5374 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005375 if (options->jo_set & JO_CHANNEL)
5376 {
5377 channel = options->jo_channel;
5378 if (channel != NULL)
5379 ++channel->ch_refcount;
5380 }
5381 else
5382 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005383 if (channel == NULL)
5384 goto failed;
5385 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005386
5387 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005388 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005389 CREATE_DEFAULT_ERROR_MODE |
5390 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005391 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005392 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005393 &si, &pi,
5394 ga.ga_data,
5395 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005396 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005397 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005398 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005399 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005400 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005401
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005402 ga_clear(&ga);
5403
Bram Moolenaar14207f42016-10-27 21:13:10 +02005404 if (!AssignProcessToJobObject(jo, pi.hProcess))
5405 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005406 // if failing, switch the way to terminate
5407 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005408 CloseHandle(jo);
5409 jo = NULL;
5410 }
5411 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005412 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005413 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005414 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005415 job->jv_status = JOB_STARTED;
5416
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005417 CloseHandle(ifd[0]);
5418 CloseHandle(ofd[1]);
5419 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005420 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005421
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005422 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005423 if (channel != NULL)
5424 {
5425 channel_set_pipes(channel,
5426 use_file_for_in || use_null_for_in
5427 ? INVALID_FD : (sock_T)ifd[1],
5428 use_file_for_out || use_null_for_out
5429 ? INVALID_FD : (sock_T)ofd[0],
5430 use_out_for_err || use_file_for_err || use_null_for_err
5431 ? INVALID_FD : (sock_T)efd[0]);
5432 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005433 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005434 return;
5435
5436failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005437 CloseHandle(ifd[0]);
5438 CloseHandle(ofd[0]);
5439 CloseHandle(efd[0]);
5440 CloseHandle(ifd[1]);
5441 CloseHandle(ofd[1]);
5442 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005443 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005444 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005445}
5446
5447 char *
5448mch_job_status(job_T *job)
5449{
5450 DWORD dwExitCode = 0;
5451
Bram Moolenaar76467df2016-02-12 19:30:26 +01005452 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5453 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005454 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005455 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005456 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005457 {
5458 ch_log(job->jv_channel, "Job ended");
5459 job->jv_status = JOB_ENDED;
5460 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005461 return "dead";
5462 }
5463 return "run";
5464}
5465
Bram Moolenaar97792de2016-10-15 18:36:49 +02005466 job_T *
5467mch_detect_ended_job(job_T *job_list)
5468{
5469 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5470 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5471 job_T *job = job_list;
5472
5473 while (job != NULL)
5474 {
5475 DWORD n;
5476 DWORD result;
5477
5478 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5479 && job != NULL; job = job->jv_next)
5480 {
5481 if (job->jv_status == JOB_STARTED)
5482 {
5483 jobHandles[n] = job->jv_proc_info.hProcess;
5484 jobArray[n] = job;
5485 ++n;
5486 }
5487 }
5488 if (n == 0)
5489 continue;
5490 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5491 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5492 {
5493 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5494
5495 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5496 return wait_job;
5497 }
5498 }
5499 return NULL;
5500}
5501
Bram Moolenaarfb630902016-10-29 14:55:00 +02005502 static BOOL
5503terminate_all(HANDLE process, int code)
5504{
5505 PROCESSENTRY32 pe;
5506 HANDLE h = INVALID_HANDLE_VALUE;
5507 DWORD pid = GetProcessId(process);
5508
5509 if (pid != 0)
5510 {
5511 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5512 if (h != INVALID_HANDLE_VALUE)
5513 {
5514 pe.dwSize = sizeof(PROCESSENTRY32);
5515 if (!Process32First(h, &pe))
5516 goto theend;
5517
5518 do
5519 {
5520 if (pe.th32ParentProcessID == pid)
5521 {
5522 HANDLE ph = OpenProcess(
5523 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5524 if (ph != NULL)
5525 {
5526 terminate_all(ph, code);
5527 CloseHandle(ph);
5528 }
5529 }
5530 } while (Process32Next(h, &pe));
5531
5532 CloseHandle(h);
5533 }
5534 }
5535
5536theend:
5537 return TerminateProcess(process, code);
5538}
5539
5540/*
5541 * Send a (deadly) signal to "job".
5542 * Return FAIL if it didn't work.
5543 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005544 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005545mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005546{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005547 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005548
Bram Moolenaar923d9262016-02-25 20:56:01 +01005549 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005550 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005551 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005552 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005553 {
5554 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5555 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005556 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005557 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005558 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005559 }
5560
5561 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5562 return FAIL;
5563 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005564 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5565 job->jv_proc_info.dwProcessId)
5566 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005567 FreeConsole();
5568 return ret;
5569}
5570
5571/*
5572 * Clear the data related to "job".
5573 */
5574 void
5575mch_clear_job(job_T *job)
5576{
5577 if (job->jv_status != JOB_FAILED)
5578 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005579 if (job->jv_job_object != NULL)
5580 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005581 CloseHandle(job->jv_proc_info.hProcess);
5582 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005583}
5584#endif
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005587#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589/*
5590 * Start termcap mode
5591 */
5592 static void
5593termcap_mode_start(void)
5594{
5595 DWORD cmodein;
5596
5597 if (g_fTermcapMode)
5598 return;
5599
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005600 if (!p_rs && USE_VTP)
5601 vtp_printf("\033[?1049h");
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 SaveConsoleBuffer(&g_cbNonTermcap);
5604
5605 if (g_cbTermcap.IsValid)
5606 {
5607 /*
5608 * We've been in termcap mode before. Restore certain screen
5609 * characteristics, including the buffer size and the window
5610 * size. Since we will be redrawing the screen, we don't need
5611 * to restore the actual contents of the buffer.
5612 */
5613 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005614 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5616 Rows = g_cbTermcap.Info.dwSize.Y;
5617 Columns = g_cbTermcap.Info.dwSize.X;
5618 }
5619 else
5620 {
5621 /*
5622 * This is our first time entering termcap mode. Clear the console
5623 * screen buffer, and resize the buffer to match the current window
5624 * size. We will use this as the size of our editing environment.
5625 */
5626 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005627 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5629 }
5630
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005631# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005639 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 cmodein &= ~ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005644 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
5645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 cmodein |= ENABLE_WINDOW_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005647 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 redraw_later_clear();
5650 g_fTermcapMode = TRUE;
5651}
5652
5653
5654/*
5655 * End termcap mode
5656 */
5657 static void
5658termcap_mode_end(void)
5659{
5660 DWORD cmodein;
5661 ConsoleBuffer *cb;
5662 COORD coord;
5663 DWORD dwDummy;
5664
5665 if (!g_fTermcapMode)
5666 return;
5667
5668 SaveConsoleBuffer(&g_cbTermcap);
5669
5670 GetConsoleMode(g_hConIn, &cmodein);
5671 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
Wez Furlong6ef5ab52021-05-30 19:29:41 +02005672 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
5673 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005675# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005676 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005677# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005679# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005680 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005681 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 SetConsoleCursorInfo(g_hConOut, &g_cci);
5683
5684 if (p_rs || exiting)
5685 {
5686 /*
5687 * Clear anything that happens to be on the current line.
5688 */
5689 coord.X = 0;
5690 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5691 FillConsoleOutputCharacter(g_hConOut, ' ',
5692 cb->Info.dwSize.X, coord, &dwDummy);
5693 /*
5694 * The following is just for aesthetics. If we are exiting without
5695 * restoring the screen, then we want to have a prompt string
5696 * appear at the bottom line. However, the command interpreter
5697 * seems to always advance the cursor one line before displaying
5698 * the prompt string, which causes the screen to scroll. To
5699 * counter this, move the cursor up one line before exiting.
5700 */
5701 if (exiting && !p_rs)
5702 coord.Y--;
5703 /*
5704 * Position the cursor at the leftmost column of the desired row.
5705 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005706 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005709 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005710 vtp_printf("\033[?1049l");
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 g_fTermcapMode = FALSE;
5713}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005714#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005717#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 void
5719mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005720 char_u *s UNUSED,
5721 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005723 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726#else
5727
5728/*
5729 * clear `n' chars, starting from `coord'
5730 */
5731 static void
5732clear_chars(
5733 COORD coord,
5734 DWORD n)
5735{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005736 if (!USE_VTP)
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02005737 {
5738 DWORD dwDummy;
5739
5740 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
5741 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord,
5742 &dwDummy);
5743 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005744 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005745 {
5746 set_console_color_rgb();
5747 gotoxy(coord.X + 1, coord.Y + 1);
5748 vtp_printf("\033[%dX", n);
5749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750}
5751
5752
5753/*
5754 * Clear the screen
5755 */
5756 static void
5757clear_screen(void)
5758{
5759 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005760
5761 if (!USE_VTP)
5762 clear_chars(g_coord, Rows * Columns);
5763 else
5764 {
5765 set_console_color_rgb();
5766 gotoxy(1, 1);
5767 vtp_printf("\033[2J");
5768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769}
5770
5771
5772/*
5773 * Clear to end of display
5774 */
5775 static void
5776clear_to_end_of_display(void)
5777{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005778 COORD save = g_coord;
5779
5780 if (!USE_VTP)
5781 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005783 else
5784 {
5785 set_console_color_rgb();
5786 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5787 vtp_printf("\033[0J");
5788
5789 gotoxy(save.X + 1, save.Y + 1);
5790 g_coord = save;
5791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
5794
5795/*
5796 * Clear to end of line
5797 */
5798 static void
5799clear_to_end_of_line(void)
5800{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005801 COORD save = g_coord;
5802
5803 if (!USE_VTP)
5804 clear_chars(g_coord, Columns - g_coord.X);
5805 else
5806 {
5807 set_console_color_rgb();
5808 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5809 vtp_printf("\033[0K");
5810
5811 gotoxy(save.X + 1, save.Y + 1);
5812 g_coord = save;
5813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814}
5815
5816
5817/*
5818 * Scroll the scroll region up by `cLines' lines
5819 */
5820 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005821scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 COORD oldcoord = g_coord;
5824
5825 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5826 delete_lines(cLines);
5827
5828 g_coord = oldcoord;
5829}
5830
5831
5832/*
5833 * Set the scroll region
5834 */
5835 static void
5836set_scroll_region(
5837 unsigned left,
5838 unsigned top,
5839 unsigned right,
5840 unsigned bottom)
5841{
5842 if (left >= right
5843 || top >= bottom
5844 || right > (unsigned) Columns - 1
5845 || bottom > (unsigned) Rows - 1)
5846 return;
5847
5848 g_srScrollRegion.Left = left;
5849 g_srScrollRegion.Top = top;
5850 g_srScrollRegion.Right = right;
5851 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005852}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005853
Bram Moolenaar6982f422019-02-16 16:48:01 +01005854 static void
5855set_scroll_region_tb(
5856 unsigned top,
5857 unsigned bottom)
5858{
5859 if (top >= bottom || bottom > (unsigned)Rows - 1)
5860 return;
5861
5862 g_srScrollRegion.Top = top;
5863 g_srScrollRegion.Bottom = bottom;
5864}
5865
5866 static void
5867set_scroll_region_lr(
5868 unsigned left,
5869 unsigned right)
5870{
5871 if (left >= right || right > (unsigned)Columns - 1)
5872 return;
5873
5874 g_srScrollRegion.Left = left;
5875 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
5878
5879/*
5880 * Insert `cLines' lines at the current cursor position
5881 */
5882 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005883insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005885 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 COORD dest;
5887 CHAR_INFO fill;
5888
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005889 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5890
Bram Moolenaar6982f422019-02-16 16:48:01 +01005891 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 dest.Y = g_coord.Y + cLines;
5893
Bram Moolenaar6982f422019-02-16 16:48:01 +01005894 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 source.Top = g_coord.Y;
5896 source.Right = g_srScrollRegion.Right;
5897 source.Bottom = g_srScrollRegion.Bottom - cLines;
5898
Bram Moolenaar6982f422019-02-16 16:48:01 +01005899 clip.Left = g_srScrollRegion.Left;
5900 clip.Top = g_coord.Y;
5901 clip.Right = g_srScrollRegion.Right;
5902 clip.Bottom = g_srScrollRegion.Bottom;
5903
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005904 fill.Char.AsciiChar = ' ';
5905 if (!USE_VTP)
5906 fill.Attributes = g_attrCurrent;
5907 else
5908 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005910 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005911
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005912 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5913
Bram Moolenaar6982f422019-02-16 16:48:01 +01005914 // Here we have to deal with a win32 console flake: If the scroll
5915 // region looks like abc and we scroll c to a and fill with d we get
5916 // cbd... if we scroll block c one line at a time to a, we get cdd...
5917 // vim expects cdd consistently... So we have to deal with that
5918 // here... (this also occurs scrolling the same way in the other
5919 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 if (source.Bottom < dest.Y)
5922 {
5923 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005924 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar6982f422019-02-16 16:48:01 +01005926 coord.X = source.Left;
5927 for (i = clip.Top; i < dest.Y; ++i)
5928 {
5929 coord.Y = i;
5930 clear_chars(coord, source.Right - source.Left + 1);
5931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02005933
5934 if (USE_WT)
5935 {
5936 COORD coord;
5937 int i;
5938
5939 coord.X = source.Left;
5940 for (i = source.Top; i < dest.Y; ++i)
5941 {
5942 coord.Y = i;
5943 clear_chars(coord, source.Right - source.Left + 1);
5944 }
5945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946}
5947
5948
5949/*
5950 * Delete `cLines' lines at the current cursor position
5951 */
5952 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005953delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005955 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 COORD dest;
5957 CHAR_INFO fill;
5958 int nb;
5959
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005960 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5961
Bram Moolenaar6982f422019-02-16 16:48:01 +01005962 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 dest.Y = g_coord.Y;
5964
Bram Moolenaar6982f422019-02-16 16:48:01 +01005965 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 source.Top = g_coord.Y + cLines;
5967 source.Right = g_srScrollRegion.Right;
5968 source.Bottom = g_srScrollRegion.Bottom;
5969
Bram Moolenaar6982f422019-02-16 16:48:01 +01005970 clip.Left = g_srScrollRegion.Left;
5971 clip.Top = g_coord.Y;
5972 clip.Right = g_srScrollRegion.Right;
5973 clip.Bottom = g_srScrollRegion.Bottom;
5974
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005975 fill.Char.AsciiChar = ' ';
5976 if (!USE_VTP)
5977 fill.Attributes = g_attrCurrent;
5978 else
5979 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005981 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005982
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005983 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5984
Bram Moolenaar6982f422019-02-16 16:48:01 +01005985 // Here we have to deal with a win32 console flake; See insert_lines()
5986 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
5988 nb = dest.Y + (source.Bottom - source.Top) + 1;
5989
5990 if (nb < source.Top)
5991 {
5992 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005993 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
Bram Moolenaar6982f422019-02-16 16:48:01 +01005995 coord.X = source.Left;
5996 for (i = nb; i < clip.Bottom; ++i)
5997 {
5998 coord.Y = i;
5999 clear_chars(coord, source.Right - source.Left + 1);
6000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02006002
6003 if (USE_WT)
6004 {
6005 COORD coord;
6006 int i;
6007
6008 coord.X = source.Left;
6009 for (i = nb; i <= source.Bottom; ++i)
6010 {
6011 coord.Y = i;
6012 clear_chars(coord, source.Right - source.Left + 1);
6013 }
6014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015}
6016
6017
6018/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02006019 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 static void
6022gotoxy(
6023 unsigned x,
6024 unsigned y)
6025{
6026 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
6027 return;
6028
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006029 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02006030 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01006031 // There are reports of double-width characters not displayed
6032 // correctly. This workaround should fix it, similar to how it's done
6033 // for VTP.
6034 g_coord.X = 0;
6035 SetConsoleCursorPosition(g_hConOut, g_coord);
6036
Bram Moolenaar2313b612019-09-27 14:14:32 +02006037 // external cursor coords are 1-based; internal are 0-based
6038 g_coord.X = x - 1;
6039 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006040 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006041 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006042 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02006043 {
6044 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02006045 // destruction. Insider build bug. Always enabled because it's cheap
6046 // and avoids mistakes with recognizing the build.
6047 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006048
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006049 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006050
6051 g_coord.X = x - 1;
6052 g_coord.Y = y - 1;
6053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054}
6055
6056
6057/*
6058 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006059 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 */
6061 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006062textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006064 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
6066 SetConsoleTextAttribute(g_hConOut, wAttr);
6067}
6068
6069
6070 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006071textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006073 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006075 if (!USE_VTP)
6076 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6077 else
6078 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079}
6080
6081
6082 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006083textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006085 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006087 if (!USE_VTP)
6088 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6089 else
6090 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091}
6092
6093
6094/*
6095 * restore the default text attribute (whatever we started with)
6096 */
6097 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006098normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006100 if (!USE_VTP)
6101 textattr(g_attrDefault);
6102 else
6103 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104}
6105
6106
6107static WORD g_attrPreStandout = 0;
6108
6109/*
6110 * Make the text standout, by brightening it
6111 */
6112 static void
6113standout(void)
6114{
6115 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
6118}
6119
6120
6121/*
6122 * Turn off standout mode
6123 */
6124 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006125standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006129
6130 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
6132
6133
6134/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00006135 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006138mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *p;
6141 int n;
6142
6143 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
6144 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006145 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006146# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006147 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006148# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006149 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 {
6151 p = T_ME + 2;
6152 n = getdigits(&p);
6153 if (*p == 'm' && n > 0)
6154 {
6155 cterm_normal_fg_color = (n & 0xf) + 1;
6156 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
6157 }
6158 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006159# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006160 cterm_normal_fg_gui_color = INVALCOLOR;
6161 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163}
6164
6165
6166/*
6167 * visual bell: flash the screen
6168 */
6169 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006170visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 COORD coordOrigin = {0, 0};
6173 WORD attrFlash = ~g_attrCurrent & 0xff;
6174
6175 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006176 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 if (oldattrs == NULL)
6179 return;
6180 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
6181 coordOrigin, &dwDummy);
6182 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6183 coordOrigin, &dwDummy);
6184
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006186 if (!USE_VTP)
6187 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 coordOrigin, &dwDummy);
6189 vim_free(oldattrs);
6190}
6191
6192
6193/*
6194 * Make the cursor visible or invisible
6195 */
6196 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006197cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006200
6201 if (USE_VTP)
6202 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6203
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006204# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207}
6208
6209
6210/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006211 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006212 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006214 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006216 char_u *pchBuf,
6217 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006219 COORD coord = g_coord;
6220 DWORD written;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006221 DWORD n, cchwritten;
6222 static DWORD cells;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006223 static WCHAR *unicodebuf = NULL;
6224 static int unibuflen = 0;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006225 static int length;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006226 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006227 static WCHAR *utf8spbuf = NULL;
6228 static int utf8splength;
6229 static DWORD utf8spcells;
6230 static WCHAR **utf8usingbuf = &unicodebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006232 if (cbToWrite != 1 || *pchBuf != ' ' || !enc_utf8)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006233 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006234 utf8usingbuf = &unicodebuf;
6235 do
6236 {
6237 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6238 unicodebuf, unibuflen);
6239 if (length && length <= unibuflen)
6240 break;
6241 vim_free(unicodebuf);
6242 unicodebuf = length ? LALLOC_MULT(WCHAR, length) : NULL;
6243 unibuflen = unibuflen ? 0 : length;
6244 } while(1);
6245 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006246 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006247 else // cbToWrite == 1 && *pchBuf == ' ' && enc_utf8
6248 {
6249 if (utf8usingbuf != &utf8spbuf)
6250 {
6251 if (utf8spbuf == NULL)
6252 {
6253 cells = mb_string2cells((char_u *)" ", 1);
6254 length = MultiByteToWideChar(CP_UTF8, 0, " ", 1, NULL, 0);
6255 utf8spbuf = LALLOC_MULT(WCHAR, length);
6256 if (utf8spbuf != NULL)
6257 {
6258 MultiByteToWideChar(CP_UTF8, 0, " ", 1, utf8spbuf, length);
6259 utf8usingbuf = &utf8spbuf;
6260 utf8splength = length;
6261 utf8spcells = cells;
6262 }
6263 }
6264 else
6265 {
6266 utf8usingbuf = &utf8spbuf;
6267 length = utf8splength;
6268 cells = utf8spcells;
6269 }
6270 }
6271 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006272
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006273 if (!USE_VTP)
6274 {
6275 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6276 coord, &written);
6277 // When writing fails or didn't write a single character, pretend one
6278 // character was written, otherwise we get stuck.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006279 if (WriteConsoleOutputCharacterW(g_hConOut, *utf8usingbuf, length,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006280 coord, &cchwritten) == 0
6281 || cchwritten == 0 || cchwritten == (DWORD)-1)
6282 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006283 }
6284 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006285 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006286 if (WriteConsoleW(g_hConOut, *utf8usingbuf, length, &cchwritten,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006287 NULL) == 0 || cchwritten == 0)
6288 cchwritten = 1;
6289 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006291 if (cchwritten == length)
6292 {
6293 written = cbToWrite;
6294 g_coord.X += (SHORT)cells;
6295 }
6296 else
6297 {
6298 char_u *p = pchBuf;
6299 for (n = 0; n < cchwritten; n++)
6300 MB_CPTR_ADV(p);
6301 written = p - pchBuf;
6302 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305 while (g_coord.X > g_srScrollRegion.Right)
6306 {
6307 g_coord.X -= (SHORT) Columns;
6308 if (g_coord.Y < g_srScrollRegion.Bottom)
6309 g_coord.Y++;
6310 }
6311
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006312 // Cursor under VTP is always in the correct position, no need to reset.
6313 if (!USE_VTP)
6314 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 return written;
6317}
6318
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006319 static char_u *
6320get_seq(
6321 int *args,
6322 int *count,
6323 char_u *head)
6324{
6325 int argc;
6326 char_u *p;
6327
6328 if (head == NULL || *head != '\033')
6329 return NULL;
6330
6331 argc = 0;
6332 p = head;
6333 ++p;
6334 do
6335 {
6336 ++p;
6337 args[argc] = getdigits(&p);
6338 argc += (argc < 15) ? 1 : 0;
6339 } while (*p == ';');
6340 *count = argc;
6341
6342 return p;
6343}
6344
6345 static char_u *
6346get_sgr(
6347 int *args,
6348 int *count,
6349 char_u *head)
6350{
6351 char_u *p = get_seq(args, count, head);
6352
6353 return (p && *p == 'm') ? ++p : NULL;
6354}
6355
6356/*
6357 * Pointer to next if SGR (^[[n;2;*;*;*m), NULL otherwise.
6358 */
6359 static char_u *
6360sgrn2(
6361 char_u *head,
6362 int n)
6363{
6364 int argc;
6365 int args[16];
6366 char_u *p = get_sgr(args, &argc, head);
6367
6368 return p && argc == 5 && args[0] == n && args[1] == 2 ? p : NULL;
6369}
6370
6371/*
6372 * Pointer to next if SGR(^[[nm)<space>ESC, NULL otherwise.
6373 */
6374 static char_u *
6375sgrnc(
6376 char_u *head,
6377 int n)
6378{
6379 int argc;
6380 int args[16];
6381 char_u *p = get_sgr(args, &argc, head);
6382
6383 return p && argc == 1 && args[0] == n && (p = skipwhite(p)) && *p == '\033'
6384 ? p : NULL;
6385}
6386
6387 static char_u *
6388skipblank(char_u *q)
6389{
6390 char_u *p = q;
6391
6392 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
6393 ++p;
6394 return p;
6395}
6396
6397/*
6398 * Pointer to the next if any whitespace that may follow SGR is ESC, otherwise
6399 * NULL.
6400 */
6401 static char_u *
6402sgrn2c(
6403 char_u *head,
6404 int n)
6405{
6406 char_u *p = sgrn2(head, n);
6407
6408 return p && *p != NUL && (p = skipblank(p)) && *p == '\033' ? p : NULL;
6409}
6410
6411/*
6412 * If there is only a newline between the sequence immediately following it,
6413 * a pointer to the character following the newline is returned.
6414 * Otherwise NULL.
6415 */
6416 static char_u *
6417sgrn2cn(
6418 char_u *head,
6419 int n)
6420{
6421 char_u *p = sgrn2(head, n);
6422
6423 return p && p[0] == 0x0a && p[1] == '\033' ? ++p : NULL;
6424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426/*
6427 * mch_write(): write the output buffer to the screen, translating ESC
6428 * sequences into calls to console output routines.
6429 */
6430 void
6431mch_write(
6432 char_u *s,
6433 int len)
6434{
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02006435 char_u *end = s + len;
6436
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006437# ifdef VIMDLL
6438 if (gui.in_use)
6439 return;
6440# endif
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (!term_console)
6443 {
6444 write(1, s, (unsigned)len);
6445 return;
6446 }
6447
Bram Moolenaar0f873732019-12-05 20:28:46 +01006448 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 while (len--)
6450 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006451 int prefix = -1;
6452 char_u ch;
6453
6454 // While processing a sequence, on rare occasions it seems that another
6455 // sequence may be inserted asynchronously.
6456 if (len < 0)
6457 {
6458 redraw_all_later(CLEAR);
6459 return;
6460 }
6461
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02006462 while (s + ++prefix < end)
6463 {
6464 ch = s[prefix];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006465 if (ch <= 0x1e && !(ch != '\n' && ch != '\r' && ch != '\b'
6466 && ch != '\a' && ch != '\033'))
6467 break;
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02006468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
6470 if (p_wd)
6471 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006472 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 if (prefix != 0)
6474 prefix = 1;
6475 }
6476
6477 if (prefix != 0)
6478 {
6479 DWORD nWritten;
6480
6481 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006482# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (fdDump)
6484 {
6485 fputc('>', fdDump);
6486 fwrite(s, sizeof(char_u), nWritten, fdDump);
6487 fputs("<\n", fdDump);
6488 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 len -= (nWritten - 1);
6491 s += nWritten;
6492 }
6493 else if (s[0] == '\n')
6494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (g_coord.Y == g_srScrollRegion.Bottom)
6497 {
6498 scroll(1);
6499 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6500 }
6501 else
6502 {
6503 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6504 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006505# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (fdDump)
6507 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 s++;
6510 }
6511 else if (s[0] == '\r')
6512 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006513 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006515# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (fdDump)
6517 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 s++;
6520 }
6521 else if (s[0] == '\b')
6522 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006523 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (g_coord.X > g_srScrollRegion.Left)
6525 g_coord.X--;
6526 else if (g_coord.Y > g_srScrollRegion.Top)
6527 {
6528 g_coord.X = g_srScrollRegion.Right;
6529 g_coord.Y--;
6530 }
6531 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006532# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (fdDump)
6534 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 s++;
6537 }
6538 else if (s[0] == '\a')
6539 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006540 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006542# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (fdDump)
6544 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 s++;
6547 }
6548 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6549 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006550# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006551 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006552# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006553 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006554 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006555 char_u *sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 switch (s[2])
6558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 case '0': case '1': case '2': case '3': case '4':
6560 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006561 if (*(p = get_seq(args, &argc, s)) != 'm')
6562 goto notsgr;
6563
6564 p = s;
6565
6566 // Handling frequent optional sequences. Output to the screen
6567 // takes too long, so do not output as much as possible.
6568
6569 // If resetFG,FG,BG,<cr>,BG,FG are connected, the preceding
6570 // resetFG,FG,BG are omitted.
6571 if (sgrn2(sgrn2(sgrn2cn(sgrn2(sgrnc(p, 39), 38), 48), 48), 38))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006572 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006573 p = sgrn2(sgrn2(sgrnc(p, 39), 38), 48);
6574 len = len + 1 - (int)(p - s);
6575 s = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 break;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006579 // If FG,BG,BG,FG of SGR are connected, the first FG can be
6580 // omitted.
6581 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 48), 38))
6582 p = sp;
6583
6584 // If FG,BG,FG,BG of SGR are connected, the first FG can be
6585 // omitted.
6586 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 38), 48))
6587 p = sp;
6588
6589 // If BG,BG of SGR are connected, the first BG can be omitted.
6590 if (sgrn2((sp = sgrn2(p, 48)), 48))
6591 p = sp;
6592
6593 // If restoreFG and FG are connected, the restoreFG can be
6594 // omitted.
6595 if (sgrn2((sp = sgrnc(p, 39)), 38))
6596 p = sp;
6597
6598 p = get_seq(args, &argc, p);
6599
6600notsgr:
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006601 arg1 = args[0];
6602 arg2 = args[1];
6603 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006605 if (argc == 1 && args[0] == 0)
6606 normvideo();
6607 else if (argc == 1)
6608 {
6609 if (USE_VTP)
6610 textcolor((WORD) arg1);
6611 else
6612 textattr((WORD) arg1);
6613 }
6614 else if (USE_VTP)
6615 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006617 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006619 gotoxy(arg2, arg1);
6620 }
6621 else if (argc == 2 && *p == 'r')
6622 {
6623 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6624 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006625 else if (argc == 2 && *p == 'R')
6626 {
6627 set_scroll_region_tb(arg1, arg2);
6628 }
6629 else if (argc == 2 && *p == 'V')
6630 {
6631 set_scroll_region_lr(arg1, arg2);
6632 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006633 else if (argc == 1 && *p == 'A')
6634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 gotoxy(g_coord.X + 1,
6636 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6637 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006638 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 textbackground((WORD) arg1);
6641 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006642 else if (argc == 1 && *p == 'C')
6643 {
6644 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6645 g_coord.Y + 1);
6646 }
6647 else if (argc == 1 && *p == 'f')
6648 {
6649 textcolor((WORD) arg1);
6650 }
6651 else if (argc == 1 && *p == 'H')
6652 {
6653 gotoxy(1, arg1);
6654 }
6655 else if (argc == 1 && *p == 'L')
6656 {
6657 insert_lines(arg1);
6658 }
6659 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 delete_lines(arg1);
6662 }
6663
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006664 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 s = p + 1;
6666 break;
6667
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 gotoxy(g_coord.X + 1,
6670 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6671 goto got3;
6672
6673 case 'B':
6674 visual_bell();
6675 goto got3;
6676
6677 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6679 g_coord.Y + 1);
6680 goto got3;
6681
6682 case 'E':
6683 termcap_mode_end();
6684 goto got3;
6685
6686 case 'F':
6687 standout();
6688 goto got3;
6689
6690 case 'f':
6691 standend();
6692 goto got3;
6693
6694 case 'H':
6695 gotoxy(1, 1);
6696 goto got3;
6697
6698 case 'j':
6699 clear_to_end_of_display();
6700 goto got3;
6701
6702 case 'J':
6703 clear_screen();
6704 goto got3;
6705
6706 case 'K':
6707 clear_to_end_of_line();
6708 goto got3;
6709
6710 case 'L':
6711 insert_lines(1);
6712 goto got3;
6713
6714 case 'M':
6715 delete_lines(1);
6716 goto got3;
6717
6718 case 'S':
6719 termcap_mode_start();
6720 goto got3;
6721
6722 case 'V':
6723 cursor_visible(TRUE);
6724 goto got3;
6725
6726 case 'v':
6727 cursor_visible(FALSE);
6728 goto got3;
6729
6730 got3:
6731 s += 3;
6732 len -= 2;
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 {
6738 fputs("ESC | ", fdDump);
6739 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6740 fputc('\n', fdDump);
6741 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744 else
6745 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 DWORD nWritten;
6748
6749 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006750# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (fdDump)
6752 {
6753 fputc('>', fdDump);
6754 fwrite(s, sizeof(char_u), nWritten, fdDump);
6755 fputs("<\n", fdDump);
6756 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 len -= (nWritten - 1);
6760 s += nWritten;
6761 }
6762 }
6763
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006764# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (fdDump)
6766 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768}
6769
Bram Moolenaar0f873732019-12-05 20:28:46 +01006770#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772
6773/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006774 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
6776 void
6777mch_delay(
6778 long msec,
Bram Moolenaar0981c872020-08-23 14:28:37 +02006779 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006781#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 Sleep((int)msec); // never wait for input
6783#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006784# ifdef VIMDLL
6785 if (gui.in_use)
6786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006788 return;
6789 }
6790# endif
Bram Moolenaar0981c872020-08-23 14:28:37 +02006791 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006792# ifdef FEAT_MZSCHEME
6793 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6794 {
6795 int towait = p_mzq;
6796
Bram Moolenaar0f873732019-12-05 20:28:46 +01006797 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006798 while (msec > 0)
6799 {
6800 mzvim_check_threads();
6801 if (msec < towait)
6802 towait = msec;
6803 Sleep(towait);
6804 msec -= towait;
6805 }
6806 }
6807 else
6808# endif
6809 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006811 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812#endif
6813}
6814
6815
6816/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006817 * This version of remove is not scared by a readonly (backup) file.
6818 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 * Return 0 for success, -1 for failure.
6820 */
6821 int
6822mch_remove(char_u *name)
6823{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006824 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 int n;
6826
Bram Moolenaar203258c2016-01-17 22:15:16 +01006827 /*
6828 * On Windows, deleting a directory's symbolic link is done by
6829 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6830 */
6831 if (mch_isdir(name) && mch_is_symbolic_link(name))
6832 return mch_rmdir(name);
6833
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006834 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6835
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006836 wn = enc_to_utf16(name, NULL);
6837 if (wn == NULL)
6838 return -1;
6839
6840 n = DeleteFileW(wn) ? 0 : -1;
6841 vim_free(wn);
6842 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843}
6844
6845
6846/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006847 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 */
6849 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006850mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006852#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6853# ifdef VIMDLL
6854 if (!gui.in_use)
6855# endif
6856 if (g_fCtrlCPressed || g_fCBrkPressed)
6857 {
6858 ctrl_break_was_pressed = g_fCBrkPressed;
6859 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6860 got_int = TRUE;
6861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862#endif
6863}
6864
Bram Moolenaar0f873732019-12-05 20:28:46 +01006865// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006866#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006867
6868/*
6869 * How much main memory in KiB that can be used by VIM.
6870 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006871 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006872mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006873{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006874 MEMORYSTATUSEX ms;
6875
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 // Need to use GlobalMemoryStatusEx() when there is more memory than
6877 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006878 ms.dwLength = sizeof(MEMORYSTATUSEX);
6879 GlobalMemoryStatusEx(&ms);
6880 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006882 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006883 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006884 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006885 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006888 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006889 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006891 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006892}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006895 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6897 * file whose short file name is "FOO.BAR" (its long file name will
6898 * be correct: "foo.bar~"). Because a file can be accessed by
6899 * either its SFN or its LFN, "foo.bar" has effectively been
6900 * renamed to "foo.bar", which is not at all what was wanted. This
6901 * seems to happen only when renaming files with three-character
6902 * extensions by appending a suffix that does not include ".".
6903 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6904 *
6905 * There is another problem, which isn't really a bug but isn't right either:
6906 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6907 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6908 * service pack 6. Doesn't seem to happen on Windows 98.
6909 *
6910 * Like rename(), returns 0 upon success, non-zero upon failure.
6911 * Should probably set errno appropriately when errors occur.
6912 */
6913 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006914mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006916 WCHAR *p;
6917 int i;
6918 WCHAR szTempFile[_MAX_PATH + 1];
6919 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006922 // No need to play tricks unless the file name contains a "~" as the
6923 // seventh character.
6924 p = wold;
6925 for (i = 0; wold[i] != NUL; ++i)
6926 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6927 && wold[i + 1] != 0)
6928 p = wold + i + 1;
6929 if ((int)(wold + i - p) < 8 || p[6] != '~')
6930 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006932 // Get base path of new file name. Undocumented feature: If pszNewFile is
6933 // a directory, no error is returned and pszFilePart will be NULL.
6934 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006936 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006938 // Get (and create) a unique temporary file name in directory of new file
6939 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 return -2;
6941
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006942 // blow the temp file away
6943 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 return -3;
6945
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006946 // rename old file to the temp file
6947 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 return -4;
6949
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006950 // now create an empty file called pszOldFile; this prevents the operating
6951 // system using pszOldFile as an alias (SFN) if we're renaming within the
6952 // same directory. For example, we're editing a file called
6953 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6954 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6955 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6956 // cause all sorts of problems later in buf_write(). So, we create an
6957 // empty file called filena~1.txt and the system will have to find some
6958 // other SFN for filena~1.txt~, such as filena~2.txt
6959 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6961 return -5;
6962 if (!CloseHandle(hf))
6963 return -6;
6964
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006965 // rename the temp file to the new file
6966 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006968 // Renaming failed. Rename the file back to its old name, so that it
6969 // looks like nothing happened.
6970 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 return -7;
6972 }
6973
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006974 // Seems to be left around on Novell filesystems
6975 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006977 // finally, remove the empty old file
6978 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 return -8;
6980
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006981 return 0;
6982}
6983
6984
6985/*
6986 * Converts the filenames to UTF-16, then call mch_wrename().
6987 * Like rename(), returns 0 upon success, non-zero upon failure.
6988 */
6989 int
6990mch_rename(
6991 const char *pszOldFile,
6992 const char *pszNewFile)
6993{
6994 WCHAR *wold = NULL;
6995 WCHAR *wnew = NULL;
6996 int retval = -1;
6997
6998 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6999 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
7000 if (wold != NULL && wnew != NULL)
7001 retval = mch_wrename(wold, wnew);
7002 vim_free(wold);
7003 vim_free(wnew);
7004 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005}
7006
7007/*
7008 * Get the default shell for the current hardware platform
7009 */
7010 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007011default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007013 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
7016/*
7017 * mch_access() extends access() to do more detailed check on network drives.
7018 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
7019 */
7020 int
7021mch_access(char *n, int p)
7022{
7023 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007024 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007025 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007027 wn = enc_to_utf16((char_u *)n, NULL);
7028 if (wn == NULL)
7029 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01007031 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 if (p & R_OK)
7036 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007037 // Read check is performed by seeing if we can do a find file on
7038 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007039 int i;
7040 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007042 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
7043 TempNameW[i] = wn[i];
7044 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
7045 TempNameW[i++] = '\\';
7046 TempNameW[i++] = '*';
7047 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007049 hFile = FindFirstFileW(TempNameW, &d);
7050 if (hFile == INVALID_HANDLE_VALUE)
7051 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055
7056 if (p & W_OK)
7057 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007058 // Trying to create a temporary file in the directory should catch
7059 // directories on read-only network shares. However, in
7060 // directories whose ACL allows writes but denies deletes will end
7061 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007062 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
7063 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007064 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007065 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067 }
7068 else
7069 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007070 // Don't consider a file read-only if another process has opened it.
7071 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
7072
Bram Moolenaar0f873732019-12-05 20:28:46 +01007073 // Trying to open the file for the required access does ACL, read-only
7074 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007075 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
7076 | ((p & R_OK) ? GENERIC_READ : 0);
7077
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007078 hFile = CreateFileW(wn, access_mode, share_mode,
7079 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (hFile == INVALID_HANDLE_VALUE)
7081 goto getout;
7082 CloseHandle(hFile);
7083 }
7084
Bram Moolenaar0f873732019-12-05 20:28:46 +01007085 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 return retval;
7089}
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007092 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 */
7094 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007095mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 WCHAR *wn;
7098 int f;
7099
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007100 wn = enc_to_utf16((char_u *)name, NULL);
7101 if (wn == NULL)
7102 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007104 f = _wopen(wn, flags, mode);
7105 vim_free(wn);
7106 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107}
7108
7109/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007110 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 */
7112 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007113mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 WCHAR *wn, *wm;
7116 FILE *f = NULL;
7117
Bram Moolenaara12a1612019-01-24 16:39:02 +01007118#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01007119 // Work around an annoying assertion in the Microsoft debug CRT
7120 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007121 char newMode = mode[strlen(mode) - 1];
7122 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007123
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007124 _get_fmode(&oldMode);
7125 if (newMode == 't')
7126 _set_fmode(_O_TEXT);
7127 else if (newMode == 'b')
7128 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007129#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007130 wn = enc_to_utf16((char_u *)name, NULL);
7131 wm = enc_to_utf16((char_u *)mode, NULL);
7132 if (wn != NULL && wm != NULL)
7133 f = _wfopen(wn, wm);
7134 vim_free(wn);
7135 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007136
Bram Moolenaara12a1612019-01-24 16:39:02 +01007137#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007138 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007139#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007140 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143/*
7144 * SUB STREAM (aka info stream) handling:
7145 *
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007146 * NTFS can have sub streams for each file. The normal contents of a file is
7147 * stored in the main stream, and extra contents (author information, title and
7148 * so on) can be stored in a sub stream. After Windows 2000, the user can
7149 * access and store this information in sub streams via an explorer's property
7150 * menu item in the right click menu. This information in sub streams was lost
7151 * when copying only the main stream. Therefore we have to copy sub streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 *
7153 * Incomplete explanation:
7154 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
7155 * More useful info and an example:
7156 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
7157 */
7158
7159/*
7160 * Copy info stream data "substream". Read from the file with BackupRead(sh)
7161 * and write to stream "substream" of file "to".
7162 * Errors are ignored.
7163 */
7164 static void
7165copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
7166{
7167 HANDLE hTo;
7168 WCHAR *to_name;
7169
7170 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
7171 wcscpy(to_name, to);
7172 wcscat(to_name, substream);
7173
7174 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
7175 FILE_ATTRIBUTE_NORMAL, NULL);
7176 if (hTo != INVALID_HANDLE_VALUE)
7177 {
7178 long done;
7179 DWORD todo;
7180 DWORD readcnt, written;
7181 char buf[4096];
7182
Bram Moolenaar0f873732019-12-05 20:28:46 +01007183 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 for (done = 0; done < len; done += written)
7185 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007187 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
7188 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
7190 FALSE, FALSE, context)
7191 || readcnt != todo
7192 || !WriteFile(hTo, buf, todo, &written, NULL)
7193 || written != todo)
7194 break;
7195 }
7196 CloseHandle(hTo);
7197 }
7198
7199 free(to_name);
7200}
7201
7202/*
7203 * Copy info streams from file "from" to file "to".
7204 */
7205 static void
7206copy_infostreams(char_u *from, char_u *to)
7207{
7208 WCHAR *fromw;
7209 WCHAR *tow;
7210 HANDLE sh;
7211 WIN32_STREAM_ID sid;
7212 int headersize;
7213 WCHAR streamname[_MAX_PATH];
7214 DWORD readcount;
7215 void *context = NULL;
7216 DWORD lo, hi;
7217 int len;
7218
Bram Moolenaar0f873732019-12-05 20:28:46 +01007219 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007220 fromw = enc_to_utf16(from, NULL);
7221 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (fromw != NULL && tow != NULL)
7223 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007224 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
7226 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
7227 if (sh != INVALID_HANDLE_VALUE)
7228 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007229 // Use BackupRead() to find the info streams. Repeat until we
7230 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 for (;;)
7232 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007233 // Get the header to find the length of the stream name. If
7234 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
7238 &readcount, FALSE, FALSE, &context)
7239 || readcount == 0)
7240 break;
7241
Bram Moolenaar0f873732019-12-05 20:28:46 +01007242 // We only deal with streams that have a name. The normal
7243 // file data appears to be without a name, even though docs
7244 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (sid.dwStreamNameSize > 0)
7246 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007247 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (!BackupRead(sh, (LPBYTE)streamname,
7249 sid.dwStreamNameSize,
7250 &readcount, FALSE, FALSE, &context))
7251 break;
7252
Bram Moolenaar0f873732019-12-05 20:28:46 +01007253 // Copy an info stream with a name ":anything:$DATA".
7254 // Skip "::$DATA", it has no stream name (examples suggest
7255 // it might be used for the normal file contents).
7256 // Note that BackupRead() counts bytes, but the name is in
7257 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 len = readcount / sizeof(WCHAR);
7259 streamname[len] = 0;
7260 if (len > 7 && wcsicmp(streamname + len - 6,
7261 L":$DATA") == 0)
7262 {
7263 streamname[len - 6] = 0;
7264 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007265 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 }
7268
Bram Moolenaar0f873732019-12-05 20:28:46 +01007269 // Advance to the next stream. We might try seeking too far,
7270 // but BackupSeek() doesn't skip over stream borders, thus
7271 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007272 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 &lo, &hi, &context);
7274 }
7275
Bram Moolenaar0f873732019-12-05 20:28:46 +01007276 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
7278
7279 CloseHandle(sh);
7280 }
7281 }
7282 vim_free(fromw);
7283 vim_free(tow);
7284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
7286/*
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007287 * ntdll.dll definitions
7288 */
7289#define FileEaInformation 7
7290#ifndef STATUS_SUCCESS
7291# define STATUS_SUCCESS ((NTSTATUS) 0x00000000L)
7292#endif
7293
7294typedef struct _FILE_FULL_EA_INFORMATION_ {
7295 ULONG NextEntryOffset;
7296 UCHAR Flags;
7297 UCHAR EaNameLength;
7298 USHORT EaValueLength;
7299 CHAR EaName[1];
7300} FILE_FULL_EA_INFORMATION_, *PFILE_FULL_EA_INFORMATION_;
7301
7302typedef struct _FILE_EA_INFORMATION_ {
7303 ULONG EaSize;
7304} FILE_EA_INFORMATION_, *PFILE_EA_INFORMATION_;
7305
7306typedef NTSTATUS (NTAPI *PfnNtOpenFile)(
7307 PHANDLE FileHandle,
7308 ACCESS_MASK DesiredAccess,
7309 POBJECT_ATTRIBUTES ObjectAttributes,
7310 PIO_STATUS_BLOCK IoStatusBlock,
7311 ULONG ShareAccess,
7312 ULONG OpenOptions);
7313typedef NTSTATUS (NTAPI *PfnNtClose)(
7314 HANDLE Handle);
7315typedef NTSTATUS (NTAPI *PfnNtSetEaFile)(
7316 HANDLE FileHandle,
7317 PIO_STATUS_BLOCK IoStatusBlock,
7318 PVOID Buffer,
7319 ULONG Length);
7320typedef NTSTATUS (NTAPI *PfnNtQueryEaFile)(
7321 HANDLE FileHandle,
7322 PIO_STATUS_BLOCK IoStatusBlock,
7323 PVOID Buffer,
7324 ULONG Length,
7325 BOOLEAN ReturnSingleEntry,
7326 PVOID EaList,
7327 ULONG EaListLength,
7328 PULONG EaIndex,
7329 BOOLEAN RestartScan);
7330typedef NTSTATUS (NTAPI *PfnNtQueryInformationFile)(
7331 HANDLE FileHandle,
7332 PIO_STATUS_BLOCK IoStatusBlock,
7333 PVOID FileInformation,
7334 ULONG Length,
7335 FILE_INFORMATION_CLASS FileInformationClass);
7336typedef VOID (NTAPI *PfnRtlInitUnicodeString)(
7337 PUNICODE_STRING DestinationString,
7338 PCWSTR SourceString);
7339
7340PfnNtOpenFile pNtOpenFile = NULL;
7341PfnNtClose pNtClose = NULL;
7342PfnNtSetEaFile pNtSetEaFile = NULL;
7343PfnNtQueryEaFile pNtQueryEaFile = NULL;
7344PfnNtQueryInformationFile pNtQueryInformationFile = NULL;
7345PfnRtlInitUnicodeString pRtlInitUnicodeString = NULL;
7346
7347/*
7348 * Load ntdll.dll functions.
7349 */
7350 static BOOL
7351load_ntdll(void)
7352{
7353 static int loaded = -1;
7354
7355 if (loaded == -1)
7356 {
7357 HMODULE hNtdll = GetModuleHandle("ntdll.dll");
7358 if (hNtdll != NULL)
7359 {
7360 pNtOpenFile = (PfnNtOpenFile) GetProcAddress(hNtdll, "NtOpenFile");
7361 pNtClose = (PfnNtClose) GetProcAddress(hNtdll, "NtClose");
7362 pNtSetEaFile = (PfnNtSetEaFile)
7363 GetProcAddress(hNtdll, "NtSetEaFile");
7364 pNtQueryEaFile = (PfnNtQueryEaFile)
7365 GetProcAddress(hNtdll, "NtQueryEaFile");
7366 pNtQueryInformationFile = (PfnNtQueryInformationFile)
7367 GetProcAddress(hNtdll, "NtQueryInformationFile");
7368 pRtlInitUnicodeString = (PfnRtlInitUnicodeString)
7369 GetProcAddress(hNtdll, "RtlInitUnicodeString");
7370 }
7371 if (pNtOpenFile == NULL
7372 || pNtClose == NULL
7373 || pNtSetEaFile == NULL
7374 || pNtQueryEaFile == NULL
7375 || pNtQueryInformationFile == NULL
7376 || pRtlInitUnicodeString == NULL)
7377 loaded = FALSE;
7378 else
7379 loaded = TRUE;
7380 }
7381 return (BOOL) loaded;
7382}
7383
7384/*
7385 * Copy extended attributes (EA) from file "from" to file "to".
7386 */
7387 static void
7388copy_extattr(char_u *from, char_u *to)
7389{
7390 char_u *fromf = NULL;
7391 char_u *tof = NULL;
7392 WCHAR *fromw = NULL;
7393 WCHAR *tow = NULL;
7394 UNICODE_STRING u;
7395 HANDLE h;
7396 OBJECT_ATTRIBUTES oa;
7397 IO_STATUS_BLOCK iosb;
7398 FILE_EA_INFORMATION_ eainfo = {0};
7399 void *ea = NULL;
7400
7401 if (!load_ntdll())
7402 return;
7403
7404 // Convert the file names to the fully qualified object names.
7405 fromf = alloc(STRLEN(from) + 5);
7406 tof = alloc(STRLEN(to) + 5);
7407 if (fromf == NULL || tof == NULL)
7408 goto theend;
7409 STRCPY(fromf, "\\??\\");
7410 STRCAT(fromf, from);
7411 STRCPY(tof, "\\??\\");
7412 STRCAT(tof, to);
7413
7414 // Convert the names to wide characters.
7415 fromw = enc_to_utf16(fromf, NULL);
7416 tow = enc_to_utf16(tof, NULL);
7417 if (fromw == NULL || tow == NULL)
7418 goto theend;
7419
7420 // Get the EA.
7421 pRtlInitUnicodeString(&u, fromw);
7422 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
7423 if (pNtOpenFile(&h, FILE_READ_EA, &oa, &iosb, 0,
7424 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
7425 goto theend;
7426 pNtQueryInformationFile(h, &iosb, &eainfo, sizeof(eainfo),
7427 FileEaInformation);
7428 if (eainfo.EaSize != 0)
7429 {
7430 ea = alloc(eainfo.EaSize);
7431 if (ea != NULL)
7432 {
7433 if (pNtQueryEaFile(h, &iosb, ea, eainfo.EaSize, FALSE,
7434 NULL, 0, NULL, TRUE) != STATUS_SUCCESS)
7435 {
7436 vim_free(ea);
7437 ea = NULL;
7438 }
7439 }
7440 }
7441 pNtClose(h);
7442
7443 // Set the EA.
7444 if (ea != NULL)
7445 {
7446 pRtlInitUnicodeString(&u, tow);
7447 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
7448 if (pNtOpenFile(&h, FILE_WRITE_EA, &oa, &iosb, 0,
7449 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
7450 goto theend;
7451
7452 pNtSetEaFile(h, &iosb, ea, eainfo.EaSize);
7453 pNtClose(h);
7454 }
7455
7456theend:
7457 vim_free(fromf);
7458 vim_free(tof);
7459 vim_free(fromw);
7460 vim_free(tow);
7461 vim_free(ea);
7462}
7463
7464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 * Copy file attributes from file "from" to file "to".
7466 * For Windows NT and later we copy info streams.
7467 * Always returns zero, errors are ignored.
7468 */
7469 int
7470mch_copy_file_attribute(char_u *from, char_u *to)
7471{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007472 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007473 copy_infostreams(from, to);
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007474 copy_extattr(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 return 0;
7476}
7477
7478#if defined(MYRESETSTKOFLW) || defined(PROTO)
7479/*
7480 * Recreate a destroyed stack guard page in win32.
7481 * Written by Benjamin Peterson.
7482 */
7483
Bram Moolenaar0f873732019-12-05 20:28:46 +01007484// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007485# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
7487/*
7488 * This function does the same thing as _resetstkoflw(), which is only
7489 * available in DevStudio .net and later.
7490 * Returns 0 for failure, 1 for success.
7491 */
7492 int
7493myresetstkoflw(void)
7494{
7495 BYTE *pStackPtr;
7496 BYTE *pGuardPage;
7497 BYTE *pStackBase;
7498 BYTE *pLowestPossiblePage;
7499 MEMORY_BASIC_INFORMATION mbi;
7500 SYSTEM_INFO si;
7501 DWORD nPageSize;
7502 DWORD dummy;
7503
Bram Moolenaar0f873732019-12-05 20:28:46 +01007504 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 GetSystemInfo(&si);
7506 nPageSize = si.dwPageSize;
7507
Bram Moolenaar0f873732019-12-05 20:28:46 +01007508 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 pStackPtr = (BYTE*)_alloca(1);
7510
Bram Moolenaar0f873732019-12-05 20:28:46 +01007511 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
7513 return 0;
7514 pStackBase = (BYTE*)mbi.AllocationBase;
7515
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007516 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01007517 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007518 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007521 // We want the first committed page in the stack Start at the stack
7522 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 BYTE *pBlock = pStackBase;
7524
Bram Moolenaara466c992005-07-09 21:03:22 +00007525 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {
7527 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
7528 return 0;
7529
7530 pBlock += mbi.RegionSize;
7531
7532 if (mbi.State & MEM_COMMIT)
7533 break;
7534 }
7535
Bram Moolenaar0f873732019-12-05 20:28:46 +01007536 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (mbi.Protect & PAGE_GUARD)
7538 return 1;
7539
Bram Moolenaar0f873732019-12-05 20:28:46 +01007540 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
7542 pGuardPage = pLowestPossiblePage;
7543 else
7544 pGuardPage = (BYTE*)mbi.BaseAddress;
7545
Bram Moolenaar0f873732019-12-05 20:28:46 +01007546 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7548 return 0;
7549
Bram Moolenaar0f873732019-12-05 20:28:46 +01007550 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7552 &dummy))
7553 return 0;
7554 }
7555
7556 return 1;
7557}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007560
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007561/*
7562 * The command line arguments in UCS2
7563 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007564static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007565static LPWSTR *ArglistW = NULL;
7566static int global_argc = 0;
7567static char **global_argv;
7568
Bram Moolenaar0f873732019-12-05 20:28:46 +01007569static int used_file_argc = 0; // last argument in global_argv[] used
7570 // for the argument list.
7571static int *used_file_indexes = NULL; // indexes in global_argv[] for
7572 // command line arguments added to
7573 // the argument list
7574static int used_file_count = 0; // nr of entries in used_file_indexes
7575static int used_file_literal = FALSE; // take file names literally
7576static int used_file_full_path = FALSE; // file name was full path
7577static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007578static int used_alist_count = 0;
7579
7580
7581/*
7582 * Get the command line arguments. Unicode version.
7583 * Returns argc. Zero when something fails.
7584 */
7585 int
7586get_cmd_argsW(char ***argvp)
7587{
7588 char **argv = NULL;
7589 int argc = 0;
7590 int i;
7591
Bram Moolenaar14993322014-09-09 12:25:33 +02007592 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007593 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7594 if (ArglistW != NULL)
7595 {
7596 argv = malloc((nArgsW + 1) * sizeof(char *));
7597 if (argv != NULL)
7598 {
7599 argc = nArgsW;
7600 argv[argc] = NULL;
7601 for (i = 0; i < argc; ++i)
7602 {
7603 int len;
7604
Bram Moolenaar0f873732019-12-05 20:28:46 +01007605 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007606 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007607 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007608 (LPSTR *)&argv[i], &len, 0, 0);
7609 if (argv[i] == NULL)
7610 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007611 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007612 while (i > 0)
7613 free(argv[--i]);
7614 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007615 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007616 argc = 0;
7617 }
7618 }
7619 }
7620 }
7621
7622 global_argc = argc;
7623 global_argv = argv;
7624 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007625 {
7626 if (used_file_indexes != NULL)
7627 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007628 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007629 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007630
7631 if (argvp != NULL)
7632 *argvp = argv;
7633 return argc;
7634}
7635
7636 void
7637free_cmd_argsW(void)
7638{
7639 if (ArglistW != NULL)
7640 {
7641 GlobalFree(ArglistW);
7642 ArglistW = NULL;
7643 }
7644}
7645
7646/*
7647 * Remember "name" is an argument that was added to the argument list.
7648 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7649 * is called.
7650 */
7651 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007652used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007653{
7654 int i;
7655
7656 if (used_file_indexes == NULL)
7657 return;
7658 for (i = used_file_argc + 1; i < global_argc; ++i)
7659 if (STRCMP(global_argv[i], name) == 0)
7660 {
7661 used_file_argc = i;
7662 used_file_indexes[used_file_count++] = i;
7663 break;
7664 }
7665 used_file_literal = literal;
7666 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007667 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007668}
7669
7670/*
7671 * Remember the length of the argument list as it was. If it changes then we
7672 * leave it alone when 'encoding' is set.
7673 */
7674 void
7675set_alist_count(void)
7676{
7677 used_alist_count = GARGCOUNT;
7678}
7679
7680/*
7681 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7682 * has been changed while starting up. Use the UCS-2 command line arguments
7683 * and convert them to 'encoding'.
7684 */
7685 void
7686fix_arg_enc(void)
7687{
7688 int i;
7689 int idx;
7690 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007691 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007692
Bram Moolenaar0f873732019-12-05 20:28:46 +01007693 // Safety checks:
7694 // - if argument count differs between the wide and non-wide argument
7695 // list, something must be wrong.
7696 // - the file name arguments must have been located.
7697 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007698 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007699 || ArglistW == NULL
7700 || used_file_indexes == NULL
7701 || used_file_count == 0
7702 || used_alist_count != GARGCOUNT)
7703 return;
7704
Bram Moolenaar0f873732019-12-05 20:28:46 +01007705 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007706 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007707 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007708 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007709 for (i = 0; i < GARGCOUNT; ++i)
7710 fnum_list[i] = GARGLIST[i].ae_fnum;
7711
Bram Moolenaar0f873732019-12-05 20:28:46 +01007712 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007713 alist_clear(&global_alist);
7714 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007715 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007716
7717 for (i = 0; i < used_file_count; ++i)
7718 {
7719 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007720 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007721 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007722 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007723 int literal = used_file_literal;
7724
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007725#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007726 // When using diff mode may need to concatenate file name to
7727 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007728 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7729 && !mch_isdir(alist_name(&GARGLIST[0])))
7730 {
7731 char_u *r;
7732
7733 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7734 if (r != NULL)
7735 {
7736 vim_free(str);
7737 str = r;
7738 }
7739 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007740#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007741 // Re-use the old buffer by renaming it. When not using literal
7742 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007743 if (used_file_literal)
7744 buf_set_name(fnum_list[i], str);
7745
Bram Moolenaar0f873732019-12-05 20:28:46 +01007746 // Check backtick literal. backtick literal is already expanded in
7747 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007748 if (literal == FALSE)
7749 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007750 size_t len = STRLEN(str);
7751
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007752 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7753 literal = TRUE;
7754 }
7755 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007756 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007757 }
7758
7759 if (!used_file_literal)
7760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007761 // Now expand wildcards in the arguments.
7762 // Temporarily add '(' and ')' to 'isfname'. These are valid
7763 // filename characters but are excluded from 'isfname' to make
7764 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7765 // Also, unset wildignore to not be influenced by this option.
7766 // The arguments specified in command-line should be kept even if
7767 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007768 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007769 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007770 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007771 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007772 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007773 }
7774
Bram Moolenaar0f873732019-12-05 20:28:46 +01007775 // If wildcard expansion failed, we are editing the first file of the
7776 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007777 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7778 {
7779 do_cmdline_cmd((char_u *)":rewind");
7780 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007781 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007782 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007783
7784 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007785}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007786
7787 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007788mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007789{
7790 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007791 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007792
Bram Moolenaar964b3742019-05-24 18:54:09 +02007793 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007794 if (envbuf == NULL)
7795 return -1;
7796
7797 sprintf((char *)envbuf, "%s=%s", var, value);
7798
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007799 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007800
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007801 vim_free(envbuf);
7802 if (p == NULL)
7803 return -1;
7804 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007805#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007806 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007807#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007808 // Unlike Un*x systems, we can free the string for _wputenv().
7809 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007810
7811 return 0;
7812}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007813
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007814/*
7815 * Support for 256 colors and 24-bit colors was added in Windows 10
7816 * version 1703 (Creators update).
7817 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007818#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7819
7820/*
7821 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007822 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007823 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007824#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007825
7826/*
7827 * ConPTY differences between versions, need different logic.
7828 * version 1903 (May 2019 update).
7829 */
7830#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7831
7832/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007833 * version 1909 (November 2019 update).
7834 */
7835#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7836
7837/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007838 * Stay ahead of the next update, and when it's done, fix this.
7839 * version ? (2020 update, temporarily use the build number of insider preview)
7840 */
7841#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
7842
7843/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007844 * Confirm until this version. Also the logic changes.
7845 * insider preview.
7846 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007847#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007848
7849/*
7850 * Not stable now.
7851 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007852#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007853
7854 static void
7855vtp_flag_init(void)
7856{
7857 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007858#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007859 DWORD mode;
7860 HANDLE out;
7861
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007862# ifdef VIMDLL
7863 if (!gui.in_use)
7864# endif
7865 {
7866 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007867
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007868 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7869 GetConsoleMode(out, &mode);
7870 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7871 if (SetConsoleMode(out, mode) == 0)
7872 vtp_working = 0;
7873 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007874#endif
7875
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007876 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007877 conpty_working = 1;
7878 if (ver >= CONPTY_STABLE_BUILD)
7879 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007880
Bram Moolenaar57da6982019-09-13 22:30:11 +02007881 if (ver <= CONPTY_INSIDER_BUILD)
7882 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007883 if (ver <= CONPTY_1909_BUILD)
7884 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007885 if (ver <= CONPTY_1903_BUILD)
7886 conpty_type = 2;
7887 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7888 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007889
7890 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
7891 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007892}
7893
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007894#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007895
7896 static void
7897vtp_init(void)
7898{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007899 HMODULE hKerneldll;
7900 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007901# ifdef FEAT_TERMGUICOLORS
7902 COLORREF fg, bg;
7903# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007904
Bram Moolenaar0f873732019-12-05 20:28:46 +01007905 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007906 hKerneldll = GetModuleHandle("kernel32.dll");
7907 if (hKerneldll != NULL)
7908 {
7909 pGetConsoleScreenBufferInfoEx =
7910 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7911 hKerneldll, "GetConsoleScreenBufferInfoEx");
7912 pSetConsoleScreenBufferInfoEx =
7913 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7914 hKerneldll, "SetConsoleScreenBufferInfoEx");
7915 if (pGetConsoleScreenBufferInfoEx != NULL
7916 && pSetConsoleScreenBufferInfoEx != NULL)
7917 has_csbiex = TRUE;
7918 }
7919
7920 csbi.cbSize = sizeof(csbi);
7921 if (has_csbiex)
7922 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007923 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7924 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007925 store_console_bg_rgb = save_console_bg_rgb;
7926 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007927
7928# ifdef FEAT_TERMGUICOLORS
7929 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7930 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7931 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7932 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7933 default_console_color_bg = bg;
7934 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007935# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007936
7937 set_console_color_rgb();
7938}
7939
7940 static void
7941vtp_exit(void)
7942{
Bram Moolenaardf543822020-01-30 11:53:59 +01007943 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007944}
7945
Bram Moolenaar06b7b582020-05-30 17:49:25 +02007946 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007947vtp_printf(
7948 char *format,
7949 ...)
7950{
7951 char_u buf[100];
7952 va_list list;
7953 DWORD result;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007954 int len;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007955
7956 va_start(list, format);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007957 len = vim_vsnprintf((char *)buf, 100, (char *)format, list);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007958 va_end(list);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007959 WriteConsoleA(g_hConOut, buf, (DWORD)len, &result, NULL);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007960 return (int)result;
7961}
7962
7963 static void
7964vtp_sgr_bulk(
7965 int arg)
7966{
7967 int args[1];
7968
7969 args[0] = arg;
7970 vtp_sgr_bulks(1, args);
7971}
7972
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007973#define FAST256(x) \
7974 if ((*p-- = "0123456789"[(n = x % 10)]) \
7975 && x >= 10 && (*p-- = "0123456789"[((m = x % 100) - n) / 10]) \
7976 && x >= 100 && (*p-- = "012"[((x & 0xff) - m) / 100]));
7977
7978#define FAST256CASE(x) \
7979 case x: \
7980 FAST256(newargs[x - 1]);
7981
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007982 static void
7983vtp_sgr_bulks(
7984 int argc,
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007985 int *args)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007986{
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007987#define MAXSGR 16
7988#define SGRBUFSIZE 2 + 4 * MAXSGR + 1 // '\033[' + SGR + 'm'
7989 char_u buf[SGRBUFSIZE];
7990 char_u *p;
7991 int in, out;
7992 int newargs[16];
7993 static int sgrfgr = -1, sgrfgg, sgrfgb;
7994 static int sgrbgr = -1, sgrbgg, sgrbgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007995
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007996 if (argc == 0)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007997 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007998 sgrfgr = sgrbgr = -1;
7999 vtp_printf("033[m");
8000 return;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008001 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008002
8003 in = out = 0;
8004 while (in < argc)
8005 {
8006 int s = args[in];
8007 int copylen = 1;
8008
8009 if (s == 38)
8010 {
8011 if (argc - in >= 5 && args[in + 1] == 2)
8012 {
8013 if (sgrfgr == args[in + 2] && sgrfgg == args[in + 3]
8014 && sgrfgb == args[in + 4])
8015 {
8016 in += 5;
8017 copylen = 0;
8018 }
8019 else
8020 {
8021 sgrfgr = args[in + 2];
8022 sgrfgg = args[in + 3];
8023 sgrfgb = args[in + 4];
8024 copylen = 5;
8025 }
8026 }
8027 else if (argc - in >= 3 && args[in + 1] == 5)
8028 {
8029 sgrfgr = -1;
8030 copylen = 3;
8031 }
8032 }
8033 else if (s == 48)
8034 {
8035 if (argc - in >= 5 && args[in + 1] == 2)
8036 {
8037 if (sgrbgr == args[in + 2] && sgrbgg == args[in + 3]
8038 && sgrbgb == args[in + 4])
8039 {
8040 in += 5;
8041 copylen = 0;
8042 }
8043 else
8044 {
8045 sgrbgr = args[in + 2];
8046 sgrbgg = args[in + 3];
8047 sgrbgb = args[in + 4];
8048 copylen = 5;
8049 }
8050 }
8051 else if (argc - in >= 3 && args[in + 1] == 5)
8052 {
8053 sgrbgr = -1;
8054 copylen = 3;
8055 }
8056 }
8057 else if (30 <= s && s <= 39)
8058 sgrfgr = -1;
8059 else if (90 <= s && s <= 97)
8060 sgrfgr = -1;
8061 else if (40 <= s && s <= 49)
8062 sgrbgr = -1;
8063 else if (100 <= s && s <= 107)
8064 sgrbgr = -1;
8065 else if (s == 0)
8066 sgrfgr = sgrbgr = -1;
8067
8068 while (copylen--)
8069 newargs[out++] = args[in++];
8070 }
8071
8072 p = &buf[sizeof(buf) - 1];
8073 *p-- = 'm';
8074
8075 switch (out)
8076 {
8077 int n, m;
8078 DWORD r;
8079
8080 FAST256CASE(16);
8081 *p-- = ';';
8082 FAST256CASE(15);
8083 *p-- = ';';
8084 FAST256CASE(14);
8085 *p-- = ';';
8086 FAST256CASE(13);
8087 *p-- = ';';
8088 FAST256CASE(12);
8089 *p-- = ';';
8090 FAST256CASE(11);
8091 *p-- = ';';
8092 FAST256CASE(10);
8093 *p-- = ';';
8094 FAST256CASE(9);
8095 *p-- = ';';
8096 FAST256CASE(8);
8097 *p-- = ';';
8098 FAST256CASE(7);
8099 *p-- = ';';
8100 FAST256CASE(6);
8101 *p-- = ';';
8102 FAST256CASE(5);
8103 *p-- = ';';
8104 FAST256CASE(4);
8105 *p-- = ';';
8106 FAST256CASE(3);
8107 *p-- = ';';
8108 FAST256CASE(2);
8109 *p-- = ';';
8110 FAST256CASE(1);
8111 *p-- = '[';
8112 *p = '\033';
8113 WriteConsoleA(g_hConOut, p, (DWORD)(&buf[SGRBUFSIZE] - p), &r, NULL);
8114 default:
8115 break;
8116 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008117}
8118
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008119 static void
8120wt_init(void)
8121{
8122 wt_working = (mch_getenv("WT_SESSION") != NULL);
8123}
8124
8125 int
8126use_wt(void)
8127{
8128 return USE_WT;
8129}
8130
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008131# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008132 static int
8133ctermtoxterm(
8134 int cterm)
8135{
Bram Moolenaar9894e392018-05-05 14:29:06 +02008136 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008137
8138 cterm_color2rgb(cterm, &r, &g, &b, &idx);
8139 return (((int)r << 16) | ((int)g << 8) | (int)b);
8140}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008141# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008142
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008143 static void
8144set_console_color_rgb(void)
8145{
8146# ifdef FEAT_TERMGUICOLORS
8147 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01008148 guicolor_T fg, bg;
8149 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008150
8151 if (!USE_VTP)
8152 return;
8153
Bram Moolenaara050b942019-12-02 21:35:31 +01008154 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
8155
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008156 if (USE_WT)
8157 {
8158 term_fg_rgb_color(fg);
8159 term_bg_rgb_color(bg);
8160 return;
8161 }
8162
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008163 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
8164 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
8165
8166 csbi.cbSize = sizeof(csbi);
8167 if (has_csbiex)
8168 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8169
8170 csbi.cbSize = sizeof(csbi);
8171 csbi.srWindow.Right += 1;
8172 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008173 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
8174 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008175 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
8176 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008177 if (has_csbiex)
8178 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8179# endif
8180}
8181
Bram Moolenaara050b942019-12-02 21:35:31 +01008182# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
8183 void
8184get_default_console_color(
8185 int *cterm_fg,
8186 int *cterm_bg,
8187 guicolor_T *gui_fg,
8188 guicolor_T *gui_bg)
8189{
8190 int id;
8191 guicolor_T guifg = INVALCOLOR;
8192 guicolor_T guibg = INVALCOLOR;
8193 int ctermfg = 0;
8194 int ctermbg = 0;
8195
8196 id = syn_name2id((char_u *)"Normal");
8197 if (id > 0 && p_tgc)
8198 syn_id2colors(id, &guifg, &guibg);
8199 if (guifg == INVALCOLOR)
8200 {
8201 ctermfg = -1;
8202 if (id > 0)
8203 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
8204 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
8205 : default_console_color_fg;
8206 cterm_normal_fg_gui_color = guifg;
8207 ctermfg = ctermfg < 0 ? 0 : ctermfg;
8208 }
8209 if (guibg == INVALCOLOR)
8210 {
8211 ctermbg = -1;
8212 if (id > 0)
8213 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
8214 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
8215 : default_console_color_bg;
8216 cterm_normal_bg_gui_color = guibg;
8217 ctermbg = ctermbg < 0 ? 0 : ctermbg;
8218 }
8219
8220 *cterm_fg = ctermfg;
8221 *cterm_bg = ctermbg;
8222 *gui_fg = guifg;
8223 *gui_bg = guibg;
8224}
8225# endif
8226
Bram Moolenaardf543822020-01-30 11:53:59 +01008227/*
8228 * Set the console colors to the original colors or the last set colors.
8229 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008230 static void
8231reset_console_color_rgb(void)
8232{
8233# ifdef FEAT_TERMGUICOLORS
8234 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8235
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008236 if (USE_WT)
8237 return;
8238
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008239 csbi.cbSize = sizeof(csbi);
8240 if (has_csbiex)
8241 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8242
8243 csbi.cbSize = sizeof(csbi);
8244 csbi.srWindow.Right += 1;
8245 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008246 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
8247 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
8248 if (has_csbiex)
8249 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8250# endif
8251}
8252
8253/*
8254 * Set the console colors to the original colors.
8255 */
8256 static void
8257restore_console_color_rgb(void)
8258{
8259# ifdef FEAT_TERMGUICOLORS
8260 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8261
8262 csbi.cbSize = sizeof(csbi);
8263 if (has_csbiex)
8264 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8265
8266 csbi.cbSize = sizeof(csbi);
8267 csbi.srWindow.Right += 1;
8268 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008269 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
8270 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008271 if (has_csbiex)
8272 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8273# endif
8274}
8275
8276 void
8277control_console_color_rgb(void)
8278{
8279 if (USE_VTP)
8280 set_console_color_rgb();
8281 else
8282 reset_console_color_rgb();
8283}
8284
8285 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008286use_vtp(void)
8287{
8288 return USE_VTP;
8289}
8290
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008291 int
8292is_term_win32(void)
8293{
8294 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
8295}
8296
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008297 int
8298has_vtp_working(void)
8299{
8300 return vtp_working;
8301}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008302
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01008303#endif
8304
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008305 int
8306has_conpty_working(void)
8307{
8308 return conpty_working;
8309}
8310
8311 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02008312get_conpty_type(void)
8313{
8314 return conpty_type;
8315}
8316
8317 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008318is_conpty_stable(void)
8319{
8320 return conpty_stable;
8321}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008322
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008323 int
8324get_conpty_fix_type(void)
8325{
8326 return conpty_fix_type;
8327}
8328
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008329#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008330 void
8331resize_console_buf(void)
8332{
8333 CONSOLE_SCREEN_BUFFER_INFO csbi;
8334 COORD coord;
8335 SMALL_RECT newsize;
8336
8337 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
8338 {
8339 coord.X = SRWIDTH(csbi.srWindow);
8340 coord.Y = SRHEIGHT(csbi.srWindow);
8341 SetConsoleScreenBufferSize(g_hConOut, coord);
8342
8343 newsize.Left = 0;
8344 newsize.Top = 0;
8345 newsize.Right = coord.X - 1;
8346 newsize.Bottom = coord.Y - 1;
8347 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
8348
8349 SetConsoleScreenBufferSize(g_hConOut, coord);
8350 }
8351}
8352#endif