blob: e281b373cefe310e902b256a275aa2515f9092b2 [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
33/* cproto fails on missing include files */
34#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
85/* Record all output and all keyboard & mouse input */
86/* #define MCH_WRITE_DUMP */
87
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;
146# 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 Moolenaar071d4272004-06-13 20:20:40 +0000150/* Win32 Console handles for input and output */
151static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
152static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
153
154/* Win32 Screen buffer,coordinate,console I/O information */
155static SMALL_RECT g_srScrollRegion;
156static COORD g_coord; /* 0-based, but external coords are 1-based */
157
158/* The attribute of the screen when the editor was started */
159static WORD g_attrDefault = 7; /* lightgray text on black background */
160static WORD g_attrCurrent;
161
162static 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 */
165
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 Moolenaaraa5df7e2019-02-03 14:53:10 +0100191static void vtp_flag_init();
192
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200193#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100194static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100195static void vtp_init();
196static void vtp_exit();
197static int vtp_printf(char *format, ...);
198static void vtp_sgr_bulk(int arg);
199static void vtp_sgr_bulks(int argc, int *argv);
200
201static guicolor_T save_console_bg_rgb;
202static guicolor_T save_console_fg_rgb;
203
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200204static int g_color_index_bg = 0;
205static int g_color_index_fg = 7;
206
207# ifdef FEAT_TERMGUICOLORS
208static int default_console_color_bg = 0x000000; // black
209static int default_console_color_fg = 0xc0c0c0; // white
210# endif
211
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100212# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200213# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100214# else
215# define USE_VTP 0
216# endif
217
218static void set_console_color_rgb(void);
219static void reset_console_color_rgb(void);
220#endif
221
222/* This flag is newly created from Windows 10 */
223#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
224# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
225#endif
226
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200227#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static int suppress_winsize = 1; /* don't fiddle with console */
229#endif
230
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200231static char_u *exe_path = NULL;
232
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100233static BOOL win8_or_later = FALSE;
234
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200235#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100236/* Dynamic loading for portability */
237typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
238{
239 ULONG cbSize;
240 COORD dwSize;
241 COORD dwCursorPosition;
242 WORD wAttributes;
243 SMALL_RECT srWindow;
244 COORD dwMaximumWindowSize;
245 WORD wPopupAttributes;
246 BOOL bFullscreenSupported;
247 COLORREF ColorTable[16];
248} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
249typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
250static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
251typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
252static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
253static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100254#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100255
256/*
257 * Get version number including build number
258 */
259typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100260#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100261 (((major) << 24) | ((minor) << 16) | (build))
262
263 static DWORD
264get_build_number(void)
265{
266 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
267 HMODULE hNtdll;
268 PfnRtlGetVersion pRtlGetVersion;
269 DWORD ver = MAKE_VER(0, 0, 0);
270
271 hNtdll = GetModuleHandle("ntdll.dll");
272 if (hNtdll != NULL)
273 {
274 pRtlGetVersion =
275 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
276 pRtlGetVersion(&osver);
277 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
278 min(osver.dwMinorVersion, 255),
279 min(osver.dwBuildNumber, 32767));
280 }
281 return ver;
282}
283
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200284#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100285/*
286 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100287 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100288 */
289 static BOOL
290read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100291 HANDLE hInput,
292 INPUT_RECORD *lpBuffer,
293 DWORD nLength,
294 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100295{
296 enum
297 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100298 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100299 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100300 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100301 static DWORD s_dwIndex = 0;
302 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100303 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100304 int head;
305 int tail;
306 int i;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100307
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200308 if (nLength == -2)
309 return (s_dwMax > 0) ? TRUE : FALSE;
310
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100311 if (!win8_or_later)
312 {
313 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200314 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
315 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100316 }
317
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100318 if (s_dwMax == 0)
319 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100320 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200321 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
322 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100324 s_dwIndex = 0;
325 s_dwMax = dwEvents;
326 if (dwEvents == 0)
327 {
328 *lpEvents = 0;
329 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100330 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100331
332 if (s_dwMax > 1)
333 {
334 head = 0;
335 tail = s_dwMax - 1;
336 while (head != tail)
337 {
338 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
339 && s_irCache[head + 1].EventType
340 == WINDOW_BUFFER_SIZE_EVENT)
341 {
342 /* Remove duplicate event to avoid flicker. */
343 for (i = head; i < tail; ++i)
344 s_irCache[i] = s_irCache[i + 1];
345 --tail;
346 continue;
347 }
348 head++;
349 }
350 s_dwMax = tail + 1;
351 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100352 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100353
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100354 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200355 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100356 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100357 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100358 return TRUE;
359}
360
361/*
362 * Version of PeekConsoleInput() that works with IME.
363 */
364 static BOOL
365peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100366 HANDLE hInput,
367 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200368 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100369 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100370{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100371 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100372}
373
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100374# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200375 static DWORD
376msg_wait_for_multiple_objects(
377 DWORD nCount,
378 LPHANDLE pHandles,
379 BOOL fWaitAll,
380 DWORD dwMilliseconds,
381 DWORD dwWakeMask)
382{
383 if (read_console_input(NULL, NULL, -2, NULL))
384 return WAIT_OBJECT_0;
385 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
386 dwMilliseconds, dwWakeMask);
387}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100388# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200389
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100390# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200391 static DWORD
392wait_for_single_object(
393 HANDLE hHandle,
394 DWORD dwMilliseconds)
395{
396 if (read_console_input(NULL, NULL, -2, NULL))
397 return WAIT_OBJECT_0;
398 return WaitForSingleObject(hHandle, dwMilliseconds);
399}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100400# endif
401#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 static void
404get_exe_name(void)
405{
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100406 /* Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
407 * as the maximum length that works (plus a NUL byte). */
408#define MAX_ENV_PATH_LEN 8192
409 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200410 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 if (exe_name == NULL)
413 {
414 /* store the name of the executable, may be used for $VIM */
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100415 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (*temp != NUL)
417 exe_name = FullName_save((char_u *)temp, FALSE);
418 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000419
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200420 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000421 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200422 exe_path = vim_strnsave(exe_name,
423 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200424 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000425 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200426 /* Append our starting directory to $PATH, so that when doing
427 * "!xxd" it's found in our starting directory. Needed because
428 * SearchPath() also looks there. */
429 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100430 if (p == NULL
431 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200432 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 if (p == NULL || *p == NUL)
434 temp[0] = NUL;
435 else
436 {
437 STRCPY(temp, p);
438 STRCAT(temp, ";");
439 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200440 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100441 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200442 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000443 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445}
446
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200447/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100448 * Unescape characters in "p" that appear in "escaped".
449 */
450 static void
451unescape_shellxquote(char_u *p, char_u *escaped)
452{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100453 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100454 int n;
455
456 while (*p != NUL)
457 {
458 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
459 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100460 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100461 p += n;
462 l -= n;
463 }
464}
465
466/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200467 * Load library "name".
468 */
469 HINSTANCE
470vimLoadLib(char *name)
471{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200472 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200473
Bram Moolenaarfaca8402012-10-21 02:37:10 +0200474 /* NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
475 * vimLoadLib() recursively, which causes a stack overflow. */
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200476 if (exe_path == NULL)
477 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200478 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200480 WCHAR old_dirw[MAXPATHL];
481
482 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
483 {
484 /* Change directory to where the executable is, both to make
485 * sure we find a .dll there and to avoid looking for a .dll
486 * in the current directory. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100487 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200488 dll = LoadLibrary(name);
489 SetCurrentDirectoryW(old_dirw);
490 return dll;
491 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200492 }
493 return dll;
494}
495
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200496#if defined(VIMDLL) || defined(PROTO)
497/*
498 * Check if the current executable file is for the GUI subsystem.
499 */
500 int
501mch_is_gui_executable(void)
502{
503 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
504 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
505 PIMAGE_NT_HEADERS pPE;
506
507 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
508 return FALSE;
509 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
510 if (pPE->Signature != IMAGE_NT_SIGNATURE)
511 return FALSE;
512 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
513 return TRUE;
514 return FALSE;
515}
516#endif
517
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100518#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
519/*
520 * Get related information about 'funcname' which is imported by 'hInst'.
521 * If 'info' is 0, return the function address.
522 * If 'info' is 1, return the module name which the function is imported from.
523 */
524 static void *
525get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
526{
527 PBYTE pImage = (PBYTE)hInst;
528 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
529 PIMAGE_NT_HEADERS pPE;
530 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
531 PIMAGE_THUNK_DATA pIAT; /* Import Address Table */
532 PIMAGE_THUNK_DATA pINT; /* Import Name Table */
533 PIMAGE_IMPORT_BY_NAME pImpName;
534
535 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
536 return NULL;
537 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
538 if (pPE->Signature != IMAGE_NT_SIGNATURE)
539 return NULL;
540 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
541 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
542 .VirtualAddress);
543 for (; pImpDesc->FirstThunk; ++pImpDesc)
544 {
545 if (!pImpDesc->OriginalFirstThunk)
546 continue;
547 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
548 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
549 for (; pIAT->u1.Function; ++pIAT, ++pINT)
550 {
551 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
552 continue;
553 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
554 + (UINT_PTR)(pINT->u1.AddressOfData));
555 if (strcmp((char *)pImpName->Name, funcname) == 0)
556 {
557 switch (info)
558 {
559 case 0:
560 return (void *)pIAT->u1.Function;
561 case 1:
562 return (void *)(pImage + pImpDesc->Name);
563 default:
564 return NULL;
565 }
566 }
567 }
568 }
569 return NULL;
570}
571
572/*
573 * Get the module handle which 'funcname' in 'hInst' is imported from.
574 */
575 HINSTANCE
576find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
577{
578 char *modulename;
579
580 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
581 if (modulename != NULL)
582 return GetModuleHandleA(modulename);
583 return NULL;
584}
585
586/*
587 * Get the address of 'funcname' which is imported by 'hInst' DLL.
588 */
589 void *
590get_dll_import_func(HINSTANCE hInst, const char *funcname)
591{
592 return get_imported_func_info(hInst, funcname, 0);
593}
594#endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
597# ifndef GETTEXT_DLL
598# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200599# define GETTEXT_DLL_ALT1 "libintl-8.dll"
600# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# endif
Bram Moolenaarf6a2b082012-06-29 13:14:03 +0200602/* Dummy functions */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000603static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200604static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000605static char *null_libintl_textdomain(const char *);
606static char *null_libintl_bindtextdomain(const char *, const char *);
607static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100608static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200610static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000611char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200612char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
613 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000614char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
615char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000617char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
618 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100619int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100622dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int i;
625 static struct
626 {
627 char *name;
628 FARPROC *ptr;
629 } libintl_entry[] =
630 {
631 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200632 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
634 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
635 {NULL, NULL}
636 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100637 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar7554c542018-10-06 15:03:15 +0200639 // No need to initialize twice.
640 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200642 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100643 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100644# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100645 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200646 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100647# endif
648# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200649 if (!hLibintlDLL)
650 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100651# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100652 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200654 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200656 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100657 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200658 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200660 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 for (i = 0; libintl_entry[i].name != NULL
663 && libintl_entry[i].ptr != NULL; ++i)
664 {
665 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
666 libintl_entry[i].name)) == NULL)
667 {
668 dyn_libintl_end();
669 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000670 {
671 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000673 verbose_leave();
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return 0;
676 }
677 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000678
679 /* The bind_textdomain_codeset() function is optional. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000681 "bind_textdomain_codeset");
682 if (dyn_libintl_bind_textdomain_codeset == NULL)
683 dyn_libintl_bind_textdomain_codeset =
684 null_libintl_bind_textdomain_codeset;
685
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200686 /* _wputenv() function for the libintl.dll is optional. */
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100687 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
688 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100689 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100690 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
691 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return 1;
694}
695
696 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100697dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 if (hLibintlDLL)
700 FreeLibrary(hLibintlDLL);
701 hLibintlDLL = NULL;
702 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200703 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 dyn_libintl_textdomain = null_libintl_textdomain;
705 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000706 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000711null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 return (char*)msgid;
714}
715
716 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200717null_libintl_ngettext(
718 const char *msgid,
719 const char *msgid_plural,
720 unsigned long n)
721{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200722 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200723}
724
Bram Moolenaaree695f72016-08-03 22:08:45 +0200725 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100726null_libintl_bindtextdomain(
727 const char *domainname UNUSED,
728 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 return NULL;
731}
732
733 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100734null_libintl_bind_textdomain_codeset(
735 const char *domainname UNUSED,
736 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000737{
738 return NULL;
739}
740
741 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100742null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 return NULL;
745}
746
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200747 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100748null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100749{
750 return 0;
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif /* DYNAMIC_GETTEXT */
754
755/* This symbol is not defined in older versions of the SDK or Visual C++ */
756
757#ifndef VER_PLATFORM_WIN32_WINDOWS
758# define VER_PLATFORM_WIN32_WINDOWS 1
759#endif
760
761DWORD g_PlatformId;
762
763#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100764# ifndef PROTO
765# include <aclapi.h>
766# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200767# ifndef PROTECTED_DACL_SECURITY_INFORMATION
768# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771
Bram Moolenaar27515922013-06-29 15:36:26 +0200772#ifdef HAVE_ACL
773/*
774 * Enables or disables the specified privilege.
775 */
776 static BOOL
777win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
778{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100779 BOOL bResult;
780 LUID luid;
781 HANDLE hToken;
782 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200783
784 if (!OpenProcessToken(GetCurrentProcess(),
785 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
786 return FALSE;
787
788 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
789 {
790 CloseHandle(hToken);
791 return FALSE;
792 }
793
Bram Moolenaar45500912014-07-09 20:51:07 +0200794 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200795 tokenPrivileges.Privileges[0].Luid = luid;
796 tokenPrivileges.Privileges[0].Attributes = bEnable ?
797 SE_PRIVILEGE_ENABLED : 0;
798
799 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
800 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
801
802 CloseHandle(hToken);
803
804 return bResult && GetLastError() == ERROR_SUCCESS;
805}
806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or
810 * VER_PLATFORM_WIN32_WINDOWS (Win95).
811 */
812 void
813PlatformId(void)
814{
815 static int done = FALSE;
816
817 if (!done)
818 {
819 OSVERSIONINFO ovi;
820
821 ovi.dwOSVersionInfoSize = sizeof(ovi);
822 GetVersionEx(&ovi);
823
824 g_PlatformId = ovi.dwPlatformId;
825
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100826 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
827 || ovi.dwMajorVersion > 6)
828 win8_or_later = TRUE;
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef HAVE_ACL
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200831 /* Enable privilege for getting or setting SACLs. */
832 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#endif
834 done = TRUE;
835 }
836}
837
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200838#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100840# define SHIFT (SHIFT_PRESSED)
841# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
842# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
843# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845
846/* When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
847 * We map function keys to their ANSI terminal equivalents, as produced
848 * by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
849 * ANSI key with a value >= '\300' is nonstandard, but provided anyway
850 * so that the user can have access to all SHIFT-, CTRL-, and ALT-
851 * combinations of function/arrow/etc keys.
852 */
853
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000854static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 WORD wVirtKey;
857 BOOL fAnsiKey;
858 int chAlone;
859 int chShift;
860 int chCtrl;
861 int chAlt;
862} VirtKeyMap[] =
863{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200864// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
866
867 { VK_F1, TRUE, ';', 'T', '^', 'h', },
868 { VK_F2, TRUE, '<', 'U', '_', 'i', },
869 { VK_F3, TRUE, '=', 'V', '`', 'j', },
870 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
871 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
872 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
873 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
874 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
875 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
876 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200877 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
878 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200880 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
881 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
882 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
883 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
884 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
885 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
886 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
887 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
888 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
889 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100890 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200892 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100894# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200895 // Most people don't have F13-F20, but what the hell...
896 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
897 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
898 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
899 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
900 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
901 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
902 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
903 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100904# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200905 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
906 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
907 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
908 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200910 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
911 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
912 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
913 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
914 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
915 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
916 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
917 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
918 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
919 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100920 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921};
922
923
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100924# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
925# define UChar UnicodeChar
926# else
927# define UChar uChar.UnicodeChar
928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930/* The return code indicates key code size. */
931 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000933 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 UINT uMods = pker->dwControlKeyState;
936 static int s_iIsDead = 0;
937 static WORD awAnsiCode[2];
938 static BYTE abKeystate[256];
939
940
941 if (s_iIsDead == 2)
942 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200943 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 s_iIsDead = 0;
945 return 1;
946 }
947
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200948 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return 1;
950
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200951 vim_memset(abKeystate, 0, sizeof (abKeystate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /* Clear any pending dead keys */
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200954 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 if (uMods & SHIFT_PRESSED)
957 abKeystate[VK_SHIFT] = 0x80;
958 if (uMods & CAPSLOCK_ON)
959 abKeystate[VK_CAPITAL] = 1;
960
961 if ((uMods & ALT_GR) == ALT_GR)
962 {
963 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
964 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
965 }
966
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200967 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
968 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200971 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 return s_iIsDead;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976static BOOL g_fJustGotFocus = FALSE;
977
978/*
979 * Decode a KEY_EVENT into one or two keystrokes
980 */
981 static BOOL
982decode_key_event(
983 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200984 WCHAR *pch,
985 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200987 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 int i;
990 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
991
992 *pch = *pch2 = NUL;
993 g_fJustGotFocus = FALSE;
994
995 /* ignore key up events */
996 if (!pker->bKeyDown)
997 return FALSE;
998
999 /* ignore some keystrokes */
1000 switch (pker->wVirtualKeyCode)
1001 {
1002 /* modifiers */
1003 case VK_SHIFT:
1004 case VK_CONTROL:
1005 case VK_MENU: /* Alt key */
1006 return FALSE;
1007
1008 default:
1009 break;
1010 }
1011
1012 /* special cases */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001013 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
1015 /* Ctrl-6 is Ctrl-^ */
1016 if (pker->wVirtualKeyCode == '6')
1017 {
1018 *pch = Ctrl_HAT;
1019 return TRUE;
1020 }
1021 /* Ctrl-2 is Ctrl-@ */
1022 else if (pker->wVirtualKeyCode == '2')
1023 {
1024 *pch = NUL;
1025 return TRUE;
1026 }
1027 /* Ctrl-- is Ctrl-_ */
1028 else if (pker->wVirtualKeyCode == 0xBD)
1029 {
1030 *pch = Ctrl__;
1031 return TRUE;
1032 }
1033 }
1034
1035 /* Shift-TAB */
1036 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1037 {
1038 *pch = K_NUL;
1039 *pch2 = '\017';
1040 return TRUE;
1041 }
1042
1043 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1044 {
1045 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1046 {
1047 if (nModifs == 0)
1048 *pch = VirtKeyMap[i].chAlone;
1049 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1050 *pch = VirtKeyMap[i].chShift;
1051 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1052 *pch = VirtKeyMap[i].chCtrl;
1053 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1054 *pch = VirtKeyMap[i].chAlt;
1055
1056 if (*pch != 0)
1057 {
1058 if (VirtKeyMap[i].fAnsiKey)
1059 {
1060 *pch2 = *pch;
1061 *pch = K_NUL;
1062 }
1063
1064 return TRUE;
1065 }
1066 }
1067 }
1068
1069 i = win32_kbd_patch_key(pker);
1070
1071 if (i < 0)
1072 *pch = NUL;
1073 else
1074 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001075 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
1077 if (pmodifiers != NULL)
1078 {
1079 /* Pass on the ALT key as a modifier, but only when not combined
1080 * with CTRL (which is ALTGR). */
1081 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1082 *pmodifiers |= MOD_MASK_ALT;
1083
1084 /* Pass on SHIFT only for special keys, because we don't know when
1085 * it's already included with the character. */
1086 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1087 *pmodifiers |= MOD_MASK_SHIFT;
1088
1089 /* Pass on CTRL only for non-special keys, because we don't know
1090 * when it's already included with the character. And not when
1091 * combined with ALT (which is ALTGR). */
1092 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1093 && *pch >= 0x20 && *pch < 0x80)
1094 *pmodifiers |= MOD_MASK_CTRL;
1095 }
1096 }
1097
1098 return (*pch != NUL);
1099}
1100
Bram Moolenaar4f974752019-02-17 17:44:42 +01001101#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * For the GUI the mouse handling is in gui_w32.c.
1106 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001107#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001109mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001112#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113static int g_fMouseAvail = FALSE; /* mouse present */
1114static int g_fMouseActive = FALSE; /* mouse enabled */
1115static int g_nMouseClick = -1; /* mouse status */
1116static int g_xMouse; /* mouse x coordinate */
1117static int g_yMouse; /* mouse y coordinate */
1118
1119/*
1120 * Enable or disable mouse input
1121 */
1122 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001123mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 DWORD cmodein;
1126
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001127# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001128 if (gui.in_use)
1129 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (!g_fMouseAvail)
1132 return;
1133
1134 g_fMouseActive = on;
1135 GetConsoleMode(g_hConIn, &cmodein);
1136
1137 if (g_fMouseActive)
1138 cmodein |= ENABLE_MOUSE_INPUT;
1139 else
1140 cmodein &= ~ENABLE_MOUSE_INPUT;
1141
1142 SetConsoleMode(g_hConIn, cmodein);
1143}
1144
Bram Moolenaar157d8132018-03-06 17:09:20 +01001145
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001146# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001147/*
1148 * Called when 'balloonevalterm' changed.
1149 */
1150 void
1151mch_bevalterm_changed(void)
1152{
1153 mch_setmouse(g_fMouseActive);
1154}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001155# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157/*
1158 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1159 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1160 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1161 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1162 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1163 * and we return the mouse position in g_xMouse and g_yMouse.
1164 *
1165 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1166 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1167 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1168 *
1169 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1170 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1171 *
1172 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1173 * moves, even if it stays within the same character cell. We ignore
1174 * all MOUSE_MOVED messages if the position hasn't really changed, and
1175 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1176 * we're only interested in MOUSE_DRAG).
1177 *
1178 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1179 * 2-button mouses by pressing the left & right buttons simultaneously.
1180 * In practice, it's almost impossible to click both at the same time,
1181 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1182 * in such cases, if the user is clicking quickly.
1183 */
1184 static BOOL
1185decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001186 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 static int s_nOldButton = -1;
1189 static int s_nOldMouseClick = -1;
1190 static int s_xOldMouse = -1;
1191 static int s_yOldMouse = -1;
1192 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001193# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 static int s_cClicks = 1;
1197 static BOOL s_fReleased = TRUE;
1198 static DWORD s_dwLastClickTime = 0;
1199 static BOOL s_fNextIsMiddle = FALSE;
1200
1201 static DWORD cButtons = 0; /* number of buttons supported */
1202
1203 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1204 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1205 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1206 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1207
1208 int nButton;
1209
1210 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1211 cButtons = 2;
1212
1213 if (!g_fMouseAvail || !g_fMouseActive)
1214 {
1215 g_nMouseClick = -1;
1216 return FALSE;
1217 }
1218
1219 /* get a spurious MOUSE_EVENT immediately after receiving focus; ignore */
1220 if (g_fJustGotFocus)
1221 {
1222 g_fJustGotFocus = FALSE;
1223 return FALSE;
1224 }
1225
1226 /* unprocessed mouse click? */
1227 if (g_nMouseClick != -1)
1228 return TRUE;
1229
1230 nButton = -1;
1231 g_xMouse = pmer->dwMousePosition.X;
1232 g_yMouse = pmer->dwMousePosition.Y;
1233
1234 if (pmer->dwEventFlags == MOUSE_MOVED)
1235 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001236 /* Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 * events even when the mouse moves only within a char cell.) */
1238 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1239 return FALSE;
1240 }
1241
1242 /* If no buttons are pressed... */
1243 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1244 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001245 nButton = MOUSE_RELEASE;
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 /* If the last thing returned was MOUSE_RELEASE, ignore this */
1248 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001249 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001250# ifdef FEAT_BEVAL_TERM
Bram Moolenaar157d8132018-03-06 17:09:20 +01001251 /* do return mouse move events when we want them */
1252 if (p_bevalterm)
1253 nButton = MOUSE_DRAG;
1254 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001255# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001256 return FALSE;
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 s_fReleased = TRUE;
1260 }
1261 else /* one or more buttons pressed */
1262 {
1263 /* on a 2-button mouse, hold down left and right buttons
1264 * simultaneously to get MIDDLE. */
1265
1266 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1267 {
1268 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1269
1270 /* if either left or right button only is pressed, see if the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001271 * next mouse event has both of them pressed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (dwLR == LEFT || dwLR == RIGHT)
1273 {
1274 for (;;)
1275 {
1276 /* wait a short time for next input event */
1277 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1278 != WAIT_OBJECT_0)
1279 break;
1280 else
1281 {
1282 DWORD cRecords = 0;
1283 INPUT_RECORD ir;
1284 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1285
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001286 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1289 || !(pmer2->dwButtonState & LEFT_RIGHT))
1290 break;
1291 else
1292 {
1293 if (pmer2->dwEventFlags != MOUSE_MOVED)
1294 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001295 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 return decode_mouse_event(pmer2);
1298 }
1299 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1300 s_yOldMouse == pmer2->dwMousePosition.Y)
1301 {
1302 /* throw away spurious mouse move */
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001303 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 /* are there any more mouse events in queue? */
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001306 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
1308 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1309 break;
1310 }
1311 else
1312 break;
1313 }
1314 }
1315 }
1316 }
1317 }
1318
1319 if (s_fNextIsMiddle)
1320 {
1321 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1322 ? MOUSE_DRAG : MOUSE_MIDDLE;
1323 s_fNextIsMiddle = FALSE;
1324 }
1325 else if (cButtons == 2 &&
1326 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1327 {
1328 nButton = MOUSE_MIDDLE;
1329
1330 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1331 {
1332 s_fNextIsMiddle = TRUE;
1333 nButton = MOUSE_RELEASE;
1334 }
1335 }
1336 else if ((pmer->dwButtonState & LEFT) == LEFT)
1337 nButton = MOUSE_LEFT;
1338 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1339 nButton = MOUSE_MIDDLE;
1340 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1341 nButton = MOUSE_RIGHT;
1342
1343 if (! s_fReleased && ! s_fNextIsMiddle
1344 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1345 return FALSE;
1346
1347 s_fReleased = s_fNextIsMiddle;
1348 }
1349
1350 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1351 {
1352 /* button pressed or released, without mouse moving */
1353 if (nButton != -1 && nButton != MOUSE_RELEASE)
1354 {
1355 DWORD dwCurrentTime = GetTickCount();
1356
1357 if (s_xOldMouse != g_xMouse
1358 || s_yOldMouse != g_yMouse
1359 || s_nOldButton != nButton
1360 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001361# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1365 {
1366 s_cClicks = 1;
1367 }
1368 else if (++s_cClicks > 4)
1369 {
1370 s_cClicks = 1;
1371 }
1372
1373 s_dwLastClickTime = dwCurrentTime;
1374 }
1375 }
1376 else if (pmer->dwEventFlags == MOUSE_MOVED)
1377 {
1378 if (nButton != -1 && nButton != MOUSE_RELEASE)
1379 nButton = MOUSE_DRAG;
1380
1381 s_cClicks = 1;
1382 }
1383
1384 if (nButton == -1)
1385 return FALSE;
1386
1387 if (nButton != MOUSE_RELEASE)
1388 s_nOldButton = nButton;
1389
1390 g_nMouseClick = nButton;
1391
1392 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1393 g_nMouseClick |= MOUSE_SHIFT;
1394 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1395 g_nMouseClick |= MOUSE_CTRL;
1396 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1397 g_nMouseClick |= MOUSE_ALT;
1398
1399 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1400 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1401
1402 /* only pass on interesting (i.e., different) mouse events */
1403 if (s_xOldMouse == g_xMouse
1404 && s_yOldMouse == g_yMouse
1405 && s_nOldMouseClick == g_nMouseClick)
1406 {
1407 g_nMouseClick = -1;
1408 return FALSE;
1409 }
1410
1411 s_xOldMouse = g_xMouse;
1412 s_yOldMouse = g_yMouse;
1413 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001414# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 s_nOldMouseClick = g_nMouseClick;
1418
1419 return TRUE;
1420}
1421
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001422#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424
1425#ifdef MCH_CURSOR_SHAPE
1426/*
1427 * Set the shape of the cursor.
1428 * 'thickness' can be from 1 (thin) to 99 (block)
1429 */
1430 static void
1431mch_set_cursor_shape(int thickness)
1432{
1433 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1434 ConsoleCursorInfo.dwSize = thickness;
1435 ConsoleCursorInfo.bVisible = s_cursor_visible;
1436
1437 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1438 if (s_cursor_visible)
1439 SetConsoleCursorPosition(g_hConOut, g_coord);
1440}
1441
1442 void
1443mch_update_cursor(void)
1444{
1445 int idx;
1446 int thickness;
1447
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001448# ifdef VIMDLL
1449 if (gui.in_use)
1450 return;
1451# endif
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 /*
1454 * How the cursor is drawn depends on the current mode.
1455 */
1456 idx = get_shape_idx(FALSE);
1457
1458 if (shape_table[idx].shape == SHAPE_BLOCK)
1459 thickness = 99; /* 100 doesn't work on W95 */
1460 else
1461 thickness = shape_table[idx].percentage;
1462 mch_set_cursor_shape(thickness);
1463}
1464#endif
1465
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001466#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467/*
1468 * Handle FOCUS_EVENT.
1469 */
1470 static void
1471handle_focus_event(INPUT_RECORD ir)
1472{
1473 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1474 ui_focus_change((int)g_fJustGotFocus);
1475}
1476
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001477static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479/*
1480 * Wait until console input from keyboard or mouse is available,
1481 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001482 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 * Return TRUE if something is available FALSE if not.
1484 */
1485 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001486WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 DWORD dwNow = 0, dwEndTime = 0;
1489 INPUT_RECORD ir;
1490 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001491 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001492# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001493 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 if (msec > 0)
1497 /* Wait until the specified time has elapsed. */
1498 dwEndTime = GetTickCount() + msec;
1499 else if (msec < 0)
1500 /* Wait forever. */
1501 dwEndTime = INFINITE;
1502
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001503 // We need to loop until the end of the time period, because
1504 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 for (;;)
1506 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001507 // Only process messages when waiting.
1508 if (msec != 0)
1509 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001510# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001511 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001512# endif
1513# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001514 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001515# endif
1516# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001517 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001518# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001519 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001520
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001521 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001522# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001523 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 )
1526 return TRUE;
1527
1528 if (msec > 0)
1529 {
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001530 /* If the specified wait time has passed, return. Beware that
1531 * GetTickCount() may wrap around (overflow). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001533 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 break;
1535 }
1536 if (msec != 0)
1537 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001538 DWORD dwWaitTime = dwEndTime - dwNow;
1539
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001540# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001541 /* Check channel while waiting for input. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01001542 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001543 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001544 dwWaitTime = 100;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001545 /* If there is readahead then parse_queued_messages() timed out
1546 * and we should call it again soon. */
1547 if (channel_any_readahead())
1548 dwWaitTime = 10;
1549 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001550# endif
1551# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001552 if (p_beval && dwWaitTime > 100)
1553 /* The 'balloonexpr' may indirectly invoke a callback while
1554 * waiting for a character, need to check often. */
1555 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001556# endif
1557# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001558 if (mzthreads_allowed() && p_mzq > 0
1559 && (msec < 0 || (long)dwWaitTime > p_mzq))
1560 dwWaitTime = p_mzq; /* don't wait longer than 'mzquantum' */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001561# endif
1562# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001563 // When waiting very briefly don't trigger timers.
1564 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001565 {
1566 long due_time;
1567
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001568 // Trigger timers and then get the time in msec until the next
1569 // one is due. Wait up to that time.
1570 due_time = check_due_timer();
1571 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001572 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001573 // timer may have used feedkeys().
1574 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001575 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001576 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1577 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001578 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001579# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001580 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001581# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001582 // Wait for either an event on the console input or a
1583 // message in the client-server window.
1584 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1585 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001586# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001587 wait_for_single_object(g_hConIn, dwWaitTime)
1588 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001589# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001590 )
1591 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593
1594 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001595 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001597# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (State & CMDLINE && msg_row == Rows - 1)
1599 {
1600 CONSOLE_SCREEN_BUFFER_INFO csbi;
1601
1602 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1603 {
1604 if (csbi.dwCursorPosition.Y != msg_row)
1605 {
1606 /* The screen is now messed up, must redraw the
1607 * command line and later all the windows. */
1608 redraw_all_later(CLEAR);
1609 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1610 redrawcmd();
1611 }
1612 }
1613 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 if (cRecords > 0)
1617 {
1618 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1619 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001620# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* Windows IME sends two '\n's with only one 'ENTER'. First:
1622 * wVirtualKeyCode == 13. second: wVirtualKeyCode == 0 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001623 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1625 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001626 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 continue;
1628 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1631 NULL, FALSE))
1632 return TRUE;
1633 }
1634
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001635 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 if (ir.EventType == FOCUS_EVENT)
1638 handle_focus_event(ir);
1639 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001640 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001641 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1642
1643 // Only call shell_resized() when the size actually change to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001644 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001645 if (dwSize.X != Columns || dwSize.Y != Rows)
1646 {
1647 CONSOLE_SCREEN_BUFFER_INFO csbi;
1648 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
1649 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1650 ResizeConBuf(g_hConOut, dwSize);
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001651 shell_resized();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001652 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 else if (ir.EventType == MOUSE_EVENT
1655 && decode_mouse_event(&ir.Event.MouseEvent))
1656 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 else if (msec == 0)
1659 break;
1660 }
1661
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001662# ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 /* Something might have been received while we were waiting. */
1664 if (input_available())
1665 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001666# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 return FALSE;
1669}
1670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671/*
1672 * return non-zero if a character is available
1673 */
1674 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001675mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001677# ifdef VIMDLL
1678 if (gui.in_use)
1679 return TRUE;
1680# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001681 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001683
1684# if defined(FEAT_TERMINAL) || defined(PROTO)
1685/*
1686 * Check for any pending input or messages.
1687 */
1688 int
1689mch_check_messages(void)
1690{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001691# ifdef VIMDLL
1692 if (gui.in_use)
1693 return TRUE;
1694# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001695 return WaitForChar(0L, TRUE);
1696}
1697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699/*
1700 * Create the console input. Used when reading stdin doesn't work.
1701 */
1702 static void
1703create_conin(void)
1704{
1705 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1706 FILE_SHARE_READ|FILE_SHARE_WRITE,
1707 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001708 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 did_create_conin = TRUE;
1710}
1711
1712/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001713 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001715 static WCHAR
1716tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001718 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720 for (;;)
1721 {
1722 INPUT_RECORD ir;
1723 DWORD cRecords = 0;
1724
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001725# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001726 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (input_available())
1728 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if (g_nMouseClick != -1)
1730 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001731# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001732 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
1734 if (did_create_conin)
1735 read_error_exit();
1736 create_conin();
1737 continue;
1738 }
1739
1740 if (ir.EventType == KEY_EVENT)
1741 {
1742 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1743 pmodifiers, TRUE))
1744 return ch;
1745 }
1746 else if (ir.EventType == FOCUS_EVENT)
1747 handle_focus_event(ir);
1748 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1749 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 else if (ir.EventType == MOUSE_EVENT)
1751 {
1752 if (decode_mouse_event(&ir.Event.MouseEvent))
1753 return 0;
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756}
Bram Moolenaar4f974752019-02-17 17:44:42 +01001757#endif /* !FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759
1760/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001761 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 * Get one or more characters from the keyboard or the mouse.
1763 * If time == 0, do not wait for characters.
1764 * If time == n, wait a short time for characters.
1765 * If time == -1, wait forever for characters.
1766 * Returns the number of characters read into buf.
1767 */
1768 int
1769mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001770 char_u *buf UNUSED,
1771 int maxlen UNUSED,
1772 long time UNUSED,
1773 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001775#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 int len;
1778 int c;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001779# define TYPEAHEADLEN 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 static char_u typeahead[TYPEAHEADLEN]; /* previously typed bytes. */
1781 static int typeaheadlen = 0;
1782
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001783# ifdef VIMDLL
1784 if (gui.in_use)
1785 return 0;
1786# endif
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 /* First use any typeahead that was kept because "buf" was too small. */
1789 if (typeaheadlen > 0)
1790 goto theend;
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (time >= 0)
1793 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001794 if (!WaitForChar(time, FALSE)) /* no character available */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797 else /* time == -1, wait forever */
1798 {
1799 mch_set_winsize_now(); /* Allow winsize changes from now on */
1800
Bram Moolenaar3918c952005-03-15 22:34:55 +00001801 /*
1802 * If there is no character available within 2 seconds (default)
1803 * write the autoscript file to disk. Or cause the CursorHold event
1804 * to be triggered.
1805 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001806 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001808 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001810 buf[0] = K_SPECIAL;
1811 buf[1] = KS_EXTRA;
1812 buf[2] = (int)KE_CURSORHOLD;
1813 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001815 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 }
1818
1819 /*
1820 * Try to read as many characters as there are, until the buffer is full.
1821 */
1822
1823 /* we will get at least one key. Get more if they are available. */
1824 g_fCBrkPressed = FALSE;
1825
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001826# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (fdDump)
1828 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831 /* Keep looping until there is something in the typeahead buffer and more
1832 * to get and still room in the buffer (up to two bytes for a char and
1833 * three bytes for a modifier). */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001834 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 && typeaheadlen + 5 <= TYPEAHEADLEN)
1836 {
1837 if (typebuf_changed(tb_change_cnt))
1838 {
1839 /* "buf" may be invalid now if a client put something in the
1840 * typeahead buffer and "buf" is in the typeahead buffer. */
1841 typeaheadlen = 0;
1842 break;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (g_nMouseClick != -1)
1845 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001846# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (fdDump)
1848 fprintf(fdDump, "{%02x @ %d, %d}",
1849 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 typeahead[typeaheadlen++] = ESC + 128;
1852 typeahead[typeaheadlen++] = 'M';
1853 typeahead[typeaheadlen++] = g_nMouseClick;
1854 typeahead[typeaheadlen++] = g_xMouse + '!';
1855 typeahead[typeaheadlen++] = g_yMouse + '!';
1856 g_nMouseClick = -1;
1857 }
1858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001860 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 int modifiers = 0;
1862
1863 c = tgetch(&modifiers, &ch2);
1864
1865 if (typebuf_changed(tb_change_cnt))
1866 {
1867 /* "buf" may be invalid now if a client put something in the
1868 * typeahead buffer and "buf" is in the typeahead buffer. */
1869 typeaheadlen = 0;
1870 break;
1871 }
1872
1873 if (c == Ctrl_C && ctrl_c_interrupts)
1874 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001875# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 got_int = TRUE;
1879 }
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 int n = 1;
1884
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001885 if (ch2 == NUL)
1886 {
1887 int i;
1888 char_u *p;
1889 WCHAR ch[2];
1890
1891 ch[0] = c;
1892 if (c >= 0xD800 && c <= 0xDBFF) /* High surrogate */
1893 {
1894 ch[1] = tgetch(&modifiers, &ch2);
1895 n++;
1896 }
1897 p = utf16_to_enc(ch, &n);
1898 if (p != NULL)
1899 {
1900 for (i = 0; i < n; i++)
1901 typeahead[typeaheadlen + i] = p[i];
1902 vim_free(p);
1903 }
1904 }
1905 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001906 typeahead[typeaheadlen] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (ch2 != NUL)
1908 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001909 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001910 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001911 switch (ch2)
1912 {
1913 case (WCHAR)'\324': // SHIFT+Insert
1914 case (WCHAR)'\325': // CTRL+Insert
1915 case (WCHAR)'\327': // SHIFT+Delete
1916 case (WCHAR)'\330': // CTRL+Delete
1917 typeahead[typeaheadlen + n] = (char_u)ch2;
1918 n++;
1919 break;
1920
1921 default:
1922 typeahead[typeaheadlen + n] = 3;
1923 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1924 n += 2;
1925 break;
1926 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001927 }
1928 else
1929 {
1930 typeahead[typeaheadlen + n] = 3;
1931 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1932 n += 2;
1933 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001934 }
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 /* Use the ALT key to set the 8th bit of the character
1937 * when it's one byte, the 8th bit isn't set yet and not
1938 * using a double-byte encoding (would become a lead
1939 * byte). */
1940 if ((modifiers & MOD_MASK_ALT)
1941 && n == 1
1942 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 )
1945 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001946 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1947 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 modifiers &= ~MOD_MASK_ALT;
1949 }
1950
1951 if (modifiers != 0)
1952 {
1953 /* Prepend modifiers to the character. */
1954 mch_memmove(typeahead + typeaheadlen + 3,
1955 typeahead + typeaheadlen, n);
1956 typeahead[typeaheadlen++] = K_SPECIAL;
1957 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1958 typeahead[typeaheadlen++] = modifiers;
1959 }
1960
1961 typeaheadlen += n;
1962
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001963# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (fdDump)
1965 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 }
1969 }
1970
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001971# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (fdDump)
1973 {
1974 fputs("]\n", fdDump);
1975 fflush(fdDump);
1976 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979theend:
1980 /* Move typeahead to "buf", as much as fits. */
1981 len = 0;
1982 while (len < maxlen && typeaheadlen > 0)
1983 {
1984 buf[len++] = typeahead[0];
1985 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
1986 }
1987 return len;
1988
Bram Moolenaar4f974752019-02-17 17:44:42 +01001989#else /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001991#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992}
1993
Bram Moolenaar82881492012-11-20 16:53:39 +01001994#ifndef PROTO
1995# ifndef __MINGW32__
1996# include <shellapi.h> /* required for FindExecutable() */
1997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998#endif
1999
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002000/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002001 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2002 * If "use_path" is FALSE: Return TRUE if "name" exists.
2003 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2004 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002005 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002008executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002010 WCHAR *p;
2011 WCHAR fnamew[_MAX_PATH];
2012 WCHAR *dumw;
2013 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002014 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar43663192017-03-05 14:29:12 +01002016 if (!use_path)
2017 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002018 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002019 {
2020 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002021 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002022 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002023 *path = vim_strsave((char_u *)name);
2024 else
2025 *path = FullName_save((char_u *)name, FALSE);
2026 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002027 return TRUE;
2028 }
2029 return FALSE;
2030 }
2031
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002032 p = enc_to_utf16((char_u *)name, NULL);
2033 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002034 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002035
2036 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002037 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002038 if (wnewpath == NULL)
2039 return FALSE;
2040 wcscpy(wnewpath, L".;");
2041 wcscat(wnewpath, wcurpath);
2042 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2043 vim_free(wnewpath);
2044 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002045 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002046 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002047 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002048 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002049 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002050 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002051 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002054#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2055 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002056/*
2057 * Bad parameter handler.
2058 *
2059 * Certain MS CRT functions will intentionally crash when passed invalid
2060 * parameters to highlight possible security holes. Setting this function as
2061 * the bad parameter handler will prevent the crash.
2062 *
2063 * In debug builds the parameters contain CRT information that might help track
2064 * down the source of a problem, but in non-debug builds the arguments are all
2065 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2066 * worth allowing these to make debugging of issues easier.
2067 */
2068 static void
2069bad_param_handler(const wchar_t *expression,
2070 const wchar_t *function,
2071 const wchar_t *file,
2072 unsigned int line,
2073 uintptr_t pReserved)
2074{
2075}
2076
2077# define SET_INVALID_PARAM_HANDLER \
2078 ((void)_set_invalid_parameter_handler(bad_param_handler))
2079#else
2080# define SET_INVALID_PARAM_HANDLER
2081#endif
2082
Bram Moolenaar4f974752019-02-17 17:44:42 +01002083#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085/*
2086 * GUI version of mch_init().
2087 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002088 static void
2089mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002091# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002095 /* Silently handle invalid parameters to CRT functions */
2096 SET_INVALID_PARAM_HANDLER;
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /* Let critical errors result in a failure, not in a dialog box. Required
2099 * for the timestamp test to work on removed floppies. */
2100 SetErrorMode(SEM_FAILCRITICALERRORS);
2101
2102 _fmode = O_BINARY; /* we do our own CR-LF translation */
2103
2104 /* Specify window size. Is there a place to get the default from? */
2105 Rows = 25;
2106 Columns = 80;
2107
2108 /* Look for 'vimrun' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 char_u vimrun_location[_MAX_PATH + 4];
2111
2112 /* First try in same directory as gvim.exe */
2113 STRCPY(vimrun_location, exe_name);
2114 STRCPY(gettail(vimrun_location), "vimrun.exe");
2115 if (mch_getperm(vimrun_location) >= 0)
2116 {
2117 if (*skiptowhite(vimrun_location) != NUL)
2118 {
2119 /* Enclose path with white space in double quotes. */
2120 mch_memmove(vimrun_location + 1, vimrun_location,
2121 STRLEN(vimrun_location) + 1);
2122 *vimrun_location = '"';
2123 STRCPY(gettail(vimrun_location), "vimrun\" ");
2124 }
2125 else
2126 STRCPY(gettail(vimrun_location), "vimrun ");
2127
2128 vimrun_path = (char *)vim_strsave(vimrun_location);
2129 s_dont_use_vimrun = FALSE;
2130 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002131 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 s_dont_use_vimrun = FALSE;
2133
2134 /* Don't give the warning for a missing vimrun.exe right now, but only
2135 * when vimrun was supposed to be used. Don't bother people that do
2136 * not need vimrun.exe. */
2137 if (s_dont_use_vimrun)
2138 need_vimrun_warning = TRUE;
2139 }
2140
2141 /*
2142 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2143 * Otherwise the default "findstr /n" is used.
2144 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002145 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2147
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002148# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002149 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002150# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002151
2152 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153}
2154
2155
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002156#endif /* FEAT_GUI_MSWIN */
2157
2158#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002160# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2161# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163/*
2164 * ClearConsoleBuffer()
2165 * Description:
2166 * Clears the entire contents of the console screen buffer, using the
2167 * specified attribute.
2168 * Returns:
2169 * TRUE on success
2170 */
2171 static BOOL
2172ClearConsoleBuffer(WORD wAttribute)
2173{
2174 CONSOLE_SCREEN_BUFFER_INFO csbi;
2175 COORD coord;
2176 DWORD NumCells, dummy;
2177
2178 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2179 return FALSE;
2180
2181 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2182 coord.X = 0;
2183 coord.Y = 0;
2184 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2185 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2188 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 return TRUE;
2192}
2193
2194/*
2195 * FitConsoleWindow()
2196 * Description:
2197 * Checks if the console window will fit within given buffer dimensions.
2198 * Also, if requested, will shrink the window to fit.
2199 * Returns:
2200 * TRUE on success
2201 */
2202 static BOOL
2203FitConsoleWindow(
2204 COORD dwBufferSize,
2205 BOOL WantAdjust)
2206{
2207 CONSOLE_SCREEN_BUFFER_INFO csbi;
2208 COORD dwWindowSize;
2209 BOOL NeedAdjust = FALSE;
2210
2211 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2212 {
2213 /*
2214 * A buffer resize will fail if the current console window does
2215 * not lie completely within that buffer. To avoid this, we might
2216 * have to move and possibly shrink the window.
2217 */
2218 if (csbi.srWindow.Right >= dwBufferSize.X)
2219 {
2220 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2221 if (dwWindowSize.X > dwBufferSize.X)
2222 dwWindowSize.X = dwBufferSize.X;
2223 csbi.srWindow.Right = dwBufferSize.X - 1;
2224 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2225 NeedAdjust = TRUE;
2226 }
2227 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2228 {
2229 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2230 if (dwWindowSize.Y > dwBufferSize.Y)
2231 dwWindowSize.Y = dwBufferSize.Y;
2232 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2233 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2234 NeedAdjust = TRUE;
2235 }
2236 if (NeedAdjust && WantAdjust)
2237 {
2238 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2239 return FALSE;
2240 }
2241 return TRUE;
2242 }
2243
2244 return FALSE;
2245}
2246
2247typedef struct ConsoleBufferStruct
2248{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002249 BOOL IsValid;
2250 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002251 PCHAR_INFO Buffer;
2252 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002253 PSMALL_RECT Regions;
2254 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255} ConsoleBuffer;
2256
2257/*
2258 * SaveConsoleBuffer()
2259 * Description:
2260 * Saves important information about the console buffer, including the
2261 * actual buffer contents. The saved information is suitable for later
2262 * restoration by RestoreConsoleBuffer().
2263 * Returns:
2264 * TRUE if all information was saved; FALSE otherwise
2265 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2266 */
2267 static BOOL
2268SaveConsoleBuffer(
2269 ConsoleBuffer *cb)
2270{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002271 DWORD NumCells;
2272 COORD BufferCoord;
2273 SMALL_RECT ReadRegion;
2274 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002275 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (cb == NULL)
2278 return FALSE;
2279
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002280 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 cb->IsValid = FALSE;
2283 return FALSE;
2284 }
2285 cb->IsValid = TRUE;
2286
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002287 /*
2288 * Allocate a buffer large enough to hold the entire console screen
2289 * buffer. If this ConsoleBuffer structure has already been initialized
2290 * with a buffer of the correct size, then just use that one.
2291 */
2292 if (!cb->IsValid || cb->Buffer == NULL ||
2293 cb->BufferSize.X != cb->Info.dwSize.X ||
2294 cb->BufferSize.Y != cb->Info.dwSize.Y)
2295 {
2296 cb->BufferSize.X = cb->Info.dwSize.X;
2297 cb->BufferSize.Y = cb->Info.dwSize.Y;
2298 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2299 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002300 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002301 if (cb->Buffer == NULL)
2302 return FALSE;
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002306 * We will now copy the console screen buffer into our buffer.
2307 * ReadConsoleOutput() seems to be limited as far as how much you
2308 * can read at a time. Empirically, this number seems to be about
2309 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2310 * in chunks until it is all copied. The chunks will all have the
2311 * same horizontal characteristics, so initialize them now. The
2312 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002314 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002315 ReadRegion.Left = 0;
2316 ReadRegion.Right = cb->Info.dwSize.X - 1;
2317 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002318
2319 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2320 if (cb->Regions == NULL || numregions != cb->NumRegions)
2321 {
2322 cb->NumRegions = numregions;
2323 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002324 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002325 if (cb->Regions == NULL)
2326 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002327 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002328 return FALSE;
2329 }
2330 }
2331
2332 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002334 /*
2335 * Read into position (0, Y) in our buffer.
2336 */
2337 BufferCoord.Y = Y;
2338 /*
2339 * Read the region whose top left corner is (0, Y) and whose bottom
2340 * right corner is (width - 1, Y + Y_incr - 1). This should define
2341 * a region of size width by Y_incr. Don't worry if this region is
2342 * too large for the remaining buffer; it will be cropped.
2343 */
2344 ReadRegion.Top = Y;
2345 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002346 if (!ReadConsoleOutputW(g_hConOut, /* output handle */
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002347 cb->Buffer, /* our buffer */
2348 cb->BufferSize, /* dimensions of our buffer */
2349 BufferCoord, /* offset in our buffer */
2350 &ReadRegion)) /* region to save */
2351 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002352 VIM_CLEAR(cb->Buffer);
2353 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002354 return FALSE;
2355 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002356 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
2359 return TRUE;
2360}
2361
2362/*
2363 * RestoreConsoleBuffer()
2364 * Description:
2365 * Restores important information about the console buffer, including the
2366 * actual buffer contents, if desired. The information to restore is in
2367 * the same format used by SaveConsoleBuffer().
2368 * Returns:
2369 * TRUE on success
2370 */
2371 static BOOL
2372RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002373 ConsoleBuffer *cb,
2374 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002376 COORD BufferCoord;
2377 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002378 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 if (cb == NULL || !cb->IsValid)
2381 return FALSE;
2382
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002383 /*
2384 * Before restoring the buffer contents, clear the current buffer, and
2385 * restore the cursor position and window information. Doing this now
2386 * prevents old buffer contents from "flashing" onto the screen.
2387 */
2388 if (RestoreScreen)
2389 ClearConsoleBuffer(cb->Info.wAttributes);
2390
2391 FitConsoleWindow(cb->Info.dwSize, TRUE);
2392 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2393 return FALSE;
2394 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2395 return FALSE;
2396
2397 if (!RestoreScreen)
2398 {
2399 /*
2400 * No need to restore the screen buffer contents, so we're done.
2401 */
2402 return TRUE;
2403 }
2404
2405 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2406 return FALSE;
2407 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2408 return FALSE;
2409
2410 /*
2411 * Restore the screen buffer contents.
2412 */
2413 if (cb->Buffer != NULL)
2414 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002415 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002416 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002417 BufferCoord.X = cb->Regions[i].Left;
2418 BufferCoord.Y = cb->Regions[i].Top;
2419 WriteRegion = cb->Regions[i];
2420 if (!WriteConsoleOutputW(g_hConOut, /* output handle */
2421 cb->Buffer, /* our buffer */
2422 cb->BufferSize, /* dimensions of our buffer */
2423 BufferCoord, /* offset in our buffer */
2424 &WriteRegion)) /* region to restore */
Bram Moolenaar444fda22017-08-11 20:37:00 +02002425 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002426 }
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 return TRUE;
2430}
2431
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002432# define FEAT_RESTORE_ORIG_SCREEN
2433# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002434static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436static ConsoleBuffer g_cbNonTermcap = { 0 };
2437static ConsoleBuffer g_cbTermcap = { 0 };
2438
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002439# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440char g_szOrigTitle[256] = { 0 };
2441HWND g_hWnd = NULL; /* also used in os_mswin.c */
2442static HICON g_hOrigIconSmall = NULL;
2443static HICON g_hOrigIcon = NULL;
2444static HICON g_hVimIcon = NULL;
2445static BOOL g_fCanChangeIcon = FALSE;
2446
2447/* ICON* are not defined in VC++ 4.0 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002448# ifndef ICON_SMALL
2449# define ICON_SMALL 0
2450# endif
2451# ifndef ICON_BIG
2452# define ICON_BIG 1
2453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454/*
2455 * GetConsoleIcon()
2456 * Description:
2457 * Attempts to retrieve the small icon and/or the big icon currently in
2458 * use by a given window.
2459 * Returns:
2460 * TRUE on success
2461 */
2462 static BOOL
2463GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002464 HWND hWnd,
2465 HICON *phIconSmall,
2466 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 if (hWnd == NULL)
2469 return FALSE;
2470
2471 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002472 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2473 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002475 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2476 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return TRUE;
2478}
2479
2480/*
2481 * SetConsoleIcon()
2482 * Description:
2483 * Attempts to change the small icon and/or the big icon currently in
2484 * use by a given window.
2485 * Returns:
2486 * TRUE on success
2487 */
2488 static BOOL
2489SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002490 HWND hWnd,
2491 HICON hIconSmall,
2492 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (hWnd == NULL)
2495 return FALSE;
2496
2497 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002498 SendMessage(hWnd, WM_SETICON,
2499 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002501 SendMessage(hWnd, WM_SETICON,
2502 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return TRUE;
2504}
2505
2506/*
2507 * SaveConsoleTitleAndIcon()
2508 * Description:
2509 * Saves the current console window title in g_szOrigTitle, for later
2510 * restoration. Also, attempts to obtain a handle to the console window,
2511 * and use it to save the small and big icons currently in use by the
2512 * console window. This is not always possible on some versions of Windows;
2513 * nor is it possible when running Vim remotely using Telnet (since the
2514 * console window the user sees is owned by a remote process).
2515 */
2516 static void
2517SaveConsoleTitleAndIcon(void)
2518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /* Save the original title. */
2520 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2521 return;
2522
2523 /*
2524 * Obtain a handle to the console window using GetConsoleWindow() from
2525 * KERNEL32.DLL; we need to handle in order to change the window icon.
2526 * This function only exists on NT-based Windows, starting with Windows
2527 * 2000. On older operating systems, we can't change the window icon
2528 * anyway.
2529 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002530 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (g_hWnd == NULL)
2532 return;
2533
2534 /* Save the original console window icon. */
2535 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2536 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2537 return;
2538
2539 /* Extract the first icon contained in the Vim executable. */
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002540 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002541 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (g_hVimIcon != NULL)
2543 g_fCanChangeIcon = TRUE;
2544}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547static int g_fWindInitCalled = FALSE;
2548static int g_fTermcapMode = FALSE;
2549static CONSOLE_CURSOR_INFO g_cci;
2550static DWORD g_cmodein = 0;
2551static DWORD g_cmodeout = 0;
2552
2553/*
2554 * non-GUI version of mch_init().
2555 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002556 static void
2557mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002559# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002560 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002561# endif
2562# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002566 /* Silently handle invalid parameters to CRT functions */
2567 SET_INVALID_PARAM_HANDLER;
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /* Let critical errors result in a failure, not in a dialog box. Required
2570 * for the timestamp test to work on removed floppies. */
2571 SetErrorMode(SEM_FAILCRITICALERRORS);
2572
2573 _fmode = O_BINARY; /* we do our own CR-LF translation */
2574 out_flush();
2575
2576 /* Obtain handles for the standard Console I/O devices */
2577 if (read_cmd_fd == 0)
2578 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2579 else
2580 create_conin();
2581 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2582
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002583# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002584 /* Save the initial console buffer for later restoration */
2585 SaveConsoleBuffer(&g_cbOrig);
2586 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002587# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /* Get current text attributes */
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002589 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2590 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (cterm_normal_fg_color == 0)
2593 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2594 if (cterm_normal_bg_color == 0)
2595 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2596
Bram Moolenaarbdace832019-03-02 10:13:42 +01002597 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002598 g_color_index_fg = g_attrDefault & 0xf;
2599 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 /* set termcap codes to current text attributes */
2602 update_tcap(g_attrCurrent);
2603
2604 GetConsoleCursorInfo(g_hConOut, &g_cci);
2605 GetConsoleMode(g_hConIn, &g_cmodein);
2606 GetConsoleMode(g_hConOut, &g_cmodeout);
2607
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002608# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 SaveConsoleTitleAndIcon();
2610 /*
2611 * Set both the small and big icons of the console window to Vim's icon.
2612 * Note that Vim presently only has one size of icon (32x32), but it
2613 * automatically gets scaled down to 16x16 when setting the small icon.
2614 */
2615 if (g_fCanChangeIcon)
2616 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 ui_get_shellsize();
2620
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002621# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 fdDump = fopen("dump", "wt");
2623
2624 if (fdDump)
2625 {
2626 time_t t;
2627
2628 time(&t);
2629 fputs(ctime(&t), fdDump);
2630 fflush(fdDump);
2631 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 g_fWindInitCalled = TRUE;
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002639 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002640# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002641
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002642 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002643 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644}
2645
2646/*
2647 * non-GUI version of mch_exit().
2648 * Shut down and exit with status `r'
2649 * Careful: mch_exit() may be called before mch_init()!
2650 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002651 static void
2652mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002654 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002656 vtp_exit();
2657
Bram Moolenaar955f1982017-02-05 15:10:51 +01002658 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (g_fWindInitCalled)
2660 settmode(TMODE_COOK);
2661
2662 ml_close_all(TRUE); /* remove all memfiles */
2663
2664 if (g_fWindInitCalled)
2665 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002666# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002667 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 /*
2669 * Restore both the small and big icons of the console window to
2670 * what they were at startup. Don't do this when the window is
2671 * closed, Vim would hang here.
2672 */
2673 if (g_fCanChangeIcon && !g_fForceExit)
2674 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002675# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002677# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (fdDump)
2679 {
2680 time_t t;
2681
2682 time(&t);
2683 fputs(ctime(&t), fdDump);
2684 fclose(fdDump);
2685 }
2686 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 SetConsoleCursorInfo(g_hConOut, &g_cci);
2691 SetConsoleMode(g_hConIn, g_cmodein);
2692 SetConsoleMode(g_hConOut, g_cmodeout);
2693
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002694# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 exit(r);
2699}
Bram Moolenaar4f974752019-02-17 17:44:42 +01002700#endif /* !FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002702 void
2703mch_init(void)
2704{
2705#ifdef VIMDLL
2706 if (gui.starting)
2707 mch_init_g();
2708 else
2709 mch_init_c();
2710#elif defined(FEAT_GUI_MSWIN)
2711 mch_init_g();
2712#else
2713 mch_init_c();
2714#endif
2715}
2716
2717 void
2718mch_exit(int r)
2719{
2720#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002721 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002722 mch_exit_g(r);
2723 else
2724 mch_exit_c(r);
2725#elif defined(FEAT_GUI_MSWIN)
2726 mch_exit_g(r);
2727#else
2728 mch_exit_c(r);
2729#endif
2730}
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732/*
2733 * Do we have an interactive window?
2734 */
2735 int
2736mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002737 int argc UNUSED,
2738 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 get_exe_name();
2741
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002742#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 return OK; /* GUI always has a tty */
2744#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002745# ifdef VIMDLL
2746 if (gui.in_use)
2747 return OK;
2748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (isatty(1))
2750 return OK;
2751 return FAIL;
2752#endif
2753}
2754
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002755/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002756 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 * When "len" is > 0, also expand short to long filenames.
2758 */
2759 void
2760fname_case(
2761 char_u *name,
2762 int len)
2763{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002764 int flen;
2765 WCHAR *p;
2766 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002768 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002769 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 return;
2771
2772 slash_adjust(name);
2773
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002774 p = enc_to_utf16(name, NULL);
2775 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002776 return;
2777
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002778 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002780 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002782 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002784 if (len > 0 || flen >= (int)STRLEN(q))
2785 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2786 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002789 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
2792
2793/*
2794 * Insert user name in s[len].
2795 */
2796 int
2797mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002798 char_u *s,
2799 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002801 WCHAR wszUserName[256 + 1]; /* UNLEN is 256 */
2802 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002804 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002805 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002806 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002807
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002808 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002809 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002810 vim_strncpy(s, p, len - 1);
2811 vim_free(p);
2812 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002813 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 s[0] = NUL;
2816 return FAIL;
2817}
2818
2819
2820/*
2821 * Insert host name in s[len].
2822 */
2823 void
2824mch_get_host_name(
2825 char_u *s,
2826 int len)
2827{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002828 WCHAR wszHostName[256 + 1];
2829 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002831 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002832 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002833 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002834
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002835 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002836 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002837 vim_strncpy(s, p, len - 1);
2838 vim_free(p);
2839 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002840 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842}
2843
2844
2845/*
2846 * return process ID
2847 */
2848 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002849mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 return (long)GetCurrentProcessId();
2852}
2853
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002854/*
2855 * return TRUE if process "pid" is still running
2856 */
2857 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002858mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002859{
2860 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2861 DWORD status = 0;
2862 int ret = FALSE;
2863
2864 if (hProcess == NULL)
2865 return FALSE; // might not have access
2866 if (GetExitCodeProcess(hProcess, &status) )
2867 ret = status == STILL_ACTIVE;
2868 CloseHandle(hProcess);
2869 return ret;
2870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872/*
2873 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2874 * Return OK for success, FAIL for failure.
2875 */
2876 int
2877mch_dirname(
2878 char_u *buf,
2879 int len)
2880{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002881 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 /*
2884 * Originally this was:
2885 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2886 * But the Win32s known bug list says that getcwd() doesn't work
2887 * so use the Win32 system call instead. <Negri>
2888 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002889 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002891 WCHAR wcbuf[_MAX_PATH + 1];
2892 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002894 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002896 p = utf16_to_enc(wcbuf, NULL);
2897 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002898 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002899 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002901 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002904 if (p == NULL)
2905 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002906
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002907 if (p != NULL)
2908 {
2909 vim_strncpy(buf, p, len - 1);
2910 vim_free(p);
2911 return OK;
2912 }
2913 }
2914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915}
2916
2917/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002918 * Get file permissions for "name".
2919 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002922mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002924 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002925 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002927 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002928 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929}
2930
2931
2932/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002933 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002934 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002935 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002938mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002940 long n;
2941 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002942
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002943 p = enc_to_utf16(name, NULL);
2944 if (p == NULL)
2945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002947 n = _wchmod(p, perm);
2948 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002949 if (n == -1)
2950 return FAIL;
2951
2952 win32_set_archive(name);
2953
2954 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
2957/*
2958 * Set hidden flag for "name".
2959 */
2960 void
2961mch_hide(char_u *name)
2962{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002963 int attrs = win32_getattrs(name);
2964 if (attrs == -1)
2965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002967 attrs |= FILE_ATTRIBUTE_HIDDEN;
2968 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969}
2970
2971/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002972 * Return TRUE if file "name" exists and is hidden.
2973 */
2974 int
2975mch_ishidden(char_u *name)
2976{
2977 int f = win32_getattrs(name);
2978
2979 if (f == -1)
2980 return FALSE; /* file does not exist at all */
2981
2982 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
2983}
2984
2985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 * return TRUE if "name" is a directory
2987 * return FALSE if "name" is not a directory or upon error
2988 */
2989 int
2990mch_isdir(char_u *name)
2991{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002992 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 if (f == -1)
2995 return FALSE; /* file does not exist at all */
2996
2997 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
2998}
2999
3000/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003001 * return TRUE if "name" is a directory, NOT a symlink to a directory
3002 * return FALSE if "name" is not a directory
3003 * return FALSE for error
3004 */
3005 int
3006mch_isrealdir(char_u *name)
3007{
3008 return mch_isdir(name) && !mch_is_symbolic_link(name);
3009}
3010
3011/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003012 * Create directory "name".
3013 * Return 0 on success, -1 on error.
3014 */
3015 int
3016mch_mkdir(char_u *name)
3017{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003018 WCHAR *p;
3019 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003020
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003021 p = enc_to_utf16(name, NULL);
3022 if (p == NULL)
3023 return -1;
3024 retval = _wmkdir(p);
3025 vim_free(p);
3026 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003027}
3028
3029/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003030 * Delete directory "name".
3031 * Return 0 on success, -1 on error.
3032 */
3033 int
3034mch_rmdir(char_u *name)
3035{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003036 WCHAR *p;
3037 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003038
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003039 p = enc_to_utf16(name, NULL);
3040 if (p == NULL)
3041 return -1;
3042 retval = _wrmdir(p);
3043 vim_free(p);
3044 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003045}
3046
3047/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003048 * Return TRUE if file "fname" has more than one link.
3049 */
3050 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003051mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003052{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003053 BY_HANDLE_FILE_INFORMATION info;
3054
3055 return win32_fileinfo(fname, &info) == FILEINFO_OK
3056 && info.nNumberOfLinks > 1;
3057}
3058
3059/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003060 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003061 */
3062 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003063mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003064{
3065 HANDLE hFind;
3066 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003067 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003068 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003069 WIN32_FIND_DATAW findDataW;
3070
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003071 wn = enc_to_utf16(name, NULL);
3072 if (wn == NULL)
3073 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003075 hFind = FindFirstFileW(wn, &findDataW);
3076 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003077 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003078 {
3079 fileFlags = findDataW.dwFileAttributes;
3080 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003081 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003082 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003083
3084 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003085 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3086 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003087 res = TRUE;
3088
3089 return res;
3090}
3091
3092/*
3093 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3094 * link.
3095 */
3096 int
3097mch_is_linked(char_u *fname)
3098{
3099 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3100 return TRUE;
3101 return FALSE;
3102}
3103
3104/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003105 * Get the by-handle-file-information for "fname".
3106 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003107 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003108 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3109 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3110 */
3111 int
3112win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3113{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003114 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003115 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003116 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003117
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003118 wn = enc_to_utf16(fname, NULL);
3119 if (wn == NULL)
3120 return FILEINFO_ENC_FAIL;
3121
3122 hFile = CreateFileW(wn, // file name
3123 GENERIC_READ, // access mode
3124 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3125 NULL, // security descriptor
3126 OPEN_EXISTING, // creation disposition
3127 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3128 NULL); // handle to template file
3129 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003130
3131 if (hFile != INVALID_HANDLE_VALUE)
3132 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003133 if (GetFileInformationByHandle(hFile, info) != 0)
3134 res = FILEINFO_OK;
3135 else
3136 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003137 CloseHandle(hFile);
3138 }
3139
Bram Moolenaar03f48552006-02-28 23:52:23 +00003140 return res;
3141}
3142
3143/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003144 * get file attributes for `name'
3145 * -1 : error
3146 * else FILE_ATTRIBUTE_* defined in winnt.h
3147 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003148 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003149win32_getattrs(char_u *name)
3150{
3151 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003152 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003153
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003154 p = enc_to_utf16(name, NULL);
3155 if (p == NULL)
3156 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003157
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003158 attr = GetFileAttributesW(p);
3159 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003160
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003161 return attr;
3162}
3163
3164/*
3165 * set file attributes for `name' to `attrs'
3166 *
3167 * return -1 for failure, 0 otherwise
3168 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003169 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003170win32_setattrs(char_u *name, int attrs)
3171{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003172 int res;
3173 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003174
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003175 p = enc_to_utf16(name, NULL);
3176 if (p == NULL)
3177 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003178
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003179 res = SetFileAttributesW(p, attrs);
3180 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003181
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003182 return res ? 0 : -1;
3183}
3184
3185/*
3186 * Set archive flag for "name".
3187 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003188 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003189win32_set_archive(char_u *name)
3190{
3191 int attrs = win32_getattrs(name);
3192 if (attrs == -1)
3193 return -1;
3194
3195 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3196 return win32_setattrs(name, attrs);
3197}
3198
3199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 * Return TRUE if file or directory "name" is writable (not readonly).
3201 * Strange semantics of Win32: a readonly directory is writable, but you can't
3202 * delete a file. Let's say this means it is writable.
3203 */
3204 int
3205mch_writable(char_u *name)
3206{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003207 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003209 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3210 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
3212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003214 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003215 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003216 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3217 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 */
3219 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003220mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar86621892019-03-30 21:51:28 +01003222 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3223 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3224 // UTF-8.
3225 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003226 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003227 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003228
Bram Moolenaar86621892019-03-30 21:51:28 +01003229 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003230 return FALSE;
3231
Bram Moolenaar86621892019-03-30 21:51:28 +01003232 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003233 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003234 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003235 return TRUE;
3236
3237 /*
3238 * Loop over all extensions in $PATHEXT.
3239 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003240 p = mch_getenv("PATHEXT");
3241 if (p == NULL)
3242 p = (char_u *)".com;.exe;.bat;.cmd";
3243 saved = vim_strsave(p);
3244 if (saved == NULL)
3245 return FALSE;
3246 p = saved;
3247 while (*p)
3248 {
3249 char_u *tmp = vim_strchr(p, ';');
3250
3251 if (tmp != NULL)
3252 *tmp = NUL;
3253 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3254 && executable_exists((char *)name, path, use_path))
3255 {
3256 vim_free(saved);
3257 return TRUE;
3258 }
3259 if (tmp == NULL)
3260 break;
3261 p = tmp + 1;
3262 }
3263 vim_free(saved);
3264
Bram Moolenaar86621892019-03-30 21:51:28 +01003265 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003266 p = mch_getenv("PATHEXT");
3267 if (p == NULL)
3268 p = (char_u *)".com;.exe;.bat;.cmd";
3269 while (*p)
3270 {
3271 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3272 {
3273 /* A single "." means no extension is added. */
3274 buf[len] = NUL;
3275 ++p;
3276 if (*p)
3277 ++p;
3278 }
3279 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003280 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003281 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003282 return TRUE;
3283 }
3284 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287/*
3288 * Check what "name" is:
3289 * NODE_NORMAL: file or directory (or doesn't exist)
3290 * NODE_WRITABLE: writable device, socket, fifo, etc.
3291 * NODE_OTHER: non-writable things
3292 */
3293 int
3294mch_nodetype(char_u *name)
3295{
3296 HANDLE hFile;
3297 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003298 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
Bram Moolenaar043545e2006-10-10 16:44:07 +00003300 /* We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3301 * read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3302 * here. */
3303 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3304 return NODE_WRITABLE;
3305
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003306 wn = enc_to_utf16(name, NULL);
3307 if (wn == NULL)
3308 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003309
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003310 hFile = CreateFileW(wn, // file name
3311 GENERIC_WRITE, // access mode
3312 0, // share mode
3313 NULL, // security descriptor
3314 OPEN_EXISTING, // creation disposition
3315 0, // file attributes
3316 NULL); // handle to template file
3317 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (hFile == INVALID_HANDLE_VALUE)
3319 return NODE_NORMAL;
3320
3321 type = GetFileType(hFile);
3322 CloseHandle(hFile);
3323 if (type == FILE_TYPE_CHAR)
3324 return NODE_WRITABLE;
3325 if (type == FILE_TYPE_DISK)
3326 return NODE_NORMAL;
3327 return NODE_OTHER;
3328}
3329
3330#ifdef HAVE_ACL
3331struct my_acl
3332{
3333 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3334 PSID pSidOwner;
3335 PSID pSidGroup;
3336 PACL pDacl;
3337 PACL pSacl;
3338};
3339#endif
3340
3341/*
3342 * Return a pointer to the ACL of file "fname" in allocated memory.
3343 * Return NULL if the ACL is not available for whatever reason.
3344 */
3345 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003346mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348#ifndef HAVE_ACL
3349 return (vim_acl_T)NULL;
3350#else
3351 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003352 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003354 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003355 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003357 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003358
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003359 wn = enc_to_utf16(fname, NULL);
3360 if (wn == NULL)
3361 return NULL;
3362
3363 // Try to retrieve the entire security descriptor.
3364 err = GetNamedSecurityInfoW(
3365 wn, // Abstract filename
3366 SE_FILE_OBJECT, // File Object
3367 OWNER_SECURITY_INFORMATION |
3368 GROUP_SECURITY_INFORMATION |
3369 DACL_SECURITY_INFORMATION |
3370 SACL_SECURITY_INFORMATION,
3371 &p->pSidOwner, // Ownership information.
3372 &p->pSidGroup, // Group membership.
3373 &p->pDacl, // Discretionary information.
3374 &p->pSacl, // For auditing purposes.
3375 &p->pSecurityDescriptor);
3376 if (err == ERROR_ACCESS_DENIED ||
3377 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003378 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003379 // Retrieve only DACL.
3380 (void)GetNamedSecurityInfoW(
3381 wn,
3382 SE_FILE_OBJECT,
3383 DACL_SECURITY_INFORMATION,
3384 NULL,
3385 NULL,
3386 &p->pDacl,
3387 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003388 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003389 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003390 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003391 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003392 mch_free_acl((vim_acl_T)p);
3393 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003395 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397
3398 return (vim_acl_T)p;
3399#endif
3400}
3401
Bram Moolenaar27515922013-06-29 15:36:26 +02003402#ifdef HAVE_ACL
3403/*
3404 * Check if "acl" contains inherited ACE.
3405 */
3406 static BOOL
3407is_acl_inherited(PACL acl)
3408{
3409 DWORD i;
3410 ACL_SIZE_INFORMATION acl_info;
3411 PACCESS_ALLOWED_ACE ace;
3412
3413 acl_info.AceCount = 0;
3414 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3415 for (i = 0; i < acl_info.AceCount; i++)
3416 {
3417 GetAce(acl, i, (LPVOID *)&ace);
3418 if (ace->Header.AceFlags & INHERITED_ACE)
3419 return TRUE;
3420 }
3421 return FALSE;
3422}
3423#endif
3424
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425/*
3426 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3427 * Errors are ignored.
3428 * This must only be called with "acl" equal to what mch_get_acl() returned.
3429 */
3430 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003431mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
3433#ifdef HAVE_ACL
3434 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003435 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003436 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003438 if (p == NULL)
3439 return;
3440
3441 wn = enc_to_utf16(fname, NULL);
3442 if (wn == NULL)
3443 return;
3444
3445 // Set security flags
3446 if (p->pSidOwner)
3447 sec_info |= OWNER_SECURITY_INFORMATION;
3448 if (p->pSidGroup)
3449 sec_info |= GROUP_SECURITY_INFORMATION;
3450 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003451 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003452 sec_info |= DACL_SECURITY_INFORMATION;
3453 // Do not inherit its parent's DACL.
3454 // If the DACL is inherited, Cygwin permissions would be changed.
3455 if (!is_acl_inherited(p->pDacl))
3456 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003457 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003458 if (p->pSacl)
3459 sec_info |= SACL_SECURITY_INFORMATION;
3460
3461 (void)SetNamedSecurityInfoW(
3462 wn, // Abstract filename
3463 SE_FILE_OBJECT, // File Object
3464 sec_info,
3465 p->pSidOwner, // Ownership information.
3466 p->pSidGroup, // Group membership.
3467 p->pDacl, // Discretionary information.
3468 p->pSacl // For auditing purposes.
3469 );
3470 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#endif
3472}
3473
3474 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003475mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477#ifdef HAVE_ACL
3478 struct my_acl *p = (struct my_acl *)acl;
3479
3480 if (p != NULL)
3481 {
3482 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3483 vim_free(p);
3484 }
3485#endif
3486}
3487
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003488#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490/*
3491 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3492 */
3493 static BOOL WINAPI
3494handler_routine(
3495 DWORD dwCtrlType)
3496{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003497 INPUT_RECORD ir;
3498 DWORD out;
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 switch (dwCtrlType)
3501 {
3502 case CTRL_C_EVENT:
3503 if (ctrl_c_interrupts)
3504 g_fCtrlCPressed = TRUE;
3505 return TRUE;
3506
3507 case CTRL_BREAK_EVENT:
3508 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003509 ctrl_break_was_pressed = TRUE;
3510 /* ReadConsoleInput is blocking, send a key event to continue. */
3511 ir.EventType = KEY_EVENT;
3512 ir.Event.KeyEvent.bKeyDown = TRUE;
3513 ir.Event.KeyEvent.wRepeatCount = 1;
3514 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3515 ir.Event.KeyEvent.wVirtualScanCode = 0;
3516 ir.Event.KeyEvent.dwControlKeyState = 0;
3517 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3518 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 return TRUE;
3520
3521 /* fatal events: shut down gracefully */
3522 case CTRL_CLOSE_EVENT:
3523 case CTRL_LOGOFF_EVENT:
3524 case CTRL_SHUTDOWN_EVENT:
3525 windgoto((int)Rows - 1, 0);
3526 g_fForceExit = TRUE;
3527
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003528 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 (dwCtrlType == CTRL_CLOSE_EVENT
3530 ? _("close")
3531 : dwCtrlType == CTRL_LOGOFF_EVENT
3532 ? _("logoff")
3533 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003534# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 preserve_exit(); /* output IObuff, preserve files and exit */
3539
3540 return TRUE; /* not reached */
3541
3542 default:
3543 return FALSE;
3544 }
3545}
3546
3547
3548/*
3549 * set the tty in (raw) ? "raw" : "cooked" mode
3550 */
3551 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003552mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 DWORD cmodein;
3555 DWORD cmodeout;
3556 BOOL bEnableHandler;
3557
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003558# ifdef VIMDLL
3559 if (gui.in_use)
3560 return;
3561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 GetConsoleMode(g_hConIn, &cmodein);
3563 GetConsoleMode(g_hConOut, &cmodeout);
3564 if (tmode == TMODE_RAW)
3565 {
3566 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3567 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (g_fMouseActive)
3569 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003570 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003571# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003572 /* Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003573 * VTP. */
3574 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003575# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003576 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003577# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003578 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 bEnableHandler = TRUE;
3580 }
3581 else /* cooked */
3582 {
3583 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3584 ENABLE_ECHO_INPUT);
3585 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3586 bEnableHandler = FALSE;
3587 }
3588 SetConsoleMode(g_hConIn, cmodein);
3589 SetConsoleMode(g_hConOut, cmodeout);
3590 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3591
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003592# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (fdDump)
3594 {
3595 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3596 tmode == TMODE_RAW ? "raw" :
3597 tmode == TMODE_COOK ? "cooked" : "normal",
3598 cmodein, cmodeout);
3599 fflush(fdDump);
3600 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602}
3603
3604
3605/*
3606 * Get the size of the current window in `Rows' and `Columns'
3607 * Return OK when size could be determined, FAIL otherwise.
3608 */
3609 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003610mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 CONSOLE_SCREEN_BUFFER_INFO csbi;
3613
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003614# ifdef VIMDLL
3615 if (gui.in_use)
3616 return OK;
3617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3619 {
3620 /*
3621 * For some reason, we are trying to get the screen dimensions
3622 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3623 * variables are really intended to mean the size of Vim screen
3624 * while in termcap mode.
3625 */
3626 Rows = g_cbTermcap.Info.dwSize.Y;
3627 Columns = g_cbTermcap.Info.dwSize.X;
3628 }
3629 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3630 {
3631 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3632 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3633 }
3634 else
3635 {
3636 Rows = 25;
3637 Columns = 80;
3638 }
3639 return OK;
3640}
3641
3642/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003643 * Resize console buffer to 'COORD'
3644 */
3645 static void
3646ResizeConBuf(
3647 HANDLE hConsole,
3648 COORD coordScreen)
3649{
3650 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3651 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003652# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003653 if (fdDump)
3654 {
3655 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3656 GetLastError());
3657 fflush(fdDump);
3658 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003659# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003660 }
3661}
3662
3663/*
3664 * Resize console window size to 'srWindowRect'
3665 */
3666 static void
3667ResizeWindow(
3668 HANDLE hConsole,
3669 SMALL_RECT srWindowRect)
3670{
3671 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3672 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003673# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003674 if (fdDump)
3675 {
3676 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3677 GetLastError());
3678 fflush(fdDump);
3679 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003680# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003681 }
3682}
3683
3684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 * Set a console window to `xSize' * `ySize'
3686 */
3687 static void
3688ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003689 HANDLE hConsole,
3690 int xSize,
3691 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693 CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */
3694 SMALL_RECT srWindowRect; /* hold the new console size */
3695 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003696 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003697 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003699# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (fdDump)
3701 {
3702 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3703 fflush(fdDump);
3704 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /* get the largest size we can size the console window to */
3708 coordScreen = GetLargestConsoleWindowSize(hConsole);
3709
3710 /* define the new console window size and scroll position */
3711 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3712 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3713 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3714
3715 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3716 {
3717 int sx, sy;
3718
3719 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3720 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3721 if (sy < ySize || sx < xSize)
3722 {
3723 /*
3724 * Increasing number of lines/columns, do buffer first.
3725 * Use the maximal size in x and y direction.
3726 */
3727 if (sy < ySize)
3728 coordScreen.Y = ySize;
3729 else
3730 coordScreen.Y = sy;
3731 if (sx < xSize)
3732 coordScreen.X = xSize;
3733 else
3734 coordScreen.X = sx;
3735 SetConsoleScreenBufferSize(hConsole, coordScreen);
3736 }
3737 }
3738
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003739 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 coordScreen.X = xSize;
3741 coordScreen.Y = ySize;
3742
Bram Moolenaar2551c032018-08-23 22:38:31 +02003743 // In the new console call API, only the first time in reverse order
3744 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003746 ResizeWindow(hConsole, srWindowRect);
3747 ResizeConBuf(hConsole, coordScreen);
3748 }
3749 else
3750 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003751 // Workaround for a Windows 10 bug
3752 cursor.X = srWindowRect.Left;
3753 cursor.Y = srWindowRect.Top;
3754 SetConsoleCursorPosition(hConsole, cursor);
3755
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003756 ResizeConBuf(hConsole, coordScreen);
3757 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003758 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760}
3761
3762
3763/*
3764 * Set the console window to `Rows' * `Columns'
3765 */
3766 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003767mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 COORD coordScreen;
3770
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003771# ifdef VIMDLL
3772 if (gui.in_use)
3773 return;
3774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 /* Don't change window size while still starting up */
3776 if (suppress_winsize != 0)
3777 {
3778 suppress_winsize = 2;
3779 return;
3780 }
3781
3782 if (term_console)
3783 {
3784 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3785
3786 /* Clamp Rows and Columns to reasonable values */
3787 if (Rows > coordScreen.Y)
3788 Rows = coordScreen.Y;
3789 if (Columns > coordScreen.X)
3790 Columns = coordScreen.X;
3791
3792 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3793 }
3794}
3795
3796/*
3797 * Rows and/or Columns has changed.
3798 */
3799 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003800mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003802# ifdef VIMDLL
3803 if (gui.in_use)
3804 return;
3805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3807}
3808
3809
3810/*
3811 * Called when started up, to set the winsize that was delayed.
3812 */
3813 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003814mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816 if (suppress_winsize == 2)
3817 {
3818 suppress_winsize = 0;
3819 mch_set_shellsize();
3820 shell_resized();
3821 }
3822 suppress_winsize = 0;
3823}
Bram Moolenaar4f974752019-02-17 17:44:42 +01003824#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003826 static BOOL
3827vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003828 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003829 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003830 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003831 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003832 PROCESS_INFORMATION *pi,
3833 LPVOID *env,
3834 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003835{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003836 BOOL ret = FALSE;
3837 WCHAR *wcmd, *wcwd = NULL;
3838
3839 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3840 if (wcmd == NULL)
3841 return FALSE;
3842 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003843 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003844 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3845 if (wcwd == NULL)
3846 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003847 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003848
3849 ret = CreateProcessW(
3850 NULL, // Executable name
3851 wcmd, // Command to execute
3852 NULL, // Process security attributes
3853 NULL, // Thread security attributes
3854 inherit_handles, // Inherit handles
3855 flags, // Creation flags
3856 env, // Environment
3857 wcwd, // Current directory
3858 (LPSTARTUPINFOW)si, // Startup information
3859 pi); // Process information
3860theend:
3861 vim_free(wcmd);
3862 vim_free(wcwd);
3863 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003864}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003867 static HINSTANCE
3868vim_shell_execute(
3869 char *cmd,
3870 INT n_show_cmd)
3871{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003872 HINSTANCE ret;
3873 WCHAR *wcmd;
3874
3875 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3876 if (wcmd == NULL)
3877 return (HINSTANCE) 0;
3878
3879 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3880 vim_free(wcmd);
3881 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003882}
3883
3884
Bram Moolenaar4f974752019-02-17 17:44:42 +01003885#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887/*
3888 * Specialised version of system() for Win32 GUI mode.
3889 * This version proceeds as follows:
3890 * 1. Create a console window for use by the subprocess
3891 * 2. Run the subprocess (it gets the allocated console by default)
3892 * 3. Wait for the subprocess to terminate and get its exit code
3893 * 4. Prompt the user to press a key to close the console window
3894 */
3895 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003896mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 STARTUPINFO si;
3899 PROCESS_INFORMATION pi;
3900 DWORD ret = 0;
3901 HWND hwnd = GetFocus();
3902
3903 si.cb = sizeof(si);
3904 si.lpReserved = NULL;
3905 si.lpDesktop = NULL;
3906 si.lpTitle = NULL;
3907 si.dwFlags = STARTF_USESHOWWINDOW;
3908 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003909 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003910 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003912 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003913 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 else
3915 si.wShowWindow = SW_SHOWNORMAL;
3916 si.cbReserved2 = 0;
3917 si.lpReserved2 = NULL;
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /* Now, run the command */
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003920 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003921 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3922 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 /* Wait for the command to terminate before continuing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003926# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 int delay = 1;
3928
3929 /* Keep updating the window while waiting for the shell to finish. */
3930 for (;;)
3931 {
3932 MSG msg;
3933
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003934 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003937 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003938 delay = 1;
3939 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3942 break;
3943
3944 /* We start waiting for a very short time and then increase it, so
3945 * that we respond quickly when the process is quick, and don't
3946 * consume too much overhead when it's slow. */
3947 if (delay < 50)
3948 delay += 10;
3949 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003950# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 /* Get the command exit code */
3955 GetExitCodeProcess(pi.hProcess, &ret);
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958 /* Close the handles to the subprocess, so that it goes away */
3959 CloseHandle(pi.hThread);
3960 CloseHandle(pi.hProcess);
3961
3962 /* Try to get input focus back. Doesn't always work though. */
3963 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3964
3965 return ret;
3966}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003967
3968/*
3969 * Thread launched by the gui to send the current buffer data to the
3970 * process. This way avoid to hang up vim totally if the children
3971 * process take a long time to process the lines.
3972 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02003973 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003974sub_process_writer(LPVOID param)
3975{
3976 HANDLE g_hChildStd_IN_Wr = param;
3977 linenr_T lnum = curbuf->b_op_start.lnum;
3978 DWORD len = 0;
3979 DWORD l;
3980 char_u *lp = ml_get(lnum);
3981 char_u *s;
3982 int written = 0;
3983
3984 for (;;)
3985 {
3986 l = (DWORD)STRLEN(lp + written);
3987 if (l == 0)
3988 len = 0;
3989 else if (lp[written] == NL)
3990 {
3991 /* NL -> NUL translation */
3992 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
3993 }
3994 else
3995 {
3996 s = vim_strchr(lp + written, NL);
3997 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
3998 s == NULL ? l : (DWORD)(s - (lp + written)),
3999 &len, NULL);
4000 }
4001 if (len == (int)l)
4002 {
4003 /* Finished a line, add a NL, unless this line should not have
4004 * one. */
4005 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004006 || (!curbuf->b_p_bin
4007 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004008 || (lnum != curbuf->b_no_eol_lnum
4009 && (lnum != curbuf->b_ml.ml_line_count
4010 || curbuf->b_p_eol)))
4011 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004012 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4013 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004014 }
4015
4016 ++lnum;
4017 if (lnum > curbuf->b_op_end.lnum)
4018 break;
4019
4020 lp = ml_get(lnum);
4021 written = 0;
4022 }
4023 else if (len > 0)
4024 written += len;
4025 }
4026
4027 /* finished all the lines, close pipe */
4028 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004029 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004030}
4031
4032
4033# define BUFLEN 100 /* length for buffer, stolen from unix version */
4034
4035/*
4036 * This function read from the children's stdout and write the
4037 * data on screen or in the buffer accordingly.
4038 */
4039 static void
4040dump_pipe(int options,
4041 HANDLE g_hChildStd_OUT_Rd,
4042 garray_T *ga,
4043 char_u buffer[],
4044 DWORD *buffer_off)
4045{
4046 DWORD availableBytes = 0;
4047 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004048 int ret;
4049 DWORD len;
4050 DWORD toRead;
4051 int repeatCount;
4052
4053 /* we query the pipe to see if there is any data to read
4054 * to avoid to perform a blocking read */
4055 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, /* pipe to query */
4056 NULL, /* optional buffer */
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004057 0, /* buffer size */
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004058 NULL, /* number of read bytes */
4059 &availableBytes, /* available bytes total */
4060 NULL); /* byteLeft */
4061
4062 repeatCount = 0;
4063 /* We got real data in the pipe, read it */
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004064 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004065 {
4066 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004067 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004068 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004069 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004070
4071 /* If we haven't read anything, there is a problem */
4072 if (len == 0)
4073 break;
4074
4075 availableBytes -= len;
4076
4077 if (options & SHELL_READ)
4078 {
4079 /* Do NUL -> NL translation, append NL separated
4080 * lines to the current buffer. */
4081 for (i = 0; i < len; ++i)
4082 {
4083 if (buffer[i] == NL)
4084 append_ga_line(ga);
4085 else if (buffer[i] == NUL)
4086 ga_append(ga, NL);
4087 else
4088 ga_append(ga, buffer[i]);
4089 }
4090 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004091 else if (has_mbyte)
4092 {
4093 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004094 int c;
4095 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004096
4097 len += *buffer_off;
4098 buffer[len] = NUL;
4099
4100 /* Check if the last character in buffer[] is
4101 * incomplete, keep these bytes for the next
4102 * round. */
4103 for (p = buffer; p < buffer + len; p += l)
4104 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004105 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004106 if (l == 0)
4107 l = 1; /* NUL byte? */
4108 else if (MB_BYTE2LEN(*p) != l)
4109 break;
4110 }
4111 if (p == buffer) /* no complete character */
4112 {
4113 /* avoid getting stuck at an illegal byte */
4114 if (len >= 12)
4115 ++p;
4116 else
4117 {
4118 *buffer_off = len;
4119 return;
4120 }
4121 }
4122 c = *p;
4123 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004124 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004125 if (p < buffer + len)
4126 {
4127 *p = c;
4128 *buffer_off = (DWORD)((buffer + len) - p);
4129 mch_memmove(buffer, p, *buffer_off);
4130 return;
4131 }
4132 *buffer_off = 0;
4133 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004134 else
4135 {
4136 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004137 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004138 }
4139
4140 windgoto(msg_row, msg_col);
4141 cursor_on();
4142 out_flush();
4143 }
4144}
4145
4146/*
4147 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4148 * for communication and doesn't open any new window.
4149 */
4150 static int
4151mch_system_piped(char *cmd, int options)
4152{
4153 STARTUPINFO si;
4154 PROCESS_INFORMATION pi;
4155 DWORD ret = 0;
4156
4157 HANDLE g_hChildStd_IN_Rd = NULL;
4158 HANDLE g_hChildStd_IN_Wr = NULL;
4159 HANDLE g_hChildStd_OUT_Rd = NULL;
4160 HANDLE g_hChildStd_OUT_Wr = NULL;
4161
4162 char_u buffer[BUFLEN + 1]; /* reading buffer + size */
4163 DWORD len;
4164
4165 /* buffer used to receive keys */
4166 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4167 int ta_len = 0; /* valid bytes in ta_buf[] */
4168
4169 DWORD i;
4170 int c;
4171 int noread_cnt = 0;
4172 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004173 int delay = 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004174 DWORD buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004175 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004176
4177 SECURITY_ATTRIBUTES saAttr;
4178
4179 /* Set the bInheritHandle flag so pipe handles are inherited. */
4180 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4181 saAttr.bInheritHandle = TRUE;
4182 saAttr.lpSecurityDescriptor = NULL;
4183
4184 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
4185 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004186 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004187 /* Create a pipe for the child process's STDIN. */
4188 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
4189 /* Ensure the write handle to the pipe for STDIN is not inherited. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004190 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004191 {
4192 CloseHandle(g_hChildStd_IN_Rd);
4193 CloseHandle(g_hChildStd_IN_Wr);
4194 CloseHandle(g_hChildStd_OUT_Rd);
4195 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004196 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004197 }
4198
4199 si.cb = sizeof(si);
4200 si.lpReserved = NULL;
4201 si.lpDesktop = NULL;
4202 si.lpTitle = NULL;
4203 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4204
4205 /* set-up our file redirection */
4206 si.hStdError = g_hChildStd_OUT_Wr;
4207 si.hStdOutput = g_hChildStd_OUT_Wr;
4208 si.hStdInput = g_hChildStd_IN_Rd;
4209 si.wShowWindow = SW_HIDE;
4210 si.cbReserved2 = 0;
4211 si.lpReserved2 = NULL;
4212
4213 if (options & SHELL_READ)
4214 ga_init2(&ga, 1, BUFLEN);
4215
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004216 if (cmd != NULL)
4217 {
4218 p = (char *)vim_strsave((char_u *)cmd);
4219 if (p != NULL)
4220 unescape_shellxquote((char_u *)p, p_sxe);
4221 else
4222 p = cmd;
4223 }
4224
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004225 /* Now, run the command.
4226 * About "Inherit handles" being TRUE: this command can be litigious,
4227 * handle inheritance was deactivated for pending temp file, but, if we
4228 * deactivate it, the pipes don't work for some reason. */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004229 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4230 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004231
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004232 if (p != cmd)
4233 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004234
4235 /* Close our unused side of the pipes */
4236 CloseHandle(g_hChildStd_IN_Rd);
4237 CloseHandle(g_hChildStd_OUT_Wr);
4238
4239 if (options & SHELL_WRITE)
4240 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004241 HANDLE thread = (HANDLE)
4242 _beginthreadex(NULL, /* security attributes */
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004243 0, /* default stack size */
4244 sub_process_writer, /* function to be executed */
4245 g_hChildStd_IN_Wr, /* parameter */
4246 0, /* creation flag, start immediately */
4247 NULL); /* we don't care about thread id */
4248 CloseHandle(thread);
4249 g_hChildStd_IN_Wr = NULL;
4250 }
4251
4252 /* Keep updating the window while waiting for the shell to finish. */
4253 for (;;)
4254 {
4255 MSG msg;
4256
Bram Moolenaar175d0702013-12-11 18:36:33 +01004257 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004258 {
4259 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004260 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004261 }
4262
4263 /* write pipe information in the window */
4264 if ((options & (SHELL_READ|SHELL_WRITE))
4265# ifdef FEAT_GUI
4266 || gui.in_use
4267# endif
4268 )
4269 {
4270 len = 0;
4271 if (!(options & SHELL_EXPAND)
4272 && ((options &
4273 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4274 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4275# ifdef FEAT_GUI
4276 || gui.in_use
4277# endif
4278 )
4279 && (ta_len > 0 || noread_cnt > 4))
4280 {
4281 if (ta_len == 0)
4282 {
4283 /* Get extra characters when we don't have any. Reset the
4284 * counter and timer. */
4285 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004286 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4287 }
4288 if (ta_len > 0 || len > 0)
4289 {
4290 /*
4291 * For pipes: Check for CTRL-C: send interrupt signal to
4292 * child. Check for CTRL-D: EOF, close pipe to child.
4293 */
4294 if (len == 1 && cmd != NULL)
4295 {
4296 if (ta_buf[ta_len] == Ctrl_C)
4297 {
4298 /* Learn what exit code is expected, for
4299 * now put 9 as SIGKILL */
4300 TerminateProcess(pi.hProcess, 9);
4301 }
4302 if (ta_buf[ta_len] == Ctrl_D)
4303 {
4304 CloseHandle(g_hChildStd_IN_Wr);
4305 g_hChildStd_IN_Wr = NULL;
4306 }
4307 }
4308
4309 /* replace K_BS by <BS> and K_DEL by <DEL> */
4310 for (i = ta_len; i < ta_len + len; ++i)
4311 {
4312 if (ta_buf[i] == CSI && len - i > 2)
4313 {
4314 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4315 if (c == K_DEL || c == K_KDEL || c == K_BS)
4316 {
4317 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4318 (size_t)(len - i - 2));
4319 if (c == K_DEL || c == K_KDEL)
4320 ta_buf[i] = DEL;
4321 else
4322 ta_buf[i] = Ctrl_H;
4323 len -= 2;
4324 }
4325 }
4326 else if (ta_buf[i] == '\r')
4327 ta_buf[i] = '\n';
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004328 if (has_mbyte)
4329 i += (*mb_ptr2len_len)(ta_buf + i,
4330 ta_len + len - i) - 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004331 }
4332
4333 /*
4334 * For pipes: echo the typed characters. For a pty this
4335 * does not seem to work.
4336 */
4337 for (i = ta_len; i < ta_len + len; ++i)
4338 {
4339 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4340 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004341 else if (has_mbyte)
4342 {
4343 int l = (*mb_ptr2len)(ta_buf + i);
4344
4345 msg_outtrans_len(ta_buf + i, l);
4346 i += l - 1;
4347 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004348 else
4349 msg_outtrans_len(ta_buf + i, 1);
4350 }
4351 windgoto(msg_row, msg_col);
4352 out_flush();
4353
4354 ta_len += len;
4355
4356 /*
4357 * Write the characters to the child, unless EOF has been
4358 * typed for pipes. Write one character at a time, to
4359 * avoid losing too much typeahead. When writing buffer
4360 * lines, drop the typed characters (only check for
4361 * CTRL-C).
4362 */
4363 if (options & SHELL_WRITE)
4364 ta_len = 0;
4365 else if (g_hChildStd_IN_Wr != NULL)
4366 {
4367 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4368 1, &len, NULL);
4369 // if we are typing in, we want to keep things reactive
4370 delay = 1;
4371 if (len > 0)
4372 {
4373 ta_len -= len;
4374 mch_memmove(ta_buf, ta_buf + len, ta_len);
4375 }
4376 }
4377 }
4378 }
4379 }
4380
4381 if (ta_len)
4382 ui_inchar_undo(ta_buf, ta_len);
4383
4384 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4385 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004386 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004387 break;
4388 }
4389
4390 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004391 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004392
4393 /* We start waiting for a very short time and then increase it, so
4394 * that we respond quickly when the process is quick, and don't
4395 * consume too much overhead when it's slow. */
4396 if (delay < 50)
4397 delay += 10;
4398 }
4399
4400 /* Close the pipe */
4401 CloseHandle(g_hChildStd_OUT_Rd);
4402 if (g_hChildStd_IN_Wr != NULL)
4403 CloseHandle(g_hChildStd_IN_Wr);
4404
4405 WaitForSingleObject(pi.hProcess, INFINITE);
4406
4407 /* Get the command exit code */
4408 GetExitCodeProcess(pi.hProcess, &ret);
4409
4410 if (options & SHELL_READ)
4411 {
4412 if (ga.ga_len > 0)
4413 {
4414 append_ga_line(&ga);
4415 /* remember that the NL was missing */
4416 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4417 }
4418 else
4419 curbuf->b_no_eol_lnum = 0;
4420 ga_clear(&ga);
4421 }
4422
4423 /* Close the handles to the subprocess, so that it goes away */
4424 CloseHandle(pi.hThread);
4425 CloseHandle(pi.hProcess);
4426
4427 return ret;
4428}
4429
4430 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004431mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004432{
4433 /* if we can pipe and the shelltemp option is off */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004434 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004435 return mch_system_piped(cmd, options);
4436 else
4437 return mch_system_classic(cmd, options);
4438}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004441#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004442 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004443mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004444{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004445 int ret;
4446 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004447 char_u *buf;
4448 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004449
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004450 // If the command starts and ends with double quotes, enclose the command
4451 // in parentheses.
4452 len = STRLEN(cmd);
4453 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4454 {
4455 len += 3;
4456 buf = alloc(len);
4457 if (buf == NULL)
4458 return -1;
4459 vim_snprintf((char *)buf, len, "(%s)", cmd);
4460 wcmd = enc_to_utf16(buf, NULL);
4461 free(buf);
4462 }
4463 else
4464 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4465
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004466 if (wcmd == NULL)
4467 return -1;
4468
4469 ret = _wsystem(wcmd);
4470 vim_free(wcmd);
4471 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474#endif
4475
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004476 static int
4477mch_system(char *cmd, int options)
4478{
4479#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004480 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004481 return mch_system_g(cmd, options);
4482 else
4483 return mch_system_c(cmd, options);
4484#elif defined(FEAT_GUI_MSWIN)
4485 return mch_system_g(cmd, options);
4486#else
4487 return mch_system_c(cmd, options);
4488#endif
4489}
4490
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004491#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4492/*
4493 * Use a terminal window to run a shell command in.
4494 */
4495 static int
4496mch_call_shell_terminal(
4497 char_u *cmd,
4498 int options UNUSED) /* SHELL_*, see vim.h */
4499{
4500 jobopt_T opt;
4501 char_u *newcmd = NULL;
4502 typval_T argvar[2];
4503 long_u cmdlen;
4504 int retval = -1;
4505 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004506 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004507 aco_save_T aco;
4508 oparg_T oa; /* operator arguments */
4509
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004510 if (cmd == NULL)
4511 cmdlen = STRLEN(p_sh) + 1;
4512 else
4513 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004514 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004515 if (newcmd == NULL)
4516 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004517 if (cmd == NULL)
4518 {
4519 STRCPY(newcmd, p_sh);
4520 ch_log(NULL, "starting terminal to run a shell");
4521 }
4522 else
4523 {
4524 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4525 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4526 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004527
4528 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004529
4530 argvar[0].v_type = VAR_STRING;
4531 argvar[0].vval.v_string = newcmd;
4532 argvar[1].v_type = VAR_UNKNOWN;
4533 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004534 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004535 {
4536 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004537 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004538 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004539
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004540 job = term_getjob(buf->b_term);
4541 ++job->jv_refcount;
4542
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004543 /* Find a window to make "buf" curbuf. */
4544 aucmd_prepbuf(&aco, buf);
4545
4546 clear_oparg(&oa);
4547 while (term_use_loop())
4548 {
4549 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4550 {
4551 /* If terminal_loop() returns OK we got a key that is handled
4552 * in Normal model. We don't do redrawing anyway. */
4553 if (terminal_loop(TRUE) == OK)
4554 normal_cmd(&oa, TRUE);
4555 }
4556 else
4557 normal_cmd(&oa, TRUE);
4558 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004559 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004560 ch_log(NULL, "system command finished");
4561
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004562 job_unref(job);
4563
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004564 /* restore curwin/curbuf and a few other things */
4565 aucmd_restbuf(&aco);
4566
4567 wait_return(TRUE);
4568 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4569
4570 vim_free(newcmd);
4571 return retval;
4572}
4573#endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575/*
4576 * Either execute a command by calling the shell or start a new shell
4577 */
4578 int
4579mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004580 char_u *cmd,
4581 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583 int x = 0;
4584 int tmode = cur_tmode;
4585#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004586 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004587
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004588 /* Change the title to reflect that we are in a subshell. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004589 if (GetConsoleTitleW(szShellTitle,
4590 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004591 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004592 if (cmd == NULL)
4593 wcscat(szShellTitle, L" :sh");
4594 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004595 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004596 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004597
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004598 if (wn != NULL)
4599 {
4600 wcscat(szShellTitle, L" - !");
4601 if ((wcslen(szShellTitle) + wcslen(wn) <
4602 sizeof(szShellTitle)/sizeof(WCHAR)))
4603 wcscat(szShellTitle, wn);
4604 SetConsoleTitleW(szShellTitle);
4605 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004606 }
4607 }
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#endif
4610
4611 out_flush();
4612
4613#ifdef MCH_WRITE_DUMP
4614 if (fdDump)
4615 {
4616 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4617 fflush(fdDump);
4618 }
4619#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004620#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004621 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004622 if (
4623# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004624 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004625# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004626 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004627 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4628 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004629 char_u *cmdbase = cmd;
4630
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004631 if (cmdbase != NULL)
4632 // Skip a leading quote and (.
4633 while (*cmdbase == '"' || *cmdbase == '(')
4634 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004635
4636 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004637 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4638 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004639 {
4640 // Use a terminal window to run the command in.
4641 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004642# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004643 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004644# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004645 return x;
4646 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004647 }
4648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 /*
4651 * Catch all deadly signals while running the external command, because a
4652 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4653 */
4654 signal(SIGINT, SIG_IGN);
4655#if defined(__GNUC__) && !defined(__MINGW32__)
4656 signal(SIGKILL, SIG_IGN);
4657#else
4658 signal(SIGBREAK, SIG_IGN);
4659#endif
4660 signal(SIGILL, SIG_IGN);
4661 signal(SIGFPE, SIG_IGN);
4662 signal(SIGSEGV, SIG_IGN);
4663 signal(SIGTERM, SIG_IGN);
4664 signal(SIGABRT, SIG_IGN);
4665
4666 if (options & SHELL_COOKED)
4667 settmode(TMODE_COOK); /* set to normal mode */
4668
4669 if (cmd == NULL)
4670 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004671 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 else
4674 {
4675 /* we use "command" or "cmd" to start the shell; slow but easy */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004676 char_u *newcmd = NULL;
4677 char_u *cmdbase = cmd;
4678 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004679
4680 /* Skip a leading ", ( and "(. */
4681 if (*cmdbase == '"' )
4682 ++cmdbase;
4683 if (*cmdbase == '(')
4684 ++cmdbase;
4685
Bram Moolenaar1c465442017-03-12 20:10:05 +01004686 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004687 {
4688 STARTUPINFO si;
4689 PROCESS_INFORMATION pi;
4690 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004691 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004692 char_u *p;
4693
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004694 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004695 si.cb = sizeof(si);
4696 si.lpReserved = NULL;
4697 si.lpDesktop = NULL;
4698 si.lpTitle = NULL;
4699 si.dwFlags = 0;
4700 si.cbReserved2 = 0;
4701 si.lpReserved2 = NULL;
4702
4703 cmdbase = skipwhite(cmdbase + 5);
4704 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004705 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004706 {
4707 cmdbase = skipwhite(cmdbase + 4);
4708 si.dwFlags = STARTF_USESHOWWINDOW;
4709 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004710 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004711 }
4712 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004713 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004714 {
4715 cmdbase = skipwhite(cmdbase + 2);
4716 flags = CREATE_NO_WINDOW;
4717 si.dwFlags = STARTF_USESTDHANDLES;
4718 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004719 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004720 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004721 NULL, // Security att.
4722 OPEN_EXISTING, // Open flags
4723 FILE_ATTRIBUTE_NORMAL, // File att.
4724 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004725 si.hStdOutput = si.hStdInput;
4726 si.hStdError = si.hStdInput;
4727 }
4728
4729 /* Remove a trailing ", ) and )" if they have a match
4730 * at the start of the command. */
4731 if (cmdbase > cmd)
4732 {
4733 p = cmdbase + STRLEN(cmdbase);
4734 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4735 *--p = NUL;
4736 if (p > cmdbase && p[-1] == ')'
4737 && (*cmd =='(' || cmd[1] == '('))
4738 *--p = NUL;
4739 }
4740
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004741 newcmd = cmdbase;
4742 unescape_shellxquote(cmdbase, p_sxe);
4743
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004744 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004745 * If creating new console, arguments are passed to the
4746 * 'cmd.exe' as-is. If it's not, arguments are not treated
4747 * correctly for current 'cmd.exe'. So unescape characters in
4748 * shellxescape except '|' for avoiding to be treated as
4749 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004750 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004751 if (flags != CREATE_NEW_CONSOLE)
4752 {
4753 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004754 char_u *cmd_shell = mch_getenv("COMSPEC");
4755
4756 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004757 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004758
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004759 subcmd = vim_strsave_escaped_ext(cmdbase,
4760 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004761 if (subcmd != NULL)
4762 {
4763 /* make "cmd.exe /c arguments" */
4764 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004765 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004766 if (newcmd != NULL)
4767 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004768 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004769 else
4770 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004771 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004772 }
4773 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004774
4775 /*
4776 * Now, start the command as a process, so that it doesn't
4777 * inherit our handles which causes unpleasant dangling swap
4778 * files if we exit before the spawned process
4779 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004780 if (vim_create_process((char *)newcmd, FALSE, flags,
4781 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004782 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004783 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4784 > (HINSTANCE)32)
4785 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004786 else
4787 {
4788 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004789#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004790# ifdef VIMDLL
4791 if (gui.in_use)
4792# endif
4793 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004794#endif
4795 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004796
4797 if (newcmd != cmdbase)
4798 vim_free(newcmd);
4799
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004800 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004801 {
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004802 /* Close the handle to \\.\NUL created above. */
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004803 CloseHandle(si.hStdInput);
4804 }
4805 /* Close the handles to the subprocess, so that it goes away */
4806 CloseHandle(pi.hThread);
4807 CloseHandle(pi.hProcess);
4808 }
4809 else
4810 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004811 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004812#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004813 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004814 (!s_dont_use_vimrun && p_stmp ?
4815 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4816 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004818 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004819
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004820 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004821 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004823#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004824 if (
4825# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004826 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004827# endif
4828 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004830 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4831 "External commands will not pause after completion.\n"
4832 "See :help win32-vimrun for more information.");
4833 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004834 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4835 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004836
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004837 if (wmsg != NULL && wtitle != NULL)
4838 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4839 vim_free(wmsg);
4840 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 need_vimrun_warning = FALSE;
4842 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004843 if (
4844# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004845 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004846# endif
4847 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004848 // Use vimrun to execute the command. It opens a console
4849 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004850 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 vimrun_path,
4852 (msg_silent != 0 || (options & SHELL_DOOUT))
4853 ? "-s " : "",
4854 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004855 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004856# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004857 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004858# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004859 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004860 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004861 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4862 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004865 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004866 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004868 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 }
4872
4873 if (tmode == TMODE_RAW)
4874 settmode(TMODE_RAW); /* set to raw mode */
4875
4876 /* Print the return value, unless "vimrun" was used. */
4877 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004878#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004879 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004880 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
4882 )
4883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004884 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 msg_putchar('\n');
4886 }
4887#ifdef FEAT_TITLE
4888 resettitle();
4889#endif
4890
4891 signal(SIGINT, SIG_DFL);
4892#if defined(__GNUC__) && !defined(__MINGW32__)
4893 signal(SIGKILL, SIG_DFL);
4894#else
4895 signal(SIGBREAK, SIG_DFL);
4896#endif
4897 signal(SIGILL, SIG_DFL);
4898 signal(SIGFPE, SIG_DFL);
4899 signal(SIGSEGV, SIG_DFL);
4900 signal(SIGTERM, SIG_DFL);
4901 signal(SIGABRT, SIG_DFL);
4902
4903 return x;
4904}
4905
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004906#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004907 static HANDLE
4908job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004909 char_u *fname,
4910 DWORD dwDesiredAccess,
4911 DWORD dwShareMode,
4912 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4913 DWORD dwCreationDisposition,
4914 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004915{
4916 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004917 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004918
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004919 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004920 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004921 return INVALID_HANDLE_VALUE;
4922
4923 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4924 lpSecurityAttributes, dwCreationDisposition,
4925 dwFlagsAndAttributes, NULL);
4926 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004927 return h;
4928}
4929
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004930/*
4931 * Turn the dictionary "env" into a NUL separated list that can be used as the
4932 * environment argument of vim_create_process().
4933 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004934 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004935win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004936{
4937 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004938 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004939 LPVOID base = GetEnvironmentStringsW();
4940
4941 /* for last \0 */
4942 if (ga_grow(gap, 1) == FAIL)
4943 return;
4944
4945 if (base)
4946 {
4947 WCHAR *p = (WCHAR*) base;
4948
4949 /* for last \0 */
4950 if (ga_grow(gap, 1) == FAIL)
4951 return;
4952
4953 while (*p != 0 || *(p + 1) != 0)
4954 {
4955 if (ga_grow(gap, 1) == OK)
4956 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4957 p++;
4958 }
4959 FreeEnvironmentStrings(base);
4960 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4961 }
4962
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004963 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004964 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004965 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004966 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004967 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004968 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004969 typval_T *item = &dict_lookup(hi)->di_tv;
4970 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004971 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004972 --todo;
4973 if (wkey != NULL && wval != NULL)
4974 {
4975 size_t n;
4976 size_t lkey = wcslen(wkey);
4977 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004978
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004979 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4980 continue;
4981 for (n = 0; n < lkey; n++)
4982 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4983 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4984 for (n = 0; n < lval; n++)
4985 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4986 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4987 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004988 vim_free(wkey);
4989 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004990 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004991 }
4992 }
4993
Bram Moolenaar493359e2018-06-12 20:25:52 +02004994# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004995 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02004996# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004997 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004998 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004999# endif
5000# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005001 char_u *version = get_vim_var_str(VV_VERSION);
5002 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005003# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005004 // size of "VIM_SERVERNAME=" and value,
5005 // plus "VIM_TERMINAL=" and value,
5006 // plus two terminating NULs
5007 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005008# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005009 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005010# endif
5011# ifdef FEAT_TERMINAL
5012 + 13 + version_len + 2
5013# endif
5014 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005015
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005016 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005017 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005018# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005019 for (n = 0; n < 15; n++)
5020 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5021 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005022 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005023 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5024 (WCHAR)servername[n];
5025 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005026# endif
5027# ifdef FEAT_TERMINAL
5028 if (is_terminal)
5029 {
5030 for (n = 0; n < 13; n++)
5031 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5032 (WCHAR)"VIM_TERMINAL="[n];
5033 for (n = 0; n < version_len; n++)
5034 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5035 (WCHAR)version[n];
5036 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5037 }
5038# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005039 }
5040 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005041# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005042}
5043
Bram Moolenaarb091f302019-01-19 14:37:00 +01005044/*
5045 * Create a pair of pipes.
5046 * Return TRUE for success, FALSE for failure.
5047 */
5048 static BOOL
5049create_pipe_pair(HANDLE handles[2])
5050{
5051 static LONG s;
5052 char name[64];
5053 SECURITY_ATTRIBUTES sa;
5054
5055 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5056 GetCurrentProcessId(),
5057 InterlockedIncrement(&s));
5058
5059 // Create named pipe. Max size of named pipe is 65535.
5060 handles[1] = CreateNamedPipe(
5061 name,
5062 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5063 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005064 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005065
5066 if (handles[1] == INVALID_HANDLE_VALUE)
5067 return FALSE;
5068
5069 sa.nLength = sizeof(sa);
5070 sa.bInheritHandle = TRUE;
5071 sa.lpSecurityDescriptor = NULL;
5072
5073 handles[0] = CreateFile(name,
5074 FILE_GENERIC_READ,
5075 FILE_SHARE_READ, &sa,
5076 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5077
5078 if (handles[0] == INVALID_HANDLE_VALUE)
5079 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005080 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005081 return FALSE;
5082 }
5083
5084 return TRUE;
5085}
5086
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005087 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005088mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005089{
5090 STARTUPINFO si;
5091 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005092 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005093 SECURITY_ATTRIBUTES saAttr;
5094 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005095 HANDLE ifd[2];
5096 HANDLE ofd[2];
5097 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005098 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005099
5100 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5101 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5102 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5103 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5104 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5105 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5106 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5107
5108 if (use_out_for_err && use_null_for_out)
5109 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005110
5111 ifd[0] = INVALID_HANDLE_VALUE;
5112 ifd[1] = INVALID_HANDLE_VALUE;
5113 ofd[0] = INVALID_HANDLE_VALUE;
5114 ofd[1] = INVALID_HANDLE_VALUE;
5115 efd[0] = INVALID_HANDLE_VALUE;
5116 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005117 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005118
Bram Moolenaar14207f42016-10-27 21:13:10 +02005119 jo = CreateJobObject(NULL, NULL);
5120 if (jo == NULL)
5121 {
5122 job->jv_status = JOB_FAILED;
5123 goto failed;
5124 }
5125
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005126 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005127 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005128
Bram Moolenaar76467df2016-02-12 19:30:26 +01005129 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005130 ZeroMemory(&si, sizeof(si));
5131 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005132 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005133 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005134
Bram Moolenaard8070362016-02-15 21:56:54 +01005135 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5136 saAttr.bInheritHandle = TRUE;
5137 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005138
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005139 if (use_file_for_in)
5140 {
5141 char_u *fname = options->jo_io_name[PART_IN];
5142
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005143 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5144 FILE_SHARE_READ | FILE_SHARE_WRITE,
5145 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5146 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005148 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005149 goto failed;
5150 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005151 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005152 else if (!use_null_for_in
5153 && (!create_pipe_pair(ifd)
5154 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005155 goto failed;
5156
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005157 if (use_file_for_out)
5158 {
5159 char_u *fname = options->jo_io_name[PART_OUT];
5160
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005161 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5162 FILE_SHARE_READ | FILE_SHARE_WRITE,
5163 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5164 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005166 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005167 goto failed;
5168 }
5169 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005170 else if (!use_null_for_out &&
5171 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005172 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005173 goto failed;
5174
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005175 if (use_file_for_err)
5176 {
5177 char_u *fname = options->jo_io_name[PART_ERR];
5178
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005179 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5180 FILE_SHARE_READ | FILE_SHARE_WRITE,
5181 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5182 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005184 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005185 goto failed;
5186 }
5187 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005188 else if (!use_out_for_err && !use_null_for_err &&
5189 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005190 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005191 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005192
Bram Moolenaard8070362016-02-15 21:56:54 +01005193 si.dwFlags |= STARTF_USESTDHANDLES;
5194 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005195 si.hStdOutput = ofd[1];
5196 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5197
5198 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5199 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005200 if (options->jo_set & JO_CHANNEL)
5201 {
5202 channel = options->jo_channel;
5203 if (channel != NULL)
5204 ++channel->ch_refcount;
5205 }
5206 else
5207 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005208 if (channel == NULL)
5209 goto failed;
5210 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005211
5212 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005213 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005214 CREATE_DEFAULT_ERROR_MODE |
5215 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005216 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005217 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005218 &si, &pi,
5219 ga.ga_data,
5220 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005221 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005222 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005223 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005224 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005225 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005226
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005227 ga_clear(&ga);
5228
Bram Moolenaar14207f42016-10-27 21:13:10 +02005229 if (!AssignProcessToJobObject(jo, pi.hProcess))
5230 {
5231 /* if failing, switch the way to terminate
5232 * process with TerminateProcess. */
5233 CloseHandle(jo);
5234 jo = NULL;
5235 }
5236 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005237 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005238 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005239 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005240 job->jv_status = JOB_STARTED;
5241
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005242 CloseHandle(ifd[0]);
5243 CloseHandle(ofd[1]);
5244 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005245 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005246
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005247 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005248 if (channel != NULL)
5249 {
5250 channel_set_pipes(channel,
5251 use_file_for_in || use_null_for_in
5252 ? INVALID_FD : (sock_T)ifd[1],
5253 use_file_for_out || use_null_for_out
5254 ? INVALID_FD : (sock_T)ofd[0],
5255 use_out_for_err || use_file_for_err || use_null_for_err
5256 ? INVALID_FD : (sock_T)efd[0]);
5257 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005258 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005259 return;
5260
5261failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005262 CloseHandle(ifd[0]);
5263 CloseHandle(ofd[0]);
5264 CloseHandle(efd[0]);
5265 CloseHandle(ifd[1]);
5266 CloseHandle(ofd[1]);
5267 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005268 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005269 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005270}
5271
5272 char *
5273mch_job_status(job_T *job)
5274{
5275 DWORD dwExitCode = 0;
5276
Bram Moolenaar76467df2016-02-12 19:30:26 +01005277 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5278 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005279 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005280 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005281 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005282 {
5283 ch_log(job->jv_channel, "Job ended");
5284 job->jv_status = JOB_ENDED;
5285 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005286 return "dead";
5287 }
5288 return "run";
5289}
5290
Bram Moolenaar97792de2016-10-15 18:36:49 +02005291 job_T *
5292mch_detect_ended_job(job_T *job_list)
5293{
5294 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5295 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5296 job_T *job = job_list;
5297
5298 while (job != NULL)
5299 {
5300 DWORD n;
5301 DWORD result;
5302
5303 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5304 && job != NULL; job = job->jv_next)
5305 {
5306 if (job->jv_status == JOB_STARTED)
5307 {
5308 jobHandles[n] = job->jv_proc_info.hProcess;
5309 jobArray[n] = job;
5310 ++n;
5311 }
5312 }
5313 if (n == 0)
5314 continue;
5315 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5316 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5317 {
5318 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5319
5320 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5321 return wait_job;
5322 }
5323 }
5324 return NULL;
5325}
5326
Bram Moolenaarfb630902016-10-29 14:55:00 +02005327 static BOOL
5328terminate_all(HANDLE process, int code)
5329{
5330 PROCESSENTRY32 pe;
5331 HANDLE h = INVALID_HANDLE_VALUE;
5332 DWORD pid = GetProcessId(process);
5333
5334 if (pid != 0)
5335 {
5336 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5337 if (h != INVALID_HANDLE_VALUE)
5338 {
5339 pe.dwSize = sizeof(PROCESSENTRY32);
5340 if (!Process32First(h, &pe))
5341 goto theend;
5342
5343 do
5344 {
5345 if (pe.th32ParentProcessID == pid)
5346 {
5347 HANDLE ph = OpenProcess(
5348 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5349 if (ph != NULL)
5350 {
5351 terminate_all(ph, code);
5352 CloseHandle(ph);
5353 }
5354 }
5355 } while (Process32Next(h, &pe));
5356
5357 CloseHandle(h);
5358 }
5359 }
5360
5361theend:
5362 return TerminateProcess(process, code);
5363}
5364
5365/*
5366 * Send a (deadly) signal to "job".
5367 * Return FAIL if it didn't work.
5368 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005369 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005370mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005371{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005372 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005373
Bram Moolenaar923d9262016-02-25 20:56:01 +01005374 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005375 {
Bram Moolenaarfb630902016-10-29 14:55:00 +02005376 /* deadly signal */
Bram Moolenaar14207f42016-10-27 21:13:10 +02005377 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005378 {
5379 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5380 job->jv_channel->ch_killing = TRUE;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005381 return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005382 }
Bram Moolenaarfb630902016-10-29 14:55:00 +02005383 return terminate_all(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005384 }
5385
5386 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5387 return FAIL;
5388 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005389 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5390 job->jv_proc_info.dwProcessId)
5391 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005392 FreeConsole();
5393 return ret;
5394}
5395
5396/*
5397 * Clear the data related to "job".
5398 */
5399 void
5400mch_clear_job(job_T *job)
5401{
5402 if (job->jv_status != JOB_FAILED)
5403 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005404 if (job->jv_job_object != NULL)
5405 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005406 CloseHandle(job->jv_proc_info.hProcess);
5407 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005408}
5409#endif
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005412#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414/*
5415 * Start termcap mode
5416 */
5417 static void
5418termcap_mode_start(void)
5419{
5420 DWORD cmodein;
5421
5422 if (g_fTermcapMode)
5423 return;
5424
5425 SaveConsoleBuffer(&g_cbNonTermcap);
5426
5427 if (g_cbTermcap.IsValid)
5428 {
5429 /*
5430 * We've been in termcap mode before. Restore certain screen
5431 * characteristics, including the buffer size and the window
5432 * size. Since we will be redrawing the screen, we don't need
5433 * to restore the actual contents of the buffer.
5434 */
5435 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005436 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5438 Rows = g_cbTermcap.Info.dwSize.Y;
5439 Columns = g_cbTermcap.Info.dwSize.X;
5440 }
5441 else
5442 {
5443 /*
5444 * This is our first time entering termcap mode. Clear the console
5445 * screen buffer, and resize the buffer to match the current window
5446 * size. We will use this as the size of our editing environment.
5447 */
5448 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005449 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5451 }
5452
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005453# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (g_fMouseActive)
5459 cmodein |= ENABLE_MOUSE_INPUT;
5460 else
5461 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 cmodein |= ENABLE_WINDOW_INPUT;
5463 SetConsoleMode(g_hConIn, cmodein);
5464
5465 redraw_later_clear();
5466 g_fTermcapMode = TRUE;
5467}
5468
5469
5470/*
5471 * End termcap mode
5472 */
5473 static void
5474termcap_mode_end(void)
5475{
5476 DWORD cmodein;
5477 ConsoleBuffer *cb;
5478 COORD coord;
5479 DWORD dwDummy;
5480
5481 if (!g_fTermcapMode)
5482 return;
5483
5484 SaveConsoleBuffer(&g_cbTermcap);
5485
5486 GetConsoleMode(g_hConIn, &cmodein);
5487 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5488 SetConsoleMode(g_hConIn, cmodein);
5489
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005490# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005491 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005492# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005496 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 SetConsoleCursorInfo(g_hConOut, &g_cci);
5498
5499 if (p_rs || exiting)
5500 {
5501 /*
5502 * Clear anything that happens to be on the current line.
5503 */
5504 coord.X = 0;
5505 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5506 FillConsoleOutputCharacter(g_hConOut, ' ',
5507 cb->Info.dwSize.X, coord, &dwDummy);
5508 /*
5509 * The following is just for aesthetics. If we are exiting without
5510 * restoring the screen, then we want to have a prompt string
5511 * appear at the bottom line. However, the command interpreter
5512 * seems to always advance the cursor one line before displaying
5513 * the prompt string, which causes the screen to scroll. To
5514 * counter this, move the cursor up one line before exiting.
5515 */
5516 if (exiting && !p_rs)
5517 coord.Y--;
5518 /*
5519 * Position the cursor at the leftmost column of the desired row.
5520 */
5521 SetConsoleCursorPosition(g_hConOut, coord);
5522 }
5523
5524 g_fTermcapMode = FALSE;
5525}
Bram Moolenaar4f974752019-02-17 17:44:42 +01005526#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005529#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 void
5531mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005532 char_u *s UNUSED,
5533 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 /* never used */
5536}
5537
5538#else
5539
5540/*
5541 * clear `n' chars, starting from `coord'
5542 */
5543 static void
5544clear_chars(
5545 COORD coord,
5546 DWORD n)
5547{
5548 DWORD dwDummy;
5549
5550 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005551
5552 if (!USE_VTP)
5553 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5554 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005555 {
5556 set_console_color_rgb();
5557 gotoxy(coord.X + 1, coord.Y + 1);
5558 vtp_printf("\033[%dX", n);
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561
5562
5563/*
5564 * Clear the screen
5565 */
5566 static void
5567clear_screen(void)
5568{
5569 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005570
5571 if (!USE_VTP)
5572 clear_chars(g_coord, Rows * Columns);
5573 else
5574 {
5575 set_console_color_rgb();
5576 gotoxy(1, 1);
5577 vtp_printf("\033[2J");
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581
5582/*
5583 * Clear to end of display
5584 */
5585 static void
5586clear_to_end_of_display(void)
5587{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005588 COORD save = g_coord;
5589
5590 if (!USE_VTP)
5591 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005593 else
5594 {
5595 set_console_color_rgb();
5596 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5597 vtp_printf("\033[0J");
5598
5599 gotoxy(save.X + 1, save.Y + 1);
5600 g_coord = save;
5601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602}
5603
5604
5605/*
5606 * Clear to end of line
5607 */
5608 static void
5609clear_to_end_of_line(void)
5610{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005611 COORD save = g_coord;
5612
5613 if (!USE_VTP)
5614 clear_chars(g_coord, Columns - g_coord.X);
5615 else
5616 {
5617 set_console_color_rgb();
5618 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5619 vtp_printf("\033[0K");
5620
5621 gotoxy(save.X + 1, save.Y + 1);
5622 g_coord = save;
5623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626
5627/*
5628 * Scroll the scroll region up by `cLines' lines
5629 */
5630 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005631scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 COORD oldcoord = g_coord;
5634
5635 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5636 delete_lines(cLines);
5637
5638 g_coord = oldcoord;
5639}
5640
5641
5642/*
5643 * Set the scroll region
5644 */
5645 static void
5646set_scroll_region(
5647 unsigned left,
5648 unsigned top,
5649 unsigned right,
5650 unsigned bottom)
5651{
5652 if (left >= right
5653 || top >= bottom
5654 || right > (unsigned) Columns - 1
5655 || bottom > (unsigned) Rows - 1)
5656 return;
5657
5658 g_srScrollRegion.Left = left;
5659 g_srScrollRegion.Top = top;
5660 g_srScrollRegion.Right = right;
5661 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005662}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005663
Bram Moolenaar6982f422019-02-16 16:48:01 +01005664 static void
5665set_scroll_region_tb(
5666 unsigned top,
5667 unsigned bottom)
5668{
5669 if (top >= bottom || bottom > (unsigned)Rows - 1)
5670 return;
5671
5672 g_srScrollRegion.Top = top;
5673 g_srScrollRegion.Bottom = bottom;
5674}
5675
5676 static void
5677set_scroll_region_lr(
5678 unsigned left,
5679 unsigned right)
5680{
5681 if (left >= right || right > (unsigned)Columns - 1)
5682 return;
5683
5684 g_srScrollRegion.Left = left;
5685 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686}
5687
5688
5689/*
5690 * Insert `cLines' lines at the current cursor position
5691 */
5692 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005693insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005695 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 COORD dest;
5697 CHAR_INFO fill;
5698
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005699 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5700
Bram Moolenaar6982f422019-02-16 16:48:01 +01005701 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 dest.Y = g_coord.Y + cLines;
5703
Bram Moolenaar6982f422019-02-16 16:48:01 +01005704 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 source.Top = g_coord.Y;
5706 source.Right = g_srScrollRegion.Right;
5707 source.Bottom = g_srScrollRegion.Bottom - cLines;
5708
Bram Moolenaar6982f422019-02-16 16:48:01 +01005709 clip.Left = g_srScrollRegion.Left;
5710 clip.Top = g_coord.Y;
5711 clip.Right = g_srScrollRegion.Right;
5712 clip.Bottom = g_srScrollRegion.Bottom;
5713
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005714 fill.Char.AsciiChar = ' ';
5715 if (!USE_VTP)
5716 fill.Attributes = g_attrCurrent;
5717 else
5718 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005720 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005721
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005722 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5723
Bram Moolenaar6982f422019-02-16 16:48:01 +01005724 // Here we have to deal with a win32 console flake: If the scroll
5725 // region looks like abc and we scroll c to a and fill with d we get
5726 // cbd... if we scroll block c one line at a time to a, we get cdd...
5727 // vim expects cdd consistently... So we have to deal with that
5728 // here... (this also occurs scrolling the same way in the other
5729 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
5731 if (source.Bottom < dest.Y)
5732 {
5733 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005734 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaar6982f422019-02-16 16:48:01 +01005736 coord.X = source.Left;
5737 for (i = clip.Top; i < dest.Y; ++i)
5738 {
5739 coord.Y = i;
5740 clear_chars(coord, source.Right - source.Left + 1);
5741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743}
5744
5745
5746/*
5747 * Delete `cLines' lines at the current cursor position
5748 */
5749 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005750delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005752 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 COORD dest;
5754 CHAR_INFO fill;
5755 int nb;
5756
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005757 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5758
Bram Moolenaar6982f422019-02-16 16:48:01 +01005759 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 dest.Y = g_coord.Y;
5761
Bram Moolenaar6982f422019-02-16 16:48:01 +01005762 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 source.Top = g_coord.Y + cLines;
5764 source.Right = g_srScrollRegion.Right;
5765 source.Bottom = g_srScrollRegion.Bottom;
5766
Bram Moolenaar6982f422019-02-16 16:48:01 +01005767 clip.Left = g_srScrollRegion.Left;
5768 clip.Top = g_coord.Y;
5769 clip.Right = g_srScrollRegion.Right;
5770 clip.Bottom = g_srScrollRegion.Bottom;
5771
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005772 fill.Char.AsciiChar = ' ';
5773 if (!USE_VTP)
5774 fill.Attributes = g_attrCurrent;
5775 else
5776 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005778 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005779
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005780 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5781
Bram Moolenaar6982f422019-02-16 16:48:01 +01005782 // Here we have to deal with a win32 console flake; See insert_lines()
5783 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 nb = dest.Y + (source.Bottom - source.Top) + 1;
5786
5787 if (nb < source.Top)
5788 {
5789 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005790 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar6982f422019-02-16 16:48:01 +01005792 coord.X = source.Left;
5793 for (i = nb; i < clip.Bottom; ++i)
5794 {
5795 coord.Y = i;
5796 clear_chars(coord, source.Right - source.Left + 1);
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799}
5800
5801
5802/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005803 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 */
5805 static void
5806gotoxy(
5807 unsigned x,
5808 unsigned y)
5809{
5810 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5811 return;
5812
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005813 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005814 {
5815 // external cursor coords are 1-based; internal are 0-based
5816 g_coord.X = x - 1;
5817 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005818 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005819 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005820 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005821 {
5822 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005823 // destruction. Insider build bug. Always enabled because it's cheap
5824 // and avoids mistakes with recognizing the build.
5825 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005826
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005827 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005828
5829 g_coord.X = x - 1;
5830 g_coord.Y = y - 1;
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
5834
5835/*
5836 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005837 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 */
5839 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005840textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005842 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 SetConsoleTextAttribute(g_hConOut, wAttr);
5845}
5846
5847
5848 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005849textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005851 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005853 if (!USE_VTP)
5854 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5855 else
5856 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857}
5858
5859
5860 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005861textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005863 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005865 if (!USE_VTP)
5866 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5867 else
5868 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869}
5870
5871
5872/*
5873 * restore the default text attribute (whatever we started with)
5874 */
5875 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005876normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005878 if (!USE_VTP)
5879 textattr(g_attrDefault);
5880 else
5881 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882}
5883
5884
5885static WORD g_attrPreStandout = 0;
5886
5887/*
5888 * Make the text standout, by brightening it
5889 */
5890 static void
5891standout(void)
5892{
5893 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5896}
5897
5898
5899/*
5900 * Turn off standout mode
5901 */
5902 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005903standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005907
5908 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909}
5910
5911
5912/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005913 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 */
5915 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005916mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
5918 char_u *p;
5919 int n;
5920
5921 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5922 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005923 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005924# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005925 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005926# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005927 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
5929 p = T_ME + 2;
5930 n = getdigits(&p);
5931 if (*p == 'm' && n > 0)
5932 {
5933 cterm_normal_fg_color = (n & 0xf) + 1;
5934 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5935 }
5936 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005937# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005938 cterm_normal_fg_gui_color = INVALCOLOR;
5939 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941}
5942
5943
5944/*
5945 * visual bell: flash the screen
5946 */
5947 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005948visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 COORD coordOrigin = {0, 0};
5951 WORD attrFlash = ~g_attrCurrent & 0xff;
5952
5953 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005954 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
5956 if (oldattrs == NULL)
5957 return;
5958 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5959 coordOrigin, &dwDummy);
5960 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5961 coordOrigin, &dwDummy);
5962
5963 Sleep(15); /* wait for 15 msec */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005964 if (!USE_VTP)
5965 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 coordOrigin, &dwDummy);
5967 vim_free(oldattrs);
5968}
5969
5970
5971/*
5972 * Make the cursor visible or invisible
5973 */
5974 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005975cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005978# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981}
5982
5983
5984/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005985 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005986 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005988 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005990 char_u *pchBuf,
5991 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005993 COORD coord = g_coord;
5994 DWORD written;
5995 DWORD n, cchwritten, cells;
5996 static WCHAR *unicodebuf = NULL;
5997 static int unibuflen = 0;
5998 int length;
5999 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006001 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6002 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006003 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006004 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006005 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006006 unibuflen = length;
6007 }
6008 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6009 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006011 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006012
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006013 if (!USE_VTP)
6014 {
6015 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6016 coord, &written);
6017 // When writing fails or didn't write a single character, pretend one
6018 // character was written, otherwise we get stuck.
6019 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6020 coord, &cchwritten) == 0
6021 || cchwritten == 0 || cchwritten == (DWORD)-1)
6022 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006023 }
6024 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006025 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006026 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6027 NULL) == 0 || cchwritten == 0)
6028 cchwritten = 1;
6029 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006030
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006031 if (cchwritten == length)
6032 {
6033 written = cbToWrite;
6034 g_coord.X += (SHORT)cells;
6035 }
6036 else
6037 {
6038 char_u *p = pchBuf;
6039 for (n = 0; n < cchwritten; n++)
6040 MB_CPTR_ADV(p);
6041 written = p - pchBuf;
6042 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045 while (g_coord.X > g_srScrollRegion.Right)
6046 {
6047 g_coord.X -= (SHORT) Columns;
6048 if (g_coord.Y < g_srScrollRegion.Bottom)
6049 g_coord.Y++;
6050 }
6051
6052 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6053
6054 return written;
6055}
6056
6057
6058/*
6059 * mch_write(): write the output buffer to the screen, translating ESC
6060 * sequences into calls to console output routines.
6061 */
6062 void
6063mch_write(
6064 char_u *s,
6065 int len)
6066{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006067# ifdef VIMDLL
6068 if (gui.in_use)
6069 return;
6070# endif
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 s[len] = NUL;
6073
6074 if (!term_console)
6075 {
6076 write(1, s, (unsigned)len);
6077 return;
6078 }
6079
6080 /* translate ESC | sequences into faked bios calls */
6081 while (len--)
6082 {
6083 /* optimization: use one single write_chars for runs of text,
6084 * rather than once per character It ain't curses, but it helps. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006085 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 if (p_wd)
6088 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006089 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (prefix != 0)
6091 prefix = 1;
6092 }
6093
6094 if (prefix != 0)
6095 {
6096 DWORD nWritten;
6097
6098 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006099# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (fdDump)
6101 {
6102 fputc('>', fdDump);
6103 fwrite(s, sizeof(char_u), nWritten, fdDump);
6104 fputs("<\n", fdDump);
6105 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 len -= (nWritten - 1);
6108 s += nWritten;
6109 }
6110 else if (s[0] == '\n')
6111 {
6112 /* \n, newline: go to the beginning of the next line or scroll */
6113 if (g_coord.Y == g_srScrollRegion.Bottom)
6114 {
6115 scroll(1);
6116 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6117 }
6118 else
6119 {
6120 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6121 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006122# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 if (fdDump)
6124 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 s++;
6127 }
6128 else if (s[0] == '\r')
6129 {
6130 /* \r, carriage return: go to beginning of line */
6131 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006132# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (fdDump)
6134 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 s++;
6137 }
6138 else if (s[0] == '\b')
6139 {
6140 /* \b, backspace: move cursor one position left */
6141 if (g_coord.X > g_srScrollRegion.Left)
6142 g_coord.X--;
6143 else if (g_coord.Y > g_srScrollRegion.Top)
6144 {
6145 g_coord.X = g_srScrollRegion.Right;
6146 g_coord.Y--;
6147 }
6148 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006149# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (fdDump)
6151 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 s++;
6154 }
6155 else if (s[0] == '\a')
6156 {
6157 /* \a, bell */
6158 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006159# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (fdDump)
6161 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 s++;
6164 }
6165 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6166 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006167# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006168 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006169# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006170 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006171 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 switch (s[2])
6174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 case '0': case '1': case '2': case '3': case '4':
6176 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006177 p = s + 1;
6178 do
6179 {
6180 ++p;
6181 args[argc] = getdigits(&p);
6182 argc += (argc < 15) ? 1 : 0;
6183 if (p > s + len)
6184 break;
6185 } while (*p == ';');
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (p > s + len)
6188 break;
6189
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006190 arg1 = args[0];
6191 arg2 = args[1];
6192 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006194 if (argc == 1 && args[0] == 0)
6195 normvideo();
6196 else if (argc == 1)
6197 {
6198 if (USE_VTP)
6199 textcolor((WORD) arg1);
6200 else
6201 textattr((WORD) arg1);
6202 }
6203 else if (USE_VTP)
6204 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006206 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006208 gotoxy(arg2, arg1);
6209 }
6210 else if (argc == 2 && *p == 'r')
6211 {
6212 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6213 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006214 else if (argc == 2 && *p == 'R')
6215 {
6216 set_scroll_region_tb(arg1, arg2);
6217 }
6218 else if (argc == 2 && *p == 'V')
6219 {
6220 set_scroll_region_lr(arg1, arg2);
6221 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006222 else if (argc == 1 && *p == 'A')
6223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 gotoxy(g_coord.X + 1,
6225 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6226 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006227 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 textbackground((WORD) arg1);
6230 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006231 else if (argc == 1 && *p == 'C')
6232 {
6233 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6234 g_coord.Y + 1);
6235 }
6236 else if (argc == 1 && *p == 'f')
6237 {
6238 textcolor((WORD) arg1);
6239 }
6240 else if (argc == 1 && *p == 'H')
6241 {
6242 gotoxy(1, arg1);
6243 }
6244 else if (argc == 1 && *p == 'L')
6245 {
6246 insert_lines(arg1);
6247 }
6248 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 delete_lines(arg1);
6251 }
6252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006253 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 s = p + 1;
6255 break;
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 gotoxy(g_coord.X + 1,
6259 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6260 goto got3;
6261
6262 case 'B':
6263 visual_bell();
6264 goto got3;
6265
6266 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6268 g_coord.Y + 1);
6269 goto got3;
6270
6271 case 'E':
6272 termcap_mode_end();
6273 goto got3;
6274
6275 case 'F':
6276 standout();
6277 goto got3;
6278
6279 case 'f':
6280 standend();
6281 goto got3;
6282
6283 case 'H':
6284 gotoxy(1, 1);
6285 goto got3;
6286
6287 case 'j':
6288 clear_to_end_of_display();
6289 goto got3;
6290
6291 case 'J':
6292 clear_screen();
6293 goto got3;
6294
6295 case 'K':
6296 clear_to_end_of_line();
6297 goto got3;
6298
6299 case 'L':
6300 insert_lines(1);
6301 goto got3;
6302
6303 case 'M':
6304 delete_lines(1);
6305 goto got3;
6306
6307 case 'S':
6308 termcap_mode_start();
6309 goto got3;
6310
6311 case 'V':
6312 cursor_visible(TRUE);
6313 goto got3;
6314
6315 case 'v':
6316 cursor_visible(FALSE);
6317 goto got3;
6318
6319 got3:
6320 s += 3;
6321 len -= 2;
6322 }
6323
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006324# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (fdDump)
6326 {
6327 fputs("ESC | ", fdDump);
6328 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6329 fputc('\n', fdDump);
6330 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 else
6334 {
6335 /* Write a single character */
6336 DWORD nWritten;
6337
6338 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006339# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if (fdDump)
6341 {
6342 fputc('>', fdDump);
6343 fwrite(s, sizeof(char_u), nWritten, fdDump);
6344 fputs("<\n", fdDump);
6345 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348 len -= (nWritten - 1);
6349 s += nWritten;
6350 }
6351 }
6352
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006353# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (fdDump)
6355 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
Bram Moolenaar4f974752019-02-17 17:44:42 +01006359#endif /* FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361
6362/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006363 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 */
6365 void
6366mch_delay(
6367 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006368 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006370#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 Sleep((int)msec); /* never wait for input */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006372#else /* Console */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006373# ifdef VIMDLL
6374 if (gui.in_use)
6375 {
6376 Sleep((int)msec); /* never wait for input */
6377 return;
6378 }
6379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006381# ifdef FEAT_MZSCHEME
6382 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6383 {
6384 int towait = p_mzq;
6385
6386 /* if msec is large enough, wait by portions in p_mzq */
6387 while (msec > 0)
6388 {
6389 mzvim_check_threads();
6390 if (msec < towait)
6391 towait = msec;
6392 Sleep(towait);
6393 msec -= towait;
6394 }
6395 }
6396 else
6397# endif
6398 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006400 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401#endif
6402}
6403
6404
6405/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006406 * This version of remove is not scared by a readonly (backup) file.
6407 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 * Return 0 for success, -1 for failure.
6409 */
6410 int
6411mch_remove(char_u *name)
6412{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006413 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 int n;
6415
Bram Moolenaar203258c2016-01-17 22:15:16 +01006416 /*
6417 * On Windows, deleting a directory's symbolic link is done by
6418 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6419 */
6420 if (mch_isdir(name) && mch_is_symbolic_link(name))
6421 return mch_rmdir(name);
6422
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006423 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6424
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006425 wn = enc_to_utf16(name, NULL);
6426 if (wn == NULL)
6427 return -1;
6428
6429 n = DeleteFileW(wn) ? 0 : -1;
6430 vim_free(wn);
6431 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
6434
6435/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006436 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
6438 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006439mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006441#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6442# ifdef VIMDLL
6443 if (!gui.in_use)
6444# endif
6445 if (g_fCtrlCPressed || g_fCBrkPressed)
6446 {
6447 ctrl_break_was_pressed = g_fCBrkPressed;
6448 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6449 got_int = TRUE;
6450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451#endif
6452}
6453
Bram Moolenaaree273972016-01-02 21:11:51 +01006454/* physical RAM to leave for the OS */
6455#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006456
6457/*
6458 * How much main memory in KiB that can be used by VIM.
6459 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006460 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006461mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006462{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006463 MEMORYSTATUSEX ms;
6464
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006465 /* Need to use GlobalMemoryStatusEx() when there is more memory than
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006466 * what fits in 32 bits. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006467 ms.dwLength = sizeof(MEMORYSTATUSEX);
6468 GlobalMemoryStatusEx(&ms);
6469 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006470 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006471 /* Process address space fits in physical RAM, use all of it. */
6472 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006473 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006474 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006475 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006476 /* Catch old NT box or perverse hardware setup. */
6477 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006478 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006479 /* Use physical RAM less reserve for OS + data. */
6480 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006484 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6486 * file whose short file name is "FOO.BAR" (its long file name will
6487 * be correct: "foo.bar~"). Because a file can be accessed by
6488 * either its SFN or its LFN, "foo.bar" has effectively been
6489 * renamed to "foo.bar", which is not at all what was wanted. This
6490 * seems to happen only when renaming files with three-character
6491 * extensions by appending a suffix that does not include ".".
6492 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6493 *
6494 * There is another problem, which isn't really a bug but isn't right either:
6495 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6496 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6497 * service pack 6. Doesn't seem to happen on Windows 98.
6498 *
6499 * Like rename(), returns 0 upon success, non-zero upon failure.
6500 * Should probably set errno appropriately when errors occur.
6501 */
6502 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006503mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006505 WCHAR *p;
6506 int i;
6507 WCHAR szTempFile[_MAX_PATH + 1];
6508 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006511 // No need to play tricks unless the file name contains a "~" as the
6512 // seventh character.
6513 p = wold;
6514 for (i = 0; wold[i] != NUL; ++i)
6515 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6516 && wold[i + 1] != 0)
6517 p = wold + i + 1;
6518 if ((int)(wold + i - p) < 8 || p[6] != '~')
6519 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006521 // Get base path of new file name. Undocumented feature: If pszNewFile is
6522 // a directory, no error is returned and pszFilePart will be NULL.
6523 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006525 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006527 // Get (and create) a unique temporary file name in directory of new file
6528 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 return -2;
6530
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006531 // blow the temp file away
6532 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 return -3;
6534
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006535 // rename old file to the temp file
6536 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 return -4;
6538
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006539 // now create an empty file called pszOldFile; this prevents the operating
6540 // system using pszOldFile as an alias (SFN) if we're renaming within the
6541 // same directory. For example, we're editing a file called
6542 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6543 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6544 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6545 // cause all sorts of problems later in buf_write(). So, we create an
6546 // empty file called filena~1.txt and the system will have to find some
6547 // other SFN for filena~1.txt~, such as filena~2.txt
6548 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6550 return -5;
6551 if (!CloseHandle(hf))
6552 return -6;
6553
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006554 // rename the temp file to the new file
6555 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006557 // Renaming failed. Rename the file back to its old name, so that it
6558 // looks like nothing happened.
6559 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 return -7;
6561 }
6562
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 // Seems to be left around on Novell filesystems
6564 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006566 // finally, remove the empty old file
6567 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 return -8;
6569
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006570 return 0;
6571}
6572
6573
6574/*
6575 * Converts the filenames to UTF-16, then call mch_wrename().
6576 * Like rename(), returns 0 upon success, non-zero upon failure.
6577 */
6578 int
6579mch_rename(
6580 const char *pszOldFile,
6581 const char *pszNewFile)
6582{
6583 WCHAR *wold = NULL;
6584 WCHAR *wnew = NULL;
6585 int retval = -1;
6586
6587 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6588 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6589 if (wold != NULL && wnew != NULL)
6590 retval = mch_wrename(wold, wnew);
6591 vim_free(wold);
6592 vim_free(wnew);
6593 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596/*
6597 * Get the default shell for the current hardware platform
6598 */
6599 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006600default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006602 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605/*
6606 * mch_access() extends access() to do more detailed check on network drives.
6607 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6608 */
6609 int
6610mch_access(char *n, int p)
6611{
6612 HANDLE hFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 int retval = -1; /* default: fail */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006614 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006616 wn = enc_to_utf16((char_u *)n, NULL);
6617 if (wn == NULL)
6618 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006620 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 if (p & R_OK)
6625 {
6626 /* Read check is performed by seeing if we can do a find file on
6627 * the directory for any file. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006628 int i;
6629 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006631 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6632 TempNameW[i] = wn[i];
6633 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6634 TempNameW[i++] = '\\';
6635 TempNameW[i++] = '*';
6636 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006638 hFile = FindFirstFileW(TempNameW, &d);
6639 if (hFile == INVALID_HANDLE_VALUE)
6640 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644
6645 if (p & W_OK)
6646 {
6647 /* Trying to create a temporary file in the directory should catch
6648 * directories on read-only network shares. However, in
6649 * directories whose ACL allows writes but denies deletes will end
6650 * up keeping the temporary file :-(. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006651 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6652 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006653 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006654 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 }
6656 }
6657 else
6658 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006659 // Don't consider a file read-only if another process has opened it.
6660 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 /* Trying to open the file for the required access does ACL, read-only
6663 * network share, and file attribute checks. */
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006664 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6665 | ((p & R_OK) ? GENERIC_READ : 0);
6666
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006667 hFile = CreateFileW(wn, access_mode, share_mode,
6668 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (hFile == INVALID_HANDLE_VALUE)
6670 goto getout;
6671 CloseHandle(hFile);
6672 }
6673
6674 retval = 0; /* success */
6675getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 return retval;
6678}
6679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006681 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
6683 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006684mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 WCHAR *wn;
6687 int f;
6688
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006689 wn = enc_to_utf16((char_u *)name, NULL);
6690 if (wn == NULL)
6691 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006693 f = _wopen(wn, flags, mode);
6694 vim_free(wn);
6695 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697
6698/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006699 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 */
6701 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006702mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 WCHAR *wn, *wm;
6705 FILE *f = NULL;
6706
Bram Moolenaara12a1612019-01-24 16:39:02 +01006707#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006708 /* Work around an annoying assertion in the Microsoft debug CRT
6709 * when mode's text/binary setting doesn't match _get_fmode(). */
6710 char newMode = mode[strlen(mode) - 1];
6711 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006712
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006713 _get_fmode(&oldMode);
6714 if (newMode == 't')
6715 _set_fmode(_O_TEXT);
6716 else if (newMode == 'b')
6717 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006718#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006719 wn = enc_to_utf16((char_u *)name, NULL);
6720 wm = enc_to_utf16((char_u *)mode, NULL);
6721 if (wn != NULL && wm != NULL)
6722 f = _wfopen(wn, wm);
6723 vim_free(wn);
6724 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006725
Bram Moolenaara12a1612019-01-24 16:39:02 +01006726#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006727 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006728#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006729 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732/*
6733 * SUB STREAM (aka info stream) handling:
6734 *
6735 * NTFS can have sub streams for each file. Normal contents of file is
6736 * stored in the main stream, and extra contents (author information and
6737 * title and so on) can be stored in sub stream. After Windows 2000, user
6738 * can access and store those informations in sub streams via explorer's
6739 * property menuitem in right click menu. Those informations in sub streams
6740 * were lost when copying only the main stream. So we have to copy sub
6741 * streams.
6742 *
6743 * Incomplete explanation:
6744 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6745 * More useful info and an example:
6746 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6747 */
6748
6749/*
6750 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6751 * and write to stream "substream" of file "to".
6752 * Errors are ignored.
6753 */
6754 static void
6755copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6756{
6757 HANDLE hTo;
6758 WCHAR *to_name;
6759
6760 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6761 wcscpy(to_name, to);
6762 wcscat(to_name, substream);
6763
6764 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6765 FILE_ATTRIBUTE_NORMAL, NULL);
6766 if (hTo != INVALID_HANDLE_VALUE)
6767 {
6768 long done;
6769 DWORD todo;
6770 DWORD readcnt, written;
6771 char buf[4096];
6772
6773 /* Copy block of bytes at a time. Abort when something goes wrong. */
6774 for (done = 0; done < len; done += written)
6775 {
6776 /* (size_t) cast for Borland C 5.5 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006777 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6778 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6780 FALSE, FALSE, context)
6781 || readcnt != todo
6782 || !WriteFile(hTo, buf, todo, &written, NULL)
6783 || written != todo)
6784 break;
6785 }
6786 CloseHandle(hTo);
6787 }
6788
6789 free(to_name);
6790}
6791
6792/*
6793 * Copy info streams from file "from" to file "to".
6794 */
6795 static void
6796copy_infostreams(char_u *from, char_u *to)
6797{
6798 WCHAR *fromw;
6799 WCHAR *tow;
6800 HANDLE sh;
6801 WIN32_STREAM_ID sid;
6802 int headersize;
6803 WCHAR streamname[_MAX_PATH];
6804 DWORD readcount;
6805 void *context = NULL;
6806 DWORD lo, hi;
6807 int len;
6808
6809 /* Convert the file names to wide characters. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006810 fromw = enc_to_utf16(from, NULL);
6811 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (fromw != NULL && tow != NULL)
6813 {
6814 /* Open the file for reading. */
6815 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6816 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6817 if (sh != INVALID_HANDLE_VALUE)
6818 {
6819 /* Use BackupRead() to find the info streams. Repeat until we
6820 * have done them all.*/
6821 for (;;)
6822 {
6823 /* Get the header to find the length of the stream name. If
6824 * the "readcount" is zero we have done all info streams. */
6825 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006826 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6828 &readcount, FALSE, FALSE, &context)
6829 || readcount == 0)
6830 break;
6831
6832 /* We only deal with streams that have a name. The normal
6833 * file data appears to be without a name, even though docs
6834 * suggest it is called "::$DATA". */
6835 if (sid.dwStreamNameSize > 0)
6836 {
6837 /* Read the stream name. */
6838 if (!BackupRead(sh, (LPBYTE)streamname,
6839 sid.dwStreamNameSize,
6840 &readcount, FALSE, FALSE, &context))
6841 break;
6842
6843 /* Copy an info stream with a name ":anything:$DATA".
6844 * Skip "::$DATA", it has no stream name (examples suggest
6845 * it might be used for the normal file contents).
6846 * Note that BackupRead() counts bytes, but the name is in
6847 * wide characters. */
6848 len = readcount / sizeof(WCHAR);
6849 streamname[len] = 0;
6850 if (len > 7 && wcsicmp(streamname + len - 6,
6851 L":$DATA") == 0)
6852 {
6853 streamname[len - 6] = 0;
6854 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006855 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
6857 }
6858
6859 /* Advance to the next stream. We might try seeking too far,
6860 * but BackupSeek() doesn't skip over stream borders, thus
6861 * that's OK. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006862 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 &lo, &hi, &context);
6864 }
6865
6866 /* Clear the context. */
6867 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6868
6869 CloseHandle(sh);
6870 }
6871 }
6872 vim_free(fromw);
6873 vim_free(tow);
6874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876/*
6877 * Copy file attributes from file "from" to file "to".
6878 * For Windows NT and later we copy info streams.
6879 * Always returns zero, errors are ignored.
6880 */
6881 int
6882mch_copy_file_attribute(char_u *from, char_u *to)
6883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 /* File streams only work on Windows NT and later. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006885 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 return 0;
6887}
6888
6889#if defined(MYRESETSTKOFLW) || defined(PROTO)
6890/*
6891 * Recreate a destroyed stack guard page in win32.
6892 * Written by Benjamin Peterson.
6893 */
6894
6895/* These magic numbers are from the MS header files */
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006896# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898/*
6899 * This function does the same thing as _resetstkoflw(), which is only
6900 * available in DevStudio .net and later.
6901 * Returns 0 for failure, 1 for success.
6902 */
6903 int
6904myresetstkoflw(void)
6905{
6906 BYTE *pStackPtr;
6907 BYTE *pGuardPage;
6908 BYTE *pStackBase;
6909 BYTE *pLowestPossiblePage;
6910 MEMORY_BASIC_INFORMATION mbi;
6911 SYSTEM_INFO si;
6912 DWORD nPageSize;
6913 DWORD dummy;
6914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 /* We need to know the system page size. */
6916 GetSystemInfo(&si);
6917 nPageSize = si.dwPageSize;
6918
6919 /* ...and the current stack pointer */
6920 pStackPtr = (BYTE*)_alloca(1);
6921
6922 /* ...and the base of the stack. */
6923 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6924 return 0;
6925 pStackBase = (BYTE*)mbi.AllocationBase;
6926
6927 /* ...and the page thats min_stack_req pages away from stack base; this is
6928 * the lowest page we could use. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006929 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006932 /* We want the first committed page in the stack Start at the stack
6933 * base and move forward through memory until we find a committed block.
6934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 BYTE *pBlock = pStackBase;
6936
Bram Moolenaara466c992005-07-09 21:03:22 +00006937 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
6939 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6940 return 0;
6941
6942 pBlock += mbi.RegionSize;
6943
6944 if (mbi.State & MEM_COMMIT)
6945 break;
6946 }
6947
6948 /* mbi now describes the first committed block in the stack. */
6949 if (mbi.Protect & PAGE_GUARD)
6950 return 1;
6951
6952 /* decide where the guard page should start */
6953 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6954 pGuardPage = pLowestPossiblePage;
6955 else
6956 pGuardPage = (BYTE*)mbi.BaseAddress;
6957
6958 /* allocate the guard page */
6959 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6960 return 0;
6961
6962 /* apply the guard attribute to the page */
6963 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6964 &dummy))
6965 return 0;
6966 }
6967
6968 return 1;
6969}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006973/*
6974 * The command line arguments in UCS2
6975 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006976static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006977static LPWSTR *ArglistW = NULL;
6978static int global_argc = 0;
6979static char **global_argv;
6980
6981static int used_file_argc = 0; /* last argument in global_argv[] used
6982 for the argument list. */
6983static int *used_file_indexes = NULL; /* indexes in global_argv[] for
6984 command line arguments added to
6985 the argument list */
6986static int used_file_count = 0; /* nr of entries in used_file_indexes */
6987static int used_file_literal = FALSE; /* take file names literally */
6988static int used_file_full_path = FALSE; /* file name was full path */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006989static int used_file_diff_mode = FALSE; /* file name was with diff mode */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006990static int used_alist_count = 0;
6991
6992
6993/*
6994 * Get the command line arguments. Unicode version.
6995 * Returns argc. Zero when something fails.
6996 */
6997 int
6998get_cmd_argsW(char ***argvp)
6999{
7000 char **argv = NULL;
7001 int argc = 0;
7002 int i;
7003
Bram Moolenaar14993322014-09-09 12:25:33 +02007004 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007005 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7006 if (ArglistW != NULL)
7007 {
7008 argv = malloc((nArgsW + 1) * sizeof(char *));
7009 if (argv != NULL)
7010 {
7011 argc = nArgsW;
7012 argv[argc] = NULL;
7013 for (i = 0; i < argc; ++i)
7014 {
7015 int len;
7016
7017 /* Convert each Unicode argument to the current codepage. */
7018 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007019 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007020 (LPSTR *)&argv[i], &len, 0, 0);
7021 if (argv[i] == NULL)
7022 {
7023 /* Out of memory, clear everything. */
7024 while (i > 0)
7025 free(argv[--i]);
7026 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007027 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007028 argc = 0;
7029 }
7030 }
7031 }
7032 }
7033
7034 global_argc = argc;
7035 global_argv = argv;
7036 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007037 {
7038 if (used_file_indexes != NULL)
7039 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007040 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007041 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007042
7043 if (argvp != NULL)
7044 *argvp = argv;
7045 return argc;
7046}
7047
7048 void
7049free_cmd_argsW(void)
7050{
7051 if (ArglistW != NULL)
7052 {
7053 GlobalFree(ArglistW);
7054 ArglistW = NULL;
7055 }
7056}
7057
7058/*
7059 * Remember "name" is an argument that was added to the argument list.
7060 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7061 * is called.
7062 */
7063 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007064used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007065{
7066 int i;
7067
7068 if (used_file_indexes == NULL)
7069 return;
7070 for (i = used_file_argc + 1; i < global_argc; ++i)
7071 if (STRCMP(global_argv[i], name) == 0)
7072 {
7073 used_file_argc = i;
7074 used_file_indexes[used_file_count++] = i;
7075 break;
7076 }
7077 used_file_literal = literal;
7078 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007079 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007080}
7081
7082/*
7083 * Remember the length of the argument list as it was. If it changes then we
7084 * leave it alone when 'encoding' is set.
7085 */
7086 void
7087set_alist_count(void)
7088{
7089 used_alist_count = GARGCOUNT;
7090}
7091
7092/*
7093 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7094 * has been changed while starting up. Use the UCS-2 command line arguments
7095 * and convert them to 'encoding'.
7096 */
7097 void
7098fix_arg_enc(void)
7099{
7100 int i;
7101 int idx;
7102 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007103 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007104
7105 /* Safety checks:
7106 * - if argument count differs between the wide and non-wide argument
7107 * list, something must be wrong.
7108 * - the file name arguments must have been located.
7109 * - the length of the argument list wasn't changed by the user.
7110 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007111 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007112 || ArglistW == NULL
7113 || used_file_indexes == NULL
7114 || used_file_count == 0
7115 || used_alist_count != GARGCOUNT)
7116 return;
7117
Bram Moolenaar86b68352004-12-27 21:59:20 +00007118 /* Remember the buffer numbers for the arguments. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007119 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007120 if (fnum_list == NULL)
7121 return; /* out of memory */
7122 for (i = 0; i < GARGCOUNT; ++i)
7123 fnum_list[i] = GARGLIST[i].ae_fnum;
7124
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007125 /* Clear the argument list. Make room for the new arguments. */
7126 alist_clear(&global_alist);
7127 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007128 return; /* out of memory */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007129
7130 for (i = 0; i < used_file_count; ++i)
7131 {
7132 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007133 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007134 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007135 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007136 int literal = used_file_literal;
7137
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007138#ifdef FEAT_DIFF
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007139 /* When using diff mode may need to concatenate file name to
7140 * directory name. Just like it's done in main(). */
7141 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7142 && !mch_isdir(alist_name(&GARGLIST[0])))
7143 {
7144 char_u *r;
7145
7146 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7147 if (r != NULL)
7148 {
7149 vim_free(str);
7150 str = r;
7151 }
7152 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007153#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00007154 /* Re-use the old buffer by renaming it. When not using literal
7155 * names it's done by alist_expand() below. */
7156 if (used_file_literal)
7157 buf_set_name(fnum_list[i], str);
7158
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007159 /* Check backtick literal. backtick literal is already expanded in
7160 * main.c, so this part add str as literal. */
7161 if (literal == FALSE)
7162 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007163 size_t len = STRLEN(str);
7164
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007165 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7166 literal = TRUE;
7167 }
7168 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007169 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007170 }
7171
7172 if (!used_file_literal)
7173 {
7174 /* Now expand wildcards in the arguments. */
7175 /* Temporarily add '(' and ')' to 'isfname'. These are valid
7176 * filename characters but are excluded from 'isfname' to make
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007177 * "gf" work on a file name in parenthesis (e.g.: see vim.h).
7178 * Also, unset wildignore to not be influenced by this option.
7179 * The arguments specified in command-line should be kept even if
7180 * encoding options were changed. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007181 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007182 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007183 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007184 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007185 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007186 }
7187
7188 /* If wildcard expansion failed, we are editing the first file of the
7189 * arglist and there is no file name: Edit the first argument now. */
7190 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7191 {
7192 do_cmdline_cmd((char_u *)":rewind");
7193 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007194 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007195 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007196
7197 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007198}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007199
7200 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007201mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007202{
7203 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007204 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007205
Bram Moolenaar964b3742019-05-24 18:54:09 +02007206 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007207 if (envbuf == NULL)
7208 return -1;
7209
7210 sprintf((char *)envbuf, "%s=%s", var, value);
7211
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007212 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007213
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007214 vim_free(envbuf);
7215 if (p == NULL)
7216 return -1;
7217 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007218#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007219 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007220#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007221 // Unlike Un*x systems, we can free the string for _wputenv().
7222 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007223
7224 return 0;
7225}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007226
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007227/*
7228 * Support for 256 colors and 24-bit colors was added in Windows 10
7229 * version 1703 (Creators update).
7230 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007231#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7232
7233/*
7234 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007235 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007236 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007237#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007238
7239/*
7240 * ConPTY differences between versions, need different logic.
7241 * version 1903 (May 2019 update).
7242 */
7243#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7244
7245/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007246 * version 1909 (November 2019 update).
7247 */
7248#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7249
7250/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007251 * Confirm until this version. Also the logic changes.
7252 * insider preview.
7253 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007254#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007255
7256/*
7257 * Not stable now.
7258 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007259#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007260
7261 static void
7262vtp_flag_init(void)
7263{
7264 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007265#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007266 DWORD mode;
7267 HANDLE out;
7268
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007269# ifdef VIMDLL
7270 if (!gui.in_use)
7271# endif
7272 {
7273 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007274
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007275 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7276 GetConsoleMode(out, &mode);
7277 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7278 if (SetConsoleMode(out, mode) == 0)
7279 vtp_working = 0;
7280 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007281#endif
7282
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007283 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007284 conpty_working = 1;
7285 if (ver >= CONPTY_STABLE_BUILD)
7286 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007287
Bram Moolenaar57da6982019-09-13 22:30:11 +02007288 if (ver <= CONPTY_INSIDER_BUILD)
7289 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007290 if (ver <= CONPTY_1909_BUILD)
7291 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007292 if (ver <= CONPTY_1903_BUILD)
7293 conpty_type = 2;
7294 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7295 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007296}
7297
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007298#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007299
7300 static void
7301vtp_init(void)
7302{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007303 HMODULE hKerneldll;
7304 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007305# ifdef FEAT_TERMGUICOLORS
7306 COLORREF fg, bg;
7307# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007308
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007309 /* Use functions supported from Vista */
7310 hKerneldll = GetModuleHandle("kernel32.dll");
7311 if (hKerneldll != NULL)
7312 {
7313 pGetConsoleScreenBufferInfoEx =
7314 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7315 hKerneldll, "GetConsoleScreenBufferInfoEx");
7316 pSetConsoleScreenBufferInfoEx =
7317 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7318 hKerneldll, "SetConsoleScreenBufferInfoEx");
7319 if (pGetConsoleScreenBufferInfoEx != NULL
7320 && pSetConsoleScreenBufferInfoEx != NULL)
7321 has_csbiex = TRUE;
7322 }
7323
7324 csbi.cbSize = sizeof(csbi);
7325 if (has_csbiex)
7326 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007327 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7328 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
7329
7330# ifdef FEAT_TERMGUICOLORS
7331 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7332 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7333 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7334 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7335 default_console_color_bg = bg;
7336 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007337# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007338
7339 set_console_color_rgb();
7340}
7341
7342 static void
7343vtp_exit(void)
7344{
7345 reset_console_color_rgb();
7346}
7347
7348 static int
7349vtp_printf(
7350 char *format,
7351 ...)
7352{
7353 char_u buf[100];
7354 va_list list;
7355 DWORD result;
7356
7357 va_start(list, format);
7358 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7359 va_end(list);
7360 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7361 return (int)result;
7362}
7363
7364 static void
7365vtp_sgr_bulk(
7366 int arg)
7367{
7368 int args[1];
7369
7370 args[0] = arg;
7371 vtp_sgr_bulks(1, args);
7372}
7373
7374 static void
7375vtp_sgr_bulks(
7376 int argc,
7377 int *args
7378)
7379{
7380 /* 2('\033[') + 4('255.') * 16 + NUL */
7381 char_u buf[2 + (4 * 16) + 1];
7382 char_u *p;
7383 int i;
7384
7385 p = buf;
7386 *p++ = '\033';
7387 *p++ = '[';
7388
7389 for (i = 0; i < argc; ++i)
7390 {
7391 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7392 *p++ = ';';
7393 }
7394 p--;
7395 *p++ = 'm';
7396 *p = NUL;
7397 vtp_printf((char *)buf);
7398}
7399
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007400# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007401 static int
7402ctermtoxterm(
7403 int cterm)
7404{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007405 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007406
7407 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7408 return (((int)r << 16) | ((int)g << 8) | (int)b);
7409}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007410# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007411
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007412 static void
7413set_console_color_rgb(void)
7414{
7415# ifdef FEAT_TERMGUICOLORS
7416 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7417 int id;
7418 guicolor_T fg = INVALCOLOR;
7419 guicolor_T bg = INVALCOLOR;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007420 int ctermfg;
7421 int ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007422
7423 if (!USE_VTP)
7424 return;
7425
7426 id = syn_name2id((char_u *)"Normal");
Bram Moolenaard5a58862019-03-07 06:40:27 +01007427 if (id > 0 && p_tgc)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007428 syn_id2colors(id, &fg, &bg);
7429 if (fg == INVALCOLOR)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007430 {
7431 ctermfg = -1;
7432 if (id > 0)
7433 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007434 fg = ctermfg != -1 ? ctermtoxterm(ctermfg) : default_console_color_fg;
7435 cterm_normal_fg_gui_color = fg;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007436 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007437 if (bg == INVALCOLOR)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007438 {
7439 ctermbg = -1;
7440 if (id > 0)
7441 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007442 bg = ctermbg != -1 ? ctermtoxterm(ctermbg) : default_console_color_bg;
7443 cterm_normal_bg_gui_color = bg;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007444 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007445 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7446 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7447
7448 csbi.cbSize = sizeof(csbi);
7449 if (has_csbiex)
7450 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7451
7452 csbi.cbSize = sizeof(csbi);
7453 csbi.srWindow.Right += 1;
7454 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007455 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7456 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007457 if (has_csbiex)
7458 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7459# endif
7460}
7461
7462 static void
7463reset_console_color_rgb(void)
7464{
7465# ifdef FEAT_TERMGUICOLORS
7466 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7467
7468 csbi.cbSize = sizeof(csbi);
7469 if (has_csbiex)
7470 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7471
7472 csbi.cbSize = sizeof(csbi);
7473 csbi.srWindow.Right += 1;
7474 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007475 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7476 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007477 if (has_csbiex)
7478 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7479# endif
7480}
7481
7482 void
7483control_console_color_rgb(void)
7484{
7485 if (USE_VTP)
7486 set_console_color_rgb();
7487 else
7488 reset_console_color_rgb();
7489}
7490
7491 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007492use_vtp(void)
7493{
7494 return USE_VTP;
7495}
7496
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007497 int
7498is_term_win32(void)
7499{
7500 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7501}
7502
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007503 int
7504has_vtp_working(void)
7505{
7506 return vtp_working;
7507}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007508
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007509#endif
7510
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007511 int
7512has_conpty_working(void)
7513{
7514 return conpty_working;
7515}
7516
7517 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007518get_conpty_type(void)
7519{
7520 return conpty_type;
7521}
7522
7523 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007524is_conpty_stable(void)
7525{
7526 return conpty_stable;
7527}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007528
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007529#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007530 void
7531resize_console_buf(void)
7532{
7533 CONSOLE_SCREEN_BUFFER_INFO csbi;
7534 COORD coord;
7535 SMALL_RECT newsize;
7536
7537 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7538 {
7539 coord.X = SRWIDTH(csbi.srWindow);
7540 coord.Y = SRHEIGHT(csbi.srWindow);
7541 SetConsoleScreenBufferSize(g_hConOut, coord);
7542
7543 newsize.Left = 0;
7544 newsize.Top = 0;
7545 newsize.Right = coord.X - 1;
7546 newsize.Bottom = coord.Y - 1;
7547 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7548
7549 SetConsoleScreenBufferSize(g_hConOut, coord);
7550 }
7551}
7552#endif