blob: 6356076eb91fca60a1c07d2fecb7c57836a1a566 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * os_win32.c
11 *
12 * Used for both the console version and the Win32 GUI. A lot of code is for
Bram Moolenaar4f974752019-02-17 17:44:42 +010013 * the console version only, so there is a lot of "#ifndef FEAT_GUI_MSWIN".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * Win32 (Windows NT and Windows 95) system-dependent routines.
16 * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
17 * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
18 *
19 * George V. Reilly <george@reilly.org> wrote most of this.
20 * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0.
21 */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
Bram Moolenaar325b7a22004-07-05 15:58:32 +000025#ifdef FEAT_MZSCHEME
26# include "if_mzsch.h"
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#include <signal.h>
31#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010032
Bram Moolenaar0f873732019-12-05 20:28:46 +010033// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# include <process.h>
36#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38#undef chdir
39#ifdef __GNUC__
40# ifndef __MINGW32__
41# include <dirent.h>
42# endif
43#else
44# include <direct.h>
45#endif
46
Bram Moolenaar82881492012-11-20 16:53:39 +010047#ifndef PROTO
Bram Moolenaar4f974752019-02-17 17:44:42 +010048# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010049# include <shellapi.h>
50# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
52
Bram Moolenaarfb630902016-10-29 14:55:00 +020053#ifdef FEAT_JOB_CHANNEL
54# include <tlhelp32.h>
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef __MINGW32__
58# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
59# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
60# endif
61# ifndef RIGHTMOST_BUTTON_PRESSED
62# define RIGHTMOST_BUTTON_PRESSED 0x0002
63# endif
64# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
65# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
66# endif
67# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
68# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
69# endif
70# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
71# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
72# endif
73
74/*
75 * EventFlags
76 */
77# ifndef MOUSE_MOVED
78# define MOUSE_MOVED 0x0001
79# endif
80# ifndef DOUBLE_CLICK
81# define DOUBLE_CLICK 0x0002
82# endif
83#endif
84
Bram Moolenaar0f873732019-12-05 20:28:46 +010085// Record all output and all keyboard & mouse input
86// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88#ifdef MCH_WRITE_DUMP
89FILE* fdDump = NULL;
90#endif
91
92/*
93 * When generating prototypes for Win32 on Unix, these lines make the syntax
94 * errors disappear. They do not need to be correct.
95 */
96#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010097# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000099typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100typedef int ACCESS_MASK;
101typedef int BOOL;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100107typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HINSTANCE;
112typedef int HWND;
113typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200114typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115typedef int KEY_EVENT_RECORD;
116typedef int LOGFONT;
117typedef int LPBOOL;
118typedef int LPCTSTR;
119typedef int LPDWORD;
120typedef int LPSTR;
121typedef int LPTSTR;
122typedef int LPVOID;
123typedef int MOUSE_EVENT_RECORD;
124typedef int PACL;
125typedef int PDWORD;
126typedef int PHANDLE;
127typedef int PRINTDLG;
128typedef int PSECURITY_DESCRIPTOR;
129typedef int PSID;
130typedef int SECURITY_INFORMATION;
131typedef int SHORT;
132typedef int SMALL_RECT;
133typedef int TEXTMETRIC;
134typedef int TOKEN_INFORMATION_CLASS;
135typedef int TRUSTEE;
136typedef int WORD;
137typedef int WCHAR;
138typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100139typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200140typedef int SE_OBJECT_TYPE;
141typedef int PSNSECINFO;
142typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100143typedef int STARTUPINFO;
144typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200145typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
148
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200149#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100150// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
152static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
153
Bram Moolenaar0f873732019-12-05 20:28:46 +0100154// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar0f873732019-12-05 20:28:46 +0100158// The attribute of the screen when the editor was started
159static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static WORD g_attrCurrent;
161
Bram Moolenaar0f873732019-12-05 20:28:46 +0100162static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
163static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
164static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static void scroll(unsigned cLines);
167static void set_scroll_region(unsigned left, unsigned top,
168 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100169static void set_scroll_region_tb(unsigned top, unsigned bottom);
170static void set_scroll_region_lr(unsigned left, unsigned right);
171static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void delete_lines(unsigned cLines);
173static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int s_cursor_visible = TRUE;
176static int did_create_conin = FALSE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200177#endif
178#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static int s_dont_use_vimrun = TRUE;
180static int need_vimrun_warning = FALSE;
181static char *vimrun_path = "vimrun ";
182#endif
183
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200184static int win32_getattrs(char_u *name);
185static int win32_setattrs(char_u *name, int attrs);
186static int win32_set_archive(char_u *name);
187
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100188static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200189static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100190static int conpty_stable = 0;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +0200191static int conpty_fix_type = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100192static void vtp_flag_init();
193
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200194#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100195static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100196static void vtp_init();
197static void vtp_exit();
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100198static void vtp_sgr_bulk(int arg);
199static void vtp_sgr_bulks(int argc, int *argv);
200
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200201static int wt_working = 0;
202static void wt_init();
203
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100204static guicolor_T save_console_bg_rgb;
205static guicolor_T save_console_fg_rgb;
Bram Moolenaardf543822020-01-30 11:53:59 +0100206static guicolor_T store_console_bg_rgb;
207static guicolor_T store_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100208
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200209static int g_color_index_bg = 0;
210static int g_color_index_fg = 7;
211
212# ifdef FEAT_TERMGUICOLORS
213static int default_console_color_bg = 0x000000; // black
214static int default_console_color_fg = 0xc0c0c0; // white
215# endif
216
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100217# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200218# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200219# define USE_WT (wt_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100220# else
221# define USE_VTP 0
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200222# define USE_WT 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100223# endif
224
225static void set_console_color_rgb(void);
226static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100227static void restore_console_color_rgb(void);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100228#endif
229
Bram Moolenaar0f873732019-12-05 20:28:46 +0100230// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100231#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
232# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
233#endif
234
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200235#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100236static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#endif
238
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200239static char_u *exe_path = NULL;
240
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100241static BOOL win8_or_later = FALSE;
242
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200243# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
244# define UChar UnicodeChar
245# else
246# define UChar uChar.UnicodeChar
247# endif
248
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200249#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100250// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100251typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
252{
253 ULONG cbSize;
254 COORD dwSize;
255 COORD dwCursorPosition;
256 WORD wAttributes;
257 SMALL_RECT srWindow;
258 COORD dwMaximumWindowSize;
259 WORD wPopupAttributes;
260 BOOL bFullscreenSupported;
261 COLORREF ColorTable[16];
262} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
263typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
264static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
265typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
266static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
267static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100268#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100269
270/*
271 * Get version number including build number
272 */
273typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100274#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100275 (((major) << 24) | ((minor) << 16) | (build))
276
277 static DWORD
278get_build_number(void)
279{
280 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
281 HMODULE hNtdll;
282 PfnRtlGetVersion pRtlGetVersion;
283 DWORD ver = MAKE_VER(0, 0, 0);
284
285 hNtdll = GetModuleHandle("ntdll.dll");
286 if (hNtdll != NULL)
287 {
288 pRtlGetVersion =
289 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
290 pRtlGetVersion(&osver);
291 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
292 min(osver.dwMinorVersion, 255),
293 min(osver.dwBuildNumber, 32767));
294 }
295 return ver;
296}
297
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200298#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200299 static BOOL
300is_ambiwidth_event(
301 INPUT_RECORD *ir)
302{
303 return ir->EventType == KEY_EVENT
304 && ir->Event.KeyEvent.bKeyDown
305 && ir->Event.KeyEvent.wRepeatCount == 1
306 && ir->Event.KeyEvent.wVirtualKeyCode == 0x12
307 && ir->Event.KeyEvent.wVirtualScanCode == 0x38
308 && ir->Event.KeyEvent.UChar == 0
309 && ir->Event.KeyEvent.dwControlKeyState == 2;
310}
311
312 static void
313make_ambiwidth_event(
314 INPUT_RECORD *down,
315 INPUT_RECORD *up)
316{
317 down->Event.KeyEvent.wVirtualKeyCode = 0;
318 down->Event.KeyEvent.wVirtualScanCode = 0;
319 down->Event.KeyEvent.UChar = up->Event.KeyEvent.UChar;
320 down->Event.KeyEvent.dwControlKeyState = 0;
321}
322
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323/*
324 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100325 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100326 */
327 static BOOL
328read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100329 HANDLE hInput,
330 INPUT_RECORD *lpBuffer,
331 DWORD nLength,
332 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100333{
334 enum
335 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100336 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100337 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100338 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100339 static DWORD s_dwIndex = 0;
340 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100341 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100342 int head;
343 int tail;
344 int i;
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200345 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100346
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200347 if (nLength == -2)
348 return (s_dwMax > 0) ? TRUE : FALSE;
349
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100350 if (!win8_or_later)
351 {
352 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200353 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
354 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100355 }
356
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100357 if (s_dwMax == 0)
358 {
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200359 if (!USE_WT && nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200360 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200361 GetNumberOfConsoleInputEvents(hInput, &dwEvents);
362 if (dwEvents == 0 && nLength == -1)
363 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
364 ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents);
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100365 s_dwIndex = 0;
366 s_dwMax = dwEvents;
367 if (dwEvents == 0)
368 {
369 *lpEvents = 0;
370 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100371 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100372
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200373 for (i = s_dwIndex; i < (int)s_dwMax - 1; ++i)
374 if (is_ambiwidth_event(&s_irCache[i]))
375 make_ambiwidth_event(&s_irCache[i], &s_irCache[i + 1]);
376
Bram Moolenaardd415a62014-02-05 14:02:27 +0100377 if (s_dwMax > 1)
378 {
379 head = 0;
380 tail = s_dwMax - 1;
381 while (head != tail)
382 {
383 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
384 && s_irCache[head + 1].EventType
385 == WINDOW_BUFFER_SIZE_EVENT)
386 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100387 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100388 for (i = head; i < tail; ++i)
389 s_irCache[i] = s_irCache[i + 1];
390 --tail;
391 continue;
392 }
393 head++;
394 }
395 s_dwMax = tail + 1;
396 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100397 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100398
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200399 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
400 {
401 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
402 {
403 s_irPseudo = s_irCache[s_dwIndex];
404 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
405 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
406 *lpBuffer = s_irPseudo;
407 *lpEvents = 1;
408 return TRUE;
409 }
410 }
411
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100412 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200413 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100414 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100415 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100416 return TRUE;
417}
418
419/*
420 * Version of PeekConsoleInput() that works with IME.
421 */
422 static BOOL
423peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100424 HANDLE hInput,
425 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200426 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100427 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100428{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100429 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100430}
431
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100432# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200433 static DWORD
434msg_wait_for_multiple_objects(
435 DWORD nCount,
436 LPHANDLE pHandles,
437 BOOL fWaitAll,
438 DWORD dwMilliseconds,
439 DWORD dwWakeMask)
440{
441 if (read_console_input(NULL, NULL, -2, NULL))
442 return WAIT_OBJECT_0;
443 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
444 dwMilliseconds, dwWakeMask);
445}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100446# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200447
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100448# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200449 static DWORD
450wait_for_single_object(
451 HANDLE hHandle,
452 DWORD dwMilliseconds)
453{
454 if (read_console_input(NULL, NULL, -2, NULL))
455 return WAIT_OBJECT_0;
456 return WaitForSingleObject(hHandle, dwMilliseconds);
457}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100458# endif
459#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200460
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 static void
462get_exe_name(void)
463{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100464 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
465 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100466#define MAX_ENV_PATH_LEN 8192
467 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200468 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470 if (exe_name == NULL)
471 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100472 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100473 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (*temp != NUL)
475 exe_name = FullName_save((char_u *)temp, FALSE);
476 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000477
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200478 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000479 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200480 exe_path = vim_strnsave(exe_name,
481 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200482 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // Append our starting directory to $PATH, so that when doing
485 // "!xxd" it's found in our starting directory. Needed because
486 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200487 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100488 if (p == NULL
489 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200490 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100491 if (p == NULL || *p == NUL)
492 temp[0] = NUL;
493 else
494 {
495 STRCPY(temp, p);
496 STRCAT(temp, ";");
497 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200498 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100499 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200500 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200505/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100506 * Unescape characters in "p" that appear in "escaped".
507 */
508 static void
509unescape_shellxquote(char_u *p, char_u *escaped)
510{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100511 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100512 int n;
513
514 while (*p != NUL)
515 {
516 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
517 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100518 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100519 p += n;
520 l -= n;
521 }
522}
523
524/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200525 * Load library "name".
526 */
527 HINSTANCE
528vimLoadLib(char *name)
529{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200530 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200531
Bram Moolenaar0f873732019-12-05 20:28:46 +0100532 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
533 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200534 if (exe_path == NULL)
535 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200536 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200537 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200538 WCHAR old_dirw[MAXPATHL];
539
540 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
541 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100542 // Change directory to where the executable is, both to make
543 // sure we find a .dll there and to avoid looking for a .dll
544 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100545 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200546 dll = LoadLibrary(name);
547 SetCurrentDirectoryW(old_dirw);
548 return dll;
549 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200550 }
551 return dll;
552}
553
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200554#if defined(VIMDLL) || defined(PROTO)
555/*
556 * Check if the current executable file is for the GUI subsystem.
557 */
558 int
559mch_is_gui_executable(void)
560{
561 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
562 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
563 PIMAGE_NT_HEADERS pPE;
564
565 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
566 return FALSE;
567 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
568 if (pPE->Signature != IMAGE_NT_SIGNATURE)
569 return FALSE;
570 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
571 return TRUE;
572 return FALSE;
573}
574#endif
575
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100576#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
577/*
578 * Get related information about 'funcname' which is imported by 'hInst'.
579 * If 'info' is 0, return the function address.
580 * If 'info' is 1, return the module name which the function is imported from.
581 */
582 static void *
583get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
584{
585 PBYTE pImage = (PBYTE)hInst;
586 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
587 PIMAGE_NT_HEADERS pPE;
588 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100589 PIMAGE_THUNK_DATA pIAT; // Import Address Table
590 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100591 PIMAGE_IMPORT_BY_NAME pImpName;
592
593 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
594 return NULL;
595 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
596 if (pPE->Signature != IMAGE_NT_SIGNATURE)
597 return NULL;
598 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
599 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
600 .VirtualAddress);
601 for (; pImpDesc->FirstThunk; ++pImpDesc)
602 {
603 if (!pImpDesc->OriginalFirstThunk)
604 continue;
605 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
606 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
607 for (; pIAT->u1.Function; ++pIAT, ++pINT)
608 {
609 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
610 continue;
611 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
612 + (UINT_PTR)(pINT->u1.AddressOfData));
613 if (strcmp((char *)pImpName->Name, funcname) == 0)
614 {
615 switch (info)
616 {
617 case 0:
618 return (void *)pIAT->u1.Function;
619 case 1:
620 return (void *)(pImage + pImpDesc->Name);
621 default:
622 return NULL;
623 }
624 }
625 }
626 }
627 return NULL;
628}
629
630/*
631 * Get the module handle which 'funcname' in 'hInst' is imported from.
632 */
633 HINSTANCE
634find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
635{
636 char *modulename;
637
638 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
639 if (modulename != NULL)
640 return GetModuleHandleA(modulename);
641 return NULL;
642}
643
644/*
645 * Get the address of 'funcname' which is imported by 'hInst' DLL.
646 */
647 void *
648get_dll_import_func(HINSTANCE hInst, const char *funcname)
649{
650 return get_imported_func_info(hInst, funcname, 0);
651}
652#endif
653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
655# ifndef GETTEXT_DLL
656# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200657# define GETTEXT_DLL_ALT1 "libintl-8.dll"
658# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100660// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000661static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200662static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000663static char *null_libintl_textdomain(const char *);
664static char *null_libintl_bindtextdomain(const char *, const char *);
665static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100666static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200668static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000669char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200670char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
671 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000672char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
673char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000675char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
676 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100677int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100680dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 int i;
683 static struct
684 {
685 char *name;
686 FARPROC *ptr;
687 } libintl_entry[] =
688 {
689 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200690 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
692 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
693 {NULL, NULL}
694 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100695 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar7554c542018-10-06 15:03:15 +0200697 // No need to initialize twice.
698 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200700 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100701 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100702# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100703 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200704 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100705# endif
706# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200707 if (!hLibintlDLL)
708 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100709# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100710 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200712 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200714 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200716 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200718 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 for (i = 0; libintl_entry[i].name != NULL
721 && libintl_entry[i].ptr != NULL; ++i)
722 {
723 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
724 libintl_entry[i].name)) == NULL)
725 {
726 dyn_libintl_end();
727 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000728 {
729 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100730 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000731 verbose_leave();
732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return 0;
734 }
735 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000736
Bram Moolenaar0f873732019-12-05 20:28:46 +0100737 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000738 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000739 "bind_textdomain_codeset");
740 if (dyn_libintl_bind_textdomain_codeset == NULL)
741 dyn_libintl_bind_textdomain_codeset =
742 null_libintl_bind_textdomain_codeset;
743
Bram Moolenaar0f873732019-12-05 20:28:46 +0100744 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100745 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
746 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100747 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100748 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
749 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 return 1;
752}
753
754 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100755dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 if (hLibintlDLL)
758 FreeLibrary(hLibintlDLL);
759 hLibintlDLL = NULL;
760 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200761 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 dyn_libintl_textdomain = null_libintl_textdomain;
763 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000764 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100765 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766}
767
768 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000769null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 return (char*)msgid;
772}
773
774 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200775null_libintl_ngettext(
776 const char *msgid,
777 const char *msgid_plural,
778 unsigned long n)
779{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200780 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200781}
782
Bram Moolenaaree695f72016-08-03 22:08:45 +0200783 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100784null_libintl_bindtextdomain(
785 const char *domainname UNUSED,
786 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 return NULL;
789}
790
791 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100792null_libintl_bind_textdomain_codeset(
793 const char *domainname UNUSED,
794 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000795{
796 return NULL;
797}
798
799 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 return NULL;
803}
804
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200805 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100806null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100807{
808 return 0;
809}
810
Bram Moolenaar0f873732019-12-05 20:28:46 +0100811#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar0f873732019-12-05 20:28:46 +0100813// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815#ifndef VER_PLATFORM_WIN32_WINDOWS
816# define VER_PLATFORM_WIN32_WINDOWS 1
817#endif
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100820# ifndef PROTO
821# include <aclapi.h>
822# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200823# ifndef PROTECTED_DACL_SECURITY_INFORMATION
824# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
827
Bram Moolenaar27515922013-06-29 15:36:26 +0200828#ifdef HAVE_ACL
829/*
830 * Enables or disables the specified privilege.
831 */
832 static BOOL
833win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
834{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100835 BOOL bResult;
836 LUID luid;
837 HANDLE hToken;
838 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200839
840 if (!OpenProcessToken(GetCurrentProcess(),
841 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
842 return FALSE;
843
844 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
845 {
846 CloseHandle(hToken);
847 return FALSE;
848 }
849
Bram Moolenaar45500912014-07-09 20:51:07 +0200850 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200851 tokenPrivileges.Privileges[0].Luid = luid;
852 tokenPrivileges.Privileges[0].Attributes = bEnable ?
853 SE_PRIVILEGE_ENABLED : 0;
854
855 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
856 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
857
858 CloseHandle(hToken);
859
860 return bResult && GetLastError() == ERROR_SUCCESS;
861}
862#endif
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864/*
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200865 * Set "win8_or_later" and fill in "windowsVersion" if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 */
867 void
868PlatformId(void)
869{
870 static int done = FALSE;
871
872 if (!done)
873 {
874 OSVERSIONINFO ovi;
875
876 ovi.dwOSVersionInfoSize = sizeof(ovi);
877 GetVersionEx(&ovi);
878
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200879#ifdef FEAT_EVAL
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100880 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
881 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200882#endif
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100883 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
884 || ovi.dwMajorVersion > 6)
885 win8_or_later = TRUE;
886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100888 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200889 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890#endif
891 done = TRUE;
892 }
893}
894
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200895#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100897# define SHIFT (SHIFT_PRESSED)
898# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
899# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
900# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902
Bram Moolenaar0f873732019-12-05 20:28:46 +0100903// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
904// We map function keys to their ANSI terminal equivalents, as produced
905// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
906// ANSI key with a value >= '\300' is nonstandard, but provided anyway
907// so that the user can have access to all SHIFT-, CTRL-, and ALT-
908// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000910static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 WORD wVirtKey;
913 BOOL fAnsiKey;
914 int chAlone;
915 int chShift;
916 int chCtrl;
917 int chAlt;
918} VirtKeyMap[] =
919{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200920// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
922
923 { VK_F1, TRUE, ';', 'T', '^', 'h', },
924 { VK_F2, TRUE, '<', 'U', '_', 'i', },
925 { VK_F3, TRUE, '=', 'V', '`', 'j', },
926 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
927 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
928 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
929 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
930 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
931 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
932 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200933 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
934 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200936 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
937 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
938 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
939 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
940 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
941 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
942 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
943 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
944 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
945 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100946 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200948 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100950# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200951 // Most people don't have F13-F20, but what the hell...
952 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
953 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
954 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
955 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
956 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
957 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
958 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
959 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100960# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200961 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
962 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
963 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
964 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200966 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
967 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
968 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
969 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
970 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
971 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
972 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
973 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
974 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
975 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100976 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977};
978
979
Bram Moolenaar0f873732019-12-05 20:28:46 +0100980/*
981 * The return code indicates key code size.
982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000985 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 UINT uMods = pker->dwControlKeyState;
988 static int s_iIsDead = 0;
989 static WORD awAnsiCode[2];
990 static BYTE abKeystate[256];
991
992
993 if (s_iIsDead == 2)
994 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200995 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 s_iIsDead = 0;
997 return 1;
998 }
999
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001000 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 return 1;
1002
Bram Moolenaara80faa82020-04-12 19:37:17 +02001003 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar0f873732019-12-05 20:28:46 +01001005 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001006 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 if (uMods & SHIFT_PRESSED)
1009 abKeystate[VK_SHIFT] = 0x80;
1010 if (uMods & CAPSLOCK_ON)
1011 abKeystate[VK_CAPITAL] = 1;
1012
1013 if ((uMods & ALT_GR) == ALT_GR)
1014 {
1015 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
1016 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
1017 }
1018
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001019 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
1020 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001023 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 return s_iIsDead;
1026}
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028static BOOL g_fJustGotFocus = FALSE;
1029
1030/*
1031 * Decode a KEY_EVENT into one or two keystrokes
1032 */
1033 static BOOL
1034decode_key_event(
1035 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001036 WCHAR *pch,
1037 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001039 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 int i;
1042 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1043
1044 *pch = *pch2 = NUL;
1045 g_fJustGotFocus = FALSE;
1046
Bram Moolenaar0f873732019-12-05 20:28:46 +01001047 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (!pker->bKeyDown)
1049 return FALSE;
1050
Bram Moolenaar0f873732019-12-05 20:28:46 +01001051 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 switch (pker->wVirtualKeyCode)
1053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001054 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 case VK_SHIFT:
1056 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001057 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 return FALSE;
1059
1060 default:
1061 break;
1062 }
1063
Bram Moolenaar0f873732019-12-05 20:28:46 +01001064 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001065 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001067 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (pker->wVirtualKeyCode == '6')
1069 {
1070 *pch = Ctrl_HAT;
1071 return TRUE;
1072 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001073 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 else if (pker->wVirtualKeyCode == '2')
1075 {
1076 *pch = NUL;
1077 return TRUE;
1078 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001079 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 else if (pker->wVirtualKeyCode == 0xBD)
1081 {
1082 *pch = Ctrl__;
1083 return TRUE;
1084 }
1085 }
1086
Bram Moolenaar0f873732019-12-05 20:28:46 +01001087 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1089 {
1090 *pch = K_NUL;
1091 *pch2 = '\017';
1092 return TRUE;
1093 }
1094
1095 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1096 {
1097 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1098 {
1099 if (nModifs == 0)
1100 *pch = VirtKeyMap[i].chAlone;
1101 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1102 *pch = VirtKeyMap[i].chShift;
1103 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1104 *pch = VirtKeyMap[i].chCtrl;
1105 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1106 *pch = VirtKeyMap[i].chAlt;
1107
1108 if (*pch != 0)
1109 {
1110 if (VirtKeyMap[i].fAnsiKey)
1111 {
1112 *pch2 = *pch;
1113 *pch = K_NUL;
1114 }
1115
1116 return TRUE;
1117 }
1118 }
1119 }
1120
1121 i = win32_kbd_patch_key(pker);
1122
1123 if (i < 0)
1124 *pch = NUL;
1125 else
1126 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001127 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 if (pmodifiers != NULL)
1130 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001131 // Pass on the ALT key as a modifier, but only when not combined
1132 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1134 *pmodifiers |= MOD_MASK_ALT;
1135
Bram Moolenaar0f873732019-12-05 20:28:46 +01001136 // Pass on SHIFT only for special keys, because we don't know when
1137 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1139 *pmodifiers |= MOD_MASK_SHIFT;
1140
Bram Moolenaar0f873732019-12-05 20:28:46 +01001141 // Pass on CTRL only for non-special keys, because we don't know
1142 // when it's already included with the character. And not when
1143 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1145 && *pch >= 0x20 && *pch < 0x80)
1146 *pmodifiers |= MOD_MASK_CTRL;
1147 }
1148 }
1149
1150 return (*pch != NUL);
1151}
1152
Bram Moolenaar0f873732019-12-05 20:28:46 +01001153#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*
1157 * For the GUI the mouse handling is in gui_w32.c.
1158 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001159#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001161mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
1163}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001164#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165static int g_fMouseAvail = FALSE; // mouse present
1166static int g_fMouseActive = FALSE; // mouse enabled
1167static int g_nMouseClick = -1; // mouse status
1168static int g_xMouse; // mouse x coordinate
1169static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171/*
1172 * Enable or disable mouse input
1173 */
1174 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001175mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 DWORD cmodein;
1178
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001179# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001180 if (gui.in_use)
1181 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!g_fMouseAvail)
1184 return;
1185
1186 g_fMouseActive = on;
1187 GetConsoleMode(g_hConIn, &cmodein);
1188
1189 if (g_fMouseActive)
1190 cmodein |= ENABLE_MOUSE_INPUT;
1191 else
1192 cmodein &= ~ENABLE_MOUSE_INPUT;
1193
1194 SetConsoleMode(g_hConIn, cmodein);
1195}
1196
Bram Moolenaar157d8132018-03-06 17:09:20 +01001197
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001198# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001199/*
1200 * Called when 'balloonevalterm' changed.
1201 */
1202 void
1203mch_bevalterm_changed(void)
1204{
1205 mch_setmouse(g_fMouseActive);
1206}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001207# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209/*
1210 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1211 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1212 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1213 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1214 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1215 * and we return the mouse position in g_xMouse and g_yMouse.
1216 *
1217 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1218 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1219 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1220 *
1221 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1222 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1223 *
1224 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1225 * moves, even if it stays within the same character cell. We ignore
1226 * all MOUSE_MOVED messages if the position hasn't really changed, and
1227 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1228 * we're only interested in MOUSE_DRAG).
1229 *
1230 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1231 * 2-button mouses by pressing the left & right buttons simultaneously.
1232 * In practice, it's almost impossible to click both at the same time,
1233 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1234 * in such cases, if the user is clicking quickly.
1235 */
1236 static BOOL
1237decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001238 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 static int s_nOldButton = -1;
1241 static int s_nOldMouseClick = -1;
1242 static int s_xOldMouse = -1;
1243 static int s_yOldMouse = -1;
1244 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001245# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 static int s_cClicks = 1;
1249 static BOOL s_fReleased = TRUE;
1250 static DWORD s_dwLastClickTime = 0;
1251 static BOOL s_fNextIsMiddle = FALSE;
1252
Bram Moolenaar0f873732019-12-05 20:28:46 +01001253 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1256 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1257 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1258 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1259
1260 int nButton;
1261
1262 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1263 cButtons = 2;
1264
1265 if (!g_fMouseAvail || !g_fMouseActive)
1266 {
1267 g_nMouseClick = -1;
1268 return FALSE;
1269 }
1270
Bram Moolenaar0f873732019-12-05 20:28:46 +01001271 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (g_fJustGotFocus)
1273 {
1274 g_fJustGotFocus = FALSE;
1275 return FALSE;
1276 }
1277
Bram Moolenaar0f873732019-12-05 20:28:46 +01001278 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (g_nMouseClick != -1)
1280 return TRUE;
1281
1282 nButton = -1;
1283 g_xMouse = pmer->dwMousePosition.X;
1284 g_yMouse = pmer->dwMousePosition.Y;
1285
1286 if (pmer->dwEventFlags == MOUSE_MOVED)
1287 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001288 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1289 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1291 return FALSE;
1292 }
1293
Bram Moolenaar0f873732019-12-05 20:28:46 +01001294 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1296 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001297 nButton = MOUSE_RELEASE;
1298
Bram Moolenaar0f873732019-12-05 20:28:46 +01001299 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001301 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001302# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001303 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001304 if (p_bevalterm)
1305 nButton = MOUSE_DRAG;
1306 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001307# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001308 return FALSE;
1309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 s_fReleased = TRUE;
1312 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001313 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001315 // on a 2-button mouse, hold down left and right buttons
1316 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1319 {
1320 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1321
Bram Moolenaar0f873732019-12-05 20:28:46 +01001322 // if either left or right button only is pressed, see if the
1323 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (dwLR == LEFT || dwLR == RIGHT)
1325 {
1326 for (;;)
1327 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001328 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1330 != WAIT_OBJECT_0)
1331 break;
1332 else
1333 {
1334 DWORD cRecords = 0;
1335 INPUT_RECORD ir;
1336 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1337
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001338 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1341 || !(pmer2->dwButtonState & LEFT_RIGHT))
1342 break;
1343 else
1344 {
1345 if (pmer2->dwEventFlags != MOUSE_MOVED)
1346 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001347 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 return decode_mouse_event(pmer2);
1350 }
1351 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1352 s_yOldMouse == pmer2->dwMousePosition.Y)
1353 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001354 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001355 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar0f873732019-12-05 20:28:46 +01001357 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001358 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1361 break;
1362 }
1363 else
1364 break;
1365 }
1366 }
1367 }
1368 }
1369 }
1370
1371 if (s_fNextIsMiddle)
1372 {
1373 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1374 ? MOUSE_DRAG : MOUSE_MIDDLE;
1375 s_fNextIsMiddle = FALSE;
1376 }
1377 else if (cButtons == 2 &&
1378 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1379 {
1380 nButton = MOUSE_MIDDLE;
1381
1382 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1383 {
1384 s_fNextIsMiddle = TRUE;
1385 nButton = MOUSE_RELEASE;
1386 }
1387 }
1388 else if ((pmer->dwButtonState & LEFT) == LEFT)
1389 nButton = MOUSE_LEFT;
1390 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1391 nButton = MOUSE_MIDDLE;
1392 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1393 nButton = MOUSE_RIGHT;
1394
1395 if (! s_fReleased && ! s_fNextIsMiddle
1396 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1397 return FALSE;
1398
1399 s_fReleased = s_fNextIsMiddle;
1400 }
1401
1402 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1403 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001404 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (nButton != -1 && nButton != MOUSE_RELEASE)
1406 {
1407 DWORD dwCurrentTime = GetTickCount();
1408
1409 if (s_xOldMouse != g_xMouse
1410 || s_yOldMouse != g_yMouse
1411 || s_nOldButton != nButton
1412 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001413# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1417 {
1418 s_cClicks = 1;
1419 }
1420 else if (++s_cClicks > 4)
1421 {
1422 s_cClicks = 1;
1423 }
1424
1425 s_dwLastClickTime = dwCurrentTime;
1426 }
1427 }
1428 else if (pmer->dwEventFlags == MOUSE_MOVED)
1429 {
1430 if (nButton != -1 && nButton != MOUSE_RELEASE)
1431 nButton = MOUSE_DRAG;
1432
1433 s_cClicks = 1;
1434 }
1435
1436 if (nButton == -1)
1437 return FALSE;
1438
1439 if (nButton != MOUSE_RELEASE)
1440 s_nOldButton = nButton;
1441
1442 g_nMouseClick = nButton;
1443
1444 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1445 g_nMouseClick |= MOUSE_SHIFT;
1446 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1447 g_nMouseClick |= MOUSE_CTRL;
1448 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1449 g_nMouseClick |= MOUSE_ALT;
1450
1451 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1452 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1453
Bram Moolenaar0f873732019-12-05 20:28:46 +01001454 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (s_xOldMouse == g_xMouse
1456 && s_yOldMouse == g_yMouse
1457 && s_nOldMouseClick == g_nMouseClick)
1458 {
1459 g_nMouseClick = -1;
1460 return FALSE;
1461 }
1462
1463 s_xOldMouse = g_xMouse;
1464 s_yOldMouse = g_yMouse;
1465 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001466# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 s_nOldMouseClick = g_nMouseClick;
1470
1471 return TRUE;
1472}
1473
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001474#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476
1477#ifdef MCH_CURSOR_SHAPE
1478/*
1479 * Set the shape of the cursor.
1480 * 'thickness' can be from 1 (thin) to 99 (block)
1481 */
1482 static void
1483mch_set_cursor_shape(int thickness)
1484{
1485 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1486 ConsoleCursorInfo.dwSize = thickness;
1487 ConsoleCursorInfo.bVisible = s_cursor_visible;
1488
1489 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1490 if (s_cursor_visible)
1491 SetConsoleCursorPosition(g_hConOut, g_coord);
1492}
1493
1494 void
1495mch_update_cursor(void)
1496{
1497 int idx;
1498 int thickness;
1499
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001500# ifdef VIMDLL
1501 if (gui.in_use)
1502 return;
1503# endif
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /*
1506 * How the cursor is drawn depends on the current mode.
1507 */
1508 idx = get_shape_idx(FALSE);
1509
1510 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001511 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
1513 thickness = shape_table[idx].percentage;
1514 mch_set_cursor_shape(thickness);
1515}
1516#endif
1517
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001518#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519/*
1520 * Handle FOCUS_EVENT.
1521 */
1522 static void
1523handle_focus_event(INPUT_RECORD ir)
1524{
1525 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1526 ui_focus_change((int)g_fJustGotFocus);
1527}
1528
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001529static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531/*
1532 * Wait until console input from keyboard or mouse is available,
1533 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001534 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 * Return TRUE if something is available FALSE if not.
1536 */
1537 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001538WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 DWORD dwNow = 0, dwEndTime = 0;
1541 INPUT_RECORD ir;
1542 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001543 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001544# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001545 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001549 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 dwEndTime = GetTickCount() + msec;
1551 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001552 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 dwEndTime = INFINITE;
1554
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001555 // We need to loop until the end of the time period, because
1556 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 for (;;)
1558 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001559 // Only process messages when waiting.
1560 if (msec != 0)
1561 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001562# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001563 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001564# endif
1565# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001566 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001567# endif
1568# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001569 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001570# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001571 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001572
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001573 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001574# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001575 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 )
1578 return TRUE;
1579
1580 if (msec > 0)
1581 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001582 // If the specified wait time has passed, return. Beware that
1583 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001585 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 break;
1587 }
1588 if (msec != 0)
1589 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001590 DWORD dwWaitTime = dwEndTime - dwNow;
1591
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001592# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001593 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001594 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001595 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001596 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001597 // If there is readahead then parse_queued_messages() timed out
1598 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001599 if (channel_any_readahead())
1600 dwWaitTime = 10;
1601 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001602# endif
1603# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001604 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001605 // The 'balloonexpr' may indirectly invoke a callback while
1606 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001607 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001608# endif
1609# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001610 if (mzthreads_allowed() && p_mzq > 0
1611 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001612 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001613# endif
1614# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001615 // When waiting very briefly don't trigger timers.
1616 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001617 {
1618 long due_time;
1619
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001620 // Trigger timers and then get the time in msec until the next
1621 // one is due. Wait up to that time.
1622 due_time = check_due_timer();
1623 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001624 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001625 // timer may have used feedkeys().
1626 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001627 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001628 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1629 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001630 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001631# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001632 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001633# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001634 // Wait for either an event on the console input or a
1635 // message in the client-server window.
1636 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1637 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001638# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001639 wait_for_single_object(g_hConIn, dwWaitTime)
1640 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001641# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001642 )
1643 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645
1646 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001647 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001649# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (State & CMDLINE && msg_row == Rows - 1)
1651 {
1652 CONSOLE_SCREEN_BUFFER_INFO csbi;
1653
1654 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1655 {
1656 if (csbi.dwCursorPosition.Y != msg_row)
1657 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001658 // The screen is now messed up, must redraw the
1659 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 redraw_all_later(CLEAR);
1661 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1662 redrawcmd();
1663 }
1664 }
1665 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 if (cRecords > 0)
1669 {
1670 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1671 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001672# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001673 // Windows IME sends two '\n's with only one 'ENTER'. First:
1674 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001675 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1677 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001678 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 continue;
1680 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1683 NULL, FALSE))
1684 return TRUE;
1685 }
1686
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001687 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
1689 if (ir.EventType == FOCUS_EVENT)
1690 handle_focus_event(ir);
1691 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001692 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001693 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1694
Bram Moolenaar36698e32019-12-11 22:57:40 +01001695 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001696 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001697 if (dwSize.X != Columns || dwSize.Y != Rows)
1698 {
1699 CONSOLE_SCREEN_BUFFER_INFO csbi;
1700 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001701 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001702 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001703 if (dwSize.X != Columns || dwSize.Y != Rows)
1704 {
1705 ResizeConBuf(g_hConOut, dwSize);
1706 shell_resized();
1707 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001708 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 else if (ir.EventType == MOUSE_EVENT
1711 && decode_mouse_event(&ir.Event.MouseEvent))
1712 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else if (msec == 0)
1715 break;
1716 }
1717
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001718# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001719 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (input_available())
1721 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001722# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 return FALSE;
1725}
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727/*
1728 * return non-zero if a character is available
1729 */
1730 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001731mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001733# ifdef VIMDLL
1734 if (gui.in_use)
1735 return TRUE;
1736# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001737 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001739
1740# if defined(FEAT_TERMINAL) || defined(PROTO)
1741/*
1742 * Check for any pending input or messages.
1743 */
1744 int
1745mch_check_messages(void)
1746{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001747# ifdef VIMDLL
1748 if (gui.in_use)
1749 return TRUE;
1750# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001751 return WaitForChar(0L, TRUE);
1752}
1753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755/*
1756 * Create the console input. Used when reading stdin doesn't work.
1757 */
1758 static void
1759create_conin(void)
1760{
1761 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1762 FILE_SHARE_READ|FILE_SHARE_WRITE,
1763 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001764 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 did_create_conin = TRUE;
1766}
1767
1768/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001769 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001771 static WCHAR
1772tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001774 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776 for (;;)
1777 {
1778 INPUT_RECORD ir;
1779 DWORD cRecords = 0;
1780
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001781# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001782 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (input_available())
1784 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (g_nMouseClick != -1)
1786 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001787# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001788 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 if (did_create_conin)
1791 read_error_exit();
1792 create_conin();
1793 continue;
1794 }
1795
1796 if (ir.EventType == KEY_EVENT)
1797 {
1798 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1799 pmodifiers, TRUE))
1800 return ch;
1801 }
1802 else if (ir.EventType == FOCUS_EVENT)
1803 handle_focus_event(ir);
1804 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1805 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 else if (ir.EventType == MOUSE_EVENT)
1807 {
1808 if (decode_mouse_event(&ir.Event.MouseEvent))
1809 return 0;
1810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001813#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815
1816/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001817 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 * Get one or more characters from the keyboard or the mouse.
1819 * If time == 0, do not wait for characters.
1820 * If time == n, wait a short time for characters.
1821 * If time == -1, wait forever for characters.
1822 * Returns the number of characters read into buf.
1823 */
1824 int
1825mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001826 char_u *buf UNUSED,
1827 int maxlen UNUSED,
1828 long time UNUSED,
1829 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001831#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 int len;
1834 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001835# ifdef VIMDLL
1836// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1837# define TYPEAHEADSPACE 6
1838# else
1839# define TYPEAHEADSPACE 0
1840# endif
1841# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001842 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 static int typeaheadlen = 0;
1844
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001845# ifdef VIMDLL
1846 if (gui.in_use)
1847 return 0;
1848# endif
1849
Bram Moolenaar0f873732019-12-05 20:28:46 +01001850 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (typeaheadlen > 0)
1852 goto theend;
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (time >= 0)
1855 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001856 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001859 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001861 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar3918c952005-03-15 22:34:55 +00001863 /*
1864 * If there is no character available within 2 seconds (default)
1865 * write the autoscript file to disk. Or cause the CursorHold event
1866 * to be triggered.
1867 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001868 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001870 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001872 buf[0] = K_SPECIAL;
1873 buf[1] = KS_EXTRA;
1874 buf[2] = (int)KE_CURSORHOLD;
1875 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001877 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 }
1880
1881 /*
1882 * Try to read as many characters as there are, until the buffer is full.
1883 */
1884
Bram Moolenaar0f873732019-12-05 20:28:46 +01001885 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 g_fCBrkPressed = FALSE;
1887
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001888# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (fdDump)
1890 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar0f873732019-12-05 20:28:46 +01001893 // Keep looping until there is something in the typeahead buffer and more
1894 // to get and still room in the buffer (up to two bytes for a char and
1895 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001896 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001897 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 if (typebuf_changed(tb_change_cnt))
1900 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001901 // "buf" may be invalid now if a client put something in the
1902 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 typeaheadlen = 0;
1904 break;
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (g_nMouseClick != -1)
1907 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001908# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (fdDump)
1910 fprintf(fdDump, "{%02x @ %d, %d}",
1911 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 typeahead[typeaheadlen++] = ESC + 128;
1914 typeahead[typeaheadlen++] = 'M';
1915 typeahead[typeaheadlen++] = g_nMouseClick;
1916 typeahead[typeaheadlen++] = g_xMouse + '!';
1917 typeahead[typeaheadlen++] = g_yMouse + '!';
1918 g_nMouseClick = -1;
1919 }
1920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001922 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int modifiers = 0;
1924
1925 c = tgetch(&modifiers, &ch2);
1926
1927 if (typebuf_changed(tb_change_cnt))
1928 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001929 // "buf" may be invalid now if a client put something in the
1930 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 typeaheadlen = 0;
1932 break;
1933 }
1934
1935 if (c == Ctrl_C && ctrl_c_interrupts)
1936 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001937# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 got_int = TRUE;
1941 }
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 int n = 1;
1946
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001947 if (ch2 == NUL)
1948 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001949 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001950 char_u *p;
1951 WCHAR ch[2];
1952
1953 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001954 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001955 {
1956 ch[1] = tgetch(&modifiers, &ch2);
1957 n++;
1958 }
1959 p = utf16_to_enc(ch, &n);
1960 if (p != NULL)
1961 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001962 for (i = 0, j = 0; i < n; i++)
1963 {
1964 typeahead[typeaheadlen + j++] = p[i];
1965# ifdef VIMDLL
1966 if (p[i] == CSI)
1967 {
1968 typeahead[typeaheadlen + j++] = KS_EXTRA;
1969 typeahead[typeaheadlen + j++] = KE_CSI;
1970 }
1971# endif
1972 }
1973 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001974 vim_free(p);
1975 }
1976 }
1977 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001978 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001979 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001980# ifdef VIMDLL
1981 if (c == CSI)
1982 {
1983 typeahead[typeaheadlen + 1] = KS_EXTRA;
1984 typeahead[typeaheadlen + 2] = KE_CSI;
1985 n = 3;
1986 }
1987# endif
1988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (ch2 != NUL)
1990 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001991 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001992 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001993 switch (ch2)
1994 {
1995 case (WCHAR)'\324': // SHIFT+Insert
1996 case (WCHAR)'\325': // CTRL+Insert
1997 case (WCHAR)'\327': // SHIFT+Delete
1998 case (WCHAR)'\330': // CTRL+Delete
1999 typeahead[typeaheadlen + n] = (char_u)ch2;
2000 n++;
2001 break;
2002
2003 default:
2004 typeahead[typeaheadlen + n] = 3;
2005 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2006 n += 2;
2007 break;
2008 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002009 }
2010 else
2011 {
2012 typeahead[typeaheadlen + n] = 3;
2013 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2014 n += 2;
2015 }
Bram Moolenaar45500912014-07-09 20:51:07 +02002016 }
2017
Bram Moolenaar0f873732019-12-05 20:28:46 +01002018 // Use the ALT key to set the 8th bit of the character
2019 // when it's one byte, the 8th bit isn't set yet and not
2020 // using a double-byte encoding (would become a lead
2021 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if ((modifiers & MOD_MASK_ALT)
2023 && n == 1
2024 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 )
2027 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00002028 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
2029 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 modifiers &= ~MOD_MASK_ALT;
2031 }
2032
2033 if (modifiers != 0)
2034 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002035 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 mch_memmove(typeahead + typeaheadlen + 3,
2037 typeahead + typeaheadlen, n);
2038 typeahead[typeaheadlen++] = K_SPECIAL;
2039 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2040 typeahead[typeaheadlen++] = modifiers;
2041 }
2042
2043 typeaheadlen += n;
2044
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002045# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (fdDump)
2047 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050 }
2051 }
2052
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002053# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if (fdDump)
2055 {
2056 fputs("]\n", fdDump);
2057 fflush(fdDump);
2058 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002062 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 len = 0;
2064 while (len < maxlen && typeaheadlen > 0)
2065 {
2066 buf[len++] = typeahead[0];
2067 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2068 }
2069 return len;
2070
Bram Moolenaar0f873732019-12-05 20:28:46 +01002071#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002073#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074}
2075
Bram Moolenaar82881492012-11-20 16:53:39 +01002076#ifndef PROTO
2077# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002078# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#endif
2081
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002082/*
Bram Moolenaar95da1362020-05-30 18:37:55 +02002083 * Return TRUE if "name" is an executable file, FALSE if not or it doesn't exist.
Bram Moolenaar43663192017-03-05 14:29:12 +01002084 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2085 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002086 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 static int
Bram Moolenaar95da1362020-05-30 18:37:55 +02002089executable_file(char *name, char_u **path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
Bram Moolenaar95da1362020-05-30 18:37:55 +02002091 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002092 {
Bram Moolenaar95da1362020-05-30 18:37:55 +02002093 if (path != NULL)
2094 *path = FullName_save((char_u *)name, FALSE);
2095 return TRUE;
2096 }
2097 return FALSE;
2098}
2099
2100/*
2101 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2102 * If "use_path" is FALSE: Return TRUE if "name" exists.
2103 * If "use_pathext" is TRUE search "name" with extensions in $PATHEXT.
2104 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2105 * the allocated memory.
2106 */
2107 static int
2108executable_exists(char *name, char_u **path, int use_path, int use_pathext)
2109{
2110 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
2111 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
2112 // UTF-8.
2113 char_u buf[_MAX_PATH * 3];
2114 size_t len = STRLEN(name);
2115 size_t tmplen;
2116 char_u *p, *e, *e2;
2117 char_u *pathbuf = NULL;
2118 char_u *pathext = NULL;
2119 char_u *pathextbuf = NULL;
2120 int noext = FALSE;
2121 int retval = FALSE;
2122
2123 if (len >= sizeof(buf)) // safety check
Bram Moolenaar43663192017-03-05 14:29:12 +01002124 return FALSE;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002125
2126 // Using the name directly when a Unix-shell like 'shell'.
2127 if (strstr((char *)gettail(p_sh), "sh") != NULL)
2128 noext = TRUE;
2129
2130 if (use_pathext)
2131 {
2132 pathext = mch_getenv("PATHEXT");
2133 if (pathext == NULL)
2134 pathext = (char_u *)".com;.exe;.bat;.cmd";
2135
2136 if (noext == FALSE)
2137 {
2138 /*
2139 * Loop over all extensions in $PATHEXT.
2140 * Check "name" ends with extension.
2141 */
2142 p = pathext;
2143 while (*p)
2144 {
2145 if (p[0] == ';'
2146 || (p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2147 {
2148 // Skip empty or single ".".
2149 ++p;
2150 continue;
2151 }
2152 e = vim_strchr(p, ';');
2153 if (e == NULL)
2154 e = p + STRLEN(p);
2155 tmplen = e - p;
2156
2157 if (_strnicoll(name + len - tmplen, (char *)p, tmplen) == 0)
2158 {
2159 noext = TRUE;
2160 break;
2161 }
2162
2163 p = e;
2164 }
2165 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002166 }
2167
Bram Moolenaar95da1362020-05-30 18:37:55 +02002168 // Prepend single "." to pathext, it's means no extension added.
2169 if (pathext == NULL)
2170 pathext = (char_u *)".";
2171 else if (noext == TRUE)
2172 {
2173 if (pathextbuf == NULL)
2174 pathextbuf = alloc(STRLEN(pathext) + 3);
2175 if (pathextbuf == NULL)
2176 {
2177 retval = FALSE;
2178 goto theend;
2179 }
2180 STRCPY(pathextbuf, ".;");
2181 STRCAT(pathextbuf, pathext);
2182 pathext = pathextbuf;
2183 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002184
Bram Moolenaar95da1362020-05-30 18:37:55 +02002185 // Use $PATH when "use_path" is TRUE and "name" is basename.
2186 if (use_path && gettail((char_u *)name) == (char_u *)name)
2187 {
2188 p = mch_getenv("PATH");
2189 if (p != NULL)
2190 {
2191 pathbuf = alloc(STRLEN(p) + 3);
2192 if (pathbuf == NULL)
2193 {
2194 retval = FALSE;
2195 goto theend;
2196 }
2197 STRCPY(pathbuf, ".;");
2198 STRCAT(pathbuf, p);
2199 }
2200 }
2201
2202 /*
2203 * Walk through all entries in $PATH to check if "name" exists there and
2204 * is an executable file.
2205 */
2206 p = (pathbuf != NULL) ? pathbuf : (char_u *)".";
2207 while (*p)
2208 {
2209 if (*p == ';') // Skip empty entry
2210 {
2211 ++p;
2212 continue;
2213 }
2214 e = vim_strchr(p, ';');
2215 if (e == NULL)
2216 e = p + STRLEN(p);
2217
2218 if (e - p + len + 2 > sizeof(buf))
2219 {
2220 retval = FALSE;
2221 goto theend;
2222 }
2223 // A single "." that means current dir.
2224 if (e - p == 1 && *p == '.')
2225 STRCPY(buf, name);
2226 else
2227 {
2228 vim_strncpy(buf, p, e - p);
2229 add_pathsep(buf);
2230 STRCAT(buf, name);
2231 }
2232 tmplen = STRLEN(buf);
2233
2234 /*
2235 * Loop over all extensions in $PATHEXT.
2236 * Check "name" with extension added.
2237 */
2238 p = pathext;
2239 while (*p)
2240 {
2241 if (*p == ';')
2242 {
2243 // Skip empty entry
2244 ++p;
2245 continue;
2246 }
2247 e2 = vim_strchr(p, (int)';');
2248 if (e2 == NULL)
2249 e2 = p + STRLEN(p);
2250
2251 if (!(p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2252 {
2253 // Not a single "." that means no extension is added.
2254 if (e2 - p + tmplen + 1 > sizeof(buf))
2255 {
2256 retval = FALSE;
2257 goto theend;
2258 }
2259 vim_strncpy(buf + tmplen, p, e2 - p);
2260 }
2261 if (executable_file((char *)buf, path))
2262 {
2263 retval = TRUE;
2264 goto theend;
2265 }
2266
2267 p = e2;
2268 }
2269
2270 p = e;
2271 }
2272
2273theend:
2274 free(pathextbuf);
2275 free(pathbuf);
2276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
2278
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002279#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2280 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002281/*
2282 * Bad parameter handler.
2283 *
2284 * Certain MS CRT functions will intentionally crash when passed invalid
2285 * parameters to highlight possible security holes. Setting this function as
2286 * the bad parameter handler will prevent the crash.
2287 *
2288 * In debug builds the parameters contain CRT information that might help track
2289 * down the source of a problem, but in non-debug builds the arguments are all
2290 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2291 * worth allowing these to make debugging of issues easier.
2292 */
2293 static void
2294bad_param_handler(const wchar_t *expression,
2295 const wchar_t *function,
2296 const wchar_t *file,
2297 unsigned int line,
2298 uintptr_t pReserved)
2299{
2300}
2301
2302# define SET_INVALID_PARAM_HANDLER \
2303 ((void)_set_invalid_parameter_handler(bad_param_handler))
2304#else
2305# define SET_INVALID_PARAM_HANDLER
2306#endif
2307
Bram Moolenaar4f974752019-02-17 17:44:42 +01002308#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310/*
2311 * GUI version of mch_init().
2312 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002313 static void
2314mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002316# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar0f873732019-12-05 20:28:46 +01002320 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002321 SET_INVALID_PARAM_HANDLER;
2322
Bram Moolenaar0f873732019-12-05 20:28:46 +01002323 // Let critical errors result in a failure, not in a dialog box. Required
2324 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 SetErrorMode(SEM_FAILCRITICALERRORS);
2326
Bram Moolenaar0f873732019-12-05 20:28:46 +01002327 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
Bram Moolenaar0f873732019-12-05 20:28:46 +01002329 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 Rows = 25;
2331 Columns = 80;
2332
Bram Moolenaar0f873732019-12-05 20:28:46 +01002333 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 char_u vimrun_location[_MAX_PATH + 4];
2336
Bram Moolenaar0f873732019-12-05 20:28:46 +01002337 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 STRCPY(vimrun_location, exe_name);
2339 STRCPY(gettail(vimrun_location), "vimrun.exe");
2340 if (mch_getperm(vimrun_location) >= 0)
2341 {
2342 if (*skiptowhite(vimrun_location) != NUL)
2343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 mch_memmove(vimrun_location + 1, vimrun_location,
2346 STRLEN(vimrun_location) + 1);
2347 *vimrun_location = '"';
2348 STRCPY(gettail(vimrun_location), "vimrun\" ");
2349 }
2350 else
2351 STRCPY(gettail(vimrun_location), "vimrun ");
2352
2353 vimrun_path = (char *)vim_strsave(vimrun_location);
2354 s_dont_use_vimrun = FALSE;
2355 }
Bram Moolenaar95da1362020-05-30 18:37:55 +02002356 else if (executable_exists("vimrun.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 s_dont_use_vimrun = FALSE;
2358
Bram Moolenaar0f873732019-12-05 20:28:46 +01002359 // Don't give the warning for a missing vimrun.exe right now, but only
2360 // when vimrun was supposed to be used. Don't bother people that do
2361 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (s_dont_use_vimrun)
2363 need_vimrun_warning = TRUE;
2364 }
2365
2366 /*
2367 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2368 * Otherwise the default "findstr /n" is used.
2369 */
Bram Moolenaar95da1362020-05-30 18:37:55 +02002370 if (!executable_exists("findstr.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2372
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002373# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002374 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002375# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002376
2377 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378}
2379
2380
Bram Moolenaar0f873732019-12-05 20:28:46 +01002381#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002382
2383#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002385# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2386# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388/*
2389 * ClearConsoleBuffer()
2390 * Description:
2391 * Clears the entire contents of the console screen buffer, using the
2392 * specified attribute.
2393 * Returns:
2394 * TRUE on success
2395 */
2396 static BOOL
2397ClearConsoleBuffer(WORD wAttribute)
2398{
2399 CONSOLE_SCREEN_BUFFER_INFO csbi;
2400 COORD coord;
2401 DWORD NumCells, dummy;
2402
2403 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2404 return FALSE;
2405
2406 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2407 coord.X = 0;
2408 coord.Y = 0;
2409 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2410 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2413 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 return TRUE;
2417}
2418
2419/*
2420 * FitConsoleWindow()
2421 * Description:
2422 * Checks if the console window will fit within given buffer dimensions.
2423 * Also, if requested, will shrink the window to fit.
2424 * Returns:
2425 * TRUE on success
2426 */
2427 static BOOL
2428FitConsoleWindow(
2429 COORD dwBufferSize,
2430 BOOL WantAdjust)
2431{
2432 CONSOLE_SCREEN_BUFFER_INFO csbi;
2433 COORD dwWindowSize;
2434 BOOL NeedAdjust = FALSE;
2435
2436 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2437 {
2438 /*
2439 * A buffer resize will fail if the current console window does
2440 * not lie completely within that buffer. To avoid this, we might
2441 * have to move and possibly shrink the window.
2442 */
2443 if (csbi.srWindow.Right >= dwBufferSize.X)
2444 {
2445 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2446 if (dwWindowSize.X > dwBufferSize.X)
2447 dwWindowSize.X = dwBufferSize.X;
2448 csbi.srWindow.Right = dwBufferSize.X - 1;
2449 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2450 NeedAdjust = TRUE;
2451 }
2452 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2453 {
2454 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2455 if (dwWindowSize.Y > dwBufferSize.Y)
2456 dwWindowSize.Y = dwBufferSize.Y;
2457 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2458 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2459 NeedAdjust = TRUE;
2460 }
2461 if (NeedAdjust && WantAdjust)
2462 {
2463 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2464 return FALSE;
2465 }
2466 return TRUE;
2467 }
2468
2469 return FALSE;
2470}
2471
2472typedef struct ConsoleBufferStruct
2473{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002474 BOOL IsValid;
2475 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002476 PCHAR_INFO Buffer;
2477 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002478 PSMALL_RECT Regions;
2479 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480} ConsoleBuffer;
2481
2482/*
2483 * SaveConsoleBuffer()
2484 * Description:
2485 * Saves important information about the console buffer, including the
2486 * actual buffer contents. The saved information is suitable for later
2487 * restoration by RestoreConsoleBuffer().
2488 * Returns:
2489 * TRUE if all information was saved; FALSE otherwise
2490 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2491 */
2492 static BOOL
2493SaveConsoleBuffer(
2494 ConsoleBuffer *cb)
2495{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002496 DWORD NumCells;
2497 COORD BufferCoord;
2498 SMALL_RECT ReadRegion;
2499 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002500 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (cb == NULL)
2503 return FALSE;
2504
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002505 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 cb->IsValid = FALSE;
2508 return FALSE;
2509 }
2510 cb->IsValid = TRUE;
2511
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002512 /*
2513 * Allocate a buffer large enough to hold the entire console screen
2514 * buffer. If this ConsoleBuffer structure has already been initialized
2515 * with a buffer of the correct size, then just use that one.
2516 */
2517 if (!cb->IsValid || cb->Buffer == NULL ||
2518 cb->BufferSize.X != cb->Info.dwSize.X ||
2519 cb->BufferSize.Y != cb->Info.dwSize.Y)
2520 {
2521 cb->BufferSize.X = cb->Info.dwSize.X;
2522 cb->BufferSize.Y = cb->Info.dwSize.Y;
2523 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2524 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002525 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002526 if (cb->Buffer == NULL)
2527 return FALSE;
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002531 * We will now copy the console screen buffer into our buffer.
2532 * ReadConsoleOutput() seems to be limited as far as how much you
2533 * can read at a time. Empirically, this number seems to be about
2534 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2535 * in chunks until it is all copied. The chunks will all have the
2536 * same horizontal characteristics, so initialize them now. The
2537 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002539 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002540 ReadRegion.Left = 0;
2541 ReadRegion.Right = cb->Info.dwSize.X - 1;
2542 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002543
2544 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2545 if (cb->Regions == NULL || numregions != cb->NumRegions)
2546 {
2547 cb->NumRegions = numregions;
2548 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002549 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002550 if (cb->Regions == NULL)
2551 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002552 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002553 return FALSE;
2554 }
2555 }
2556
2557 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002559 /*
2560 * Read into position (0, Y) in our buffer.
2561 */
2562 BufferCoord.Y = Y;
2563 /*
2564 * Read the region whose top left corner is (0, Y) and whose bottom
2565 * right corner is (width - 1, Y + Y_incr - 1). This should define
2566 * a region of size width by Y_incr. Don't worry if this region is
2567 * too large for the remaining buffer; it will be cropped.
2568 */
2569 ReadRegion.Top = Y;
2570 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 if (!ReadConsoleOutputW(g_hConOut, // output handle
2572 cb->Buffer, // our buffer
2573 cb->BufferSize, // dimensions of our buffer
2574 BufferCoord, // offset in our buffer
2575 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002576 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002577 VIM_CLEAR(cb->Buffer);
2578 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002579 return FALSE;
2580 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002581 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583
2584 return TRUE;
2585}
2586
2587/*
2588 * RestoreConsoleBuffer()
2589 * Description:
2590 * Restores important information about the console buffer, including the
2591 * actual buffer contents, if desired. The information to restore is in
2592 * the same format used by SaveConsoleBuffer().
2593 * Returns:
2594 * TRUE on success
2595 */
2596 static BOOL
2597RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002598 ConsoleBuffer *cb,
2599 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002601 COORD BufferCoord;
2602 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002603 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 if (cb == NULL || !cb->IsValid)
2606 return FALSE;
2607
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002608 /*
2609 * Before restoring the buffer contents, clear the current buffer, and
2610 * restore the cursor position and window information. Doing this now
2611 * prevents old buffer contents from "flashing" onto the screen.
2612 */
2613 if (RestoreScreen)
2614 ClearConsoleBuffer(cb->Info.wAttributes);
2615
2616 FitConsoleWindow(cb->Info.dwSize, TRUE);
2617 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2618 return FALSE;
2619 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2620 return FALSE;
2621
2622 if (!RestoreScreen)
2623 {
2624 /*
2625 * No need to restore the screen buffer contents, so we're done.
2626 */
2627 return TRUE;
2628 }
2629
2630 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2631 return FALSE;
2632 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2633 return FALSE;
2634
2635 /*
2636 * Restore the screen buffer contents.
2637 */
2638 if (cb->Buffer != NULL)
2639 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002640 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002641 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002642 BufferCoord.X = cb->Regions[i].Left;
2643 BufferCoord.Y = cb->Regions[i].Top;
2644 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002645 if (!WriteConsoleOutputW(g_hConOut, // output handle
2646 cb->Buffer, // our buffer
2647 cb->BufferSize, // dimensions of our buffer
2648 BufferCoord, // offset in our buffer
2649 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002650 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002651 }
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 return TRUE;
2655}
2656
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002657# define FEAT_RESTORE_ORIG_SCREEN
2658# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002659static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661static ConsoleBuffer g_cbNonTermcap = { 0 };
2662static ConsoleBuffer g_cbTermcap = { 0 };
2663
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002666HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667static HICON g_hOrigIconSmall = NULL;
2668static HICON g_hOrigIcon = NULL;
2669static HICON g_hVimIcon = NULL;
2670static BOOL g_fCanChangeIcon = FALSE;
2671
Bram Moolenaar0f873732019-12-05 20:28:46 +01002672// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002673# ifndef ICON_SMALL
2674# define ICON_SMALL 0
2675# endif
2676# ifndef ICON_BIG
2677# define ICON_BIG 1
2678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * GetConsoleIcon()
2681 * Description:
2682 * Attempts to retrieve the small icon and/or the big icon currently in
2683 * use by a given window.
2684 * Returns:
2685 * TRUE on success
2686 */
2687 static BOOL
2688GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002689 HWND hWnd,
2690 HICON *phIconSmall,
2691 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 if (hWnd == NULL)
2694 return FALSE;
2695
2696 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002697 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2698 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002700 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2701 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 return TRUE;
2703}
2704
2705/*
2706 * SetConsoleIcon()
2707 * Description:
2708 * Attempts to change the small icon and/or the big icon currently in
2709 * use by a given window.
2710 * Returns:
2711 * TRUE on success
2712 */
2713 static BOOL
2714SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002715 HWND hWnd,
2716 HICON hIconSmall,
2717 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (hWnd == NULL)
2720 return FALSE;
2721
2722 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002723 SendMessage(hWnd, WM_SETICON,
2724 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002726 SendMessage(hWnd, WM_SETICON,
2727 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return TRUE;
2729}
2730
2731/*
2732 * SaveConsoleTitleAndIcon()
2733 * Description:
2734 * Saves the current console window title in g_szOrigTitle, for later
2735 * restoration. Also, attempts to obtain a handle to the console window,
2736 * and use it to save the small and big icons currently in use by the
2737 * console window. This is not always possible on some versions of Windows;
2738 * nor is it possible when running Vim remotely using Telnet (since the
2739 * console window the user sees is owned by a remote process).
2740 */
2741 static void
2742SaveConsoleTitleAndIcon(void)
2743{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002744 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2746 return;
2747
2748 /*
2749 * Obtain a handle to the console window using GetConsoleWindow() from
2750 * KERNEL32.DLL; we need to handle in order to change the window icon.
2751 * This function only exists on NT-based Windows, starting with Windows
2752 * 2000. On older operating systems, we can't change the window icon
2753 * anyway.
2754 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002755 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (g_hWnd == NULL)
2757 return;
2758
Bram Moolenaar0f873732019-12-05 20:28:46 +01002759 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2761 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2762 return;
2763
Bram Moolenaar0f873732019-12-05 20:28:46 +01002764 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002765 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002766 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (g_hVimIcon != NULL)
2768 g_fCanChangeIcon = TRUE;
2769}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772static int g_fWindInitCalled = FALSE;
2773static int g_fTermcapMode = FALSE;
2774static CONSOLE_CURSOR_INFO g_cci;
2775static DWORD g_cmodein = 0;
2776static DWORD g_cmodeout = 0;
2777
2778/*
2779 * non-GUI version of mch_init().
2780 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002781 static void
2782mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002784# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002785 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002786# endif
2787# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
Bram Moolenaar0f873732019-12-05 20:28:46 +01002791 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002792 SET_INVALID_PARAM_HANDLER;
2793
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 // Let critical errors result in a failure, not in a dialog box. Required
2795 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 SetErrorMode(SEM_FAILCRITICALERRORS);
2797
Bram Moolenaar0f873732019-12-05 20:28:46 +01002798 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 out_flush();
2800
Bram Moolenaar0f873732019-12-05 20:28:46 +01002801 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (read_cmd_fd == 0)
2803 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2804 else
2805 create_conin();
2806 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2807
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002808# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002809 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002810 SaveConsoleBuffer(&g_cbOrig);
2811 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002812# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002813 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002814 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2815 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (cterm_normal_fg_color == 0)
2818 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2819 if (cterm_normal_bg_color == 0)
2820 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2821
Bram Moolenaarbdace832019-03-02 10:13:42 +01002822 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002823 g_color_index_fg = g_attrDefault & 0xf;
2824 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2825
Bram Moolenaar0f873732019-12-05 20:28:46 +01002826 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 update_tcap(g_attrCurrent);
2828
2829 GetConsoleCursorInfo(g_hConOut, &g_cci);
2830 GetConsoleMode(g_hConIn, &g_cmodein);
2831 GetConsoleMode(g_hConOut, &g_cmodeout);
2832
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002833# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 SaveConsoleTitleAndIcon();
2835 /*
2836 * Set both the small and big icons of the console window to Vim's icon.
2837 * Note that Vim presently only has one size of icon (32x32), but it
2838 * automatically gets scaled down to 16x16 when setting the small icon.
2839 */
2840 if (g_fCanChangeIcon)
2841 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 ui_get_shellsize();
2845
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002846# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 fdDump = fopen("dump", "wt");
2848
2849 if (fdDump)
2850 {
2851 time_t t;
2852
2853 time(&t);
2854 fputs(ctime(&t), fdDump);
2855 fflush(fdDump);
2856 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 g_fWindInitCalled = TRUE;
2860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002863# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002864 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002865# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002866
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002867 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002868 vtp_init();
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002869 wt_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871
2872/*
2873 * non-GUI version of mch_exit().
2874 * Shut down and exit with status `r'
2875 * Careful: mch_exit() may be called before mch_init()!
2876 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002877 static void
2878mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002880 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002882 vtp_exit();
2883
Bram Moolenaar955f1982017-02-05 15:10:51 +01002884 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (g_fWindInitCalled)
2886 settmode(TMODE_COOK);
2887
Bram Moolenaar0f873732019-12-05 20:28:46 +01002888 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
2890 if (g_fWindInitCalled)
2891 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002892# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002893 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 /*
2895 * Restore both the small and big icons of the console window to
2896 * what they were at startup. Don't do this when the window is
2897 * closed, Vim would hang here.
2898 */
2899 if (g_fCanChangeIcon && !g_fForceExit)
2900 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002903# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (fdDump)
2905 {
2906 time_t t;
2907
2908 time(&t);
2909 fputs(ctime(&t), fdDump);
2910 fclose(fdDump);
2911 }
2912 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915
2916 SetConsoleCursorInfo(g_hConOut, &g_cci);
2917 SetConsoleMode(g_hConIn, g_cmodein);
2918 SetConsoleMode(g_hConOut, g_cmodeout);
2919
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002920# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 exit(r);
2925}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002926#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002928 void
2929mch_init(void)
2930{
2931#ifdef VIMDLL
2932 if (gui.starting)
2933 mch_init_g();
2934 else
2935 mch_init_c();
2936#elif defined(FEAT_GUI_MSWIN)
2937 mch_init_g();
2938#else
2939 mch_init_c();
2940#endif
2941}
2942
2943 void
2944mch_exit(int r)
2945{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002946#ifdef FEAT_NETBEANS_INTG
2947 netbeans_send_disconnect();
2948#endif
2949
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002950#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002951 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002952 mch_exit_g(r);
2953 else
2954 mch_exit_c(r);
2955#elif defined(FEAT_GUI_MSWIN)
2956 mch_exit_g(r);
2957#else
2958 mch_exit_c(r);
2959#endif
2960}
2961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962/*
2963 * Do we have an interactive window?
2964 */
2965 int
2966mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002967 int argc UNUSED,
2968 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 get_exe_name();
2971
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002972#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002973 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002975# ifdef VIMDLL
2976 if (gui.in_use)
2977 return OK;
2978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (isatty(1))
2980 return OK;
2981 return FAIL;
2982#endif
2983}
2984
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002985/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002986 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 * When "len" is > 0, also expand short to long filenames.
2988 */
2989 void
2990fname_case(
2991 char_u *name,
2992 int len)
2993{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002994 int flen;
2995 WCHAR *p;
2996 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002998 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002999 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 return;
3001
3002 slash_adjust(name);
3003
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003004 p = enc_to_utf16(name, NULL);
3005 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003006 return;
3007
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003008 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003010 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003012 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003014 if (len > 0 || flen >= (int)STRLEN(q))
3015 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
3016 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003019 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022
3023/*
3024 * Insert user name in s[len].
3025 */
3026 int
3027mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003028 char_u *s,
3029 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003032 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003034 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003035 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003036 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003037
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003038 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003039 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003040 vim_strncpy(s, p, len - 1);
3041 vim_free(p);
3042 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003043 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 s[0] = NUL;
3046 return FAIL;
3047}
3048
3049
3050/*
3051 * Insert host name in s[len].
3052 */
3053 void
3054mch_get_host_name(
3055 char_u *s,
3056 int len)
3057{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003058 WCHAR wszHostName[256 + 1];
3059 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003061 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003062 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003063 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003064
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003065 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003066 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003067 vim_strncpy(s, p, len - 1);
3068 vim_free(p);
3069 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003070 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074
3075/*
3076 * return process ID
3077 */
3078 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003079mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 return (long)GetCurrentProcessId();
3082}
3083
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003084/*
3085 * return TRUE if process "pid" is still running
3086 */
3087 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02003088mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003089{
3090 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
3091 DWORD status = 0;
3092 int ret = FALSE;
3093
3094 if (hProcess == NULL)
3095 return FALSE; // might not have access
3096 if (GetExitCodeProcess(hProcess, &status) )
3097 ret = status == STILL_ACTIVE;
3098 CloseHandle(hProcess);
3099 return ret;
3100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102/*
3103 * Get name of current directory into buffer 'buf' of length 'len' bytes.
3104 * Return OK for success, FAIL for failure.
3105 */
3106 int
3107mch_dirname(
3108 char_u *buf,
3109 int len)
3110{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003111 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 /*
3114 * Originally this was:
3115 * return (getcwd(buf, len) != NULL ? OK : FAIL);
3116 * But the Win32s known bug list says that getcwd() doesn't work
3117 * so use the Win32 system call instead. <Negri>
3118 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003119 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003121 WCHAR wcbuf[_MAX_PATH + 1];
3122 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003124 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003126 p = utf16_to_enc(wcbuf, NULL);
3127 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02003128 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003129 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003131 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
3133 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003134 if (p == NULL)
3135 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003136
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003137 if (p != NULL)
3138 {
3139 vim_strncpy(buf, p, len - 1);
3140 vim_free(p);
3141 return OK;
3142 }
3143 }
3144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
3147/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01003148 * Get file permissions for "name".
3149 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003152mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003154 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01003155 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003157 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01003158 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161
3162/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003163 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003164 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003165 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
3167 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003168mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003170 long n;
3171 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003172
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003173 p = enc_to_utf16(name, NULL);
3174 if (p == NULL)
3175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003177 n = _wchmod(p, perm);
3178 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003179 if (n == -1)
3180 return FAIL;
3181
3182 win32_set_archive(name);
3183
3184 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
3187/*
3188 * Set hidden flag for "name".
3189 */
3190 void
3191mch_hide(char_u *name)
3192{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003193 int attrs = win32_getattrs(name);
3194 if (attrs == -1)
3195 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003197 attrs |= FILE_ATTRIBUTE_HIDDEN;
3198 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199}
3200
3201/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003202 * Return TRUE if file "name" exists and is hidden.
3203 */
3204 int
3205mch_ishidden(char_u *name)
3206{
3207 int f = win32_getattrs(name);
3208
3209 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003210 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003211
3212 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3213}
3214
3215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * return TRUE if "name" is a directory
3217 * return FALSE if "name" is not a directory or upon error
3218 */
3219 int
3220mch_isdir(char_u *name)
3221{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003222 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003225 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3228}
3229
3230/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003231 * return TRUE if "name" is a directory, NOT a symlink to a directory
3232 * return FALSE if "name" is not a directory
3233 * return FALSE for error
3234 */
3235 int
3236mch_isrealdir(char_u *name)
3237{
3238 return mch_isdir(name) && !mch_is_symbolic_link(name);
3239}
3240
3241/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003242 * Create directory "name".
3243 * Return 0 on success, -1 on error.
3244 */
3245 int
3246mch_mkdir(char_u *name)
3247{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003248 WCHAR *p;
3249 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003250
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003251 p = enc_to_utf16(name, NULL);
3252 if (p == NULL)
3253 return -1;
3254 retval = _wmkdir(p);
3255 vim_free(p);
3256 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003257}
3258
3259/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003260 * Delete directory "name".
3261 * Return 0 on success, -1 on error.
3262 */
3263 int
3264mch_rmdir(char_u *name)
3265{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003266 WCHAR *p;
3267 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003268
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003269 p = enc_to_utf16(name, NULL);
3270 if (p == NULL)
3271 return -1;
3272 retval = _wrmdir(p);
3273 vim_free(p);
3274 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003275}
3276
3277/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003278 * Return TRUE if file "fname" has more than one link.
3279 */
3280 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003281mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003282{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003283 BY_HANDLE_FILE_INFORMATION info;
3284
3285 return win32_fileinfo(fname, &info) == FILEINFO_OK
3286 && info.nNumberOfLinks > 1;
3287}
3288
3289/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003290 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003291 */
3292 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003293mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003294{
3295 HANDLE hFind;
3296 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003297 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003298 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003299 WIN32_FIND_DATAW findDataW;
3300
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003301 wn = enc_to_utf16(name, NULL);
3302 if (wn == NULL)
3303 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003304
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003305 hFind = FindFirstFileW(wn, &findDataW);
3306 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003307 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003308 {
3309 fileFlags = findDataW.dwFileAttributes;
3310 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003311 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003312 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003313
3314 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003315 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3316 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003317 res = TRUE;
3318
3319 return res;
3320}
3321
3322/*
3323 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3324 * link.
3325 */
3326 int
3327mch_is_linked(char_u *fname)
3328{
3329 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3330 return TRUE;
3331 return FALSE;
3332}
3333
3334/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003335 * Get the by-handle-file-information for "fname".
3336 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003337 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003338 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3339 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3340 */
3341 int
3342win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3343{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003344 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003345 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003346 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003347
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003348 wn = enc_to_utf16(fname, NULL);
3349 if (wn == NULL)
3350 return FILEINFO_ENC_FAIL;
3351
3352 hFile = CreateFileW(wn, // file name
3353 GENERIC_READ, // access mode
3354 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3355 NULL, // security descriptor
3356 OPEN_EXISTING, // creation disposition
3357 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3358 NULL); // handle to template file
3359 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003360
3361 if (hFile != INVALID_HANDLE_VALUE)
3362 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003363 if (GetFileInformationByHandle(hFile, info) != 0)
3364 res = FILEINFO_OK;
3365 else
3366 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003367 CloseHandle(hFile);
3368 }
3369
Bram Moolenaar03f48552006-02-28 23:52:23 +00003370 return res;
3371}
3372
3373/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003374 * get file attributes for `name'
3375 * -1 : error
3376 * else FILE_ATTRIBUTE_* defined in winnt.h
3377 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003378 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003379win32_getattrs(char_u *name)
3380{
3381 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003382 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003383
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003384 p = enc_to_utf16(name, NULL);
3385 if (p == NULL)
3386 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003388 attr = GetFileAttributesW(p);
3389 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003390
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003391 return attr;
3392}
3393
3394/*
3395 * set file attributes for `name' to `attrs'
3396 *
3397 * return -1 for failure, 0 otherwise
3398 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003399 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003400win32_setattrs(char_u *name, int attrs)
3401{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003402 int res;
3403 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003405 p = enc_to_utf16(name, NULL);
3406 if (p == NULL)
3407 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003409 res = SetFileAttributesW(p, attrs);
3410 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003411
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003412 return res ? 0 : -1;
3413}
3414
3415/*
3416 * Set archive flag for "name".
3417 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003418 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003419win32_set_archive(char_u *name)
3420{
3421 int attrs = win32_getattrs(name);
3422 if (attrs == -1)
3423 return -1;
3424
3425 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3426 return win32_setattrs(name, attrs);
3427}
3428
3429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * Return TRUE if file or directory "name" is writable (not readonly).
3431 * Strange semantics of Win32: a readonly directory is writable, but you can't
3432 * delete a file. Let's say this means it is writable.
3433 */
3434 int
3435mch_writable(char_u *name)
3436{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003437 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003439 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3440 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003444 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003445 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003446 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3447 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003450mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
Bram Moolenaar95da1362020-05-30 18:37:55 +02003452 return executable_exists((char *)name, path, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455/*
3456 * Check what "name" is:
3457 * NODE_NORMAL: file or directory (or doesn't exist)
3458 * NODE_WRITABLE: writable device, socket, fifo, etc.
3459 * NODE_OTHER: non-writable things
3460 */
3461 int
3462mch_nodetype(char_u *name)
3463{
3464 HANDLE hFile;
3465 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003466 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar0f873732019-12-05 20:28:46 +01003468 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3469 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3470 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003471 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3472 return NODE_WRITABLE;
3473
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003474 wn = enc_to_utf16(name, NULL);
3475 if (wn == NULL)
3476 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003477
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003478 hFile = CreateFileW(wn, // file name
3479 GENERIC_WRITE, // access mode
3480 0, // share mode
3481 NULL, // security descriptor
3482 OPEN_EXISTING, // creation disposition
3483 0, // file attributes
3484 NULL); // handle to template file
3485 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (hFile == INVALID_HANDLE_VALUE)
3487 return NODE_NORMAL;
3488
3489 type = GetFileType(hFile);
3490 CloseHandle(hFile);
3491 if (type == FILE_TYPE_CHAR)
3492 return NODE_WRITABLE;
3493 if (type == FILE_TYPE_DISK)
3494 return NODE_NORMAL;
3495 return NODE_OTHER;
3496}
3497
3498#ifdef HAVE_ACL
3499struct my_acl
3500{
3501 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3502 PSID pSidOwner;
3503 PSID pSidGroup;
3504 PACL pDacl;
3505 PACL pSacl;
3506};
3507#endif
3508
3509/*
3510 * Return a pointer to the ACL of file "fname" in allocated memory.
3511 * Return NULL if the ACL is not available for whatever reason.
3512 */
3513 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003514mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516#ifndef HAVE_ACL
3517 return (vim_acl_T)NULL;
3518#else
3519 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003520 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003522 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003523 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003525 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003526
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003527 wn = enc_to_utf16(fname, NULL);
3528 if (wn == NULL)
3529 return NULL;
3530
3531 // Try to retrieve the entire security descriptor.
3532 err = GetNamedSecurityInfoW(
3533 wn, // Abstract filename
3534 SE_FILE_OBJECT, // File Object
3535 OWNER_SECURITY_INFORMATION |
3536 GROUP_SECURITY_INFORMATION |
3537 DACL_SECURITY_INFORMATION |
3538 SACL_SECURITY_INFORMATION,
3539 &p->pSidOwner, // Ownership information.
3540 &p->pSidGroup, // Group membership.
3541 &p->pDacl, // Discretionary information.
3542 &p->pSacl, // For auditing purposes.
3543 &p->pSecurityDescriptor);
3544 if (err == ERROR_ACCESS_DENIED ||
3545 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003546 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003547 // Retrieve only DACL.
3548 (void)GetNamedSecurityInfoW(
3549 wn,
3550 SE_FILE_OBJECT,
3551 DACL_SECURITY_INFORMATION,
3552 NULL,
3553 NULL,
3554 &p->pDacl,
3555 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003556 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003557 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003558 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003559 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003560 mch_free_acl((vim_acl_T)p);
3561 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003563 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565
3566 return (vim_acl_T)p;
3567#endif
3568}
3569
Bram Moolenaar27515922013-06-29 15:36:26 +02003570#ifdef HAVE_ACL
3571/*
3572 * Check if "acl" contains inherited ACE.
3573 */
3574 static BOOL
3575is_acl_inherited(PACL acl)
3576{
3577 DWORD i;
3578 ACL_SIZE_INFORMATION acl_info;
3579 PACCESS_ALLOWED_ACE ace;
3580
3581 acl_info.AceCount = 0;
3582 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3583 for (i = 0; i < acl_info.AceCount; i++)
3584 {
3585 GetAce(acl, i, (LPVOID *)&ace);
3586 if (ace->Header.AceFlags & INHERITED_ACE)
3587 return TRUE;
3588 }
3589 return FALSE;
3590}
3591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593/*
3594 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3595 * Errors are ignored.
3596 * This must only be called with "acl" equal to what mch_get_acl() returned.
3597 */
3598 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003599mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601#ifdef HAVE_ACL
3602 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003603 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003604 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003606 if (p == NULL)
3607 return;
3608
3609 wn = enc_to_utf16(fname, NULL);
3610 if (wn == NULL)
3611 return;
3612
3613 // Set security flags
3614 if (p->pSidOwner)
3615 sec_info |= OWNER_SECURITY_INFORMATION;
3616 if (p->pSidGroup)
3617 sec_info |= GROUP_SECURITY_INFORMATION;
3618 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003619 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003620 sec_info |= DACL_SECURITY_INFORMATION;
3621 // Do not inherit its parent's DACL.
3622 // If the DACL is inherited, Cygwin permissions would be changed.
3623 if (!is_acl_inherited(p->pDacl))
3624 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003625 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003626 if (p->pSacl)
3627 sec_info |= SACL_SECURITY_INFORMATION;
3628
3629 (void)SetNamedSecurityInfoW(
3630 wn, // Abstract filename
3631 SE_FILE_OBJECT, // File Object
3632 sec_info,
3633 p->pSidOwner, // Ownership information.
3634 p->pSidGroup, // Group membership.
3635 p->pDacl, // Discretionary information.
3636 p->pSacl // For auditing purposes.
3637 );
3638 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640}
3641
3642 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003643mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645#ifdef HAVE_ACL
3646 struct my_acl *p = (struct my_acl *)acl;
3647
3648 if (p != NULL)
3649 {
3650 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3651 vim_free(p);
3652 }
3653#endif
3654}
3655
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003656#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658/*
3659 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3660 */
3661 static BOOL WINAPI
3662handler_routine(
3663 DWORD dwCtrlType)
3664{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003665 INPUT_RECORD ir;
3666 DWORD out;
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 switch (dwCtrlType)
3669 {
3670 case CTRL_C_EVENT:
3671 if (ctrl_c_interrupts)
3672 g_fCtrlCPressed = TRUE;
3673 return TRUE;
3674
3675 case CTRL_BREAK_EVENT:
3676 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003677 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003678 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003679 ir.EventType = KEY_EVENT;
3680 ir.Event.KeyEvent.bKeyDown = TRUE;
3681 ir.Event.KeyEvent.wRepeatCount = 1;
3682 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3683 ir.Event.KeyEvent.wVirtualScanCode = 0;
3684 ir.Event.KeyEvent.dwControlKeyState = 0;
3685 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3686 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return TRUE;
3688
Bram Moolenaar0f873732019-12-05 20:28:46 +01003689 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 case CTRL_CLOSE_EVENT:
3691 case CTRL_LOGOFF_EVENT:
3692 case CTRL_SHUTDOWN_EVENT:
3693 windgoto((int)Rows - 1, 0);
3694 g_fForceExit = TRUE;
3695
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003696 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 (dwCtrlType == CTRL_CLOSE_EVENT
3698 ? _("close")
3699 : dwCtrlType == CTRL_LOGOFF_EVENT
3700 ? _("logoff")
3701 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003702# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar0f873732019-12-05 20:28:46 +01003706 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
Bram Moolenaar0f873732019-12-05 20:28:46 +01003708 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 default:
3711 return FALSE;
3712 }
3713}
3714
3715
3716/*
3717 * set the tty in (raw) ? "raw" : "cooked" mode
3718 */
3719 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02003720mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 DWORD cmodein;
3723 DWORD cmodeout;
3724 BOOL bEnableHandler;
3725
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003726# ifdef VIMDLL
3727 if (gui.in_use)
3728 return;
3729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 GetConsoleMode(g_hConIn, &cmodein);
3731 GetConsoleMode(g_hConOut, &cmodeout);
3732 if (tmode == TMODE_RAW)
3733 {
3734 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3735 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (g_fMouseActive)
3737 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003738 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003739# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003740 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3741 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003742 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003743# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003744 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003745# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003746 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 bEnableHandler = TRUE;
3748 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003749 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3752 ENABLE_ECHO_INPUT);
3753 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3754 bEnableHandler = FALSE;
3755 }
3756 SetConsoleMode(g_hConIn, cmodein);
3757 SetConsoleMode(g_hConOut, cmodeout);
3758 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3759
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003760# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (fdDump)
3762 {
3763 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3764 tmode == TMODE_RAW ? "raw" :
3765 tmode == TMODE_COOK ? "cooked" : "normal",
3766 cmodein, cmodeout);
3767 fflush(fdDump);
3768 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770}
3771
3772
3773/*
3774 * Get the size of the current window in `Rows' and `Columns'
3775 * Return OK when size could be determined, FAIL otherwise.
3776 */
3777 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003778mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 CONSOLE_SCREEN_BUFFER_INFO csbi;
3781
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003782# ifdef VIMDLL
3783 if (gui.in_use)
3784 return OK;
3785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3787 {
3788 /*
3789 * For some reason, we are trying to get the screen dimensions
3790 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3791 * variables are really intended to mean the size of Vim screen
3792 * while in termcap mode.
3793 */
3794 Rows = g_cbTermcap.Info.dwSize.Y;
3795 Columns = g_cbTermcap.Info.dwSize.X;
3796 }
3797 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3798 {
3799 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3800 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3801 }
3802 else
3803 {
3804 Rows = 25;
3805 Columns = 80;
3806 }
3807 return OK;
3808}
3809
3810/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003811 * Resize console buffer to 'COORD'
3812 */
3813 static void
3814ResizeConBuf(
3815 HANDLE hConsole,
3816 COORD coordScreen)
3817{
3818 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3819 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003820# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003821 if (fdDump)
3822 {
3823 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3824 GetLastError());
3825 fflush(fdDump);
3826 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003827# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003828 }
3829}
3830
3831/*
3832 * Resize console window size to 'srWindowRect'
3833 */
3834 static void
3835ResizeWindow(
3836 HANDLE hConsole,
3837 SMALL_RECT srWindowRect)
3838{
3839 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3840 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003841# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003842 if (fdDump)
3843 {
3844 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3845 GetLastError());
3846 fflush(fdDump);
3847 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003848# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003849 }
3850}
3851
3852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * Set a console window to `xSize' * `ySize'
3854 */
3855 static void
3856ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003857 HANDLE hConsole,
3858 int xSize,
3859 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003861 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3862 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003864 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003865 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003867# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (fdDump)
3869 {
3870 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3871 fflush(fdDump);
3872 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaar0f873732019-12-05 20:28:46 +01003875 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 coordScreen = GetLargestConsoleWindowSize(hConsole);
3877
Bram Moolenaar0f873732019-12-05 20:28:46 +01003878 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3880 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3881 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3882
3883 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3884 {
3885 int sx, sy;
3886
3887 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3888 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3889 if (sy < ySize || sx < xSize)
3890 {
3891 /*
3892 * Increasing number of lines/columns, do buffer first.
3893 * Use the maximal size in x and y direction.
3894 */
3895 if (sy < ySize)
3896 coordScreen.Y = ySize;
3897 else
3898 coordScreen.Y = sy;
3899 if (sx < xSize)
3900 coordScreen.X = xSize;
3901 else
3902 coordScreen.X = sx;
3903 SetConsoleScreenBufferSize(hConsole, coordScreen);
3904 }
3905 }
3906
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003907 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 coordScreen.X = xSize;
3909 coordScreen.Y = ySize;
3910
Bram Moolenaar2551c032018-08-23 22:38:31 +02003911 // In the new console call API, only the first time in reverse order
3912 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003914 ResizeWindow(hConsole, srWindowRect);
3915 ResizeConBuf(hConsole, coordScreen);
3916 }
3917 else
3918 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003919 // Workaround for a Windows 10 bug
3920 cursor.X = srWindowRect.Left;
3921 cursor.Y = srWindowRect.Top;
3922 SetConsoleCursorPosition(hConsole, cursor);
3923
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003924 ResizeConBuf(hConsole, coordScreen);
3925 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003926 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928}
3929
3930
3931/*
3932 * Set the console window to `Rows' * `Columns'
3933 */
3934 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003935mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 COORD coordScreen;
3938
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003939# ifdef VIMDLL
3940 if (gui.in_use)
3941 return;
3942# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003943 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (suppress_winsize != 0)
3945 {
3946 suppress_winsize = 2;
3947 return;
3948 }
3949
3950 if (term_console)
3951 {
3952 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3953
Bram Moolenaar0f873732019-12-05 20:28:46 +01003954 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 if (Rows > coordScreen.Y)
3956 Rows = coordScreen.Y;
3957 if (Columns > coordScreen.X)
3958 Columns = coordScreen.X;
3959
3960 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3961 }
3962}
3963
3964/*
3965 * Rows and/or Columns has changed.
3966 */
3967 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003968mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003970# ifdef VIMDLL
3971 if (gui.in_use)
3972 return;
3973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3975}
3976
3977
3978/*
3979 * Called when started up, to set the winsize that was delayed.
3980 */
3981 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003982mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 if (suppress_winsize == 2)
3985 {
3986 suppress_winsize = 0;
3987 mch_set_shellsize();
3988 shell_resized();
3989 }
3990 suppress_winsize = 0;
3991}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003992#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003994 static BOOL
3995vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003996 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003997 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003998 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003999 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004000 PROCESS_INFORMATION *pi,
4001 LPVOID *env,
4002 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004003{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004004 BOOL ret = FALSE;
4005 WCHAR *wcmd, *wcwd = NULL;
4006
4007 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4008 if (wcmd == NULL)
4009 return FALSE;
4010 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004011 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004012 wcwd = enc_to_utf16((char_u *)cwd, NULL);
4013 if (wcwd == NULL)
4014 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004015 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004016
4017 ret = CreateProcessW(
4018 NULL, // Executable name
4019 wcmd, // Command to execute
4020 NULL, // Process security attributes
4021 NULL, // Thread security attributes
4022 inherit_handles, // Inherit handles
4023 flags, // Creation flags
4024 env, // Environment
4025 wcwd, // Current directory
4026 (LPSTARTUPINFOW)si, // Startup information
4027 pi); // Process information
4028theend:
4029 vim_free(wcmd);
4030 vim_free(wcwd);
4031 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004035 static HINSTANCE
4036vim_shell_execute(
4037 char *cmd,
4038 INT n_show_cmd)
4039{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004040 HINSTANCE ret;
4041 WCHAR *wcmd;
4042
4043 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4044 if (wcmd == NULL)
4045 return (HINSTANCE) 0;
4046
4047 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
4048 vim_free(wcmd);
4049 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004050}
4051
4052
Bram Moolenaar4f974752019-02-17 17:44:42 +01004053#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055/*
4056 * Specialised version of system() for Win32 GUI mode.
4057 * This version proceeds as follows:
4058 * 1. Create a console window for use by the subprocess
4059 * 2. Run the subprocess (it gets the allocated console by default)
4060 * 3. Wait for the subprocess to terminate and get its exit code
4061 * 4. Prompt the user to press a key to close the console window
4062 */
4063 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004064mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 STARTUPINFO si;
4067 PROCESS_INFORMATION pi;
4068 DWORD ret = 0;
4069 HWND hwnd = GetFocus();
4070
4071 si.cb = sizeof(si);
4072 si.lpReserved = NULL;
4073 si.lpDesktop = NULL;
4074 si.lpTitle = NULL;
4075 si.dwFlags = STARTF_USESHOWWINDOW;
4076 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004077 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004078 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004080 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004081 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
4083 si.wShowWindow = SW_SHOWNORMAL;
4084 si.cbReserved2 = 0;
4085 si.lpReserved2 = NULL;
4086
Bram Moolenaar0f873732019-12-05 20:28:46 +01004087 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004088 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004089 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
4090 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaar0f873732019-12-05 20:28:46 +01004092 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004094# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 int delay = 1;
4096
Bram Moolenaar0f873732019-12-05 20:28:46 +01004097 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 for (;;)
4099 {
4100 MSG msg;
4101
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004102 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02004105 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02004106 delay = 1;
4107 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4110 break;
4111
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112 // We start waiting for a very short time and then increase it, so
4113 // that we respond quickly when the process is quick, and don't
4114 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (delay < 50)
4116 delay += 10;
4117 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004118# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar0f873732019-12-05 20:28:46 +01004122 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 GetExitCodeProcess(pi.hProcess, &ret);
4124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
Bram Moolenaar0f873732019-12-05 20:28:46 +01004126 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 CloseHandle(pi.hThread);
4128 CloseHandle(pi.hProcess);
4129
Bram Moolenaar0f873732019-12-05 20:28:46 +01004130 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4132
4133 return ret;
4134}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004135
4136/*
4137 * Thread launched by the gui to send the current buffer data to the
4138 * process. This way avoid to hang up vim totally if the children
4139 * process take a long time to process the lines.
4140 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004141 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004142sub_process_writer(LPVOID param)
4143{
4144 HANDLE g_hChildStd_IN_Wr = param;
4145 linenr_T lnum = curbuf->b_op_start.lnum;
4146 DWORD len = 0;
4147 DWORD l;
4148 char_u *lp = ml_get(lnum);
4149 char_u *s;
4150 int written = 0;
4151
4152 for (;;)
4153 {
4154 l = (DWORD)STRLEN(lp + written);
4155 if (l == 0)
4156 len = 0;
4157 else if (lp[written] == NL)
4158 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004159 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004160 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4161 }
4162 else
4163 {
4164 s = vim_strchr(lp + written, NL);
4165 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4166 s == NULL ? l : (DWORD)(s - (lp + written)),
4167 &len, NULL);
4168 }
4169 if (len == (int)l)
4170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004171 // Finished a line, add a NL, unless this line should not have
4172 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004173 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004174 || (!curbuf->b_p_bin
4175 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004176 || (lnum != curbuf->b_no_eol_lnum
4177 && (lnum != curbuf->b_ml.ml_line_count
4178 || curbuf->b_p_eol)))
4179 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004180 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4181 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004182 }
4183
4184 ++lnum;
4185 if (lnum > curbuf->b_op_end.lnum)
4186 break;
4187
4188 lp = ml_get(lnum);
4189 written = 0;
4190 }
4191 else if (len > 0)
4192 written += len;
4193 }
4194
Bram Moolenaar0f873732019-12-05 20:28:46 +01004195 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004196 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004197 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004198}
4199
4200
Bram Moolenaar0f873732019-12-05 20:28:46 +01004201# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004202
4203/*
4204 * This function read from the children's stdout and write the
4205 * data on screen or in the buffer accordingly.
4206 */
4207 static void
4208dump_pipe(int options,
4209 HANDLE g_hChildStd_OUT_Rd,
4210 garray_T *ga,
4211 char_u buffer[],
4212 DWORD *buffer_off)
4213{
4214 DWORD availableBytes = 0;
4215 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004216 int ret;
4217 DWORD len;
4218 DWORD toRead;
4219 int repeatCount;
4220
Bram Moolenaar0f873732019-12-05 20:28:46 +01004221 // we query the pipe to see if there is any data to read
4222 // to avoid to perform a blocking read
4223 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4224 NULL, // optional buffer
4225 0, // buffer size
4226 NULL, // number of read bytes
4227 &availableBytes, // available bytes total
4228 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004229
4230 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004231 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004232 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004233 {
4234 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004235 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004236 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004237 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004238
Bram Moolenaar0f873732019-12-05 20:28:46 +01004239 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004240 if (len == 0)
4241 break;
4242
4243 availableBytes -= len;
4244
4245 if (options & SHELL_READ)
4246 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004247 // Do NUL -> NL translation, append NL separated
4248 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004249 for (i = 0; i < len; ++i)
4250 {
4251 if (buffer[i] == NL)
4252 append_ga_line(ga);
4253 else if (buffer[i] == NUL)
4254 ga_append(ga, NL);
4255 else
4256 ga_append(ga, buffer[i]);
4257 }
4258 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004259 else if (has_mbyte)
4260 {
4261 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004262 int c;
4263 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004264
4265 len += *buffer_off;
4266 buffer[len] = NUL;
4267
Bram Moolenaar0f873732019-12-05 20:28:46 +01004268 // Check if the last character in buffer[] is
4269 // incomplete, keep these bytes for the next
4270 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004271 for (p = buffer; p < buffer + len; p += l)
4272 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004273 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004274 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004275 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004276 else if (MB_BYTE2LEN(*p) != l)
4277 break;
4278 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004279 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004280 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004281 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004282 if (len >= 12)
4283 ++p;
4284 else
4285 {
4286 *buffer_off = len;
4287 return;
4288 }
4289 }
4290 c = *p;
4291 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004292 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004293 if (p < buffer + len)
4294 {
4295 *p = c;
4296 *buffer_off = (DWORD)((buffer + len) - p);
4297 mch_memmove(buffer, p, *buffer_off);
4298 return;
4299 }
4300 *buffer_off = 0;
4301 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004302 else
4303 {
4304 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004305 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004306 }
4307
4308 windgoto(msg_row, msg_col);
4309 cursor_on();
4310 out_flush();
4311 }
4312}
4313
4314/*
4315 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4316 * for communication and doesn't open any new window.
4317 */
4318 static int
4319mch_system_piped(char *cmd, int options)
4320{
4321 STARTUPINFO si;
4322 PROCESS_INFORMATION pi;
4323 DWORD ret = 0;
4324
4325 HANDLE g_hChildStd_IN_Rd = NULL;
4326 HANDLE g_hChildStd_IN_Wr = NULL;
4327 HANDLE g_hChildStd_OUT_Rd = NULL;
4328 HANDLE g_hChildStd_OUT_Wr = NULL;
4329
Bram Moolenaar0f873732019-12-05 20:28:46 +01004330 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004331 DWORD len;
4332
Bram Moolenaar0f873732019-12-05 20:28:46 +01004333 // buffer used to receive keys
4334 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4335 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004336
4337 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004338 int noread_cnt = 0;
4339 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004340 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004341 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004342 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004343
4344 SECURITY_ATTRIBUTES saAttr;
4345
Bram Moolenaar0f873732019-12-05 20:28:46 +01004346 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004347 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4348 saAttr.bInheritHandle = TRUE;
4349 saAttr.lpSecurityDescriptor = NULL;
4350
4351 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004352 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004353 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004354 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004355 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004356 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004357 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004358 {
4359 CloseHandle(g_hChildStd_IN_Rd);
4360 CloseHandle(g_hChildStd_IN_Wr);
4361 CloseHandle(g_hChildStd_OUT_Rd);
4362 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004363 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004364 }
4365
4366 si.cb = sizeof(si);
4367 si.lpReserved = NULL;
4368 si.lpDesktop = NULL;
4369 si.lpTitle = NULL;
4370 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4371
Bram Moolenaar0f873732019-12-05 20:28:46 +01004372 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004373 si.hStdError = g_hChildStd_OUT_Wr;
4374 si.hStdOutput = g_hChildStd_OUT_Wr;
4375 si.hStdInput = g_hChildStd_IN_Rd;
4376 si.wShowWindow = SW_HIDE;
4377 si.cbReserved2 = 0;
4378 si.lpReserved2 = NULL;
4379
4380 if (options & SHELL_READ)
4381 ga_init2(&ga, 1, BUFLEN);
4382
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004383 if (cmd != NULL)
4384 {
4385 p = (char *)vim_strsave((char_u *)cmd);
4386 if (p != NULL)
4387 unescape_shellxquote((char_u *)p, p_sxe);
4388 else
4389 p = cmd;
4390 }
4391
Bram Moolenaar0f873732019-12-05 20:28:46 +01004392 // Now, run the command.
4393 // About "Inherit handles" being TRUE: this command can be litigious,
4394 // handle inheritance was deactivated for pending temp file, but, if we
4395 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004396 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4397 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004398
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004399 if (p != cmd)
4400 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004401
Bram Moolenaar0f873732019-12-05 20:28:46 +01004402 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004403 CloseHandle(g_hChildStd_IN_Rd);
4404 CloseHandle(g_hChildStd_OUT_Wr);
4405
4406 if (options & SHELL_WRITE)
4407 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004408 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004409 _beginthreadex(NULL, // security attributes
4410 0, // default stack size
4411 sub_process_writer, // function to be executed
4412 g_hChildStd_IN_Wr, // parameter
4413 0, // creation flag, start immediately
4414 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004415 CloseHandle(thread);
4416 g_hChildStd_IN_Wr = NULL;
4417 }
4418
Bram Moolenaar0f873732019-12-05 20:28:46 +01004419 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004420 for (;;)
4421 {
4422 MSG msg;
4423
Bram Moolenaar175d0702013-12-11 18:36:33 +01004424 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004425 {
4426 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004427 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004428 }
4429
Bram Moolenaar0f873732019-12-05 20:28:46 +01004430 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004431 if ((options & (SHELL_READ|SHELL_WRITE))
4432# ifdef FEAT_GUI
4433 || gui.in_use
4434# endif
4435 )
4436 {
4437 len = 0;
4438 if (!(options & SHELL_EXPAND)
4439 && ((options &
4440 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4441 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4442# ifdef FEAT_GUI
4443 || gui.in_use
4444# endif
4445 )
4446 && (ta_len > 0 || noread_cnt > 4))
4447 {
4448 if (ta_len == 0)
4449 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004450 // Get extra characters when we don't have any. Reset the
4451 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004452 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004453 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4454 }
4455 if (ta_len > 0 || len > 0)
4456 {
4457 /*
4458 * For pipes: Check for CTRL-C: send interrupt signal to
4459 * child. Check for CTRL-D: EOF, close pipe to child.
4460 */
4461 if (len == 1 && cmd != NULL)
4462 {
4463 if (ta_buf[ta_len] == Ctrl_C)
4464 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004465 // Learn what exit code is expected, for
4466 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004467 TerminateProcess(pi.hProcess, 9);
4468 }
4469 if (ta_buf[ta_len] == Ctrl_D)
4470 {
4471 CloseHandle(g_hChildStd_IN_Wr);
4472 g_hChildStd_IN_Wr = NULL;
4473 }
4474 }
4475
Bram Moolenaarf4140482020-02-15 23:06:45 +01004476 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004477
4478 /*
4479 * For pipes: echo the typed characters. For a pty this
4480 * does not seem to work.
4481 */
4482 for (i = ta_len; i < ta_len + len; ++i)
4483 {
4484 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4485 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004486 else if (has_mbyte)
4487 {
4488 int l = (*mb_ptr2len)(ta_buf + i);
4489
4490 msg_outtrans_len(ta_buf + i, l);
4491 i += l - 1;
4492 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004493 else
4494 msg_outtrans_len(ta_buf + i, 1);
4495 }
4496 windgoto(msg_row, msg_col);
4497 out_flush();
4498
4499 ta_len += len;
4500
4501 /*
4502 * Write the characters to the child, unless EOF has been
4503 * typed for pipes. Write one character at a time, to
4504 * avoid losing too much typeahead. When writing buffer
4505 * lines, drop the typed characters (only check for
4506 * CTRL-C).
4507 */
4508 if (options & SHELL_WRITE)
4509 ta_len = 0;
4510 else if (g_hChildStd_IN_Wr != NULL)
4511 {
4512 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4513 1, &len, NULL);
4514 // if we are typing in, we want to keep things reactive
4515 delay = 1;
4516 if (len > 0)
4517 {
4518 ta_len -= len;
4519 mch_memmove(ta_buf, ta_buf + len, ta_len);
4520 }
4521 }
4522 }
4523 }
4524 }
4525
4526 if (ta_len)
4527 ui_inchar_undo(ta_buf, ta_len);
4528
4529 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4530 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004531 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004532 break;
4533 }
4534
4535 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004536 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004537
Bram Moolenaar0f873732019-12-05 20:28:46 +01004538 // We start waiting for a very short time and then increase it, so
4539 // that we respond quickly when the process is quick, and don't
4540 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004541 if (delay < 50)
4542 delay += 10;
4543 }
4544
Bram Moolenaar0f873732019-12-05 20:28:46 +01004545 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004546 CloseHandle(g_hChildStd_OUT_Rd);
4547 if (g_hChildStd_IN_Wr != NULL)
4548 CloseHandle(g_hChildStd_IN_Wr);
4549
4550 WaitForSingleObject(pi.hProcess, INFINITE);
4551
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004553 GetExitCodeProcess(pi.hProcess, &ret);
4554
4555 if (options & SHELL_READ)
4556 {
4557 if (ga.ga_len > 0)
4558 {
4559 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004560 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004561 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4562 }
4563 else
4564 curbuf->b_no_eol_lnum = 0;
4565 ga_clear(&ga);
4566 }
4567
Bram Moolenaar0f873732019-12-05 20:28:46 +01004568 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004569 CloseHandle(pi.hThread);
4570 CloseHandle(pi.hProcess);
4571
4572 return ret;
4573}
4574
4575 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004576mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004577{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004578 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004579 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004580 return mch_system_piped(cmd, options);
4581 else
4582 return mch_system_classic(cmd, options);
4583}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004586#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004587 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004588mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004589{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004590 int ret;
4591 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004592 char_u *buf;
4593 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004594
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004595 // If the command starts and ends with double quotes, enclose the command
4596 // in parentheses.
4597 len = STRLEN(cmd);
4598 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4599 {
4600 len += 3;
4601 buf = alloc(len);
4602 if (buf == NULL)
4603 return -1;
4604 vim_snprintf((char *)buf, len, "(%s)", cmd);
4605 wcmd = enc_to_utf16(buf, NULL);
4606 free(buf);
4607 }
4608 else
4609 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4610
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004611 if (wcmd == NULL)
4612 return -1;
4613
4614 ret = _wsystem(wcmd);
4615 vim_free(wcmd);
4616 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619#endif
4620
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004621 static int
4622mch_system(char *cmd, int options)
4623{
4624#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004625 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004626 return mch_system_g(cmd, options);
4627 else
4628 return mch_system_c(cmd, options);
4629#elif defined(FEAT_GUI_MSWIN)
4630 return mch_system_g(cmd, options);
4631#else
4632 return mch_system_c(cmd, options);
4633#endif
4634}
4635
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004636#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4637/*
4638 * Use a terminal window to run a shell command in.
4639 */
4640 static int
4641mch_call_shell_terminal(
4642 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004643 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004644{
4645 jobopt_T opt;
4646 char_u *newcmd = NULL;
4647 typval_T argvar[2];
4648 long_u cmdlen;
4649 int retval = -1;
4650 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004651 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004652 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004653 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004654
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004655 if (cmd == NULL)
4656 cmdlen = STRLEN(p_sh) + 1;
4657 else
4658 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004659 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004660 if (newcmd == NULL)
4661 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004662 if (cmd == NULL)
4663 {
4664 STRCPY(newcmd, p_sh);
4665 ch_log(NULL, "starting terminal to run a shell");
4666 }
4667 else
4668 {
4669 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4670 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4671 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004672
4673 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004674
4675 argvar[0].v_type = VAR_STRING;
4676 argvar[0].vval.v_string = newcmd;
4677 argvar[1].v_type = VAR_UNKNOWN;
4678 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004679 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004680 {
4681 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004682 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004683 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004684
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004685 job = term_getjob(buf->b_term);
4686 ++job->jv_refcount;
4687
Bram Moolenaar0f873732019-12-05 20:28:46 +01004688 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004689 aucmd_prepbuf(&aco, buf);
4690
4691 clear_oparg(&oa);
4692 while (term_use_loop())
4693 {
4694 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4695 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004696 // If terminal_loop() returns OK we got a key that is handled
4697 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004698 if (terminal_loop(TRUE) == OK)
4699 normal_cmd(&oa, TRUE);
4700 }
4701 else
4702 normal_cmd(&oa, TRUE);
4703 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004704 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004705 ch_log(NULL, "system command finished");
4706
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004707 job_unref(job);
4708
Bram Moolenaar0f873732019-12-05 20:28:46 +01004709 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004710 aucmd_restbuf(&aco);
4711
4712 wait_return(TRUE);
4713 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4714
4715 vim_free(newcmd);
4716 return retval;
4717}
4718#endif
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Either execute a command by calling the shell or start a new shell
4722 */
4723 int
4724mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004725 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004726 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
4728 int x = 0;
4729 int tmode = cur_tmode;
4730#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004731 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004732
Bram Moolenaar0f873732019-12-05 20:28:46 +01004733 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004734 if (GetConsoleTitleW(szShellTitle,
4735 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004736 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004737 if (cmd == NULL)
4738 wcscat(szShellTitle, L" :sh");
4739 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004740 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004741 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004742
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004743 if (wn != NULL)
4744 {
4745 wcscat(szShellTitle, L" - !");
4746 if ((wcslen(szShellTitle) + wcslen(wn) <
4747 sizeof(szShellTitle)/sizeof(WCHAR)))
4748 wcscat(szShellTitle, wn);
4749 SetConsoleTitleW(szShellTitle);
4750 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004751 }
4752 }
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#endif
4755
4756 out_flush();
4757
4758#ifdef MCH_WRITE_DUMP
4759 if (fdDump)
4760 {
4761 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4762 fflush(fdDump);
4763 }
4764#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004765#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004766 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004767 if (
4768# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004769 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004770# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004771 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004772 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4773 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004774 char_u *cmdbase = cmd;
4775
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004776 if (cmdbase != NULL)
4777 // Skip a leading quote and (.
4778 while (*cmdbase == '"' || *cmdbase == '(')
4779 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004780
4781 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004782 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4783 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004784 {
4785 // Use a terminal window to run the command in.
4786 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004787# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004788 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004789# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004790 return x;
4791 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004792 }
4793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 /*
4796 * Catch all deadly signals while running the external command, because a
4797 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4798 */
4799 signal(SIGINT, SIG_IGN);
4800#if defined(__GNUC__) && !defined(__MINGW32__)
4801 signal(SIGKILL, SIG_IGN);
4802#else
4803 signal(SIGBREAK, SIG_IGN);
4804#endif
4805 signal(SIGILL, SIG_IGN);
4806 signal(SIGFPE, SIG_IGN);
4807 signal(SIGSEGV, SIG_IGN);
4808 signal(SIGTERM, SIG_IGN);
4809 signal(SIGABRT, SIG_IGN);
4810
4811 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004812 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 if (cmd == NULL)
4815 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004816 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818 else
4819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004820 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004821 char_u *newcmd = NULL;
4822 char_u *cmdbase = cmd;
4823 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004824
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004826 if (*cmdbase == '"' )
4827 ++cmdbase;
4828 if (*cmdbase == '(')
4829 ++cmdbase;
4830
Bram Moolenaar1c465442017-03-12 20:10:05 +01004831 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004832 {
4833 STARTUPINFO si;
4834 PROCESS_INFORMATION pi;
4835 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004836 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004837 char_u *p;
4838
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004839 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004840 si.cb = sizeof(si);
4841 si.lpReserved = NULL;
4842 si.lpDesktop = NULL;
4843 si.lpTitle = NULL;
4844 si.dwFlags = 0;
4845 si.cbReserved2 = 0;
4846 si.lpReserved2 = NULL;
4847
4848 cmdbase = skipwhite(cmdbase + 5);
4849 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004850 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004851 {
4852 cmdbase = skipwhite(cmdbase + 4);
4853 si.dwFlags = STARTF_USESHOWWINDOW;
4854 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004855 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004856 }
4857 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004858 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004859 {
4860 cmdbase = skipwhite(cmdbase + 2);
4861 flags = CREATE_NO_WINDOW;
4862 si.dwFlags = STARTF_USESTDHANDLES;
4863 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004864 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004865 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004866 NULL, // Security att.
4867 OPEN_EXISTING, // Open flags
4868 FILE_ATTRIBUTE_NORMAL, // File att.
4869 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004870 si.hStdOutput = si.hStdInput;
4871 si.hStdError = si.hStdInput;
4872 }
4873
Bram Moolenaar0f873732019-12-05 20:28:46 +01004874 // Remove a trailing ", ) and )" if they have a match
4875 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004876 if (cmdbase > cmd)
4877 {
4878 p = cmdbase + STRLEN(cmdbase);
4879 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4880 *--p = NUL;
4881 if (p > cmdbase && p[-1] == ')'
4882 && (*cmd =='(' || cmd[1] == '('))
4883 *--p = NUL;
4884 }
4885
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004886 newcmd = cmdbase;
4887 unescape_shellxquote(cmdbase, p_sxe);
4888
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004889 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004890 * If creating new console, arguments are passed to the
4891 * 'cmd.exe' as-is. If it's not, arguments are not treated
4892 * correctly for current 'cmd.exe'. So unescape characters in
4893 * shellxescape except '|' for avoiding to be treated as
4894 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004895 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004896 if (flags != CREATE_NEW_CONSOLE)
4897 {
4898 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004899 char_u *cmd_shell = mch_getenv("COMSPEC");
4900
4901 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004902 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004903
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004904 subcmd = vim_strsave_escaped_ext(cmdbase,
4905 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004906 if (subcmd != NULL)
4907 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004908 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004909 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004910 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004911 if (newcmd != NULL)
4912 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004913 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004914 else
4915 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004916 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004917 }
4918 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004919
4920 /*
4921 * Now, start the command as a process, so that it doesn't
4922 * inherit our handles which causes unpleasant dangling swap
4923 * files if we exit before the spawned process
4924 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004925 if (vim_create_process((char *)newcmd, FALSE, flags,
4926 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004927 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004928 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4929 > (HINSTANCE)32)
4930 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004931 else
4932 {
4933 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004934#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004935# ifdef VIMDLL
4936 if (gui.in_use)
4937# endif
4938 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004939#endif
4940 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004941
4942 if (newcmd != cmdbase)
4943 vim_free(newcmd);
4944
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004945 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004947 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004948 CloseHandle(si.hStdInput);
4949 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004950 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004951 CloseHandle(pi.hThread);
4952 CloseHandle(pi.hProcess);
4953 }
4954 else
4955 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004956 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004957#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004958 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004959 (!s_dont_use_vimrun && p_stmp ?
4960 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4961 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004963 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004964
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004965 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004966 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004968#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004969 if (
4970# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004971 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004972# endif
4973 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004975 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4976 "External commands will not pause after completion.\n"
4977 "See :help win32-vimrun for more information.");
4978 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004979 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4980 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004981
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004982 if (wmsg != NULL && wtitle != NULL)
4983 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4984 vim_free(wmsg);
4985 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 need_vimrun_warning = FALSE;
4987 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004988 if (
4989# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004990 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004991# endif
4992 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004993 // Use vimrun to execute the command. It opens a console
4994 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004995 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 vimrun_path,
4997 (msg_silent != 0 || (options & SHELL_DOOUT))
4998 ? "-s " : "",
4999 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005000 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005001# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005002 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005003# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02005004 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005005 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005006 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
5007 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005010 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00005011 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005013 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 }
5017
5018 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005019 {
5020 // The shell may have messed with the mode, always set it.
5021 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005022 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
Bram Moolenaar0f873732019-12-05 20:28:46 +01005025 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01005027#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005028 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005029 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#endif
5031 )
5032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005033 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 msg_putchar('\n');
5035 }
5036#ifdef FEAT_TITLE
5037 resettitle();
5038#endif
5039
5040 signal(SIGINT, SIG_DFL);
5041#if defined(__GNUC__) && !defined(__MINGW32__)
5042 signal(SIGKILL, SIG_DFL);
5043#else
5044 signal(SIGBREAK, SIG_DFL);
5045#endif
5046 signal(SIGILL, SIG_DFL);
5047 signal(SIGFPE, SIG_DFL);
5048 signal(SIGSEGV, SIG_DFL);
5049 signal(SIGTERM, SIG_DFL);
5050 signal(SIGABRT, SIG_DFL);
5051
5052 return x;
5053}
5054
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005055#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005056 static HANDLE
5057job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01005058 char_u *fname,
5059 DWORD dwDesiredAccess,
5060 DWORD dwShareMode,
5061 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
5062 DWORD dwCreationDisposition,
5063 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005064{
5065 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005066 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005067
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005068 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005069 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005070 return INVALID_HANDLE_VALUE;
5071
5072 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
5073 lpSecurityAttributes, dwCreationDisposition,
5074 dwFlagsAndAttributes, NULL);
5075 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005076 return h;
5077}
5078
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005079/*
5080 * Turn the dictionary "env" into a NUL separated list that can be used as the
5081 * environment argument of vim_create_process().
5082 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005083 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005084win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005085{
5086 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005087 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005088 LPVOID base = GetEnvironmentStringsW();
5089
Bram Moolenaar0f873732019-12-05 20:28:46 +01005090 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005091 if (ga_grow(gap, 1) == FAIL)
5092 return;
5093
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005094 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005095 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005096 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005097 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005098 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005099 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005100 typval_T *item = &dict_lookup(hi)->di_tv;
5101 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005102 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005103 --todo;
5104 if (wkey != NULL && wval != NULL)
5105 {
5106 size_t n;
5107 size_t lkey = wcslen(wkey);
5108 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02005109
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005110 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
5111 continue;
5112 for (n = 0; n < lkey; n++)
5113 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
5114 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
5115 for (n = 0; n < lval; n++)
5116 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
5117 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5118 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01005119 vim_free(wkey);
5120 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005121 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005122 }
5123 }
5124
Bram Moolenaar355757a2020-02-10 22:06:32 +01005125 if (base)
5126 {
5127 WCHAR *p = (WCHAR*) base;
5128
5129 // for last \0
5130 if (ga_grow(gap, 1) == FAIL)
5131 return;
5132
5133 while (*p != 0 || *(p + 1) != 0)
5134 {
5135 if (ga_grow(gap, 1) == OK)
5136 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5137 p++;
5138 }
5139 FreeEnvironmentStrings(base);
5140 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5141 }
5142
Bram Moolenaar493359e2018-06-12 20:25:52 +02005143# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005144 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005145# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005146 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005147 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005148# endif
5149# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005150 char_u *version = get_vim_var_str(VV_VERSION);
5151 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005152# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005153 // size of "VIM_SERVERNAME=" and value,
5154 // plus "VIM_TERMINAL=" and value,
5155 // plus two terminating NULs
5156 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005157# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005158 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005159# endif
5160# ifdef FEAT_TERMINAL
5161 + 13 + version_len + 2
5162# endif
5163 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005164
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005165 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005166 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005167# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005168 for (n = 0; n < 15; n++)
5169 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5170 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005171 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005172 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5173 (WCHAR)servername[n];
5174 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005175# endif
5176# ifdef FEAT_TERMINAL
5177 if (is_terminal)
5178 {
5179 for (n = 0; n < 13; n++)
5180 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5181 (WCHAR)"VIM_TERMINAL="[n];
5182 for (n = 0; n < version_len; n++)
5183 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5184 (WCHAR)version[n];
5185 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5186 }
5187# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005188 }
5189 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005190# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005191}
5192
Bram Moolenaarb091f302019-01-19 14:37:00 +01005193/*
5194 * Create a pair of pipes.
5195 * Return TRUE for success, FALSE for failure.
5196 */
5197 static BOOL
5198create_pipe_pair(HANDLE handles[2])
5199{
5200 static LONG s;
5201 char name[64];
5202 SECURITY_ATTRIBUTES sa;
5203
5204 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5205 GetCurrentProcessId(),
5206 InterlockedIncrement(&s));
5207
5208 // Create named pipe. Max size of named pipe is 65535.
5209 handles[1] = CreateNamedPipe(
5210 name,
5211 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5212 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005213 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005214
5215 if (handles[1] == INVALID_HANDLE_VALUE)
5216 return FALSE;
5217
5218 sa.nLength = sizeof(sa);
5219 sa.bInheritHandle = TRUE;
5220 sa.lpSecurityDescriptor = NULL;
5221
5222 handles[0] = CreateFile(name,
5223 FILE_GENERIC_READ,
5224 FILE_SHARE_READ, &sa,
5225 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5226
5227 if (handles[0] == INVALID_HANDLE_VALUE)
5228 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005229 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005230 return FALSE;
5231 }
5232
5233 return TRUE;
5234}
5235
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005236 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005237mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005238{
5239 STARTUPINFO si;
5240 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005241 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005242 SECURITY_ATTRIBUTES saAttr;
5243 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005244 HANDLE ifd[2];
5245 HANDLE ofd[2];
5246 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005247 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005248
5249 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5250 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5251 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5252 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5253 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5254 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5255 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5256
5257 if (use_out_for_err && use_null_for_out)
5258 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005259
5260 ifd[0] = INVALID_HANDLE_VALUE;
5261 ifd[1] = INVALID_HANDLE_VALUE;
5262 ofd[0] = INVALID_HANDLE_VALUE;
5263 ofd[1] = INVALID_HANDLE_VALUE;
5264 efd[0] = INVALID_HANDLE_VALUE;
5265 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005266 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005267
Bram Moolenaar14207f42016-10-27 21:13:10 +02005268 jo = CreateJobObject(NULL, NULL);
5269 if (jo == NULL)
5270 {
5271 job->jv_status = JOB_FAILED;
5272 goto failed;
5273 }
5274
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005275 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005276 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005277
Bram Moolenaar76467df2016-02-12 19:30:26 +01005278 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005279 ZeroMemory(&si, sizeof(si));
5280 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005281 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005282 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005283
Bram Moolenaard8070362016-02-15 21:56:54 +01005284 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5285 saAttr.bInheritHandle = TRUE;
5286 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005287
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005288 if (use_file_for_in)
5289 {
5290 char_u *fname = options->jo_io_name[PART_IN];
5291
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005292 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5293 FILE_SHARE_READ | FILE_SHARE_WRITE,
5294 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5295 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005297 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005298 goto failed;
5299 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005300 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005301 else if (!use_null_for_in
5302 && (!create_pipe_pair(ifd)
5303 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005304 goto failed;
5305
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005306 if (use_file_for_out)
5307 {
5308 char_u *fname = options->jo_io_name[PART_OUT];
5309
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005310 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5311 FILE_SHARE_READ | FILE_SHARE_WRITE,
5312 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5313 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005315 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005316 goto failed;
5317 }
5318 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005319 else if (!use_null_for_out &&
5320 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005321 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005322 goto failed;
5323
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005324 if (use_file_for_err)
5325 {
5326 char_u *fname = options->jo_io_name[PART_ERR];
5327
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005328 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5329 FILE_SHARE_READ | FILE_SHARE_WRITE,
5330 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5331 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005333 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005334 goto failed;
5335 }
5336 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005337 else if (!use_out_for_err && !use_null_for_err &&
5338 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005339 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005340 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005341
Bram Moolenaard8070362016-02-15 21:56:54 +01005342 si.dwFlags |= STARTF_USESTDHANDLES;
5343 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005344 si.hStdOutput = ofd[1];
5345 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5346
5347 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5348 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005349 if (options->jo_set & JO_CHANNEL)
5350 {
5351 channel = options->jo_channel;
5352 if (channel != NULL)
5353 ++channel->ch_refcount;
5354 }
5355 else
5356 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005357 if (channel == NULL)
5358 goto failed;
5359 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005360
5361 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005362 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005363 CREATE_DEFAULT_ERROR_MODE |
5364 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005365 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005366 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005367 &si, &pi,
5368 ga.ga_data,
5369 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005370 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005371 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005372 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005373 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005374 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005375
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005376 ga_clear(&ga);
5377
Bram Moolenaar14207f42016-10-27 21:13:10 +02005378 if (!AssignProcessToJobObject(jo, pi.hProcess))
5379 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005380 // if failing, switch the way to terminate
5381 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005382 CloseHandle(jo);
5383 jo = NULL;
5384 }
5385 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005386 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005387 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005388 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005389 job->jv_status = JOB_STARTED;
5390
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005391 CloseHandle(ifd[0]);
5392 CloseHandle(ofd[1]);
5393 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005394 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005395
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005396 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005397 if (channel != NULL)
5398 {
5399 channel_set_pipes(channel,
5400 use_file_for_in || use_null_for_in
5401 ? INVALID_FD : (sock_T)ifd[1],
5402 use_file_for_out || use_null_for_out
5403 ? INVALID_FD : (sock_T)ofd[0],
5404 use_out_for_err || use_file_for_err || use_null_for_err
5405 ? INVALID_FD : (sock_T)efd[0]);
5406 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005407 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005408 return;
5409
5410failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005411 CloseHandle(ifd[0]);
5412 CloseHandle(ofd[0]);
5413 CloseHandle(efd[0]);
5414 CloseHandle(ifd[1]);
5415 CloseHandle(ofd[1]);
5416 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005417 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005418 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005419}
5420
5421 char *
5422mch_job_status(job_T *job)
5423{
5424 DWORD dwExitCode = 0;
5425
Bram Moolenaar76467df2016-02-12 19:30:26 +01005426 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5427 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005428 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005429 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005430 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005431 {
5432 ch_log(job->jv_channel, "Job ended");
5433 job->jv_status = JOB_ENDED;
5434 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005435 return "dead";
5436 }
5437 return "run";
5438}
5439
Bram Moolenaar97792de2016-10-15 18:36:49 +02005440 job_T *
5441mch_detect_ended_job(job_T *job_list)
5442{
5443 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5444 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5445 job_T *job = job_list;
5446
5447 while (job != NULL)
5448 {
5449 DWORD n;
5450 DWORD result;
5451
5452 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5453 && job != NULL; job = job->jv_next)
5454 {
5455 if (job->jv_status == JOB_STARTED)
5456 {
5457 jobHandles[n] = job->jv_proc_info.hProcess;
5458 jobArray[n] = job;
5459 ++n;
5460 }
5461 }
5462 if (n == 0)
5463 continue;
5464 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5465 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5466 {
5467 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5468
5469 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5470 return wait_job;
5471 }
5472 }
5473 return NULL;
5474}
5475
Bram Moolenaarfb630902016-10-29 14:55:00 +02005476 static BOOL
5477terminate_all(HANDLE process, int code)
5478{
5479 PROCESSENTRY32 pe;
5480 HANDLE h = INVALID_HANDLE_VALUE;
5481 DWORD pid = GetProcessId(process);
5482
5483 if (pid != 0)
5484 {
5485 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5486 if (h != INVALID_HANDLE_VALUE)
5487 {
5488 pe.dwSize = sizeof(PROCESSENTRY32);
5489 if (!Process32First(h, &pe))
5490 goto theend;
5491
5492 do
5493 {
5494 if (pe.th32ParentProcessID == pid)
5495 {
5496 HANDLE ph = OpenProcess(
5497 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5498 if (ph != NULL)
5499 {
5500 terminate_all(ph, code);
5501 CloseHandle(ph);
5502 }
5503 }
5504 } while (Process32Next(h, &pe));
5505
5506 CloseHandle(h);
5507 }
5508 }
5509
5510theend:
5511 return TerminateProcess(process, code);
5512}
5513
5514/*
5515 * Send a (deadly) signal to "job".
5516 * Return FAIL if it didn't work.
5517 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005518 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005519mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005520{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005521 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005522
Bram Moolenaar923d9262016-02-25 20:56:01 +01005523 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005524 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005525 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005526 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005527 {
5528 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5529 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005530 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005531 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005532 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005533 }
5534
5535 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5536 return FAIL;
5537 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005538 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5539 job->jv_proc_info.dwProcessId)
5540 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005541 FreeConsole();
5542 return ret;
5543}
5544
5545/*
5546 * Clear the data related to "job".
5547 */
5548 void
5549mch_clear_job(job_T *job)
5550{
5551 if (job->jv_status != JOB_FAILED)
5552 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005553 if (job->jv_job_object != NULL)
5554 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005555 CloseHandle(job->jv_proc_info.hProcess);
5556 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005557}
5558#endif
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005561#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563/*
5564 * Start termcap mode
5565 */
5566 static void
5567termcap_mode_start(void)
5568{
5569 DWORD cmodein;
5570
5571 if (g_fTermcapMode)
5572 return;
5573
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005574 if (!p_rs && USE_VTP)
5575 vtp_printf("\033[?1049h");
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 SaveConsoleBuffer(&g_cbNonTermcap);
5578
5579 if (g_cbTermcap.IsValid)
5580 {
5581 /*
5582 * We've been in termcap mode before. Restore certain screen
5583 * characteristics, including the buffer size and the window
5584 * size. Since we will be redrawing the screen, we don't need
5585 * to restore the actual contents of the buffer.
5586 */
5587 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005588 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5590 Rows = g_cbTermcap.Info.dwSize.Y;
5591 Columns = g_cbTermcap.Info.dwSize.X;
5592 }
5593 else
5594 {
5595 /*
5596 * This is our first time entering termcap mode. Clear the console
5597 * screen buffer, and resize the buffer to match the current window
5598 * size. We will use this as the size of our editing environment.
5599 */
5600 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005601 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5603 }
5604
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005605# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (g_fMouseActive)
5611 cmodein |= ENABLE_MOUSE_INPUT;
5612 else
5613 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 cmodein |= ENABLE_WINDOW_INPUT;
5615 SetConsoleMode(g_hConIn, cmodein);
5616
5617 redraw_later_clear();
5618 g_fTermcapMode = TRUE;
5619}
5620
5621
5622/*
5623 * End termcap mode
5624 */
5625 static void
5626termcap_mode_end(void)
5627{
5628 DWORD cmodein;
5629 ConsoleBuffer *cb;
5630 COORD coord;
5631 DWORD dwDummy;
5632
5633 if (!g_fTermcapMode)
5634 return;
5635
5636 SaveConsoleBuffer(&g_cbTermcap);
5637
5638 GetConsoleMode(g_hConIn, &cmodein);
5639 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5640 SetConsoleMode(g_hConIn, cmodein);
5641
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005642# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005643 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005644# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005646# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005647 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005648 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 SetConsoleCursorInfo(g_hConOut, &g_cci);
5650
5651 if (p_rs || exiting)
5652 {
5653 /*
5654 * Clear anything that happens to be on the current line.
5655 */
5656 coord.X = 0;
5657 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5658 FillConsoleOutputCharacter(g_hConOut, ' ',
5659 cb->Info.dwSize.X, coord, &dwDummy);
5660 /*
5661 * The following is just for aesthetics. If we are exiting without
5662 * restoring the screen, then we want to have a prompt string
5663 * appear at the bottom line. However, the command interpreter
5664 * seems to always advance the cursor one line before displaying
5665 * the prompt string, which causes the screen to scroll. To
5666 * counter this, move the cursor up one line before exiting.
5667 */
5668 if (exiting && !p_rs)
5669 coord.Y--;
5670 /*
5671 * Position the cursor at the leftmost column of the desired row.
5672 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005673 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005676 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005677 vtp_printf("\033[?1049l");
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 g_fTermcapMode = FALSE;
5680}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005681#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005684#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 void
5686mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005687 char_u *s UNUSED,
5688 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005690 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691}
5692
5693#else
5694
5695/*
5696 * clear `n' chars, starting from `coord'
5697 */
5698 static void
5699clear_chars(
5700 COORD coord,
5701 DWORD n)
5702{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005703 if (!USE_VTP)
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02005704 {
5705 DWORD dwDummy;
5706
5707 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
5708 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord,
5709 &dwDummy);
5710 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005711 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005712 {
5713 set_console_color_rgb();
5714 gotoxy(coord.X + 1, coord.Y + 1);
5715 vtp_printf("\033[%dX", n);
5716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717}
5718
5719
5720/*
5721 * Clear the screen
5722 */
5723 static void
5724clear_screen(void)
5725{
5726 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005727
5728 if (!USE_VTP)
5729 clear_chars(g_coord, Rows * Columns);
5730 else
5731 {
5732 set_console_color_rgb();
5733 gotoxy(1, 1);
5734 vtp_printf("\033[2J");
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736}
5737
5738
5739/*
5740 * Clear to end of display
5741 */
5742 static void
5743clear_to_end_of_display(void)
5744{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005745 COORD save = g_coord;
5746
5747 if (!USE_VTP)
5748 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005750 else
5751 {
5752 set_console_color_rgb();
5753 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5754 vtp_printf("\033[0J");
5755
5756 gotoxy(save.X + 1, save.Y + 1);
5757 g_coord = save;
5758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759}
5760
5761
5762/*
5763 * Clear to end of line
5764 */
5765 static void
5766clear_to_end_of_line(void)
5767{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005768 COORD save = g_coord;
5769
5770 if (!USE_VTP)
5771 clear_chars(g_coord, Columns - g_coord.X);
5772 else
5773 {
5774 set_console_color_rgb();
5775 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5776 vtp_printf("\033[0K");
5777
5778 gotoxy(save.X + 1, save.Y + 1);
5779 g_coord = save;
5780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783
5784/*
5785 * Scroll the scroll region up by `cLines' lines
5786 */
5787 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005788scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 COORD oldcoord = g_coord;
5791
5792 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5793 delete_lines(cLines);
5794
5795 g_coord = oldcoord;
5796}
5797
5798
5799/*
5800 * Set the scroll region
5801 */
5802 static void
5803set_scroll_region(
5804 unsigned left,
5805 unsigned top,
5806 unsigned right,
5807 unsigned bottom)
5808{
5809 if (left >= right
5810 || top >= bottom
5811 || right > (unsigned) Columns - 1
5812 || bottom > (unsigned) Rows - 1)
5813 return;
5814
5815 g_srScrollRegion.Left = left;
5816 g_srScrollRegion.Top = top;
5817 g_srScrollRegion.Right = right;
5818 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005819}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005820
Bram Moolenaar6982f422019-02-16 16:48:01 +01005821 static void
5822set_scroll_region_tb(
5823 unsigned top,
5824 unsigned bottom)
5825{
5826 if (top >= bottom || bottom > (unsigned)Rows - 1)
5827 return;
5828
5829 g_srScrollRegion.Top = top;
5830 g_srScrollRegion.Bottom = bottom;
5831}
5832
5833 static void
5834set_scroll_region_lr(
5835 unsigned left,
5836 unsigned right)
5837{
5838 if (left >= right || right > (unsigned)Columns - 1)
5839 return;
5840
5841 g_srScrollRegion.Left = left;
5842 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843}
5844
5845
5846/*
5847 * Insert `cLines' lines at the current cursor position
5848 */
5849 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005850insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005852 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 COORD dest;
5854 CHAR_INFO fill;
5855
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005856 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5857
Bram Moolenaar6982f422019-02-16 16:48:01 +01005858 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 dest.Y = g_coord.Y + cLines;
5860
Bram Moolenaar6982f422019-02-16 16:48:01 +01005861 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 source.Top = g_coord.Y;
5863 source.Right = g_srScrollRegion.Right;
5864 source.Bottom = g_srScrollRegion.Bottom - cLines;
5865
Bram Moolenaar6982f422019-02-16 16:48:01 +01005866 clip.Left = g_srScrollRegion.Left;
5867 clip.Top = g_coord.Y;
5868 clip.Right = g_srScrollRegion.Right;
5869 clip.Bottom = g_srScrollRegion.Bottom;
5870
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005871 fill.Char.AsciiChar = ' ';
5872 if (!USE_VTP)
5873 fill.Attributes = g_attrCurrent;
5874 else
5875 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005877 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005878
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005879 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5880
Bram Moolenaar6982f422019-02-16 16:48:01 +01005881 // Here we have to deal with a win32 console flake: If the scroll
5882 // region looks like abc and we scroll c to a and fill with d we get
5883 // cbd... if we scroll block c one line at a time to a, we get cdd...
5884 // vim expects cdd consistently... So we have to deal with that
5885 // here... (this also occurs scrolling the same way in the other
5886 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
5888 if (source.Bottom < dest.Y)
5889 {
5890 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005891 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar6982f422019-02-16 16:48:01 +01005893 coord.X = source.Left;
5894 for (i = clip.Top; i < dest.Y; ++i)
5895 {
5896 coord.Y = i;
5897 clear_chars(coord, source.Right - source.Left + 1);
5898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02005900
5901 if (USE_WT)
5902 {
5903 COORD coord;
5904 int i;
5905
5906 coord.X = source.Left;
5907 for (i = source.Top; i < dest.Y; ++i)
5908 {
5909 coord.Y = i;
5910 clear_chars(coord, source.Right - source.Left + 1);
5911 }
5912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913}
5914
5915
5916/*
5917 * Delete `cLines' lines at the current cursor position
5918 */
5919 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005920delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005922 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 COORD dest;
5924 CHAR_INFO fill;
5925 int nb;
5926
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005927 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5928
Bram Moolenaar6982f422019-02-16 16:48:01 +01005929 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 dest.Y = g_coord.Y;
5931
Bram Moolenaar6982f422019-02-16 16:48:01 +01005932 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 source.Top = g_coord.Y + cLines;
5934 source.Right = g_srScrollRegion.Right;
5935 source.Bottom = g_srScrollRegion.Bottom;
5936
Bram Moolenaar6982f422019-02-16 16:48:01 +01005937 clip.Left = g_srScrollRegion.Left;
5938 clip.Top = g_coord.Y;
5939 clip.Right = g_srScrollRegion.Right;
5940 clip.Bottom = g_srScrollRegion.Bottom;
5941
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005942 fill.Char.AsciiChar = ' ';
5943 if (!USE_VTP)
5944 fill.Attributes = g_attrCurrent;
5945 else
5946 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005948 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005949
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005950 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5951
Bram Moolenaar6982f422019-02-16 16:48:01 +01005952 // Here we have to deal with a win32 console flake; See insert_lines()
5953 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
5955 nb = dest.Y + (source.Bottom - source.Top) + 1;
5956
5957 if (nb < source.Top)
5958 {
5959 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005960 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
Bram Moolenaar6982f422019-02-16 16:48:01 +01005962 coord.X = source.Left;
5963 for (i = nb; i < clip.Bottom; ++i)
5964 {
5965 coord.Y = i;
5966 clear_chars(coord, source.Right - source.Left + 1);
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02005969
5970 if (USE_WT)
5971 {
5972 COORD coord;
5973 int i;
5974
5975 coord.X = source.Left;
5976 for (i = nb; i <= source.Bottom; ++i)
5977 {
5978 coord.Y = i;
5979 clear_chars(coord, source.Right - source.Left + 1);
5980 }
5981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982}
5983
5984
5985/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005986 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
5988 static void
5989gotoxy(
5990 unsigned x,
5991 unsigned y)
5992{
5993 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5994 return;
5995
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005996 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005997 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01005998 // There are reports of double-width characters not displayed
5999 // correctly. This workaround should fix it, similar to how it's done
6000 // for VTP.
6001 g_coord.X = 0;
6002 SetConsoleCursorPosition(g_hConOut, g_coord);
6003
Bram Moolenaar2313b612019-09-27 14:14:32 +02006004 // external cursor coords are 1-based; internal are 0-based
6005 g_coord.X = x - 1;
6006 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006007 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006008 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006009 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02006010 {
6011 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02006012 // destruction. Insider build bug. Always enabled because it's cheap
6013 // and avoids mistakes with recognizing the build.
6014 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006015
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006016 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006017
6018 g_coord.X = x - 1;
6019 g_coord.Y = y - 1;
6020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021}
6022
6023
6024/*
6025 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006026 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 */
6028 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006029textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006031 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 SetConsoleTextAttribute(g_hConOut, wAttr);
6034}
6035
6036
6037 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006038textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006040 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006042 if (!USE_VTP)
6043 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6044 else
6045 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046}
6047
6048
6049 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006050textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006052 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006054 if (!USE_VTP)
6055 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6056 else
6057 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058}
6059
6060
6061/*
6062 * restore the default text attribute (whatever we started with)
6063 */
6064 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006065normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006067 if (!USE_VTP)
6068 textattr(g_attrDefault);
6069 else
6070 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071}
6072
6073
6074static WORD g_attrPreStandout = 0;
6075
6076/*
6077 * Make the text standout, by brightening it
6078 */
6079 static void
6080standout(void)
6081{
6082 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
6085}
6086
6087
6088/*
6089 * Turn off standout mode
6090 */
6091 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006092standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006096
6097 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098}
6099
6100
6101/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00006102 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 */
6104 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006105mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 char_u *p;
6108 int n;
6109
6110 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
6111 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006112 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006113# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006114 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006115# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006116 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
6118 p = T_ME + 2;
6119 n = getdigits(&p);
6120 if (*p == 'm' && n > 0)
6121 {
6122 cterm_normal_fg_color = (n & 0xf) + 1;
6123 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
6124 }
6125 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006126# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006127 cterm_normal_fg_gui_color = INVALCOLOR;
6128 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130}
6131
6132
6133/*
6134 * visual bell: flash the screen
6135 */
6136 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006137visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 COORD coordOrigin = {0, 0};
6140 WORD attrFlash = ~g_attrCurrent & 0xff;
6141
6142 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006143 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 if (oldattrs == NULL)
6146 return;
6147 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
6148 coordOrigin, &dwDummy);
6149 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6150 coordOrigin, &dwDummy);
6151
Bram Moolenaar0f873732019-12-05 20:28:46 +01006152 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006153 if (!USE_VTP)
6154 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 coordOrigin, &dwDummy);
6156 vim_free(oldattrs);
6157}
6158
6159
6160/*
6161 * Make the cursor visible or invisible
6162 */
6163 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006164cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006167
6168 if (USE_VTP)
6169 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6170
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006171# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174}
6175
6176
6177/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006178 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006179 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006181 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006183 char_u *pchBuf,
6184 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006186 COORD coord = g_coord;
6187 DWORD written;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006188 DWORD n, cchwritten;
6189 static DWORD cells;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006190 static WCHAR *unicodebuf = NULL;
6191 static int unibuflen = 0;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006192 static int length;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006193 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006194 static WCHAR *utf8spbuf = NULL;
6195 static int utf8splength;
6196 static DWORD utf8spcells;
6197 static WCHAR **utf8usingbuf = &unicodebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006199 if (cbToWrite != 1 || *pchBuf != ' ' || !enc_utf8)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006200 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006201 utf8usingbuf = &unicodebuf;
6202 do
6203 {
6204 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6205 unicodebuf, unibuflen);
6206 if (length && length <= unibuflen)
6207 break;
6208 vim_free(unicodebuf);
6209 unicodebuf = length ? LALLOC_MULT(WCHAR, length) : NULL;
6210 unibuflen = unibuflen ? 0 : length;
6211 } while(1);
6212 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006213 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006214 else // cbToWrite == 1 && *pchBuf == ' ' && enc_utf8
6215 {
6216 if (utf8usingbuf != &utf8spbuf)
6217 {
6218 if (utf8spbuf == NULL)
6219 {
6220 cells = mb_string2cells((char_u *)" ", 1);
6221 length = MultiByteToWideChar(CP_UTF8, 0, " ", 1, NULL, 0);
6222 utf8spbuf = LALLOC_MULT(WCHAR, length);
6223 if (utf8spbuf != NULL)
6224 {
6225 MultiByteToWideChar(CP_UTF8, 0, " ", 1, utf8spbuf, length);
6226 utf8usingbuf = &utf8spbuf;
6227 utf8splength = length;
6228 utf8spcells = cells;
6229 }
6230 }
6231 else
6232 {
6233 utf8usingbuf = &utf8spbuf;
6234 length = utf8splength;
6235 cells = utf8spcells;
6236 }
6237 }
6238 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006239
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006240 if (!USE_VTP)
6241 {
6242 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6243 coord, &written);
6244 // When writing fails or didn't write a single character, pretend one
6245 // character was written, otherwise we get stuck.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006246 if (WriteConsoleOutputCharacterW(g_hConOut, *utf8usingbuf, length,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006247 coord, &cchwritten) == 0
6248 || cchwritten == 0 || cchwritten == (DWORD)-1)
6249 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006250 }
6251 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006252 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006253 if (WriteConsoleW(g_hConOut, *utf8usingbuf, length, &cchwritten,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006254 NULL) == 0 || cchwritten == 0)
6255 cchwritten = 1;
6256 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006257
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006258 if (cchwritten == length)
6259 {
6260 written = cbToWrite;
6261 g_coord.X += (SHORT)cells;
6262 }
6263 else
6264 {
6265 char_u *p = pchBuf;
6266 for (n = 0; n < cchwritten; n++)
6267 MB_CPTR_ADV(p);
6268 written = p - pchBuf;
6269 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272 while (g_coord.X > g_srScrollRegion.Right)
6273 {
6274 g_coord.X -= (SHORT) Columns;
6275 if (g_coord.Y < g_srScrollRegion.Bottom)
6276 g_coord.Y++;
6277 }
6278
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006279 // Cursor under VTP is always in the correct position, no need to reset.
6280 if (!USE_VTP)
6281 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 return written;
6284}
6285
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006286 static char_u *
6287get_seq(
6288 int *args,
6289 int *count,
6290 char_u *head)
6291{
6292 int argc;
6293 char_u *p;
6294
6295 if (head == NULL || *head != '\033')
6296 return NULL;
6297
6298 argc = 0;
6299 p = head;
6300 ++p;
6301 do
6302 {
6303 ++p;
6304 args[argc] = getdigits(&p);
6305 argc += (argc < 15) ? 1 : 0;
6306 } while (*p == ';');
6307 *count = argc;
6308
6309 return p;
6310}
6311
6312 static char_u *
6313get_sgr(
6314 int *args,
6315 int *count,
6316 char_u *head)
6317{
6318 char_u *p = get_seq(args, count, head);
6319
6320 return (p && *p == 'm') ? ++p : NULL;
6321}
6322
6323/*
6324 * Pointer to next if SGR (^[[n;2;*;*;*m), NULL otherwise.
6325 */
6326 static char_u *
6327sgrn2(
6328 char_u *head,
6329 int n)
6330{
6331 int argc;
6332 int args[16];
6333 char_u *p = get_sgr(args, &argc, head);
6334
6335 return p && argc == 5 && args[0] == n && args[1] == 2 ? p : NULL;
6336}
6337
6338/*
6339 * Pointer to next if SGR(^[[nm)<space>ESC, NULL otherwise.
6340 */
6341 static char_u *
6342sgrnc(
6343 char_u *head,
6344 int n)
6345{
6346 int argc;
6347 int args[16];
6348 char_u *p = get_sgr(args, &argc, head);
6349
6350 return p && argc == 1 && args[0] == n && (p = skipwhite(p)) && *p == '\033'
6351 ? p : NULL;
6352}
6353
6354 static char_u *
6355skipblank(char_u *q)
6356{
6357 char_u *p = q;
6358
6359 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
6360 ++p;
6361 return p;
6362}
6363
6364/*
6365 * Pointer to the next if any whitespace that may follow SGR is ESC, otherwise
6366 * NULL.
6367 */
6368 static char_u *
6369sgrn2c(
6370 char_u *head,
6371 int n)
6372{
6373 char_u *p = sgrn2(head, n);
6374
6375 return p && *p != NUL && (p = skipblank(p)) && *p == '\033' ? p : NULL;
6376}
6377
6378/*
6379 * If there is only a newline between the sequence immediately following it,
6380 * a pointer to the character following the newline is returned.
6381 * Otherwise NULL.
6382 */
6383 static char_u *
6384sgrn2cn(
6385 char_u *head,
6386 int n)
6387{
6388 char_u *p = sgrn2(head, n);
6389
6390 return p && p[0] == 0x0a && p[1] == '\033' ? ++p : NULL;
6391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393/*
6394 * mch_write(): write the output buffer to the screen, translating ESC
6395 * sequences into calls to console output routines.
6396 */
6397 void
6398mch_write(
6399 char_u *s,
6400 int len)
6401{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006402# ifdef VIMDLL
6403 if (gui.in_use)
6404 return;
6405# endif
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 s[len] = NUL;
6408
6409 if (!term_console)
6410 {
6411 write(1, s, (unsigned)len);
6412 return;
6413 }
6414
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 while (len--)
6417 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006418 int prefix = -1;
6419 char_u ch;
6420
6421 // While processing a sequence, on rare occasions it seems that another
6422 // sequence may be inserted asynchronously.
6423 if (len < 0)
6424 {
6425 redraw_all_later(CLEAR);
6426 return;
6427 }
6428
6429 while((ch = s[++prefix]))
6430 if (ch <= 0x1e && !(ch != '\n' && ch != '\r' && ch != '\b'
6431 && ch != '\a' && ch != '\033'))
6432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 if (p_wd)
6435 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006436 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (prefix != 0)
6438 prefix = 1;
6439 }
6440
6441 if (prefix != 0)
6442 {
6443 DWORD nWritten;
6444
6445 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006446# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (fdDump)
6448 {
6449 fputc('>', fdDump);
6450 fwrite(s, sizeof(char_u), nWritten, fdDump);
6451 fputs("<\n", fdDump);
6452 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 len -= (nWritten - 1);
6455 s += nWritten;
6456 }
6457 else if (s[0] == '\n')
6458 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006459 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if (g_coord.Y == g_srScrollRegion.Bottom)
6461 {
6462 scroll(1);
6463 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6464 }
6465 else
6466 {
6467 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6468 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006469# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (fdDump)
6471 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 s++;
6474 }
6475 else if (s[0] == '\r')
6476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006477 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (fdDump)
6481 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 s++;
6484 }
6485 else if (s[0] == '\b')
6486 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006487 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (g_coord.X > g_srScrollRegion.Left)
6489 g_coord.X--;
6490 else if (g_coord.Y > g_srScrollRegion.Top)
6491 {
6492 g_coord.X = g_srScrollRegion.Right;
6493 g_coord.Y--;
6494 }
6495 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006496# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (fdDump)
6498 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 s++;
6501 }
6502 else if (s[0] == '\a')
6503 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006504 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006506# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (fdDump)
6508 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 s++;
6511 }
6512 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6513 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006514# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006515 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006517 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006518 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006519 char_u *sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 switch (s[2])
6522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 case '0': case '1': case '2': case '3': case '4':
6524 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006525 if (*(p = get_seq(args, &argc, s)) != 'm')
6526 goto notsgr;
6527
6528 p = s;
6529
6530 // Handling frequent optional sequences. Output to the screen
6531 // takes too long, so do not output as much as possible.
6532
6533 // If resetFG,FG,BG,<cr>,BG,FG are connected, the preceding
6534 // resetFG,FG,BG are omitted.
6535 if (sgrn2(sgrn2(sgrn2cn(sgrn2(sgrnc(p, 39), 38), 48), 48), 38))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006536 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006537 p = sgrn2(sgrn2(sgrnc(p, 39), 38), 48);
6538 len = len + 1 - (int)(p - s);
6539 s = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 break;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006543 // If FG,BG,BG,FG of SGR are connected, the first FG can be
6544 // omitted.
6545 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 48), 38))
6546 p = sp;
6547
6548 // If FG,BG,FG,BG of SGR are connected, the first FG can be
6549 // omitted.
6550 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 38), 48))
6551 p = sp;
6552
6553 // If BG,BG of SGR are connected, the first BG can be omitted.
6554 if (sgrn2((sp = sgrn2(p, 48)), 48))
6555 p = sp;
6556
6557 // If restoreFG and FG are connected, the restoreFG can be
6558 // omitted.
6559 if (sgrn2((sp = sgrnc(p, 39)), 38))
6560 p = sp;
6561
6562 p = get_seq(args, &argc, p);
6563
6564notsgr:
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006565 arg1 = args[0];
6566 arg2 = args[1];
6567 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006569 if (argc == 1 && args[0] == 0)
6570 normvideo();
6571 else if (argc == 1)
6572 {
6573 if (USE_VTP)
6574 textcolor((WORD) arg1);
6575 else
6576 textattr((WORD) arg1);
6577 }
6578 else if (USE_VTP)
6579 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006581 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006583 gotoxy(arg2, arg1);
6584 }
6585 else if (argc == 2 && *p == 'r')
6586 {
6587 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6588 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006589 else if (argc == 2 && *p == 'R')
6590 {
6591 set_scroll_region_tb(arg1, arg2);
6592 }
6593 else if (argc == 2 && *p == 'V')
6594 {
6595 set_scroll_region_lr(arg1, arg2);
6596 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006597 else if (argc == 1 && *p == 'A')
6598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 gotoxy(g_coord.X + 1,
6600 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6601 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006602 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
6604 textbackground((WORD) arg1);
6605 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006606 else if (argc == 1 && *p == 'C')
6607 {
6608 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6609 g_coord.Y + 1);
6610 }
6611 else if (argc == 1 && *p == 'f')
6612 {
6613 textcolor((WORD) arg1);
6614 }
6615 else if (argc == 1 && *p == 'H')
6616 {
6617 gotoxy(1, arg1);
6618 }
6619 else if (argc == 1 && *p == 'L')
6620 {
6621 insert_lines(arg1);
6622 }
6623 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
6625 delete_lines(arg1);
6626 }
6627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006628 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 s = p + 1;
6630 break;
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 gotoxy(g_coord.X + 1,
6634 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6635 goto got3;
6636
6637 case 'B':
6638 visual_bell();
6639 goto got3;
6640
6641 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6643 g_coord.Y + 1);
6644 goto got3;
6645
6646 case 'E':
6647 termcap_mode_end();
6648 goto got3;
6649
6650 case 'F':
6651 standout();
6652 goto got3;
6653
6654 case 'f':
6655 standend();
6656 goto got3;
6657
6658 case 'H':
6659 gotoxy(1, 1);
6660 goto got3;
6661
6662 case 'j':
6663 clear_to_end_of_display();
6664 goto got3;
6665
6666 case 'J':
6667 clear_screen();
6668 goto got3;
6669
6670 case 'K':
6671 clear_to_end_of_line();
6672 goto got3;
6673
6674 case 'L':
6675 insert_lines(1);
6676 goto got3;
6677
6678 case 'M':
6679 delete_lines(1);
6680 goto got3;
6681
6682 case 'S':
6683 termcap_mode_start();
6684 goto got3;
6685
6686 case 'V':
6687 cursor_visible(TRUE);
6688 goto got3;
6689
6690 case 'v':
6691 cursor_visible(FALSE);
6692 goto got3;
6693
6694 got3:
6695 s += 3;
6696 len -= 2;
6697 }
6698
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006699# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (fdDump)
6701 {
6702 fputs("ESC | ", fdDump);
6703 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6704 fputc('\n', fdDump);
6705 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708 else
6709 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 DWORD nWritten;
6712
6713 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006714# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (fdDump)
6716 {
6717 fputc('>', fdDump);
6718 fwrite(s, sizeof(char_u), nWritten, fdDump);
6719 fputs("<\n", fdDump);
6720 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
6723 len -= (nWritten - 1);
6724 s += nWritten;
6725 }
6726 }
6727
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006728# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (fdDump)
6730 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736
6737/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006738 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 */
6740 void
6741mch_delay(
6742 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006743 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006745#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006746 Sleep((int)msec); // never wait for input
6747#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006748# ifdef VIMDLL
6749 if (gui.in_use)
6750 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006751 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006752 return;
6753 }
6754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006756# ifdef FEAT_MZSCHEME
6757 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6758 {
6759 int towait = p_mzq;
6760
Bram Moolenaar0f873732019-12-05 20:28:46 +01006761 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006762 while (msec > 0)
6763 {
6764 mzvim_check_threads();
6765 if (msec < towait)
6766 towait = msec;
6767 Sleep(towait);
6768 msec -= towait;
6769 }
6770 }
6771 else
6772# endif
6773 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006775 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776#endif
6777}
6778
6779
6780/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006781 * This version of remove is not scared by a readonly (backup) file.
6782 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 * Return 0 for success, -1 for failure.
6784 */
6785 int
6786mch_remove(char_u *name)
6787{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006788 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 int n;
6790
Bram Moolenaar203258c2016-01-17 22:15:16 +01006791 /*
6792 * On Windows, deleting a directory's symbolic link is done by
6793 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6794 */
6795 if (mch_isdir(name) && mch_is_symbolic_link(name))
6796 return mch_rmdir(name);
6797
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006798 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6799
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006800 wn = enc_to_utf16(name, NULL);
6801 if (wn == NULL)
6802 return -1;
6803
6804 n = DeleteFileW(wn) ? 0 : -1;
6805 vim_free(wn);
6806 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807}
6808
6809
6810/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006811 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 */
6813 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006814mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006816#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6817# ifdef VIMDLL
6818 if (!gui.in_use)
6819# endif
6820 if (g_fCtrlCPressed || g_fCBrkPressed)
6821 {
6822 ctrl_break_was_pressed = g_fCBrkPressed;
6823 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6824 got_int = TRUE;
6825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#endif
6827}
6828
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006830#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006831
6832/*
6833 * How much main memory in KiB that can be used by VIM.
6834 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006835 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006836mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006837{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006838 MEMORYSTATUSEX ms;
6839
Bram Moolenaar0f873732019-12-05 20:28:46 +01006840 // Need to use GlobalMemoryStatusEx() when there is more memory than
6841 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006842 ms.dwLength = sizeof(MEMORYSTATUSEX);
6843 GlobalMemoryStatusEx(&ms);
6844 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006845 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006846 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006847 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006848 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006849 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006851 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006852 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006853 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006854 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006855 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006859 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6861 * file whose short file name is "FOO.BAR" (its long file name will
6862 * be correct: "foo.bar~"). Because a file can be accessed by
6863 * either its SFN or its LFN, "foo.bar" has effectively been
6864 * renamed to "foo.bar", which is not at all what was wanted. This
6865 * seems to happen only when renaming files with three-character
6866 * extensions by appending a suffix that does not include ".".
6867 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6868 *
6869 * There is another problem, which isn't really a bug but isn't right either:
6870 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6871 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6872 * service pack 6. Doesn't seem to happen on Windows 98.
6873 *
6874 * Like rename(), returns 0 upon success, non-zero upon failure.
6875 * Should probably set errno appropriately when errors occur.
6876 */
6877 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006878mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006880 WCHAR *p;
6881 int i;
6882 WCHAR szTempFile[_MAX_PATH + 1];
6883 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006886 // No need to play tricks unless the file name contains a "~" as the
6887 // seventh character.
6888 p = wold;
6889 for (i = 0; wold[i] != NUL; ++i)
6890 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6891 && wold[i + 1] != 0)
6892 p = wold + i + 1;
6893 if ((int)(wold + i - p) < 8 || p[6] != '~')
6894 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006896 // Get base path of new file name. Undocumented feature: If pszNewFile is
6897 // a directory, no error is returned and pszFilePart will be NULL.
6898 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006900 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006902 // Get (and create) a unique temporary file name in directory of new file
6903 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return -2;
6905
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006906 // blow the temp file away
6907 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 return -3;
6909
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006910 // rename old file to the temp file
6911 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 return -4;
6913
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006914 // now create an empty file called pszOldFile; this prevents the operating
6915 // system using pszOldFile as an alias (SFN) if we're renaming within the
6916 // same directory. For example, we're editing a file called
6917 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6918 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6919 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6920 // cause all sorts of problems later in buf_write(). So, we create an
6921 // empty file called filena~1.txt and the system will have to find some
6922 // other SFN for filena~1.txt~, such as filena~2.txt
6923 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6925 return -5;
6926 if (!CloseHandle(hf))
6927 return -6;
6928
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006929 // rename the temp file to the new file
6930 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006932 // Renaming failed. Rename the file back to its old name, so that it
6933 // looks like nothing happened.
6934 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 return -7;
6936 }
6937
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006938 // Seems to be left around on Novell filesystems
6939 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006941 // finally, remove the empty old file
6942 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 return -8;
6944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006945 return 0;
6946}
6947
6948
6949/*
6950 * Converts the filenames to UTF-16, then call mch_wrename().
6951 * Like rename(), returns 0 upon success, non-zero upon failure.
6952 */
6953 int
6954mch_rename(
6955 const char *pszOldFile,
6956 const char *pszNewFile)
6957{
6958 WCHAR *wold = NULL;
6959 WCHAR *wnew = NULL;
6960 int retval = -1;
6961
6962 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6963 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6964 if (wold != NULL && wnew != NULL)
6965 retval = mch_wrename(wold, wnew);
6966 vim_free(wold);
6967 vim_free(wnew);
6968 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970
6971/*
6972 * Get the default shell for the current hardware platform
6973 */
6974 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006975default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006977 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978}
6979
6980/*
6981 * mch_access() extends access() to do more detailed check on network drives.
6982 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6983 */
6984 int
6985mch_access(char *n, int p)
6986{
6987 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006988 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006989 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006991 wn = enc_to_utf16((char_u *)n, NULL);
6992 if (wn == NULL)
6993 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006995 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999 if (p & R_OK)
7000 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 // Read check is performed by seeing if we can do a find file on
7002 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007003 int i;
7004 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007006 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
7007 TempNameW[i] = wn[i];
7008 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
7009 TempNameW[i++] = '\\';
7010 TempNameW[i++] = '*';
7011 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007013 hFile = FindFirstFileW(TempNameW, &d);
7014 if (hFile == INVALID_HANDLE_VALUE)
7015 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019
7020 if (p & W_OK)
7021 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007022 // Trying to create a temporary file in the directory should catch
7023 // directories on read-only network shares. However, in
7024 // directories whose ACL allows writes but denies deletes will end
7025 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007026 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
7027 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007028 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007029 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 }
7032 else
7033 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007034 // Don't consider a file read-only if another process has opened it.
7035 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
7036
Bram Moolenaar0f873732019-12-05 20:28:46 +01007037 // Trying to open the file for the required access does ACL, read-only
7038 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007039 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
7040 | ((p & R_OK) ? GENERIC_READ : 0);
7041
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007042 hFile = CreateFileW(wn, access_mode, share_mode,
7043 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (hFile == INVALID_HANDLE_VALUE)
7045 goto getout;
7046 CloseHandle(hFile);
7047 }
7048
Bram Moolenaar0f873732019-12-05 20:28:46 +01007049 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 return retval;
7053}
7054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007056 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 */
7058 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007059mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 WCHAR *wn;
7062 int f;
7063
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007064 wn = enc_to_utf16((char_u *)name, NULL);
7065 if (wn == NULL)
7066 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007068 f = _wopen(wn, flags, mode);
7069 vim_free(wn);
7070 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072
7073/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007074 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 */
7076 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007077mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 WCHAR *wn, *wm;
7080 FILE *f = NULL;
7081
Bram Moolenaara12a1612019-01-24 16:39:02 +01007082#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01007083 // Work around an annoying assertion in the Microsoft debug CRT
7084 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007085 char newMode = mode[strlen(mode) - 1];
7086 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007087
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007088 _get_fmode(&oldMode);
7089 if (newMode == 't')
7090 _set_fmode(_O_TEXT);
7091 else if (newMode == 'b')
7092 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007093#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007094 wn = enc_to_utf16((char_u *)name, NULL);
7095 wm = enc_to_utf16((char_u *)mode, NULL);
7096 if (wn != NULL && wm != NULL)
7097 f = _wfopen(wn, wm);
7098 vim_free(wn);
7099 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007100
Bram Moolenaara12a1612019-01-24 16:39:02 +01007101#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007102 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007103#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007104 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107/*
7108 * SUB STREAM (aka info stream) handling:
7109 *
7110 * NTFS can have sub streams for each file. Normal contents of file is
7111 * stored in the main stream, and extra contents (author information and
7112 * title and so on) can be stored in sub stream. After Windows 2000, user
7113 * can access and store those informations in sub streams via explorer's
7114 * property menuitem in right click menu. Those informations in sub streams
7115 * were lost when copying only the main stream. So we have to copy sub
7116 * streams.
7117 *
7118 * Incomplete explanation:
7119 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
7120 * More useful info and an example:
7121 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
7122 */
7123
7124/*
7125 * Copy info stream data "substream". Read from the file with BackupRead(sh)
7126 * and write to stream "substream" of file "to".
7127 * Errors are ignored.
7128 */
7129 static void
7130copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
7131{
7132 HANDLE hTo;
7133 WCHAR *to_name;
7134
7135 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
7136 wcscpy(to_name, to);
7137 wcscat(to_name, substream);
7138
7139 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
7140 FILE_ATTRIBUTE_NORMAL, NULL);
7141 if (hTo != INVALID_HANDLE_VALUE)
7142 {
7143 long done;
7144 DWORD todo;
7145 DWORD readcnt, written;
7146 char buf[4096];
7147
Bram Moolenaar0f873732019-12-05 20:28:46 +01007148 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 for (done = 0; done < len; done += written)
7150 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007151 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007152 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
7153 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
7155 FALSE, FALSE, context)
7156 || readcnt != todo
7157 || !WriteFile(hTo, buf, todo, &written, NULL)
7158 || written != todo)
7159 break;
7160 }
7161 CloseHandle(hTo);
7162 }
7163
7164 free(to_name);
7165}
7166
7167/*
7168 * Copy info streams from file "from" to file "to".
7169 */
7170 static void
7171copy_infostreams(char_u *from, char_u *to)
7172{
7173 WCHAR *fromw;
7174 WCHAR *tow;
7175 HANDLE sh;
7176 WIN32_STREAM_ID sid;
7177 int headersize;
7178 WCHAR streamname[_MAX_PATH];
7179 DWORD readcount;
7180 void *context = NULL;
7181 DWORD lo, hi;
7182 int len;
7183
Bram Moolenaar0f873732019-12-05 20:28:46 +01007184 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007185 fromw = enc_to_utf16(from, NULL);
7186 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 if (fromw != NULL && tow != NULL)
7188 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007189 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
7191 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
7192 if (sh != INVALID_HANDLE_VALUE)
7193 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007194 // Use BackupRead() to find the info streams. Repeat until we
7195 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 for (;;)
7197 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007198 // Get the header to find the length of the stream name. If
7199 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007201 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
7203 &readcount, FALSE, FALSE, &context)
7204 || readcount == 0)
7205 break;
7206
Bram Moolenaar0f873732019-12-05 20:28:46 +01007207 // We only deal with streams that have a name. The normal
7208 // file data appears to be without a name, even though docs
7209 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (sid.dwStreamNameSize > 0)
7211 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007212 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (!BackupRead(sh, (LPBYTE)streamname,
7214 sid.dwStreamNameSize,
7215 &readcount, FALSE, FALSE, &context))
7216 break;
7217
Bram Moolenaar0f873732019-12-05 20:28:46 +01007218 // Copy an info stream with a name ":anything:$DATA".
7219 // Skip "::$DATA", it has no stream name (examples suggest
7220 // it might be used for the normal file contents).
7221 // Note that BackupRead() counts bytes, but the name is in
7222 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 len = readcount / sizeof(WCHAR);
7224 streamname[len] = 0;
7225 if (len > 7 && wcsicmp(streamname + len - 6,
7226 L":$DATA") == 0)
7227 {
7228 streamname[len - 6] = 0;
7229 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007230 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232 }
7233
Bram Moolenaar0f873732019-12-05 20:28:46 +01007234 // Advance to the next stream. We might try seeking too far,
7235 // but BackupSeek() doesn't skip over stream borders, thus
7236 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 &lo, &hi, &context);
7239 }
7240
Bram Moolenaar0f873732019-12-05 20:28:46 +01007241 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
7243
7244 CloseHandle(sh);
7245 }
7246 }
7247 vim_free(fromw);
7248 vim_free(tow);
7249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251/*
7252 * Copy file attributes from file "from" to file "to".
7253 * For Windows NT and later we copy info streams.
7254 * Always returns zero, errors are ignored.
7255 */
7256 int
7257mch_copy_file_attribute(char_u *from, char_u *to)
7258{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007259 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007260 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 return 0;
7262}
7263
7264#if defined(MYRESETSTKOFLW) || defined(PROTO)
7265/*
7266 * Recreate a destroyed stack guard page in win32.
7267 * Written by Benjamin Peterson.
7268 */
7269
Bram Moolenaar0f873732019-12-05 20:28:46 +01007270// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007271# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273/*
7274 * This function does the same thing as _resetstkoflw(), which is only
7275 * available in DevStudio .net and later.
7276 * Returns 0 for failure, 1 for success.
7277 */
7278 int
7279myresetstkoflw(void)
7280{
7281 BYTE *pStackPtr;
7282 BYTE *pGuardPage;
7283 BYTE *pStackBase;
7284 BYTE *pLowestPossiblePage;
7285 MEMORY_BASIC_INFORMATION mbi;
7286 SYSTEM_INFO si;
7287 DWORD nPageSize;
7288 DWORD dummy;
7289
Bram Moolenaar0f873732019-12-05 20:28:46 +01007290 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 GetSystemInfo(&si);
7292 nPageSize = si.dwPageSize;
7293
Bram Moolenaar0f873732019-12-05 20:28:46 +01007294 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 pStackPtr = (BYTE*)_alloca(1);
7296
Bram Moolenaar0f873732019-12-05 20:28:46 +01007297 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
7299 return 0;
7300 pStackBase = (BYTE*)mbi.AllocationBase;
7301
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007302 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01007303 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007304 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007307 // We want the first committed page in the stack Start at the stack
7308 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 BYTE *pBlock = pStackBase;
7310
Bram Moolenaara466c992005-07-09 21:03:22 +00007311 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 {
7313 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
7314 return 0;
7315
7316 pBlock += mbi.RegionSize;
7317
7318 if (mbi.State & MEM_COMMIT)
7319 break;
7320 }
7321
Bram Moolenaar0f873732019-12-05 20:28:46 +01007322 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (mbi.Protect & PAGE_GUARD)
7324 return 1;
7325
Bram Moolenaar0f873732019-12-05 20:28:46 +01007326 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
7328 pGuardPage = pLowestPossiblePage;
7329 else
7330 pGuardPage = (BYTE*)mbi.BaseAddress;
7331
Bram Moolenaar0f873732019-12-05 20:28:46 +01007332 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7334 return 0;
7335
Bram Moolenaar0f873732019-12-05 20:28:46 +01007336 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7338 &dummy))
7339 return 0;
7340 }
7341
7342 return 1;
7343}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007346
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007347/*
7348 * The command line arguments in UCS2
7349 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007350static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007351static LPWSTR *ArglistW = NULL;
7352static int global_argc = 0;
7353static char **global_argv;
7354
Bram Moolenaar0f873732019-12-05 20:28:46 +01007355static int used_file_argc = 0; // last argument in global_argv[] used
7356 // for the argument list.
7357static int *used_file_indexes = NULL; // indexes in global_argv[] for
7358 // command line arguments added to
7359 // the argument list
7360static int used_file_count = 0; // nr of entries in used_file_indexes
7361static int used_file_literal = FALSE; // take file names literally
7362static int used_file_full_path = FALSE; // file name was full path
7363static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007364static int used_alist_count = 0;
7365
7366
7367/*
7368 * Get the command line arguments. Unicode version.
7369 * Returns argc. Zero when something fails.
7370 */
7371 int
7372get_cmd_argsW(char ***argvp)
7373{
7374 char **argv = NULL;
7375 int argc = 0;
7376 int i;
7377
Bram Moolenaar14993322014-09-09 12:25:33 +02007378 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007379 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7380 if (ArglistW != NULL)
7381 {
7382 argv = malloc((nArgsW + 1) * sizeof(char *));
7383 if (argv != NULL)
7384 {
7385 argc = nArgsW;
7386 argv[argc] = NULL;
7387 for (i = 0; i < argc; ++i)
7388 {
7389 int len;
7390
Bram Moolenaar0f873732019-12-05 20:28:46 +01007391 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007392 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007393 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007394 (LPSTR *)&argv[i], &len, 0, 0);
7395 if (argv[i] == NULL)
7396 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007397 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007398 while (i > 0)
7399 free(argv[--i]);
7400 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007401 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007402 argc = 0;
7403 }
7404 }
7405 }
7406 }
7407
7408 global_argc = argc;
7409 global_argv = argv;
7410 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007411 {
7412 if (used_file_indexes != NULL)
7413 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007414 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007415 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007416
7417 if (argvp != NULL)
7418 *argvp = argv;
7419 return argc;
7420}
7421
7422 void
7423free_cmd_argsW(void)
7424{
7425 if (ArglistW != NULL)
7426 {
7427 GlobalFree(ArglistW);
7428 ArglistW = NULL;
7429 }
7430}
7431
7432/*
7433 * Remember "name" is an argument that was added to the argument list.
7434 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7435 * is called.
7436 */
7437 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007438used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007439{
7440 int i;
7441
7442 if (used_file_indexes == NULL)
7443 return;
7444 for (i = used_file_argc + 1; i < global_argc; ++i)
7445 if (STRCMP(global_argv[i], name) == 0)
7446 {
7447 used_file_argc = i;
7448 used_file_indexes[used_file_count++] = i;
7449 break;
7450 }
7451 used_file_literal = literal;
7452 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007453 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007454}
7455
7456/*
7457 * Remember the length of the argument list as it was. If it changes then we
7458 * leave it alone when 'encoding' is set.
7459 */
7460 void
7461set_alist_count(void)
7462{
7463 used_alist_count = GARGCOUNT;
7464}
7465
7466/*
7467 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7468 * has been changed while starting up. Use the UCS-2 command line arguments
7469 * and convert them to 'encoding'.
7470 */
7471 void
7472fix_arg_enc(void)
7473{
7474 int i;
7475 int idx;
7476 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007477 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007478
Bram Moolenaar0f873732019-12-05 20:28:46 +01007479 // Safety checks:
7480 // - if argument count differs between the wide and non-wide argument
7481 // list, something must be wrong.
7482 // - the file name arguments must have been located.
7483 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007484 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007485 || ArglistW == NULL
7486 || used_file_indexes == NULL
7487 || used_file_count == 0
7488 || used_alist_count != GARGCOUNT)
7489 return;
7490
Bram Moolenaar0f873732019-12-05 20:28:46 +01007491 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007492 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007493 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007494 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007495 for (i = 0; i < GARGCOUNT; ++i)
7496 fnum_list[i] = GARGLIST[i].ae_fnum;
7497
Bram Moolenaar0f873732019-12-05 20:28:46 +01007498 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007499 alist_clear(&global_alist);
7500 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007501 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007502
7503 for (i = 0; i < used_file_count; ++i)
7504 {
7505 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007506 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007507 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007508 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007509 int literal = used_file_literal;
7510
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007511#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007512 // When using diff mode may need to concatenate file name to
7513 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007514 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7515 && !mch_isdir(alist_name(&GARGLIST[0])))
7516 {
7517 char_u *r;
7518
7519 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7520 if (r != NULL)
7521 {
7522 vim_free(str);
7523 str = r;
7524 }
7525 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007526#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007527 // Re-use the old buffer by renaming it. When not using literal
7528 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007529 if (used_file_literal)
7530 buf_set_name(fnum_list[i], str);
7531
Bram Moolenaar0f873732019-12-05 20:28:46 +01007532 // Check backtick literal. backtick literal is already expanded in
7533 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007534 if (literal == FALSE)
7535 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007536 size_t len = STRLEN(str);
7537
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007538 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7539 literal = TRUE;
7540 }
7541 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007542 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007543 }
7544
7545 if (!used_file_literal)
7546 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007547 // Now expand wildcards in the arguments.
7548 // Temporarily add '(' and ')' to 'isfname'. These are valid
7549 // filename characters but are excluded from 'isfname' to make
7550 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7551 // Also, unset wildignore to not be influenced by this option.
7552 // The arguments specified in command-line should be kept even if
7553 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007554 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007555 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007556 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007557 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007558 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007559 }
7560
Bram Moolenaar0f873732019-12-05 20:28:46 +01007561 // If wildcard expansion failed, we are editing the first file of the
7562 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007563 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7564 {
7565 do_cmdline_cmd((char_u *)":rewind");
7566 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007567 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007568 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007569
7570 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007571}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007572
7573 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007574mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007575{
7576 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007577 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007578
Bram Moolenaar964b3742019-05-24 18:54:09 +02007579 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007580 if (envbuf == NULL)
7581 return -1;
7582
7583 sprintf((char *)envbuf, "%s=%s", var, value);
7584
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007585 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007586
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007587 vim_free(envbuf);
7588 if (p == NULL)
7589 return -1;
7590 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007591#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007592 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007593#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007594 // Unlike Un*x systems, we can free the string for _wputenv().
7595 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007596
7597 return 0;
7598}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007599
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007600/*
7601 * Support for 256 colors and 24-bit colors was added in Windows 10
7602 * version 1703 (Creators update).
7603 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007604#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7605
7606/*
7607 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007608 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007609 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007610#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007611
7612/*
7613 * ConPTY differences between versions, need different logic.
7614 * version 1903 (May 2019 update).
7615 */
7616#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7617
7618/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007619 * version 1909 (November 2019 update).
7620 */
7621#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7622
7623/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007624 * Stay ahead of the next update, and when it's done, fix this.
7625 * version ? (2020 update, temporarily use the build number of insider preview)
7626 */
7627#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
7628
7629/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007630 * Confirm until this version. Also the logic changes.
7631 * insider preview.
7632 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007633#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007634
7635/*
7636 * Not stable now.
7637 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007638#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007639
7640 static void
7641vtp_flag_init(void)
7642{
7643 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007644#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007645 DWORD mode;
7646 HANDLE out;
7647
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007648# ifdef VIMDLL
7649 if (!gui.in_use)
7650# endif
7651 {
7652 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007653
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007654 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7655 GetConsoleMode(out, &mode);
7656 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7657 if (SetConsoleMode(out, mode) == 0)
7658 vtp_working = 0;
7659 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007660#endif
7661
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007662 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007663 conpty_working = 1;
7664 if (ver >= CONPTY_STABLE_BUILD)
7665 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007666
Bram Moolenaar57da6982019-09-13 22:30:11 +02007667 if (ver <= CONPTY_INSIDER_BUILD)
7668 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007669 if (ver <= CONPTY_1909_BUILD)
7670 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007671 if (ver <= CONPTY_1903_BUILD)
7672 conpty_type = 2;
7673 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7674 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007675
7676 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
7677 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007678}
7679
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007680#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007681
7682 static void
7683vtp_init(void)
7684{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007685 HMODULE hKerneldll;
7686 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007687# ifdef FEAT_TERMGUICOLORS
7688 COLORREF fg, bg;
7689# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007690
Bram Moolenaar0f873732019-12-05 20:28:46 +01007691 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007692 hKerneldll = GetModuleHandle("kernel32.dll");
7693 if (hKerneldll != NULL)
7694 {
7695 pGetConsoleScreenBufferInfoEx =
7696 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7697 hKerneldll, "GetConsoleScreenBufferInfoEx");
7698 pSetConsoleScreenBufferInfoEx =
7699 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7700 hKerneldll, "SetConsoleScreenBufferInfoEx");
7701 if (pGetConsoleScreenBufferInfoEx != NULL
7702 && pSetConsoleScreenBufferInfoEx != NULL)
7703 has_csbiex = TRUE;
7704 }
7705
7706 csbi.cbSize = sizeof(csbi);
7707 if (has_csbiex)
7708 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007709 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7710 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007711 store_console_bg_rgb = save_console_bg_rgb;
7712 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007713
7714# ifdef FEAT_TERMGUICOLORS
7715 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7716 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7717 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7718 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7719 default_console_color_bg = bg;
7720 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007721# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007722
7723 set_console_color_rgb();
7724}
7725
7726 static void
7727vtp_exit(void)
7728{
Bram Moolenaardf543822020-01-30 11:53:59 +01007729 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007730}
7731
Bram Moolenaar06b7b582020-05-30 17:49:25 +02007732 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007733vtp_printf(
7734 char *format,
7735 ...)
7736{
7737 char_u buf[100];
7738 va_list list;
7739 DWORD result;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007740 int len;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007741
7742 va_start(list, format);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007743 len = vim_vsnprintf((char *)buf, 100, (char *)format, list);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007744 va_end(list);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007745 WriteConsoleA(g_hConOut, buf, (DWORD)len, &result, NULL);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007746 return (int)result;
7747}
7748
7749 static void
7750vtp_sgr_bulk(
7751 int arg)
7752{
7753 int args[1];
7754
7755 args[0] = arg;
7756 vtp_sgr_bulks(1, args);
7757}
7758
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007759#define FAST256(x) \
7760 if ((*p-- = "0123456789"[(n = x % 10)]) \
7761 && x >= 10 && (*p-- = "0123456789"[((m = x % 100) - n) / 10]) \
7762 && x >= 100 && (*p-- = "012"[((x & 0xff) - m) / 100]));
7763
7764#define FAST256CASE(x) \
7765 case x: \
7766 FAST256(newargs[x - 1]);
7767
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007768 static void
7769vtp_sgr_bulks(
7770 int argc,
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007771 int *args)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007772{
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007773#define MAXSGR 16
7774#define SGRBUFSIZE 2 + 4 * MAXSGR + 1 // '\033[' + SGR + 'm'
7775 char_u buf[SGRBUFSIZE];
7776 char_u *p;
7777 int in, out;
7778 int newargs[16];
7779 static int sgrfgr = -1, sgrfgg, sgrfgb;
7780 static int sgrbgr = -1, sgrbgg, sgrbgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007781
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007782 if (argc == 0)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007783 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007784 sgrfgr = sgrbgr = -1;
7785 vtp_printf("033[m");
7786 return;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007787 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007788
7789 in = out = 0;
7790 while (in < argc)
7791 {
7792 int s = args[in];
7793 int copylen = 1;
7794
7795 if (s == 38)
7796 {
7797 if (argc - in >= 5 && args[in + 1] == 2)
7798 {
7799 if (sgrfgr == args[in + 2] && sgrfgg == args[in + 3]
7800 && sgrfgb == args[in + 4])
7801 {
7802 in += 5;
7803 copylen = 0;
7804 }
7805 else
7806 {
7807 sgrfgr = args[in + 2];
7808 sgrfgg = args[in + 3];
7809 sgrfgb = args[in + 4];
7810 copylen = 5;
7811 }
7812 }
7813 else if (argc - in >= 3 && args[in + 1] == 5)
7814 {
7815 sgrfgr = -1;
7816 copylen = 3;
7817 }
7818 }
7819 else if (s == 48)
7820 {
7821 if (argc - in >= 5 && args[in + 1] == 2)
7822 {
7823 if (sgrbgr == args[in + 2] && sgrbgg == args[in + 3]
7824 && sgrbgb == args[in + 4])
7825 {
7826 in += 5;
7827 copylen = 0;
7828 }
7829 else
7830 {
7831 sgrbgr = args[in + 2];
7832 sgrbgg = args[in + 3];
7833 sgrbgb = args[in + 4];
7834 copylen = 5;
7835 }
7836 }
7837 else if (argc - in >= 3 && args[in + 1] == 5)
7838 {
7839 sgrbgr = -1;
7840 copylen = 3;
7841 }
7842 }
7843 else if (30 <= s && s <= 39)
7844 sgrfgr = -1;
7845 else if (90 <= s && s <= 97)
7846 sgrfgr = -1;
7847 else if (40 <= s && s <= 49)
7848 sgrbgr = -1;
7849 else if (100 <= s && s <= 107)
7850 sgrbgr = -1;
7851 else if (s == 0)
7852 sgrfgr = sgrbgr = -1;
7853
7854 while (copylen--)
7855 newargs[out++] = args[in++];
7856 }
7857
7858 p = &buf[sizeof(buf) - 1];
7859 *p-- = 'm';
7860
7861 switch (out)
7862 {
7863 int n, m;
7864 DWORD r;
7865
7866 FAST256CASE(16);
7867 *p-- = ';';
7868 FAST256CASE(15);
7869 *p-- = ';';
7870 FAST256CASE(14);
7871 *p-- = ';';
7872 FAST256CASE(13);
7873 *p-- = ';';
7874 FAST256CASE(12);
7875 *p-- = ';';
7876 FAST256CASE(11);
7877 *p-- = ';';
7878 FAST256CASE(10);
7879 *p-- = ';';
7880 FAST256CASE(9);
7881 *p-- = ';';
7882 FAST256CASE(8);
7883 *p-- = ';';
7884 FAST256CASE(7);
7885 *p-- = ';';
7886 FAST256CASE(6);
7887 *p-- = ';';
7888 FAST256CASE(5);
7889 *p-- = ';';
7890 FAST256CASE(4);
7891 *p-- = ';';
7892 FAST256CASE(3);
7893 *p-- = ';';
7894 FAST256CASE(2);
7895 *p-- = ';';
7896 FAST256CASE(1);
7897 *p-- = '[';
7898 *p = '\033';
7899 WriteConsoleA(g_hConOut, p, (DWORD)(&buf[SGRBUFSIZE] - p), &r, NULL);
7900 default:
7901 break;
7902 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007903}
7904
Bram Moolenaar06b7b582020-05-30 17:49:25 +02007905 static void
7906wt_init(void)
7907{
7908 wt_working = (mch_getenv("WT_SESSION") != NULL);
7909}
7910
7911 int
7912use_wt(void)
7913{
7914 return USE_WT;
7915}
7916
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007917# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007918 static int
7919ctermtoxterm(
7920 int cterm)
7921{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007922 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007923
7924 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7925 return (((int)r << 16) | ((int)g << 8) | (int)b);
7926}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007927# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007928
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007929 static void
7930set_console_color_rgb(void)
7931{
7932# ifdef FEAT_TERMGUICOLORS
7933 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007934 guicolor_T fg, bg;
7935 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007936
7937 if (!USE_VTP)
7938 return;
7939
Bram Moolenaara050b942019-12-02 21:35:31 +01007940 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7941
Bram Moolenaar06b7b582020-05-30 17:49:25 +02007942 if (USE_WT)
7943 {
7944 term_fg_rgb_color(fg);
7945 term_bg_rgb_color(bg);
7946 return;
7947 }
7948
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007949 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7950 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7951
7952 csbi.cbSize = sizeof(csbi);
7953 if (has_csbiex)
7954 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7955
7956 csbi.cbSize = sizeof(csbi);
7957 csbi.srWindow.Right += 1;
7958 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007959 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7960 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007961 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7962 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007963 if (has_csbiex)
7964 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7965# endif
7966}
7967
Bram Moolenaara050b942019-12-02 21:35:31 +01007968# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7969 void
7970get_default_console_color(
7971 int *cterm_fg,
7972 int *cterm_bg,
7973 guicolor_T *gui_fg,
7974 guicolor_T *gui_bg)
7975{
7976 int id;
7977 guicolor_T guifg = INVALCOLOR;
7978 guicolor_T guibg = INVALCOLOR;
7979 int ctermfg = 0;
7980 int ctermbg = 0;
7981
7982 id = syn_name2id((char_u *)"Normal");
7983 if (id > 0 && p_tgc)
7984 syn_id2colors(id, &guifg, &guibg);
7985 if (guifg == INVALCOLOR)
7986 {
7987 ctermfg = -1;
7988 if (id > 0)
7989 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7990 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7991 : default_console_color_fg;
7992 cterm_normal_fg_gui_color = guifg;
7993 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7994 }
7995 if (guibg == INVALCOLOR)
7996 {
7997 ctermbg = -1;
7998 if (id > 0)
7999 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
8000 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
8001 : default_console_color_bg;
8002 cterm_normal_bg_gui_color = guibg;
8003 ctermbg = ctermbg < 0 ? 0 : ctermbg;
8004 }
8005
8006 *cterm_fg = ctermfg;
8007 *cterm_bg = ctermbg;
8008 *gui_fg = guifg;
8009 *gui_bg = guibg;
8010}
8011# endif
8012
Bram Moolenaardf543822020-01-30 11:53:59 +01008013/*
8014 * Set the console colors to the original colors or the last set colors.
8015 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008016 static void
8017reset_console_color_rgb(void)
8018{
8019# ifdef FEAT_TERMGUICOLORS
8020 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8021
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008022 if (USE_WT)
8023 return;
8024
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008025 csbi.cbSize = sizeof(csbi);
8026 if (has_csbiex)
8027 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8028
8029 csbi.cbSize = sizeof(csbi);
8030 csbi.srWindow.Right += 1;
8031 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008032 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
8033 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
8034 if (has_csbiex)
8035 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8036# endif
8037}
8038
8039/*
8040 * Set the console colors to the original colors.
8041 */
8042 static void
8043restore_console_color_rgb(void)
8044{
8045# ifdef FEAT_TERMGUICOLORS
8046 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8047
8048 csbi.cbSize = sizeof(csbi);
8049 if (has_csbiex)
8050 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8051
8052 csbi.cbSize = sizeof(csbi);
8053 csbi.srWindow.Right += 1;
8054 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008055 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
8056 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008057 if (has_csbiex)
8058 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8059# endif
8060}
8061
8062 void
8063control_console_color_rgb(void)
8064{
8065 if (USE_VTP)
8066 set_console_color_rgb();
8067 else
8068 reset_console_color_rgb();
8069}
8070
8071 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008072use_vtp(void)
8073{
8074 return USE_VTP;
8075}
8076
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008077 int
8078is_term_win32(void)
8079{
8080 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
8081}
8082
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008083 int
8084has_vtp_working(void)
8085{
8086 return vtp_working;
8087}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008088
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01008089#endif
8090
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008091 int
8092has_conpty_working(void)
8093{
8094 return conpty_working;
8095}
8096
8097 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02008098get_conpty_type(void)
8099{
8100 return conpty_type;
8101}
8102
8103 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008104is_conpty_stable(void)
8105{
8106 return conpty_stable;
8107}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008108
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008109 int
8110get_conpty_fix_type(void)
8111{
8112 return conpty_fix_type;
8113}
8114
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008115#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008116 void
8117resize_console_buf(void)
8118{
8119 CONSOLE_SCREEN_BUFFER_INFO csbi;
8120 COORD coord;
8121 SMALL_RECT newsize;
8122
8123 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
8124 {
8125 coord.X = SRWIDTH(csbi.srWindow);
8126 coord.Y = SRHEIGHT(csbi.srWindow);
8127 SetConsoleScreenBufferSize(g_hConOut, coord);
8128
8129 newsize.Left = 0;
8130 newsize.Top = 0;
8131 newsize.Right = coord.X - 1;
8132 newsize.Bottom = coord.Y - 1;
8133 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
8134
8135 SetConsoleScreenBufferSize(g_hConOut, coord);
8136 }
8137}
8138#endif