blob: e4d8135ca37c5078e0f12407e5b6da1f35382cbd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * os_win32.c
11 *
12 * Used for both the console version and the Win32 GUI. A lot of code is for
Bram Moolenaar4f974752019-02-17 17:44:42 +010013 * the console version only, so there is a lot of "#ifndef FEAT_GUI_MSWIN".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * Win32 (Windows NT and Windows 95) system-dependent routines.
16 * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
17 * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
18 *
19 * George V. Reilly <george@reilly.org> wrote most of this.
20 * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0.
21 */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
Bram Moolenaar325b7a22004-07-05 15:58:32 +000025#ifdef FEAT_MZSCHEME
26# include "if_mzsch.h"
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#include <signal.h>
31#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010032
Bram Moolenaar0f873732019-12-05 20:28:46 +010033// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# include <process.h>
36#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38#undef chdir
39#ifdef __GNUC__
40# ifndef __MINGW32__
41# include <dirent.h>
42# endif
43#else
44# include <direct.h>
45#endif
46
Bram Moolenaar82881492012-11-20 16:53:39 +010047#ifndef PROTO
Bram Moolenaar4f974752019-02-17 17:44:42 +010048# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010049# include <shellapi.h>
50# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
52
Bram Moolenaarfb630902016-10-29 14:55:00 +020053#ifdef FEAT_JOB_CHANNEL
54# include <tlhelp32.h>
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef __MINGW32__
58# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
59# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
60# endif
61# ifndef RIGHTMOST_BUTTON_PRESSED
62# define RIGHTMOST_BUTTON_PRESSED 0x0002
63# endif
64# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
65# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
66# endif
67# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
68# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
69# endif
70# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
71# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
72# endif
73
74/*
75 * EventFlags
76 */
77# ifndef MOUSE_MOVED
78# define MOUSE_MOVED 0x0001
79# endif
80# ifndef DOUBLE_CLICK
81# define DOUBLE_CLICK 0x0002
82# endif
83#endif
84
Bram Moolenaar0f873732019-12-05 20:28:46 +010085// Record all output and all keyboard & mouse input
86// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88#ifdef MCH_WRITE_DUMP
89FILE* fdDump = NULL;
90#endif
91
92/*
93 * When generating prototypes for Win32 on Unix, these lines make the syntax
94 * errors disappear. They do not need to be correct.
95 */
96#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010097# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000099typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100typedef int ACCESS_MASK;
101typedef int BOOL;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100107typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HINSTANCE;
112typedef int HWND;
113typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200114typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115typedef int KEY_EVENT_RECORD;
116typedef int LOGFONT;
117typedef int LPBOOL;
118typedef int LPCTSTR;
119typedef int LPDWORD;
120typedef int LPSTR;
121typedef int LPTSTR;
122typedef int LPVOID;
123typedef int MOUSE_EVENT_RECORD;
124typedef int PACL;
125typedef int PDWORD;
126typedef int PHANDLE;
127typedef int PRINTDLG;
128typedef int PSECURITY_DESCRIPTOR;
129typedef int PSID;
130typedef int SECURITY_INFORMATION;
131typedef int SHORT;
132typedef int SMALL_RECT;
133typedef int TEXTMETRIC;
134typedef int TOKEN_INFORMATION_CLASS;
135typedef int TRUSTEE;
136typedef int WORD;
137typedef int WCHAR;
138typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100139typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200140typedef int SE_OBJECT_TYPE;
141typedef int PSNSECINFO;
142typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100143typedef int STARTUPINFO;
144typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200145typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
148
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200149#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100150// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
152static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
153
Bram Moolenaar0f873732019-12-05 20:28:46 +0100154// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar0f873732019-12-05 20:28:46 +0100158// The attribute of the screen when the editor was started
159static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static WORD g_attrCurrent;
161
Bram Moolenaar0f873732019-12-05 20:28:46 +0100162static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
163static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
164static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static void scroll(unsigned cLines);
167static void set_scroll_region(unsigned left, unsigned top,
168 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100169static void set_scroll_region_tb(unsigned top, unsigned bottom);
170static void set_scroll_region_lr(unsigned left, unsigned right);
171static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void delete_lines(unsigned cLines);
173static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int s_cursor_visible = TRUE;
176static int did_create_conin = FALSE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200177#endif
178#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static int s_dont_use_vimrun = TRUE;
180static int need_vimrun_warning = FALSE;
181static char *vimrun_path = "vimrun ";
182#endif
183
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200184static int win32_getattrs(char_u *name);
185static int win32_setattrs(char_u *name, int attrs);
186static int win32_set_archive(char_u *name);
187
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100188static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200189static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100190static int conpty_stable = 0;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +0200191static int conpty_fix_type = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100192static void vtp_flag_init();
193
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200194#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100195static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100196static void vtp_init();
197static void vtp_exit();
198static int vtp_printf(char *format, ...);
199static void vtp_sgr_bulk(int arg);
200static void vtp_sgr_bulks(int argc, int *argv);
201
202static guicolor_T save_console_bg_rgb;
203static guicolor_T save_console_fg_rgb;
Bram Moolenaardf543822020-01-30 11:53:59 +0100204static guicolor_T store_console_bg_rgb;
205static guicolor_T store_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100206
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200207static int g_color_index_bg = 0;
208static int g_color_index_fg = 7;
209
210# ifdef FEAT_TERMGUICOLORS
211static int default_console_color_bg = 0x000000; // black
212static int default_console_color_fg = 0xc0c0c0; // white
213# endif
214
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100215# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200216# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100217# else
218# define USE_VTP 0
219# endif
220
221static void set_console_color_rgb(void);
222static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100223static void restore_console_color_rgb(void);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100224#endif
225
Bram Moolenaar0f873732019-12-05 20:28:46 +0100226// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100227#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
228# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
229#endif
230
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200231#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100232static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
234
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200235static char_u *exe_path = NULL;
236
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100237static BOOL win8_or_later = FALSE;
238
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200239#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100240// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100241typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
242{
243 ULONG cbSize;
244 COORD dwSize;
245 COORD dwCursorPosition;
246 WORD wAttributes;
247 SMALL_RECT srWindow;
248 COORD dwMaximumWindowSize;
249 WORD wPopupAttributes;
250 BOOL bFullscreenSupported;
251 COLORREF ColorTable[16];
252} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
253typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
254static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
255typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
256static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
257static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100258#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100259
260/*
261 * Get version number including build number
262 */
263typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100264#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100265 (((major) << 24) | ((minor) << 16) | (build))
266
267 static DWORD
268get_build_number(void)
269{
270 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
271 HMODULE hNtdll;
272 PfnRtlGetVersion pRtlGetVersion;
273 DWORD ver = MAKE_VER(0, 0, 0);
274
275 hNtdll = GetModuleHandle("ntdll.dll");
276 if (hNtdll != NULL)
277 {
278 pRtlGetVersion =
279 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
280 pRtlGetVersion(&osver);
281 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
282 min(osver.dwMinorVersion, 255),
283 min(osver.dwBuildNumber, 32767));
284 }
285 return ver;
286}
287
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200288#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100289/*
290 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100291 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100292 */
293 static BOOL
294read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100295 HANDLE hInput,
296 INPUT_RECORD *lpBuffer,
297 DWORD nLength,
298 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100299{
300 enum
301 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100302 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100303 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100304 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100305 static DWORD s_dwIndex = 0;
306 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100307 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100308 int head;
309 int tail;
310 int i;
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200311 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100312
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200313 if (nLength == -2)
314 return (s_dwMax > 0) ? TRUE : FALSE;
315
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100316 if (!win8_or_later)
317 {
318 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200319 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
320 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100321 }
322
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323 if (s_dwMax == 0)
324 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100325 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200326 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
327 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100328 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100329 s_dwIndex = 0;
330 s_dwMax = dwEvents;
331 if (dwEvents == 0)
332 {
333 *lpEvents = 0;
334 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100335 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100336
337 if (s_dwMax > 1)
338 {
339 head = 0;
340 tail = s_dwMax - 1;
341 while (head != tail)
342 {
343 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
344 && s_irCache[head + 1].EventType
345 == WINDOW_BUFFER_SIZE_EVENT)
346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100347 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100348 for (i = head; i < tail; ++i)
349 s_irCache[i] = s_irCache[i + 1];
350 --tail;
351 continue;
352 }
353 head++;
354 }
355 s_dwMax = tail + 1;
356 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100357 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100358
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200359 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
360 {
361 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
362 {
363 s_irPseudo = s_irCache[s_dwIndex];
364 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
365 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
366 *lpBuffer = s_irPseudo;
367 *lpEvents = 1;
368 return TRUE;
369 }
370 }
371
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100372 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200373 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100374 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100375 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100376 return TRUE;
377}
378
379/*
380 * Version of PeekConsoleInput() that works with IME.
381 */
382 static BOOL
383peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100384 HANDLE hInput,
385 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200386 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100387 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100388{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100389 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100390}
391
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100392# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200393 static DWORD
394msg_wait_for_multiple_objects(
395 DWORD nCount,
396 LPHANDLE pHandles,
397 BOOL fWaitAll,
398 DWORD dwMilliseconds,
399 DWORD dwWakeMask)
400{
401 if (read_console_input(NULL, NULL, -2, NULL))
402 return WAIT_OBJECT_0;
403 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
404 dwMilliseconds, dwWakeMask);
405}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100406# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200407
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100408# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200409 static DWORD
410wait_for_single_object(
411 HANDLE hHandle,
412 DWORD dwMilliseconds)
413{
414 if (read_console_input(NULL, NULL, -2, NULL))
415 return WAIT_OBJECT_0;
416 return WaitForSingleObject(hHandle, dwMilliseconds);
417}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100418# endif
419#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 static void
422get_exe_name(void)
423{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100424 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
425 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100426#define MAX_ENV_PATH_LEN 8192
427 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200428 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
430 if (exe_name == NULL)
431 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 if (*temp != NUL)
435 exe_name = FullName_save((char_u *)temp, FALSE);
436 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000437
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200438 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000439 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200440 exe_path = vim_strnsave(exe_name,
441 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200442 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000443 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100444 // Append our starting directory to $PATH, so that when doing
445 // "!xxd" it's found in our starting directory. Needed because
446 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200447 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100448 if (p == NULL
449 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200450 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100451 if (p == NULL || *p == NUL)
452 temp[0] = NUL;
453 else
454 {
455 STRCPY(temp, p);
456 STRCAT(temp, ";");
457 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200458 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100459 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200460 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000461 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463}
464
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200465/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100466 * Unescape characters in "p" that appear in "escaped".
467 */
468 static void
469unescape_shellxquote(char_u *p, char_u *escaped)
470{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100471 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100472 int n;
473
474 while (*p != NUL)
475 {
476 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
477 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100478 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100479 p += n;
480 l -= n;
481 }
482}
483
484/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200485 * Load library "name".
486 */
487 HINSTANCE
488vimLoadLib(char *name)
489{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200490 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200491
Bram Moolenaar0f873732019-12-05 20:28:46 +0100492 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
493 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200494 if (exe_path == NULL)
495 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200496 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200497 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200498 WCHAR old_dirw[MAXPATHL];
499
500 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
501 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100502 // Change directory to where the executable is, both to make
503 // sure we find a .dll there and to avoid looking for a .dll
504 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100505 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200506 dll = LoadLibrary(name);
507 SetCurrentDirectoryW(old_dirw);
508 return dll;
509 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200510 }
511 return dll;
512}
513
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200514#if defined(VIMDLL) || defined(PROTO)
515/*
516 * Check if the current executable file is for the GUI subsystem.
517 */
518 int
519mch_is_gui_executable(void)
520{
521 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
522 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
523 PIMAGE_NT_HEADERS pPE;
524
525 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
526 return FALSE;
527 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
528 if (pPE->Signature != IMAGE_NT_SIGNATURE)
529 return FALSE;
530 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
531 return TRUE;
532 return FALSE;
533}
534#endif
535
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100536#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
537/*
538 * Get related information about 'funcname' which is imported by 'hInst'.
539 * If 'info' is 0, return the function address.
540 * If 'info' is 1, return the module name which the function is imported from.
541 */
542 static void *
543get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
544{
545 PBYTE pImage = (PBYTE)hInst;
546 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
547 PIMAGE_NT_HEADERS pPE;
548 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100549 PIMAGE_THUNK_DATA pIAT; // Import Address Table
550 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100551 PIMAGE_IMPORT_BY_NAME pImpName;
552
553 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
554 return NULL;
555 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
556 if (pPE->Signature != IMAGE_NT_SIGNATURE)
557 return NULL;
558 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
559 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
560 .VirtualAddress);
561 for (; pImpDesc->FirstThunk; ++pImpDesc)
562 {
563 if (!pImpDesc->OriginalFirstThunk)
564 continue;
565 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
566 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
567 for (; pIAT->u1.Function; ++pIAT, ++pINT)
568 {
569 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
570 continue;
571 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
572 + (UINT_PTR)(pINT->u1.AddressOfData));
573 if (strcmp((char *)pImpName->Name, funcname) == 0)
574 {
575 switch (info)
576 {
577 case 0:
578 return (void *)pIAT->u1.Function;
579 case 1:
580 return (void *)(pImage + pImpDesc->Name);
581 default:
582 return NULL;
583 }
584 }
585 }
586 }
587 return NULL;
588}
589
590/*
591 * Get the module handle which 'funcname' in 'hInst' is imported from.
592 */
593 HINSTANCE
594find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
595{
596 char *modulename;
597
598 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
599 if (modulename != NULL)
600 return GetModuleHandleA(modulename);
601 return NULL;
602}
603
604/*
605 * Get the address of 'funcname' which is imported by 'hInst' DLL.
606 */
607 void *
608get_dll_import_func(HINSTANCE hInst, const char *funcname)
609{
610 return get_imported_func_info(hInst, funcname, 0);
611}
612#endif
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
615# ifndef GETTEXT_DLL
616# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200617# define GETTEXT_DLL_ALT1 "libintl-8.dll"
618# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100620// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000621static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200622static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000623static char *null_libintl_textdomain(const char *);
624static char *null_libintl_bindtextdomain(const char *, const char *);
625static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100626static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200628static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000629char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200630char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
631 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000632char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
633char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000635char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
636 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100637int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100640dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 int i;
643 static struct
644 {
645 char *name;
646 FARPROC *ptr;
647 } libintl_entry[] =
648 {
649 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200650 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
652 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
653 {NULL, NULL}
654 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100655 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar7554c542018-10-06 15:03:15 +0200657 // No need to initialize twice.
658 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200660 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100661 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100662# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100663 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200664 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100665# endif
666# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200667 if (!hLibintlDLL)
668 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100669# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100670 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200672 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200674 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200676 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200678 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 for (i = 0; libintl_entry[i].name != NULL
681 && libintl_entry[i].ptr != NULL; ++i)
682 {
683 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
684 libintl_entry[i].name)) == NULL)
685 {
686 dyn_libintl_end();
687 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000688 {
689 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100690 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000691 verbose_leave();
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return 0;
694 }
695 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000696
Bram Moolenaar0f873732019-12-05 20:28:46 +0100697 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000698 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000699 "bind_textdomain_codeset");
700 if (dyn_libintl_bind_textdomain_codeset == NULL)
701 dyn_libintl_bind_textdomain_codeset =
702 null_libintl_bind_textdomain_codeset;
703
Bram Moolenaar0f873732019-12-05 20:28:46 +0100704 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100705 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
706 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100708 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
709 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return 1;
712}
713
714 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100715dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 if (hLibintlDLL)
718 FreeLibrary(hLibintlDLL);
719 hLibintlDLL = NULL;
720 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200721 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 dyn_libintl_textdomain = null_libintl_textdomain;
723 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000724 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100725 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726}
727
728 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000729null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 return (char*)msgid;
732}
733
734 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200735null_libintl_ngettext(
736 const char *msgid,
737 const char *msgid_plural,
738 unsigned long n)
739{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200740 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200741}
742
Bram Moolenaaree695f72016-08-03 22:08:45 +0200743 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100744null_libintl_bindtextdomain(
745 const char *domainname UNUSED,
746 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
748 return NULL;
749}
750
751 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100752null_libintl_bind_textdomain_codeset(
753 const char *domainname UNUSED,
754 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000755{
756 return NULL;
757}
758
759 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100760null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762 return NULL;
763}
764
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200765 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100766null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100767{
768 return 0;
769}
770
Bram Moolenaar0f873732019-12-05 20:28:46 +0100771#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar0f873732019-12-05 20:28:46 +0100773// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775#ifndef VER_PLATFORM_WIN32_WINDOWS
776# define VER_PLATFORM_WIN32_WINDOWS 1
777#endif
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100780# ifndef PROTO
781# include <aclapi.h>
782# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200783# ifndef PROTECTED_DACL_SECURITY_INFORMATION
784# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#endif
787
Bram Moolenaar27515922013-06-29 15:36:26 +0200788#ifdef HAVE_ACL
789/*
790 * Enables or disables the specified privilege.
791 */
792 static BOOL
793win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
794{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100795 BOOL bResult;
796 LUID luid;
797 HANDLE hToken;
798 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200799
800 if (!OpenProcessToken(GetCurrentProcess(),
801 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
802 return FALSE;
803
804 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
805 {
806 CloseHandle(hToken);
807 return FALSE;
808 }
809
Bram Moolenaar45500912014-07-09 20:51:07 +0200810 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200811 tokenPrivileges.Privileges[0].Luid = luid;
812 tokenPrivileges.Privileges[0].Attributes = bEnable ?
813 SE_PRIVILEGE_ENABLED : 0;
814
815 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
816 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
817
818 CloseHandle(hToken);
819
820 return bResult && GetLastError() == ERROR_SUCCESS;
821}
822#endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100825 * Set "win8_or_later" and fill in "windowsVersion".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
827 void
828PlatformId(void)
829{
830 static int done = FALSE;
831
832 if (!done)
833 {
834 OSVERSIONINFO ovi;
835
836 ovi.dwOSVersionInfoSize = sizeof(ovi);
837 GetVersionEx(&ovi);
838
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100839 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
840 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100842 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
843 || ovi.dwMajorVersion > 6)
844 win8_or_later = TRUE;
845
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100847 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200848 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#endif
850 done = TRUE;
851 }
852}
853
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200854#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100856# define SHIFT (SHIFT_PRESSED)
857# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
858# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
859# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861
Bram Moolenaar0f873732019-12-05 20:28:46 +0100862// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
863// We map function keys to their ANSI terminal equivalents, as produced
864// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
865// ANSI key with a value >= '\300' is nonstandard, but provided anyway
866// so that the user can have access to all SHIFT-, CTRL-, and ALT-
867// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000869static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 WORD wVirtKey;
872 BOOL fAnsiKey;
873 int chAlone;
874 int chShift;
875 int chCtrl;
876 int chAlt;
877} VirtKeyMap[] =
878{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200879// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
881
882 { VK_F1, TRUE, ';', 'T', '^', 'h', },
883 { VK_F2, TRUE, '<', 'U', '_', 'i', },
884 { VK_F3, TRUE, '=', 'V', '`', 'j', },
885 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
886 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
887 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
888 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
889 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
890 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
891 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200892 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
893 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200895 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
896 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
897 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
898 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
899 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
900 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
901 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
902 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
903 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
904 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100905 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200907 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200910 // Most people don't have F13-F20, but what the hell...
911 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
912 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
913 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
914 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
915 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
916 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
917 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
918 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100919# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200920 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
921 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
922 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
923 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200925 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
926 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
927 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
928 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
929 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
930 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
931 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
932 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
933 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
934 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100935 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936};
937
938
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100939# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
940# define UChar UnicodeChar
941# else
942# define UChar uChar.UnicodeChar
943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
Bram Moolenaar0f873732019-12-05 20:28:46 +0100945/*
946 * The return code indicates key code size.
947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000950 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 UINT uMods = pker->dwControlKeyState;
953 static int s_iIsDead = 0;
954 static WORD awAnsiCode[2];
955 static BYTE abKeystate[256];
956
957
958 if (s_iIsDead == 2)
959 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200960 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 s_iIsDead = 0;
962 return 1;
963 }
964
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200965 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 return 1;
967
Bram Moolenaara80faa82020-04-12 19:37:17 +0200968 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar0f873732019-12-05 20:28:46 +0100970 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200971 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 if (uMods & SHIFT_PRESSED)
974 abKeystate[VK_SHIFT] = 0x80;
975 if (uMods & CAPSLOCK_ON)
976 abKeystate[VK_CAPITAL] = 1;
977
978 if ((uMods & ALT_GR) == ALT_GR)
979 {
980 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
981 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
982 }
983
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200984 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
985 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200988 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 return s_iIsDead;
991}
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993static BOOL g_fJustGotFocus = FALSE;
994
995/*
996 * Decode a KEY_EVENT into one or two keystrokes
997 */
998 static BOOL
999decode_key_event(
1000 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001001 WCHAR *pch,
1002 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001004 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 int i;
1007 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1008
1009 *pch = *pch2 = NUL;
1010 g_fJustGotFocus = FALSE;
1011
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (!pker->bKeyDown)
1014 return FALSE;
1015
Bram Moolenaar0f873732019-12-05 20:28:46 +01001016 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 switch (pker->wVirtualKeyCode)
1018 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001019 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 case VK_SHIFT:
1021 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return FALSE;
1024
1025 default:
1026 break;
1027 }
1028
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001030 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001032 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (pker->wVirtualKeyCode == '6')
1034 {
1035 *pch = Ctrl_HAT;
1036 return TRUE;
1037 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001038 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 else if (pker->wVirtualKeyCode == '2')
1040 {
1041 *pch = NUL;
1042 return TRUE;
1043 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 else if (pker->wVirtualKeyCode == 0xBD)
1046 {
1047 *pch = Ctrl__;
1048 return TRUE;
1049 }
1050 }
1051
Bram Moolenaar0f873732019-12-05 20:28:46 +01001052 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1054 {
1055 *pch = K_NUL;
1056 *pch2 = '\017';
1057 return TRUE;
1058 }
1059
1060 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1061 {
1062 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1063 {
1064 if (nModifs == 0)
1065 *pch = VirtKeyMap[i].chAlone;
1066 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1067 *pch = VirtKeyMap[i].chShift;
1068 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1069 *pch = VirtKeyMap[i].chCtrl;
1070 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1071 *pch = VirtKeyMap[i].chAlt;
1072
1073 if (*pch != 0)
1074 {
1075 if (VirtKeyMap[i].fAnsiKey)
1076 {
1077 *pch2 = *pch;
1078 *pch = K_NUL;
1079 }
1080
1081 return TRUE;
1082 }
1083 }
1084 }
1085
1086 i = win32_kbd_patch_key(pker);
1087
1088 if (i < 0)
1089 *pch = NUL;
1090 else
1091 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001092 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 if (pmodifiers != NULL)
1095 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001096 // Pass on the ALT key as a modifier, but only when not combined
1097 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1099 *pmodifiers |= MOD_MASK_ALT;
1100
Bram Moolenaar0f873732019-12-05 20:28:46 +01001101 // Pass on SHIFT only for special keys, because we don't know when
1102 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1104 *pmodifiers |= MOD_MASK_SHIFT;
1105
Bram Moolenaar0f873732019-12-05 20:28:46 +01001106 // Pass on CTRL only for non-special keys, because we don't know
1107 // when it's already included with the character. And not when
1108 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1110 && *pch >= 0x20 && *pch < 0x80)
1111 *pmodifiers |= MOD_MASK_CTRL;
1112 }
1113 }
1114
1115 return (*pch != NUL);
1116}
1117
Bram Moolenaar0f873732019-12-05 20:28:46 +01001118#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
1122 * For the GUI the mouse handling is in gui_w32.c.
1123 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001124#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001126mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001129#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001130static int g_fMouseAvail = FALSE; // mouse present
1131static int g_fMouseActive = FALSE; // mouse enabled
1132static int g_nMouseClick = -1; // mouse status
1133static int g_xMouse; // mouse x coordinate
1134static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136/*
1137 * Enable or disable mouse input
1138 */
1139 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001140mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
1142 DWORD cmodein;
1143
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001144# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001145 if (gui.in_use)
1146 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (!g_fMouseAvail)
1149 return;
1150
1151 g_fMouseActive = on;
1152 GetConsoleMode(g_hConIn, &cmodein);
1153
1154 if (g_fMouseActive)
1155 cmodein |= ENABLE_MOUSE_INPUT;
1156 else
1157 cmodein &= ~ENABLE_MOUSE_INPUT;
1158
1159 SetConsoleMode(g_hConIn, cmodein);
1160}
1161
Bram Moolenaar157d8132018-03-06 17:09:20 +01001162
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001163# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001164/*
1165 * Called when 'balloonevalterm' changed.
1166 */
1167 void
1168mch_bevalterm_changed(void)
1169{
1170 mch_setmouse(g_fMouseActive);
1171}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001172# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174/*
1175 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1176 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1177 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1178 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1179 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1180 * and we return the mouse position in g_xMouse and g_yMouse.
1181 *
1182 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1183 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1184 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1185 *
1186 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1187 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1188 *
1189 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1190 * moves, even if it stays within the same character cell. We ignore
1191 * all MOUSE_MOVED messages if the position hasn't really changed, and
1192 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1193 * we're only interested in MOUSE_DRAG).
1194 *
1195 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1196 * 2-button mouses by pressing the left & right buttons simultaneously.
1197 * In practice, it's almost impossible to click both at the same time,
1198 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1199 * in such cases, if the user is clicking quickly.
1200 */
1201 static BOOL
1202decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001203 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 static int s_nOldButton = -1;
1206 static int s_nOldMouseClick = -1;
1207 static int s_xOldMouse = -1;
1208 static int s_yOldMouse = -1;
1209 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001210# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 static int s_cClicks = 1;
1214 static BOOL s_fReleased = TRUE;
1215 static DWORD s_dwLastClickTime = 0;
1216 static BOOL s_fNextIsMiddle = FALSE;
1217
Bram Moolenaar0f873732019-12-05 20:28:46 +01001218 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1221 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1222 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1223 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1224
1225 int nButton;
1226
1227 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1228 cButtons = 2;
1229
1230 if (!g_fMouseAvail || !g_fMouseActive)
1231 {
1232 g_nMouseClick = -1;
1233 return FALSE;
1234 }
1235
Bram Moolenaar0f873732019-12-05 20:28:46 +01001236 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (g_fJustGotFocus)
1238 {
1239 g_fJustGotFocus = FALSE;
1240 return FALSE;
1241 }
1242
Bram Moolenaar0f873732019-12-05 20:28:46 +01001243 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (g_nMouseClick != -1)
1245 return TRUE;
1246
1247 nButton = -1;
1248 g_xMouse = pmer->dwMousePosition.X;
1249 g_yMouse = pmer->dwMousePosition.Y;
1250
1251 if (pmer->dwEventFlags == MOUSE_MOVED)
1252 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001253 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1254 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1256 return FALSE;
1257 }
1258
Bram Moolenaar0f873732019-12-05 20:28:46 +01001259 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1261 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001262 nButton = MOUSE_RELEASE;
1263
Bram Moolenaar0f873732019-12-05 20:28:46 +01001264 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001266 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001267# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001268 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001269 if (p_bevalterm)
1270 nButton = MOUSE_DRAG;
1271 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001272# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001273 return FALSE;
1274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 s_fReleased = TRUE;
1277 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001278 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001280 // on a 2-button mouse, hold down left and right buttons
1281 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1284 {
1285 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1286
Bram Moolenaar0f873732019-12-05 20:28:46 +01001287 // if either left or right button only is pressed, see if the
1288 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (dwLR == LEFT || dwLR == RIGHT)
1290 {
1291 for (;;)
1292 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001293 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1295 != WAIT_OBJECT_0)
1296 break;
1297 else
1298 {
1299 DWORD cRecords = 0;
1300 INPUT_RECORD ir;
1301 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1302
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001303 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1306 || !(pmer2->dwButtonState & LEFT_RIGHT))
1307 break;
1308 else
1309 {
1310 if (pmer2->dwEventFlags != MOUSE_MOVED)
1311 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001312 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 return decode_mouse_event(pmer2);
1315 }
1316 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1317 s_yOldMouse == pmer2->dwMousePosition.Y)
1318 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001319 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001320 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar0f873732019-12-05 20:28:46 +01001322 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001323 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1326 break;
1327 }
1328 else
1329 break;
1330 }
1331 }
1332 }
1333 }
1334 }
1335
1336 if (s_fNextIsMiddle)
1337 {
1338 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1339 ? MOUSE_DRAG : MOUSE_MIDDLE;
1340 s_fNextIsMiddle = FALSE;
1341 }
1342 else if (cButtons == 2 &&
1343 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1344 {
1345 nButton = MOUSE_MIDDLE;
1346
1347 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1348 {
1349 s_fNextIsMiddle = TRUE;
1350 nButton = MOUSE_RELEASE;
1351 }
1352 }
1353 else if ((pmer->dwButtonState & LEFT) == LEFT)
1354 nButton = MOUSE_LEFT;
1355 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1356 nButton = MOUSE_MIDDLE;
1357 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1358 nButton = MOUSE_RIGHT;
1359
1360 if (! s_fReleased && ! s_fNextIsMiddle
1361 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1362 return FALSE;
1363
1364 s_fReleased = s_fNextIsMiddle;
1365 }
1366
1367 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1368 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001369 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (nButton != -1 && nButton != MOUSE_RELEASE)
1371 {
1372 DWORD dwCurrentTime = GetTickCount();
1373
1374 if (s_xOldMouse != g_xMouse
1375 || s_yOldMouse != g_yMouse
1376 || s_nOldButton != nButton
1377 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001378# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1382 {
1383 s_cClicks = 1;
1384 }
1385 else if (++s_cClicks > 4)
1386 {
1387 s_cClicks = 1;
1388 }
1389
1390 s_dwLastClickTime = dwCurrentTime;
1391 }
1392 }
1393 else if (pmer->dwEventFlags == MOUSE_MOVED)
1394 {
1395 if (nButton != -1 && nButton != MOUSE_RELEASE)
1396 nButton = MOUSE_DRAG;
1397
1398 s_cClicks = 1;
1399 }
1400
1401 if (nButton == -1)
1402 return FALSE;
1403
1404 if (nButton != MOUSE_RELEASE)
1405 s_nOldButton = nButton;
1406
1407 g_nMouseClick = nButton;
1408
1409 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1410 g_nMouseClick |= MOUSE_SHIFT;
1411 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1412 g_nMouseClick |= MOUSE_CTRL;
1413 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1414 g_nMouseClick |= MOUSE_ALT;
1415
1416 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1417 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1418
Bram Moolenaar0f873732019-12-05 20:28:46 +01001419 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (s_xOldMouse == g_xMouse
1421 && s_yOldMouse == g_yMouse
1422 && s_nOldMouseClick == g_nMouseClick)
1423 {
1424 g_nMouseClick = -1;
1425 return FALSE;
1426 }
1427
1428 s_xOldMouse = g_xMouse;
1429 s_yOldMouse = g_yMouse;
1430 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001431# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 s_nOldMouseClick = g_nMouseClick;
1435
1436 return TRUE;
1437}
1438
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001439#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441
1442#ifdef MCH_CURSOR_SHAPE
1443/*
1444 * Set the shape of the cursor.
1445 * 'thickness' can be from 1 (thin) to 99 (block)
1446 */
1447 static void
1448mch_set_cursor_shape(int thickness)
1449{
1450 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1451 ConsoleCursorInfo.dwSize = thickness;
1452 ConsoleCursorInfo.bVisible = s_cursor_visible;
1453
1454 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1455 if (s_cursor_visible)
1456 SetConsoleCursorPosition(g_hConOut, g_coord);
1457}
1458
1459 void
1460mch_update_cursor(void)
1461{
1462 int idx;
1463 int thickness;
1464
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001465# ifdef VIMDLL
1466 if (gui.in_use)
1467 return;
1468# endif
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 /*
1471 * How the cursor is drawn depends on the current mode.
1472 */
1473 idx = get_shape_idx(FALSE);
1474
1475 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001476 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
1478 thickness = shape_table[idx].percentage;
1479 mch_set_cursor_shape(thickness);
1480}
1481#endif
1482
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001483#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484/*
1485 * Handle FOCUS_EVENT.
1486 */
1487 static void
1488handle_focus_event(INPUT_RECORD ir)
1489{
1490 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1491 ui_focus_change((int)g_fJustGotFocus);
1492}
1493
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001494static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496/*
1497 * Wait until console input from keyboard or mouse is available,
1498 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001499 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * Return TRUE if something is available FALSE if not.
1501 */
1502 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001503WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 DWORD dwNow = 0, dwEndTime = 0;
1506 INPUT_RECORD ir;
1507 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001508 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001509# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001510 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001514 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 dwEndTime = GetTickCount() + msec;
1516 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001517 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 dwEndTime = INFINITE;
1519
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001520 // We need to loop until the end of the time period, because
1521 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 for (;;)
1523 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001524 // Only process messages when waiting.
1525 if (msec != 0)
1526 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001527# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001528 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001529# endif
1530# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001531 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001532# endif
1533# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001534 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001535# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001536 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001537
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001538 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001539# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001540 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 )
1543 return TRUE;
1544
1545 if (msec > 0)
1546 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 // If the specified wait time has passed, return. Beware that
1548 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001550 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 break;
1552 }
1553 if (msec != 0)
1554 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001555 DWORD dwWaitTime = dwEndTime - dwNow;
1556
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001557# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001558 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001559 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001560 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001561 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001562 // If there is readahead then parse_queued_messages() timed out
1563 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001564 if (channel_any_readahead())
1565 dwWaitTime = 10;
1566 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001567# endif
1568# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001569 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001570 // The 'balloonexpr' may indirectly invoke a callback while
1571 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001572 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001573# endif
1574# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001575 if (mzthreads_allowed() && p_mzq > 0
1576 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001577 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001578# endif
1579# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001580 // When waiting very briefly don't trigger timers.
1581 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001582 {
1583 long due_time;
1584
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001585 // Trigger timers and then get the time in msec until the next
1586 // one is due. Wait up to that time.
1587 due_time = check_due_timer();
1588 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001589 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001590 // timer may have used feedkeys().
1591 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001592 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001593 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1594 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001595 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001596# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001597 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001598# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001599 // Wait for either an event on the console input or a
1600 // message in the client-server window.
1601 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1602 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001603# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001604 wait_for_single_object(g_hConIn, dwWaitTime)
1605 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001606# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001607 )
1608 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
1611 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001612 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001614# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (State & CMDLINE && msg_row == Rows - 1)
1616 {
1617 CONSOLE_SCREEN_BUFFER_INFO csbi;
1618
1619 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1620 {
1621 if (csbi.dwCursorPosition.Y != msg_row)
1622 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001623 // The screen is now messed up, must redraw the
1624 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 redraw_all_later(CLEAR);
1626 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1627 redrawcmd();
1628 }
1629 }
1630 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 if (cRecords > 0)
1634 {
1635 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1636 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001637# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001638 // Windows IME sends two '\n's with only one 'ENTER'. First:
1639 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001640 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1642 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001643 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 continue;
1645 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1648 NULL, FALSE))
1649 return TRUE;
1650 }
1651
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001652 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (ir.EventType == FOCUS_EVENT)
1655 handle_focus_event(ir);
1656 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001657 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001658 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1659
Bram Moolenaar36698e32019-12-11 22:57:40 +01001660 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001661 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001662 if (dwSize.X != Columns || dwSize.Y != Rows)
1663 {
1664 CONSOLE_SCREEN_BUFFER_INFO csbi;
1665 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001666 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001667 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001668 if (dwSize.X != Columns || dwSize.Y != Rows)
1669 {
1670 ResizeConBuf(g_hConOut, dwSize);
1671 shell_resized();
1672 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001673 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (ir.EventType == MOUSE_EVENT
1676 && decode_mouse_event(&ir.Event.MouseEvent))
1677 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679 else if (msec == 0)
1680 break;
1681 }
1682
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001683# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001684 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (input_available())
1686 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001687# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return FALSE;
1690}
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * return non-zero if a character is available
1694 */
1695 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001696mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001698# ifdef VIMDLL
1699 if (gui.in_use)
1700 return TRUE;
1701# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001702 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001704
1705# if defined(FEAT_TERMINAL) || defined(PROTO)
1706/*
1707 * Check for any pending input or messages.
1708 */
1709 int
1710mch_check_messages(void)
1711{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001712# ifdef VIMDLL
1713 if (gui.in_use)
1714 return TRUE;
1715# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001716 return WaitForChar(0L, TRUE);
1717}
1718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720/*
1721 * Create the console input. Used when reading stdin doesn't work.
1722 */
1723 static void
1724create_conin(void)
1725{
1726 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1727 FILE_SHARE_READ|FILE_SHARE_WRITE,
1728 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001729 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 did_create_conin = TRUE;
1731}
1732
1733/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001734 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001736 static WCHAR
1737tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001739 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741 for (;;)
1742 {
1743 INPUT_RECORD ir;
1744 DWORD cRecords = 0;
1745
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001746# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001747 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (input_available())
1749 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (g_nMouseClick != -1)
1751 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001752# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001753 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 if (did_create_conin)
1756 read_error_exit();
1757 create_conin();
1758 continue;
1759 }
1760
1761 if (ir.EventType == KEY_EVENT)
1762 {
1763 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1764 pmodifiers, TRUE))
1765 return ch;
1766 }
1767 else if (ir.EventType == FOCUS_EVENT)
1768 handle_focus_event(ir);
1769 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1770 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else if (ir.EventType == MOUSE_EVENT)
1772 {
1773 if (decode_mouse_event(&ir.Event.MouseEvent))
1774 return 0;
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001778#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780
1781/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001782 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 * Get one or more characters from the keyboard or the mouse.
1784 * If time == 0, do not wait for characters.
1785 * If time == n, wait a short time for characters.
1786 * If time == -1, wait forever for characters.
1787 * Returns the number of characters read into buf.
1788 */
1789 int
1790mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001791 char_u *buf UNUSED,
1792 int maxlen UNUSED,
1793 long time UNUSED,
1794 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001796#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 int len;
1799 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001800# ifdef VIMDLL
1801// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1802# define TYPEAHEADSPACE 6
1803# else
1804# define TYPEAHEADSPACE 0
1805# endif
1806# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001807 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 static int typeaheadlen = 0;
1809
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001810# ifdef VIMDLL
1811 if (gui.in_use)
1812 return 0;
1813# endif
1814
Bram Moolenaar0f873732019-12-05 20:28:46 +01001815 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (typeaheadlen > 0)
1817 goto theend;
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (time >= 0)
1820 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001821 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001824 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar3918c952005-03-15 22:34:55 +00001828 /*
1829 * If there is no character available within 2 seconds (default)
1830 * write the autoscript file to disk. Or cause the CursorHold event
1831 * to be triggered.
1832 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001833 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001835 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001837 buf[0] = K_SPECIAL;
1838 buf[1] = KS_EXTRA;
1839 buf[2] = (int)KE_CURSORHOLD;
1840 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001842 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
1845
1846 /*
1847 * Try to read as many characters as there are, until the buffer is full.
1848 */
1849
Bram Moolenaar0f873732019-12-05 20:28:46 +01001850 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 g_fCBrkPressed = FALSE;
1852
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001853# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (fdDump)
1855 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar0f873732019-12-05 20:28:46 +01001858 // Keep looping until there is something in the typeahead buffer and more
1859 // to get and still room in the buffer (up to two bytes for a char and
1860 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001861 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001862 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 if (typebuf_changed(tb_change_cnt))
1865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001866 // "buf" may be invalid now if a client put something in the
1867 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 typeaheadlen = 0;
1869 break;
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (g_nMouseClick != -1)
1872 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001873# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (fdDump)
1875 fprintf(fdDump, "{%02x @ %d, %d}",
1876 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 typeahead[typeaheadlen++] = ESC + 128;
1879 typeahead[typeaheadlen++] = 'M';
1880 typeahead[typeaheadlen++] = g_nMouseClick;
1881 typeahead[typeaheadlen++] = g_xMouse + '!';
1882 typeahead[typeaheadlen++] = g_yMouse + '!';
1883 g_nMouseClick = -1;
1884 }
1885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001887 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 int modifiers = 0;
1889
1890 c = tgetch(&modifiers, &ch2);
1891
1892 if (typebuf_changed(tb_change_cnt))
1893 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001894 // "buf" may be invalid now if a client put something in the
1895 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 typeaheadlen = 0;
1897 break;
1898 }
1899
1900 if (c == Ctrl_C && ctrl_c_interrupts)
1901 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001902# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 got_int = TRUE;
1906 }
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 int n = 1;
1911
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001912 if (ch2 == NUL)
1913 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001914 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001915 char_u *p;
1916 WCHAR ch[2];
1917
1918 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001919 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001920 {
1921 ch[1] = tgetch(&modifiers, &ch2);
1922 n++;
1923 }
1924 p = utf16_to_enc(ch, &n);
1925 if (p != NULL)
1926 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001927 for (i = 0, j = 0; i < n; i++)
1928 {
1929 typeahead[typeaheadlen + j++] = p[i];
1930# ifdef VIMDLL
1931 if (p[i] == CSI)
1932 {
1933 typeahead[typeaheadlen + j++] = KS_EXTRA;
1934 typeahead[typeaheadlen + j++] = KE_CSI;
1935 }
1936# endif
1937 }
1938 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001939 vim_free(p);
1940 }
1941 }
1942 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001943 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001944 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001945# ifdef VIMDLL
1946 if (c == CSI)
1947 {
1948 typeahead[typeaheadlen + 1] = KS_EXTRA;
1949 typeahead[typeaheadlen + 2] = KE_CSI;
1950 n = 3;
1951 }
1952# endif
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (ch2 != NUL)
1955 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001956 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001957 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001958 switch (ch2)
1959 {
1960 case (WCHAR)'\324': // SHIFT+Insert
1961 case (WCHAR)'\325': // CTRL+Insert
1962 case (WCHAR)'\327': // SHIFT+Delete
1963 case (WCHAR)'\330': // CTRL+Delete
1964 typeahead[typeaheadlen + n] = (char_u)ch2;
1965 n++;
1966 break;
1967
1968 default:
1969 typeahead[typeaheadlen + n] = 3;
1970 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1971 n += 2;
1972 break;
1973 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001974 }
1975 else
1976 {
1977 typeahead[typeaheadlen + n] = 3;
1978 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1979 n += 2;
1980 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001981 }
1982
Bram Moolenaar0f873732019-12-05 20:28:46 +01001983 // Use the ALT key to set the 8th bit of the character
1984 // when it's one byte, the 8th bit isn't set yet and not
1985 // using a double-byte encoding (would become a lead
1986 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if ((modifiers & MOD_MASK_ALT)
1988 && n == 1
1989 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 )
1992 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001993 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1994 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 modifiers &= ~MOD_MASK_ALT;
1996 }
1997
1998 if (modifiers != 0)
1999 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002000 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 mch_memmove(typeahead + typeaheadlen + 3,
2002 typeahead + typeaheadlen, n);
2003 typeahead[typeaheadlen++] = K_SPECIAL;
2004 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2005 typeahead[typeaheadlen++] = modifiers;
2006 }
2007
2008 typeaheadlen += n;
2009
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002010# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (fdDump)
2012 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 }
2016 }
2017
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002018# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (fdDump)
2020 {
2021 fputs("]\n", fdDump);
2022 fflush(fdDump);
2023 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002027 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 len = 0;
2029 while (len < maxlen && typeaheadlen > 0)
2030 {
2031 buf[len++] = typeahead[0];
2032 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2033 }
2034 return len;
2035
Bram Moolenaar0f873732019-12-05 20:28:46 +01002036#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002038#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
Bram Moolenaar82881492012-11-20 16:53:39 +01002041#ifndef PROTO
2042# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002043# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002047/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002048 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2049 * If "use_path" is FALSE: Return TRUE if "name" exists.
2050 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2051 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002052 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002055executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002057 WCHAR *p;
2058 WCHAR fnamew[_MAX_PATH];
2059 WCHAR *dumw;
2060 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002061 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaar43663192017-03-05 14:29:12 +01002063 if (!use_path)
2064 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002065 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002066 {
2067 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002068 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002069 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002070 *path = vim_strsave((char_u *)name);
2071 else
2072 *path = FullName_save((char_u *)name, FALSE);
2073 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002074 return TRUE;
2075 }
2076 return FALSE;
2077 }
2078
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002079 p = enc_to_utf16((char_u *)name, NULL);
2080 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002081 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002082
2083 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002084 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002085 if (wnewpath == NULL)
2086 return FALSE;
2087 wcscpy(wnewpath, L".;");
2088 wcscat(wnewpath, wcurpath);
2089 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2090 vim_free(wnewpath);
2091 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002092 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002093 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002094 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002095 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002096 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002097 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002098 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
2100
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002101#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2102 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002103/*
2104 * Bad parameter handler.
2105 *
2106 * Certain MS CRT functions will intentionally crash when passed invalid
2107 * parameters to highlight possible security holes. Setting this function as
2108 * the bad parameter handler will prevent the crash.
2109 *
2110 * In debug builds the parameters contain CRT information that might help track
2111 * down the source of a problem, but in non-debug builds the arguments are all
2112 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2113 * worth allowing these to make debugging of issues easier.
2114 */
2115 static void
2116bad_param_handler(const wchar_t *expression,
2117 const wchar_t *function,
2118 const wchar_t *file,
2119 unsigned int line,
2120 uintptr_t pReserved)
2121{
2122}
2123
2124# define SET_INVALID_PARAM_HANDLER \
2125 ((void)_set_invalid_parameter_handler(bad_param_handler))
2126#else
2127# define SET_INVALID_PARAM_HANDLER
2128#endif
2129
Bram Moolenaar4f974752019-02-17 17:44:42 +01002130#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132/*
2133 * GUI version of mch_init().
2134 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002135 static void
2136mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002138# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaar0f873732019-12-05 20:28:46 +01002142 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002143 SET_INVALID_PARAM_HANDLER;
2144
Bram Moolenaar0f873732019-12-05 20:28:46 +01002145 // Let critical errors result in a failure, not in a dialog box. Required
2146 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 SetErrorMode(SEM_FAILCRITICALERRORS);
2148
Bram Moolenaar0f873732019-12-05 20:28:46 +01002149 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar0f873732019-12-05 20:28:46 +01002151 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 Rows = 25;
2153 Columns = 80;
2154
Bram Moolenaar0f873732019-12-05 20:28:46 +01002155 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 char_u vimrun_location[_MAX_PATH + 4];
2158
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 STRCPY(vimrun_location, exe_name);
2161 STRCPY(gettail(vimrun_location), "vimrun.exe");
2162 if (mch_getperm(vimrun_location) >= 0)
2163 {
2164 if (*skiptowhite(vimrun_location) != NUL)
2165 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002166 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 mch_memmove(vimrun_location + 1, vimrun_location,
2168 STRLEN(vimrun_location) + 1);
2169 *vimrun_location = '"';
2170 STRCPY(gettail(vimrun_location), "vimrun\" ");
2171 }
2172 else
2173 STRCPY(gettail(vimrun_location), "vimrun ");
2174
2175 vimrun_path = (char *)vim_strsave(vimrun_location);
2176 s_dont_use_vimrun = FALSE;
2177 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002178 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 s_dont_use_vimrun = FALSE;
2180
Bram Moolenaar0f873732019-12-05 20:28:46 +01002181 // Don't give the warning for a missing vimrun.exe right now, but only
2182 // when vimrun was supposed to be used. Don't bother people that do
2183 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (s_dont_use_vimrun)
2185 need_vimrun_warning = TRUE;
2186 }
2187
2188 /*
2189 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2190 * Otherwise the default "findstr /n" is used.
2191 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002192 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2194
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002195# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002196 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002197# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002198
2199 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200}
2201
2202
Bram Moolenaar0f873732019-12-05 20:28:46 +01002203#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002204
2205#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002207# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2208# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210/*
2211 * ClearConsoleBuffer()
2212 * Description:
2213 * Clears the entire contents of the console screen buffer, using the
2214 * specified attribute.
2215 * Returns:
2216 * TRUE on success
2217 */
2218 static BOOL
2219ClearConsoleBuffer(WORD wAttribute)
2220{
2221 CONSOLE_SCREEN_BUFFER_INFO csbi;
2222 COORD coord;
2223 DWORD NumCells, dummy;
2224
2225 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2226 return FALSE;
2227
2228 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2229 coord.X = 0;
2230 coord.Y = 0;
2231 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2232 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2235 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 return TRUE;
2239}
2240
2241/*
2242 * FitConsoleWindow()
2243 * Description:
2244 * Checks if the console window will fit within given buffer dimensions.
2245 * Also, if requested, will shrink the window to fit.
2246 * Returns:
2247 * TRUE on success
2248 */
2249 static BOOL
2250FitConsoleWindow(
2251 COORD dwBufferSize,
2252 BOOL WantAdjust)
2253{
2254 CONSOLE_SCREEN_BUFFER_INFO csbi;
2255 COORD dwWindowSize;
2256 BOOL NeedAdjust = FALSE;
2257
2258 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2259 {
2260 /*
2261 * A buffer resize will fail if the current console window does
2262 * not lie completely within that buffer. To avoid this, we might
2263 * have to move and possibly shrink the window.
2264 */
2265 if (csbi.srWindow.Right >= dwBufferSize.X)
2266 {
2267 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2268 if (dwWindowSize.X > dwBufferSize.X)
2269 dwWindowSize.X = dwBufferSize.X;
2270 csbi.srWindow.Right = dwBufferSize.X - 1;
2271 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2272 NeedAdjust = TRUE;
2273 }
2274 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2275 {
2276 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2277 if (dwWindowSize.Y > dwBufferSize.Y)
2278 dwWindowSize.Y = dwBufferSize.Y;
2279 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2280 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2281 NeedAdjust = TRUE;
2282 }
2283 if (NeedAdjust && WantAdjust)
2284 {
2285 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2286 return FALSE;
2287 }
2288 return TRUE;
2289 }
2290
2291 return FALSE;
2292}
2293
2294typedef struct ConsoleBufferStruct
2295{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002296 BOOL IsValid;
2297 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002298 PCHAR_INFO Buffer;
2299 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002300 PSMALL_RECT Regions;
2301 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302} ConsoleBuffer;
2303
2304/*
2305 * SaveConsoleBuffer()
2306 * Description:
2307 * Saves important information about the console buffer, including the
2308 * actual buffer contents. The saved information is suitable for later
2309 * restoration by RestoreConsoleBuffer().
2310 * Returns:
2311 * TRUE if all information was saved; FALSE otherwise
2312 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2313 */
2314 static BOOL
2315SaveConsoleBuffer(
2316 ConsoleBuffer *cb)
2317{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002318 DWORD NumCells;
2319 COORD BufferCoord;
2320 SMALL_RECT ReadRegion;
2321 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002322 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (cb == NULL)
2325 return FALSE;
2326
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002327 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 cb->IsValid = FALSE;
2330 return FALSE;
2331 }
2332 cb->IsValid = TRUE;
2333
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002334 /*
2335 * Allocate a buffer large enough to hold the entire console screen
2336 * buffer. If this ConsoleBuffer structure has already been initialized
2337 * with a buffer of the correct size, then just use that one.
2338 */
2339 if (!cb->IsValid || cb->Buffer == NULL ||
2340 cb->BufferSize.X != cb->Info.dwSize.X ||
2341 cb->BufferSize.Y != cb->Info.dwSize.Y)
2342 {
2343 cb->BufferSize.X = cb->Info.dwSize.X;
2344 cb->BufferSize.Y = cb->Info.dwSize.Y;
2345 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2346 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002347 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002348 if (cb->Buffer == NULL)
2349 return FALSE;
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002353 * We will now copy the console screen buffer into our buffer.
2354 * ReadConsoleOutput() seems to be limited as far as how much you
2355 * can read at a time. Empirically, this number seems to be about
2356 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2357 * in chunks until it is all copied. The chunks will all have the
2358 * same horizontal characteristics, so initialize them now. The
2359 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002361 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002362 ReadRegion.Left = 0;
2363 ReadRegion.Right = cb->Info.dwSize.X - 1;
2364 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002365
2366 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2367 if (cb->Regions == NULL || numregions != cb->NumRegions)
2368 {
2369 cb->NumRegions = numregions;
2370 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002371 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002372 if (cb->Regions == NULL)
2373 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002374 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002375 return FALSE;
2376 }
2377 }
2378
2379 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002381 /*
2382 * Read into position (0, Y) in our buffer.
2383 */
2384 BufferCoord.Y = Y;
2385 /*
2386 * Read the region whose top left corner is (0, Y) and whose bottom
2387 * right corner is (width - 1, Y + Y_incr - 1). This should define
2388 * a region of size width by Y_incr. Don't worry if this region is
2389 * too large for the remaining buffer; it will be cropped.
2390 */
2391 ReadRegion.Top = Y;
2392 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002393 if (!ReadConsoleOutputW(g_hConOut, // output handle
2394 cb->Buffer, // our buffer
2395 cb->BufferSize, // dimensions of our buffer
2396 BufferCoord, // offset in our buffer
2397 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002398 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002399 VIM_CLEAR(cb->Buffer);
2400 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002401 return FALSE;
2402 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002403 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405
2406 return TRUE;
2407}
2408
2409/*
2410 * RestoreConsoleBuffer()
2411 * Description:
2412 * Restores important information about the console buffer, including the
2413 * actual buffer contents, if desired. The information to restore is in
2414 * the same format used by SaveConsoleBuffer().
2415 * Returns:
2416 * TRUE on success
2417 */
2418 static BOOL
2419RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002420 ConsoleBuffer *cb,
2421 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002423 COORD BufferCoord;
2424 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002425 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 if (cb == NULL || !cb->IsValid)
2428 return FALSE;
2429
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002430 /*
2431 * Before restoring the buffer contents, clear the current buffer, and
2432 * restore the cursor position and window information. Doing this now
2433 * prevents old buffer contents from "flashing" onto the screen.
2434 */
2435 if (RestoreScreen)
2436 ClearConsoleBuffer(cb->Info.wAttributes);
2437
2438 FitConsoleWindow(cb->Info.dwSize, TRUE);
2439 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2440 return FALSE;
2441 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2442 return FALSE;
2443
2444 if (!RestoreScreen)
2445 {
2446 /*
2447 * No need to restore the screen buffer contents, so we're done.
2448 */
2449 return TRUE;
2450 }
2451
2452 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2453 return FALSE;
2454 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2455 return FALSE;
2456
2457 /*
2458 * Restore the screen buffer contents.
2459 */
2460 if (cb->Buffer != NULL)
2461 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002462 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002463 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002464 BufferCoord.X = cb->Regions[i].Left;
2465 BufferCoord.Y = cb->Regions[i].Top;
2466 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002467 if (!WriteConsoleOutputW(g_hConOut, // output handle
2468 cb->Buffer, // our buffer
2469 cb->BufferSize, // dimensions of our buffer
2470 BufferCoord, // offset in our buffer
2471 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002472 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002473 }
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 return TRUE;
2477}
2478
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002479# define FEAT_RESTORE_ORIG_SCREEN
2480# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002481static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483static ConsoleBuffer g_cbNonTermcap = { 0 };
2484static ConsoleBuffer g_cbTermcap = { 0 };
2485
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002486# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002488HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489static HICON g_hOrigIconSmall = NULL;
2490static HICON g_hOrigIcon = NULL;
2491static HICON g_hVimIcon = NULL;
2492static BOOL g_fCanChangeIcon = FALSE;
2493
Bram Moolenaar0f873732019-12-05 20:28:46 +01002494// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002495# ifndef ICON_SMALL
2496# define ICON_SMALL 0
2497# endif
2498# ifndef ICON_BIG
2499# define ICON_BIG 1
2500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * GetConsoleIcon()
2503 * Description:
2504 * Attempts to retrieve the small icon and/or the big icon currently in
2505 * use by a given window.
2506 * Returns:
2507 * TRUE on success
2508 */
2509 static BOOL
2510GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002511 HWND hWnd,
2512 HICON *phIconSmall,
2513 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 if (hWnd == NULL)
2516 return FALSE;
2517
2518 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002519 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2520 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002522 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2523 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 return TRUE;
2525}
2526
2527/*
2528 * SetConsoleIcon()
2529 * Description:
2530 * Attempts to change the small icon and/or the big icon currently in
2531 * use by a given window.
2532 * Returns:
2533 * TRUE on success
2534 */
2535 static BOOL
2536SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002537 HWND hWnd,
2538 HICON hIconSmall,
2539 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (hWnd == NULL)
2542 return FALSE;
2543
2544 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002545 SendMessage(hWnd, WM_SETICON,
2546 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002548 SendMessage(hWnd, WM_SETICON,
2549 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return TRUE;
2551}
2552
2553/*
2554 * SaveConsoleTitleAndIcon()
2555 * Description:
2556 * Saves the current console window title in g_szOrigTitle, for later
2557 * restoration. Also, attempts to obtain a handle to the console window,
2558 * and use it to save the small and big icons currently in use by the
2559 * console window. This is not always possible on some versions of Windows;
2560 * nor is it possible when running Vim remotely using Telnet (since the
2561 * console window the user sees is owned by a remote process).
2562 */
2563 static void
2564SaveConsoleTitleAndIcon(void)
2565{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2568 return;
2569
2570 /*
2571 * Obtain a handle to the console window using GetConsoleWindow() from
2572 * KERNEL32.DLL; we need to handle in order to change the window icon.
2573 * This function only exists on NT-based Windows, starting with Windows
2574 * 2000. On older operating systems, we can't change the window icon
2575 * anyway.
2576 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002577 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (g_hWnd == NULL)
2579 return;
2580
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2583 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2584 return;
2585
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002587 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002588 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (g_hVimIcon != NULL)
2590 g_fCanChangeIcon = TRUE;
2591}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594static int g_fWindInitCalled = FALSE;
2595static int g_fTermcapMode = FALSE;
2596static CONSOLE_CURSOR_INFO g_cci;
2597static DWORD g_cmodein = 0;
2598static DWORD g_cmodeout = 0;
2599
2600/*
2601 * non-GUI version of mch_init().
2602 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002603 static void
2604mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002606# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002607 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002608# endif
2609# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002614 SET_INVALID_PARAM_HANDLER;
2615
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Let critical errors result in a failure, not in a dialog box. Required
2617 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 SetErrorMode(SEM_FAILCRITICALERRORS);
2619
Bram Moolenaar0f873732019-12-05 20:28:46 +01002620 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 out_flush();
2622
Bram Moolenaar0f873732019-12-05 20:28:46 +01002623 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (read_cmd_fd == 0)
2625 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2626 else
2627 create_conin();
2628 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2629
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002630# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002631 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002632 SaveConsoleBuffer(&g_cbOrig);
2633 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002635 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002636 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2637 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (cterm_normal_fg_color == 0)
2640 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2641 if (cterm_normal_bg_color == 0)
2642 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2643
Bram Moolenaarbdace832019-03-02 10:13:42 +01002644 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002645 g_color_index_fg = g_attrDefault & 0xf;
2646 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2647
Bram Moolenaar0f873732019-12-05 20:28:46 +01002648 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 update_tcap(g_attrCurrent);
2650
2651 GetConsoleCursorInfo(g_hConOut, &g_cci);
2652 GetConsoleMode(g_hConIn, &g_cmodein);
2653 GetConsoleMode(g_hConOut, &g_cmodeout);
2654
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002655# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 SaveConsoleTitleAndIcon();
2657 /*
2658 * Set both the small and big icons of the console window to Vim's icon.
2659 * Note that Vim presently only has one size of icon (32x32), but it
2660 * automatically gets scaled down to 16x16 when setting the small icon.
2661 */
2662 if (g_fCanChangeIcon)
2663 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 ui_get_shellsize();
2667
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002668# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 fdDump = fopen("dump", "wt");
2670
2671 if (fdDump)
2672 {
2673 time_t t;
2674
2675 time(&t);
2676 fputs(ctime(&t), fdDump);
2677 fflush(fdDump);
2678 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 g_fWindInitCalled = TRUE;
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002685# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002686 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002687# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002688
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002689 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002690 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691}
2692
2693/*
2694 * non-GUI version of mch_exit().
2695 * Shut down and exit with status `r'
2696 * Careful: mch_exit() may be called before mch_init()!
2697 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002698 static void
2699mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002701 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002703 vtp_exit();
2704
Bram Moolenaar955f1982017-02-05 15:10:51 +01002705 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (g_fWindInitCalled)
2707 settmode(TMODE_COOK);
2708
Bram Moolenaar0f873732019-12-05 20:28:46 +01002709 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 if (g_fWindInitCalled)
2712 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002713# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002714 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /*
2716 * Restore both the small and big icons of the console window to
2717 * what they were at startup. Don't do this when the window is
2718 * closed, Vim would hang here.
2719 */
2720 if (g_fCanChangeIcon && !g_fForceExit)
2721 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002724# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (fdDump)
2726 {
2727 time_t t;
2728
2729 time(&t);
2730 fputs(ctime(&t), fdDump);
2731 fclose(fdDump);
2732 }
2733 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 SetConsoleCursorInfo(g_hConOut, &g_cci);
2738 SetConsoleMode(g_hConIn, g_cmodein);
2739 SetConsoleMode(g_hConOut, g_cmodeout);
2740
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002741# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 exit(r);
2746}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002747#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002749 void
2750mch_init(void)
2751{
2752#ifdef VIMDLL
2753 if (gui.starting)
2754 mch_init_g();
2755 else
2756 mch_init_c();
2757#elif defined(FEAT_GUI_MSWIN)
2758 mch_init_g();
2759#else
2760 mch_init_c();
2761#endif
2762}
2763
2764 void
2765mch_exit(int r)
2766{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002767#ifdef FEAT_NETBEANS_INTG
2768 netbeans_send_disconnect();
2769#endif
2770
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002771#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002772 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002773 mch_exit_g(r);
2774 else
2775 mch_exit_c(r);
2776#elif defined(FEAT_GUI_MSWIN)
2777 mch_exit_g(r);
2778#else
2779 mch_exit_c(r);
2780#endif
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Do we have an interactive window?
2785 */
2786 int
2787mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002788 int argc UNUSED,
2789 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 get_exe_name();
2792
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002793#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002796# ifdef VIMDLL
2797 if (gui.in_use)
2798 return OK;
2799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (isatty(1))
2801 return OK;
2802 return FAIL;
2803#endif
2804}
2805
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002806/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002807 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 * When "len" is > 0, also expand short to long filenames.
2809 */
2810 void
2811fname_case(
2812 char_u *name,
2813 int len)
2814{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002815 int flen;
2816 WCHAR *p;
2817 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002819 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002820 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return;
2822
2823 slash_adjust(name);
2824
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002825 p = enc_to_utf16(name, NULL);
2826 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002827 return;
2828
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002829 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002831 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002833 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002835 if (len > 0 || flen >= (int)STRLEN(q))
2836 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2837 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002840 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841}
2842
2843
2844/*
2845 * Insert user name in s[len].
2846 */
2847 int
2848mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002849 char_u *s,
2850 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002852 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002853 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002855 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002856 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002857 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002858
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002859 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002860 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002861 vim_strncpy(s, p, len - 1);
2862 vim_free(p);
2863 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002864 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 s[0] = NUL;
2867 return FAIL;
2868}
2869
2870
2871/*
2872 * Insert host name in s[len].
2873 */
2874 void
2875mch_get_host_name(
2876 char_u *s,
2877 int len)
2878{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002879 WCHAR wszHostName[256 + 1];
2880 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002882 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002883 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002884 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002885
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002886 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002887 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002888 vim_strncpy(s, p, len - 1);
2889 vim_free(p);
2890 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002891 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893}
2894
2895
2896/*
2897 * return process ID
2898 */
2899 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002900mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 return (long)GetCurrentProcessId();
2903}
2904
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002905/*
2906 * return TRUE if process "pid" is still running
2907 */
2908 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002909mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002910{
2911 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2912 DWORD status = 0;
2913 int ret = FALSE;
2914
2915 if (hProcess == NULL)
2916 return FALSE; // might not have access
2917 if (GetExitCodeProcess(hProcess, &status) )
2918 ret = status == STILL_ACTIVE;
2919 CloseHandle(hProcess);
2920 return ret;
2921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923/*
2924 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2925 * Return OK for success, FAIL for failure.
2926 */
2927 int
2928mch_dirname(
2929 char_u *buf,
2930 int len)
2931{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002932 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 /*
2935 * Originally this was:
2936 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2937 * But the Win32s known bug list says that getcwd() doesn't work
2938 * so use the Win32 system call instead. <Negri>
2939 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002940 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002942 WCHAR wcbuf[_MAX_PATH + 1];
2943 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002945 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002947 p = utf16_to_enc(wcbuf, NULL);
2948 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002949 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002950 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002952 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002955 if (p == NULL)
2956 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002957
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002958 if (p != NULL)
2959 {
2960 vim_strncpy(buf, p, len - 1);
2961 vim_free(p);
2962 return OK;
2963 }
2964 }
2965 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966}
2967
2968/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002969 * Get file permissions for "name".
2970 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
2972 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002973mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002975 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002976 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002978 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002979 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982
2983/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002984 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002985 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002986 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
2988 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002989mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002991 long n;
2992 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002993
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002994 p = enc_to_utf16(name, NULL);
2995 if (p == NULL)
2996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002998 n = _wchmod(p, perm);
2999 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003000 if (n == -1)
3001 return FAIL;
3002
3003 win32_set_archive(name);
3004
3005 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008/*
3009 * Set hidden flag for "name".
3010 */
3011 void
3012mch_hide(char_u *name)
3013{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003014 int attrs = win32_getattrs(name);
3015 if (attrs == -1)
3016 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003018 attrs |= FILE_ATTRIBUTE_HIDDEN;
3019 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003023 * Return TRUE if file "name" exists and is hidden.
3024 */
3025 int
3026mch_ishidden(char_u *name)
3027{
3028 int f = win32_getattrs(name);
3029
3030 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003032
3033 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3034}
3035
3036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 * return TRUE if "name" is a directory
3038 * return FALSE if "name" is not a directory or upon error
3039 */
3040 int
3041mch_isdir(char_u *name)
3042{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003043 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003046 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
3048 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3049}
3050
3051/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003052 * return TRUE if "name" is a directory, NOT a symlink to a directory
3053 * return FALSE if "name" is not a directory
3054 * return FALSE for error
3055 */
3056 int
3057mch_isrealdir(char_u *name)
3058{
3059 return mch_isdir(name) && !mch_is_symbolic_link(name);
3060}
3061
3062/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003063 * Create directory "name".
3064 * Return 0 on success, -1 on error.
3065 */
3066 int
3067mch_mkdir(char_u *name)
3068{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003069 WCHAR *p;
3070 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003071
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003072 p = enc_to_utf16(name, NULL);
3073 if (p == NULL)
3074 return -1;
3075 retval = _wmkdir(p);
3076 vim_free(p);
3077 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003078}
3079
3080/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003081 * Delete directory "name".
3082 * Return 0 on success, -1 on error.
3083 */
3084 int
3085mch_rmdir(char_u *name)
3086{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003087 WCHAR *p;
3088 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003089
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003090 p = enc_to_utf16(name, NULL);
3091 if (p == NULL)
3092 return -1;
3093 retval = _wrmdir(p);
3094 vim_free(p);
3095 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003096}
3097
3098/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003099 * Return TRUE if file "fname" has more than one link.
3100 */
3101 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003102mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003103{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003104 BY_HANDLE_FILE_INFORMATION info;
3105
3106 return win32_fileinfo(fname, &info) == FILEINFO_OK
3107 && info.nNumberOfLinks > 1;
3108}
3109
3110/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003111 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003112 */
3113 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003114mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003115{
3116 HANDLE hFind;
3117 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003118 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003119 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003120 WIN32_FIND_DATAW findDataW;
3121
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003122 wn = enc_to_utf16(name, NULL);
3123 if (wn == NULL)
3124 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003125
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003126 hFind = FindFirstFileW(wn, &findDataW);
3127 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003128 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003129 {
3130 fileFlags = findDataW.dwFileAttributes;
3131 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003132 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003133 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003134
3135 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003136 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3137 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003138 res = TRUE;
3139
3140 return res;
3141}
3142
3143/*
3144 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3145 * link.
3146 */
3147 int
3148mch_is_linked(char_u *fname)
3149{
3150 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3151 return TRUE;
3152 return FALSE;
3153}
3154
3155/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003156 * Get the by-handle-file-information for "fname".
3157 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003158 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003159 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3160 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3161 */
3162 int
3163win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3164{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003165 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003166 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003167 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003168
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003169 wn = enc_to_utf16(fname, NULL);
3170 if (wn == NULL)
3171 return FILEINFO_ENC_FAIL;
3172
3173 hFile = CreateFileW(wn, // file name
3174 GENERIC_READ, // access mode
3175 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3176 NULL, // security descriptor
3177 OPEN_EXISTING, // creation disposition
3178 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3179 NULL); // handle to template file
3180 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003181
3182 if (hFile != INVALID_HANDLE_VALUE)
3183 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003184 if (GetFileInformationByHandle(hFile, info) != 0)
3185 res = FILEINFO_OK;
3186 else
3187 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003188 CloseHandle(hFile);
3189 }
3190
Bram Moolenaar03f48552006-02-28 23:52:23 +00003191 return res;
3192}
3193
3194/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003195 * get file attributes for `name'
3196 * -1 : error
3197 * else FILE_ATTRIBUTE_* defined in winnt.h
3198 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003199 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003200win32_getattrs(char_u *name)
3201{
3202 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003203 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003204
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003205 p = enc_to_utf16(name, NULL);
3206 if (p == NULL)
3207 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003208
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003209 attr = GetFileAttributesW(p);
3210 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003211
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003212 return attr;
3213}
3214
3215/*
3216 * set file attributes for `name' to `attrs'
3217 *
3218 * return -1 for failure, 0 otherwise
3219 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003220 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003221win32_setattrs(char_u *name, int attrs)
3222{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003223 int res;
3224 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003225
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003226 p = enc_to_utf16(name, NULL);
3227 if (p == NULL)
3228 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003229
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003230 res = SetFileAttributesW(p, attrs);
3231 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003232
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003233 return res ? 0 : -1;
3234}
3235
3236/*
3237 * Set archive flag for "name".
3238 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003239 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003240win32_set_archive(char_u *name)
3241{
3242 int attrs = win32_getattrs(name);
3243 if (attrs == -1)
3244 return -1;
3245
3246 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3247 return win32_setattrs(name, attrs);
3248}
3249
3250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Return TRUE if file or directory "name" is writable (not readonly).
3252 * Strange semantics of Win32: a readonly directory is writable, but you can't
3253 * delete a file. Let's say this means it is writable.
3254 */
3255 int
3256mch_writable(char_u *name)
3257{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003258 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003260 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3261 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003265 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003266 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003267 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3268 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 */
3270 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003271mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar86621892019-03-30 21:51:28 +01003273 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3274 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3275 // UTF-8.
3276 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003277 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003278 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003279
Bram Moolenaar86621892019-03-30 21:51:28 +01003280 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003281 return FALSE;
3282
Bram Moolenaar86621892019-03-30 21:51:28 +01003283 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003284 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003285 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003286 return TRUE;
3287
3288 /*
3289 * Loop over all extensions in $PATHEXT.
3290 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003291 p = mch_getenv("PATHEXT");
3292 if (p == NULL)
3293 p = (char_u *)".com;.exe;.bat;.cmd";
3294 saved = vim_strsave(p);
3295 if (saved == NULL)
3296 return FALSE;
3297 p = saved;
3298 while (*p)
3299 {
3300 char_u *tmp = vim_strchr(p, ';');
3301
3302 if (tmp != NULL)
3303 *tmp = NUL;
3304 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3305 && executable_exists((char *)name, path, use_path))
3306 {
3307 vim_free(saved);
3308 return TRUE;
3309 }
3310 if (tmp == NULL)
3311 break;
3312 p = tmp + 1;
3313 }
3314 vim_free(saved);
3315
Bram Moolenaar86621892019-03-30 21:51:28 +01003316 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003317 p = mch_getenv("PATHEXT");
3318 if (p == NULL)
3319 p = (char_u *)".com;.exe;.bat;.cmd";
3320 while (*p)
3321 {
3322 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3323 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003324 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003325 buf[len] = NUL;
3326 ++p;
3327 if (*p)
3328 ++p;
3329 }
3330 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003331 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003332 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003333 return TRUE;
3334 }
3335 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338/*
3339 * Check what "name" is:
3340 * NODE_NORMAL: file or directory (or doesn't exist)
3341 * NODE_WRITABLE: writable device, socket, fifo, etc.
3342 * NODE_OTHER: non-writable things
3343 */
3344 int
3345mch_nodetype(char_u *name)
3346{
3347 HANDLE hFile;
3348 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003349 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar0f873732019-12-05 20:28:46 +01003351 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3352 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3353 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003354 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3355 return NODE_WRITABLE;
3356
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003357 wn = enc_to_utf16(name, NULL);
3358 if (wn == NULL)
3359 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003360
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003361 hFile = CreateFileW(wn, // file name
3362 GENERIC_WRITE, // access mode
3363 0, // share mode
3364 NULL, // security descriptor
3365 OPEN_EXISTING, // creation disposition
3366 0, // file attributes
3367 NULL); // handle to template file
3368 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (hFile == INVALID_HANDLE_VALUE)
3370 return NODE_NORMAL;
3371
3372 type = GetFileType(hFile);
3373 CloseHandle(hFile);
3374 if (type == FILE_TYPE_CHAR)
3375 return NODE_WRITABLE;
3376 if (type == FILE_TYPE_DISK)
3377 return NODE_NORMAL;
3378 return NODE_OTHER;
3379}
3380
3381#ifdef HAVE_ACL
3382struct my_acl
3383{
3384 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3385 PSID pSidOwner;
3386 PSID pSidGroup;
3387 PACL pDacl;
3388 PACL pSacl;
3389};
3390#endif
3391
3392/*
3393 * Return a pointer to the ACL of file "fname" in allocated memory.
3394 * Return NULL if the ACL is not available for whatever reason.
3395 */
3396 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003397mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399#ifndef HAVE_ACL
3400 return (vim_acl_T)NULL;
3401#else
3402 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003403 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003405 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003406 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003408 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003410 wn = enc_to_utf16(fname, NULL);
3411 if (wn == NULL)
3412 return NULL;
3413
3414 // Try to retrieve the entire security descriptor.
3415 err = GetNamedSecurityInfoW(
3416 wn, // Abstract filename
3417 SE_FILE_OBJECT, // File Object
3418 OWNER_SECURITY_INFORMATION |
3419 GROUP_SECURITY_INFORMATION |
3420 DACL_SECURITY_INFORMATION |
3421 SACL_SECURITY_INFORMATION,
3422 &p->pSidOwner, // Ownership information.
3423 &p->pSidGroup, // Group membership.
3424 &p->pDacl, // Discretionary information.
3425 &p->pSacl, // For auditing purposes.
3426 &p->pSecurityDescriptor);
3427 if (err == ERROR_ACCESS_DENIED ||
3428 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003429 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003430 // Retrieve only DACL.
3431 (void)GetNamedSecurityInfoW(
3432 wn,
3433 SE_FILE_OBJECT,
3434 DACL_SECURITY_INFORMATION,
3435 NULL,
3436 NULL,
3437 &p->pDacl,
3438 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003439 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003440 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003441 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003442 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003443 mch_free_acl((vim_acl_T)p);
3444 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003446 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448
3449 return (vim_acl_T)p;
3450#endif
3451}
3452
Bram Moolenaar27515922013-06-29 15:36:26 +02003453#ifdef HAVE_ACL
3454/*
3455 * Check if "acl" contains inherited ACE.
3456 */
3457 static BOOL
3458is_acl_inherited(PACL acl)
3459{
3460 DWORD i;
3461 ACL_SIZE_INFORMATION acl_info;
3462 PACCESS_ALLOWED_ACE ace;
3463
3464 acl_info.AceCount = 0;
3465 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3466 for (i = 0; i < acl_info.AceCount; i++)
3467 {
3468 GetAce(acl, i, (LPVOID *)&ace);
3469 if (ace->Header.AceFlags & INHERITED_ACE)
3470 return TRUE;
3471 }
3472 return FALSE;
3473}
3474#endif
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476/*
3477 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3478 * Errors are ignored.
3479 * This must only be called with "acl" equal to what mch_get_acl() returned.
3480 */
3481 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003482mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484#ifdef HAVE_ACL
3485 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003486 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003487 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003489 if (p == NULL)
3490 return;
3491
3492 wn = enc_to_utf16(fname, NULL);
3493 if (wn == NULL)
3494 return;
3495
3496 // Set security flags
3497 if (p->pSidOwner)
3498 sec_info |= OWNER_SECURITY_INFORMATION;
3499 if (p->pSidGroup)
3500 sec_info |= GROUP_SECURITY_INFORMATION;
3501 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003502 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003503 sec_info |= DACL_SECURITY_INFORMATION;
3504 // Do not inherit its parent's DACL.
3505 // If the DACL is inherited, Cygwin permissions would be changed.
3506 if (!is_acl_inherited(p->pDacl))
3507 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003508 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003509 if (p->pSacl)
3510 sec_info |= SACL_SECURITY_INFORMATION;
3511
3512 (void)SetNamedSecurityInfoW(
3513 wn, // Abstract filename
3514 SE_FILE_OBJECT, // File Object
3515 sec_info,
3516 p->pSidOwner, // Ownership information.
3517 p->pSidGroup, // Group membership.
3518 p->pDacl, // Discretionary information.
3519 p->pSacl // For auditing purposes.
3520 );
3521 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523}
3524
3525 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003526mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528#ifdef HAVE_ACL
3529 struct my_acl *p = (struct my_acl *)acl;
3530
3531 if (p != NULL)
3532 {
3533 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3534 vim_free(p);
3535 }
3536#endif
3537}
3538
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003539#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541/*
3542 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3543 */
3544 static BOOL WINAPI
3545handler_routine(
3546 DWORD dwCtrlType)
3547{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003548 INPUT_RECORD ir;
3549 DWORD out;
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 switch (dwCtrlType)
3552 {
3553 case CTRL_C_EVENT:
3554 if (ctrl_c_interrupts)
3555 g_fCtrlCPressed = TRUE;
3556 return TRUE;
3557
3558 case CTRL_BREAK_EVENT:
3559 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003560 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003561 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003562 ir.EventType = KEY_EVENT;
3563 ir.Event.KeyEvent.bKeyDown = TRUE;
3564 ir.Event.KeyEvent.wRepeatCount = 1;
3565 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3566 ir.Event.KeyEvent.wVirtualScanCode = 0;
3567 ir.Event.KeyEvent.dwControlKeyState = 0;
3568 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3569 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return TRUE;
3571
Bram Moolenaar0f873732019-12-05 20:28:46 +01003572 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 case CTRL_CLOSE_EVENT:
3574 case CTRL_LOGOFF_EVENT:
3575 case CTRL_SHUTDOWN_EVENT:
3576 windgoto((int)Rows - 1, 0);
3577 g_fForceExit = TRUE;
3578
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003579 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 (dwCtrlType == CTRL_CLOSE_EVENT
3581 ? _("close")
3582 : dwCtrlType == CTRL_LOGOFF_EVENT
3583 ? _("logoff")
3584 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003585# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar0f873732019-12-05 20:28:46 +01003589 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar0f873732019-12-05 20:28:46 +01003591 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 default:
3594 return FALSE;
3595 }
3596}
3597
3598
3599/*
3600 * set the tty in (raw) ? "raw" : "cooked" mode
3601 */
3602 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003603mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 DWORD cmodein;
3606 DWORD cmodeout;
3607 BOOL bEnableHandler;
3608
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003609# ifdef VIMDLL
3610 if (gui.in_use)
3611 return;
3612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 GetConsoleMode(g_hConIn, &cmodein);
3614 GetConsoleMode(g_hConOut, &cmodeout);
3615 if (tmode == TMODE_RAW)
3616 {
3617 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3618 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (g_fMouseActive)
3620 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003621 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003622# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003623 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3624 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003625 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003626# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003627 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003628# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003629 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 bEnableHandler = TRUE;
3631 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003632 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3635 ENABLE_ECHO_INPUT);
3636 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3637 bEnableHandler = FALSE;
3638 }
3639 SetConsoleMode(g_hConIn, cmodein);
3640 SetConsoleMode(g_hConOut, cmodeout);
3641 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3642
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003643# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (fdDump)
3645 {
3646 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3647 tmode == TMODE_RAW ? "raw" :
3648 tmode == TMODE_COOK ? "cooked" : "normal",
3649 cmodein, cmodeout);
3650 fflush(fdDump);
3651 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653}
3654
3655
3656/*
3657 * Get the size of the current window in `Rows' and `Columns'
3658 * Return OK when size could be determined, FAIL otherwise.
3659 */
3660 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003661mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 CONSOLE_SCREEN_BUFFER_INFO csbi;
3664
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003665# ifdef VIMDLL
3666 if (gui.in_use)
3667 return OK;
3668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3670 {
3671 /*
3672 * For some reason, we are trying to get the screen dimensions
3673 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3674 * variables are really intended to mean the size of Vim screen
3675 * while in termcap mode.
3676 */
3677 Rows = g_cbTermcap.Info.dwSize.Y;
3678 Columns = g_cbTermcap.Info.dwSize.X;
3679 }
3680 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3681 {
3682 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3683 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3684 }
3685 else
3686 {
3687 Rows = 25;
3688 Columns = 80;
3689 }
3690 return OK;
3691}
3692
3693/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003694 * Resize console buffer to 'COORD'
3695 */
3696 static void
3697ResizeConBuf(
3698 HANDLE hConsole,
3699 COORD coordScreen)
3700{
3701 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3702 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003703# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003704 if (fdDump)
3705 {
3706 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3707 GetLastError());
3708 fflush(fdDump);
3709 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003710# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003711 }
3712}
3713
3714/*
3715 * Resize console window size to 'srWindowRect'
3716 */
3717 static void
3718ResizeWindow(
3719 HANDLE hConsole,
3720 SMALL_RECT srWindowRect)
3721{
3722 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3723 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003724# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003725 if (fdDump)
3726 {
3727 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3728 GetLastError());
3729 fflush(fdDump);
3730 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003731# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003732 }
3733}
3734
3735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Set a console window to `xSize' * `ySize'
3737 */
3738 static void
3739ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003740 HANDLE hConsole,
3741 int xSize,
3742 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003744 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3745 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003747 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003748 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003750# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (fdDump)
3752 {
3753 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3754 fflush(fdDump);
3755 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar0f873732019-12-05 20:28:46 +01003758 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 coordScreen = GetLargestConsoleWindowSize(hConsole);
3760
Bram Moolenaar0f873732019-12-05 20:28:46 +01003761 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3763 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3764 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3765
3766 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3767 {
3768 int sx, sy;
3769
3770 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3771 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3772 if (sy < ySize || sx < xSize)
3773 {
3774 /*
3775 * Increasing number of lines/columns, do buffer first.
3776 * Use the maximal size in x and y direction.
3777 */
3778 if (sy < ySize)
3779 coordScreen.Y = ySize;
3780 else
3781 coordScreen.Y = sy;
3782 if (sx < xSize)
3783 coordScreen.X = xSize;
3784 else
3785 coordScreen.X = sx;
3786 SetConsoleScreenBufferSize(hConsole, coordScreen);
3787 }
3788 }
3789
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003790 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 coordScreen.X = xSize;
3792 coordScreen.Y = ySize;
3793
Bram Moolenaar2551c032018-08-23 22:38:31 +02003794 // In the new console call API, only the first time in reverse order
3795 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003797 ResizeWindow(hConsole, srWindowRect);
3798 ResizeConBuf(hConsole, coordScreen);
3799 }
3800 else
3801 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003802 // Workaround for a Windows 10 bug
3803 cursor.X = srWindowRect.Left;
3804 cursor.Y = srWindowRect.Top;
3805 SetConsoleCursorPosition(hConsole, cursor);
3806
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003807 ResizeConBuf(hConsole, coordScreen);
3808 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003809 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811}
3812
3813
3814/*
3815 * Set the console window to `Rows' * `Columns'
3816 */
3817 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003818mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 COORD coordScreen;
3821
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003822# ifdef VIMDLL
3823 if (gui.in_use)
3824 return;
3825# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003826 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (suppress_winsize != 0)
3828 {
3829 suppress_winsize = 2;
3830 return;
3831 }
3832
3833 if (term_console)
3834 {
3835 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3836
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (Rows > coordScreen.Y)
3839 Rows = coordScreen.Y;
3840 if (Columns > coordScreen.X)
3841 Columns = coordScreen.X;
3842
3843 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3844 }
3845}
3846
3847/*
3848 * Rows and/or Columns has changed.
3849 */
3850 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003851mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003853# ifdef VIMDLL
3854 if (gui.in_use)
3855 return;
3856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3858}
3859
3860
3861/*
3862 * Called when started up, to set the winsize that was delayed.
3863 */
3864 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003865mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 if (suppress_winsize == 2)
3868 {
3869 suppress_winsize = 0;
3870 mch_set_shellsize();
3871 shell_resized();
3872 }
3873 suppress_winsize = 0;
3874}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003875#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003877 static BOOL
3878vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003879 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003880 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003881 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003882 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003883 PROCESS_INFORMATION *pi,
3884 LPVOID *env,
3885 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003886{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003887 BOOL ret = FALSE;
3888 WCHAR *wcmd, *wcwd = NULL;
3889
3890 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3891 if (wcmd == NULL)
3892 return FALSE;
3893 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003894 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003895 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3896 if (wcwd == NULL)
3897 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003898 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003899
3900 ret = CreateProcessW(
3901 NULL, // Executable name
3902 wcmd, // Command to execute
3903 NULL, // Process security attributes
3904 NULL, // Thread security attributes
3905 inherit_handles, // Inherit handles
3906 flags, // Creation flags
3907 env, // Environment
3908 wcwd, // Current directory
3909 (LPSTARTUPINFOW)si, // Startup information
3910 pi); // Process information
3911theend:
3912 vim_free(wcmd);
3913 vim_free(wcwd);
3914 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003915}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003918 static HINSTANCE
3919vim_shell_execute(
3920 char *cmd,
3921 INT n_show_cmd)
3922{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003923 HINSTANCE ret;
3924 WCHAR *wcmd;
3925
3926 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3927 if (wcmd == NULL)
3928 return (HINSTANCE) 0;
3929
3930 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3931 vim_free(wcmd);
3932 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003933}
3934
3935
Bram Moolenaar4f974752019-02-17 17:44:42 +01003936#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938/*
3939 * Specialised version of system() for Win32 GUI mode.
3940 * This version proceeds as follows:
3941 * 1. Create a console window for use by the subprocess
3942 * 2. Run the subprocess (it gets the allocated console by default)
3943 * 3. Wait for the subprocess to terminate and get its exit code
3944 * 4. Prompt the user to press a key to close the console window
3945 */
3946 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003947mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 STARTUPINFO si;
3950 PROCESS_INFORMATION pi;
3951 DWORD ret = 0;
3952 HWND hwnd = GetFocus();
3953
3954 si.cb = sizeof(si);
3955 si.lpReserved = NULL;
3956 si.lpDesktop = NULL;
3957 si.lpTitle = NULL;
3958 si.dwFlags = STARTF_USESHOWWINDOW;
3959 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003960 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003961 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003963 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003964 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 else
3966 si.wShowWindow = SW_SHOWNORMAL;
3967 si.cbReserved2 = 0;
3968 si.lpReserved2 = NULL;
3969
Bram Moolenaar0f873732019-12-05 20:28:46 +01003970 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003971 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003972 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3973 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar0f873732019-12-05 20:28:46 +01003975 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003977# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int delay = 1;
3979
Bram Moolenaar0f873732019-12-05 20:28:46 +01003980 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 for (;;)
3982 {
3983 MSG msg;
3984
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003985 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003988 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003989 delay = 1;
3990 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3993 break;
3994
Bram Moolenaar0f873732019-12-05 20:28:46 +01003995 // We start waiting for a very short time and then increase it, so
3996 // that we respond quickly when the process is quick, and don't
3997 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (delay < 50)
3999 delay += 10;
4000 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004001# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar0f873732019-12-05 20:28:46 +01004005 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 GetExitCodeProcess(pi.hProcess, &ret);
4007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 CloseHandle(pi.hThread);
4011 CloseHandle(pi.hProcess);
4012
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4015
4016 return ret;
4017}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004018
4019/*
4020 * Thread launched by the gui to send the current buffer data to the
4021 * process. This way avoid to hang up vim totally if the children
4022 * process take a long time to process the lines.
4023 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004024 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004025sub_process_writer(LPVOID param)
4026{
4027 HANDLE g_hChildStd_IN_Wr = param;
4028 linenr_T lnum = curbuf->b_op_start.lnum;
4029 DWORD len = 0;
4030 DWORD l;
4031 char_u *lp = ml_get(lnum);
4032 char_u *s;
4033 int written = 0;
4034
4035 for (;;)
4036 {
4037 l = (DWORD)STRLEN(lp + written);
4038 if (l == 0)
4039 len = 0;
4040 else if (lp[written] == NL)
4041 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004042 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004043 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4044 }
4045 else
4046 {
4047 s = vim_strchr(lp + written, NL);
4048 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4049 s == NULL ? l : (DWORD)(s - (lp + written)),
4050 &len, NULL);
4051 }
4052 if (len == (int)l)
4053 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004054 // Finished a line, add a NL, unless this line should not have
4055 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004056 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004057 || (!curbuf->b_p_bin
4058 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004059 || (lnum != curbuf->b_no_eol_lnum
4060 && (lnum != curbuf->b_ml.ml_line_count
4061 || curbuf->b_p_eol)))
4062 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004063 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4064 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004065 }
4066
4067 ++lnum;
4068 if (lnum > curbuf->b_op_end.lnum)
4069 break;
4070
4071 lp = ml_get(lnum);
4072 written = 0;
4073 }
4074 else if (len > 0)
4075 written += len;
4076 }
4077
Bram Moolenaar0f873732019-12-05 20:28:46 +01004078 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004079 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004080 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004081}
4082
4083
Bram Moolenaar0f873732019-12-05 20:28:46 +01004084# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004085
4086/*
4087 * This function read from the children's stdout and write the
4088 * data on screen or in the buffer accordingly.
4089 */
4090 static void
4091dump_pipe(int options,
4092 HANDLE g_hChildStd_OUT_Rd,
4093 garray_T *ga,
4094 char_u buffer[],
4095 DWORD *buffer_off)
4096{
4097 DWORD availableBytes = 0;
4098 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004099 int ret;
4100 DWORD len;
4101 DWORD toRead;
4102 int repeatCount;
4103
Bram Moolenaar0f873732019-12-05 20:28:46 +01004104 // we query the pipe to see if there is any data to read
4105 // to avoid to perform a blocking read
4106 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4107 NULL, // optional buffer
4108 0, // buffer size
4109 NULL, // number of read bytes
4110 &availableBytes, // available bytes total
4111 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004112
4113 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004115 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004116 {
4117 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004118 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004119 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004120 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004121
Bram Moolenaar0f873732019-12-05 20:28:46 +01004122 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004123 if (len == 0)
4124 break;
4125
4126 availableBytes -= len;
4127
4128 if (options & SHELL_READ)
4129 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004130 // Do NUL -> NL translation, append NL separated
4131 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004132 for (i = 0; i < len; ++i)
4133 {
4134 if (buffer[i] == NL)
4135 append_ga_line(ga);
4136 else if (buffer[i] == NUL)
4137 ga_append(ga, NL);
4138 else
4139 ga_append(ga, buffer[i]);
4140 }
4141 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004142 else if (has_mbyte)
4143 {
4144 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004145 int c;
4146 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004147
4148 len += *buffer_off;
4149 buffer[len] = NUL;
4150
Bram Moolenaar0f873732019-12-05 20:28:46 +01004151 // Check if the last character in buffer[] is
4152 // incomplete, keep these bytes for the next
4153 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004154 for (p = buffer; p < buffer + len; p += l)
4155 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004156 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004157 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004158 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004159 else if (MB_BYTE2LEN(*p) != l)
4160 break;
4161 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004162 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004163 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004164 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004165 if (len >= 12)
4166 ++p;
4167 else
4168 {
4169 *buffer_off = len;
4170 return;
4171 }
4172 }
4173 c = *p;
4174 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004176 if (p < buffer + len)
4177 {
4178 *p = c;
4179 *buffer_off = (DWORD)((buffer + len) - p);
4180 mch_memmove(buffer, p, *buffer_off);
4181 return;
4182 }
4183 *buffer_off = 0;
4184 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004185 else
4186 {
4187 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004188 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004189 }
4190
4191 windgoto(msg_row, msg_col);
4192 cursor_on();
4193 out_flush();
4194 }
4195}
4196
4197/*
4198 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4199 * for communication and doesn't open any new window.
4200 */
4201 static int
4202mch_system_piped(char *cmd, int options)
4203{
4204 STARTUPINFO si;
4205 PROCESS_INFORMATION pi;
4206 DWORD ret = 0;
4207
4208 HANDLE g_hChildStd_IN_Rd = NULL;
4209 HANDLE g_hChildStd_IN_Wr = NULL;
4210 HANDLE g_hChildStd_OUT_Rd = NULL;
4211 HANDLE g_hChildStd_OUT_Wr = NULL;
4212
Bram Moolenaar0f873732019-12-05 20:28:46 +01004213 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004214 DWORD len;
4215
Bram Moolenaar0f873732019-12-05 20:28:46 +01004216 // buffer used to receive keys
4217 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4218 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004219
4220 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004221 int noread_cnt = 0;
4222 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004223 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004224 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004225 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004226
4227 SECURITY_ATTRIBUTES saAttr;
4228
Bram Moolenaar0f873732019-12-05 20:28:46 +01004229 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004230 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4231 saAttr.bInheritHandle = TRUE;
4232 saAttr.lpSecurityDescriptor = NULL;
4233
4234 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004235 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004236 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004237 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004238 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004239 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004240 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004241 {
4242 CloseHandle(g_hChildStd_IN_Rd);
4243 CloseHandle(g_hChildStd_IN_Wr);
4244 CloseHandle(g_hChildStd_OUT_Rd);
4245 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004246 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004247 }
4248
4249 si.cb = sizeof(si);
4250 si.lpReserved = NULL;
4251 si.lpDesktop = NULL;
4252 si.lpTitle = NULL;
4253 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4254
Bram Moolenaar0f873732019-12-05 20:28:46 +01004255 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004256 si.hStdError = g_hChildStd_OUT_Wr;
4257 si.hStdOutput = g_hChildStd_OUT_Wr;
4258 si.hStdInput = g_hChildStd_IN_Rd;
4259 si.wShowWindow = SW_HIDE;
4260 si.cbReserved2 = 0;
4261 si.lpReserved2 = NULL;
4262
4263 if (options & SHELL_READ)
4264 ga_init2(&ga, 1, BUFLEN);
4265
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004266 if (cmd != NULL)
4267 {
4268 p = (char *)vim_strsave((char_u *)cmd);
4269 if (p != NULL)
4270 unescape_shellxquote((char_u *)p, p_sxe);
4271 else
4272 p = cmd;
4273 }
4274
Bram Moolenaar0f873732019-12-05 20:28:46 +01004275 // Now, run the command.
4276 // About "Inherit handles" being TRUE: this command can be litigious,
4277 // handle inheritance was deactivated for pending temp file, but, if we
4278 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004279 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4280 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004281
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004282 if (p != cmd)
4283 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004284
Bram Moolenaar0f873732019-12-05 20:28:46 +01004285 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004286 CloseHandle(g_hChildStd_IN_Rd);
4287 CloseHandle(g_hChildStd_OUT_Wr);
4288
4289 if (options & SHELL_WRITE)
4290 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004291 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004292 _beginthreadex(NULL, // security attributes
4293 0, // default stack size
4294 sub_process_writer, // function to be executed
4295 g_hChildStd_IN_Wr, // parameter
4296 0, // creation flag, start immediately
4297 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004298 CloseHandle(thread);
4299 g_hChildStd_IN_Wr = NULL;
4300 }
4301
Bram Moolenaar0f873732019-12-05 20:28:46 +01004302 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004303 for (;;)
4304 {
4305 MSG msg;
4306
Bram Moolenaar175d0702013-12-11 18:36:33 +01004307 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004308 {
4309 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004310 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004311 }
4312
Bram Moolenaar0f873732019-12-05 20:28:46 +01004313 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004314 if ((options & (SHELL_READ|SHELL_WRITE))
4315# ifdef FEAT_GUI
4316 || gui.in_use
4317# endif
4318 )
4319 {
4320 len = 0;
4321 if (!(options & SHELL_EXPAND)
4322 && ((options &
4323 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4324 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4325# ifdef FEAT_GUI
4326 || gui.in_use
4327# endif
4328 )
4329 && (ta_len > 0 || noread_cnt > 4))
4330 {
4331 if (ta_len == 0)
4332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004333 // Get extra characters when we don't have any. Reset the
4334 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004335 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004336 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4337 }
4338 if (ta_len > 0 || len > 0)
4339 {
4340 /*
4341 * For pipes: Check for CTRL-C: send interrupt signal to
4342 * child. Check for CTRL-D: EOF, close pipe to child.
4343 */
4344 if (len == 1 && cmd != NULL)
4345 {
4346 if (ta_buf[ta_len] == Ctrl_C)
4347 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004348 // Learn what exit code is expected, for
4349 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004350 TerminateProcess(pi.hProcess, 9);
4351 }
4352 if (ta_buf[ta_len] == Ctrl_D)
4353 {
4354 CloseHandle(g_hChildStd_IN_Wr);
4355 g_hChildStd_IN_Wr = NULL;
4356 }
4357 }
4358
Bram Moolenaarf4140482020-02-15 23:06:45 +01004359 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004360
4361 /*
4362 * For pipes: echo the typed characters. For a pty this
4363 * does not seem to work.
4364 */
4365 for (i = ta_len; i < ta_len + len; ++i)
4366 {
4367 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4368 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004369 else if (has_mbyte)
4370 {
4371 int l = (*mb_ptr2len)(ta_buf + i);
4372
4373 msg_outtrans_len(ta_buf + i, l);
4374 i += l - 1;
4375 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004376 else
4377 msg_outtrans_len(ta_buf + i, 1);
4378 }
4379 windgoto(msg_row, msg_col);
4380 out_flush();
4381
4382 ta_len += len;
4383
4384 /*
4385 * Write the characters to the child, unless EOF has been
4386 * typed for pipes. Write one character at a time, to
4387 * avoid losing too much typeahead. When writing buffer
4388 * lines, drop the typed characters (only check for
4389 * CTRL-C).
4390 */
4391 if (options & SHELL_WRITE)
4392 ta_len = 0;
4393 else if (g_hChildStd_IN_Wr != NULL)
4394 {
4395 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4396 1, &len, NULL);
4397 // if we are typing in, we want to keep things reactive
4398 delay = 1;
4399 if (len > 0)
4400 {
4401 ta_len -= len;
4402 mch_memmove(ta_buf, ta_buf + len, ta_len);
4403 }
4404 }
4405 }
4406 }
4407 }
4408
4409 if (ta_len)
4410 ui_inchar_undo(ta_buf, ta_len);
4411
4412 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4413 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004414 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004415 break;
4416 }
4417
4418 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004419 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004420
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 // We start waiting for a very short time and then increase it, so
4422 // that we respond quickly when the process is quick, and don't
4423 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004424 if (delay < 50)
4425 delay += 10;
4426 }
4427
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004429 CloseHandle(g_hChildStd_OUT_Rd);
4430 if (g_hChildStd_IN_Wr != NULL)
4431 CloseHandle(g_hChildStd_IN_Wr);
4432
4433 WaitForSingleObject(pi.hProcess, INFINITE);
4434
Bram Moolenaar0f873732019-12-05 20:28:46 +01004435 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004436 GetExitCodeProcess(pi.hProcess, &ret);
4437
4438 if (options & SHELL_READ)
4439 {
4440 if (ga.ga_len > 0)
4441 {
4442 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004443 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004444 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4445 }
4446 else
4447 curbuf->b_no_eol_lnum = 0;
4448 ga_clear(&ga);
4449 }
4450
Bram Moolenaar0f873732019-12-05 20:28:46 +01004451 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004452 CloseHandle(pi.hThread);
4453 CloseHandle(pi.hProcess);
4454
4455 return ret;
4456}
4457
4458 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004459mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004460{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004461 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004462 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004463 return mch_system_piped(cmd, options);
4464 else
4465 return mch_system_classic(cmd, options);
4466}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004469#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004470 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004471mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004472{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004473 int ret;
4474 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004475 char_u *buf;
4476 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004477
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004478 // If the command starts and ends with double quotes, enclose the command
4479 // in parentheses.
4480 len = STRLEN(cmd);
4481 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4482 {
4483 len += 3;
4484 buf = alloc(len);
4485 if (buf == NULL)
4486 return -1;
4487 vim_snprintf((char *)buf, len, "(%s)", cmd);
4488 wcmd = enc_to_utf16(buf, NULL);
4489 free(buf);
4490 }
4491 else
4492 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4493
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004494 if (wcmd == NULL)
4495 return -1;
4496
4497 ret = _wsystem(wcmd);
4498 vim_free(wcmd);
4499 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502#endif
4503
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004504 static int
4505mch_system(char *cmd, int options)
4506{
4507#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004508 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004509 return mch_system_g(cmd, options);
4510 else
4511 return mch_system_c(cmd, options);
4512#elif defined(FEAT_GUI_MSWIN)
4513 return mch_system_g(cmd, options);
4514#else
4515 return mch_system_c(cmd, options);
4516#endif
4517}
4518
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004519#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4520/*
4521 * Use a terminal window to run a shell command in.
4522 */
4523 static int
4524mch_call_shell_terminal(
4525 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004526 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004527{
4528 jobopt_T opt;
4529 char_u *newcmd = NULL;
4530 typval_T argvar[2];
4531 long_u cmdlen;
4532 int retval = -1;
4533 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004534 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004535 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004537
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004538 if (cmd == NULL)
4539 cmdlen = STRLEN(p_sh) + 1;
4540 else
4541 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004542 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004543 if (newcmd == NULL)
4544 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004545 if (cmd == NULL)
4546 {
4547 STRCPY(newcmd, p_sh);
4548 ch_log(NULL, "starting terminal to run a shell");
4549 }
4550 else
4551 {
4552 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4553 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4554 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004555
4556 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004557
4558 argvar[0].v_type = VAR_STRING;
4559 argvar[0].vval.v_string = newcmd;
4560 argvar[1].v_type = VAR_UNKNOWN;
4561 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004562 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004563 {
4564 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004565 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004566 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004567
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004568 job = term_getjob(buf->b_term);
4569 ++job->jv_refcount;
4570
Bram Moolenaar0f873732019-12-05 20:28:46 +01004571 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004572 aucmd_prepbuf(&aco, buf);
4573
4574 clear_oparg(&oa);
4575 while (term_use_loop())
4576 {
4577 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4578 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004579 // If terminal_loop() returns OK we got a key that is handled
4580 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004581 if (terminal_loop(TRUE) == OK)
4582 normal_cmd(&oa, TRUE);
4583 }
4584 else
4585 normal_cmd(&oa, TRUE);
4586 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004587 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004588 ch_log(NULL, "system command finished");
4589
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004590 job_unref(job);
4591
Bram Moolenaar0f873732019-12-05 20:28:46 +01004592 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004593 aucmd_restbuf(&aco);
4594
4595 wait_return(TRUE);
4596 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4597
4598 vim_free(newcmd);
4599 return retval;
4600}
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603/*
4604 * Either execute a command by calling the shell or start a new shell
4605 */
4606 int
4607mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004608 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004609 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 int x = 0;
4612 int tmode = cur_tmode;
4613#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004614 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004615
Bram Moolenaar0f873732019-12-05 20:28:46 +01004616 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004617 if (GetConsoleTitleW(szShellTitle,
4618 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004619 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004620 if (cmd == NULL)
4621 wcscat(szShellTitle, L" :sh");
4622 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004623 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004624 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004625
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004626 if (wn != NULL)
4627 {
4628 wcscat(szShellTitle, L" - !");
4629 if ((wcslen(szShellTitle) + wcslen(wn) <
4630 sizeof(szShellTitle)/sizeof(WCHAR)))
4631 wcscat(szShellTitle, wn);
4632 SetConsoleTitleW(szShellTitle);
4633 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004634 }
4635 }
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
4639 out_flush();
4640
4641#ifdef MCH_WRITE_DUMP
4642 if (fdDump)
4643 {
4644 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4645 fflush(fdDump);
4646 }
4647#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004648#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004649 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004650 if (
4651# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004652 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004653# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004654 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004655 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4656 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004657 char_u *cmdbase = cmd;
4658
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004659 if (cmdbase != NULL)
4660 // Skip a leading quote and (.
4661 while (*cmdbase == '"' || *cmdbase == '(')
4662 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004663
4664 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004665 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4666 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004667 {
4668 // Use a terminal window to run the command in.
4669 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004670# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004671 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004672# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004673 return x;
4674 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004675 }
4676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /*
4679 * Catch all deadly signals while running the external command, because a
4680 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4681 */
4682 signal(SIGINT, SIG_IGN);
4683#if defined(__GNUC__) && !defined(__MINGW32__)
4684 signal(SIGKILL, SIG_IGN);
4685#else
4686 signal(SIGBREAK, SIG_IGN);
4687#endif
4688 signal(SIGILL, SIG_IGN);
4689 signal(SIGFPE, SIG_IGN);
4690 signal(SIGSEGV, SIG_IGN);
4691 signal(SIGTERM, SIG_IGN);
4692 signal(SIGABRT, SIG_IGN);
4693
4694 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004695 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 if (cmd == NULL)
4698 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004699 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701 else
4702 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004704 char_u *newcmd = NULL;
4705 char_u *cmdbase = cmd;
4706 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004707
Bram Moolenaar0f873732019-12-05 20:28:46 +01004708 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004709 if (*cmdbase == '"' )
4710 ++cmdbase;
4711 if (*cmdbase == '(')
4712 ++cmdbase;
4713
Bram Moolenaar1c465442017-03-12 20:10:05 +01004714 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004715 {
4716 STARTUPINFO si;
4717 PROCESS_INFORMATION pi;
4718 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004719 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004720 char_u *p;
4721
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004722 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004723 si.cb = sizeof(si);
4724 si.lpReserved = NULL;
4725 si.lpDesktop = NULL;
4726 si.lpTitle = NULL;
4727 si.dwFlags = 0;
4728 si.cbReserved2 = 0;
4729 si.lpReserved2 = NULL;
4730
4731 cmdbase = skipwhite(cmdbase + 5);
4732 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004733 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004734 {
4735 cmdbase = skipwhite(cmdbase + 4);
4736 si.dwFlags = STARTF_USESHOWWINDOW;
4737 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004738 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004739 }
4740 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004741 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004742 {
4743 cmdbase = skipwhite(cmdbase + 2);
4744 flags = CREATE_NO_WINDOW;
4745 si.dwFlags = STARTF_USESTDHANDLES;
4746 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004747 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004748 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004749 NULL, // Security att.
4750 OPEN_EXISTING, // Open flags
4751 FILE_ATTRIBUTE_NORMAL, // File att.
4752 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004753 si.hStdOutput = si.hStdInput;
4754 si.hStdError = si.hStdInput;
4755 }
4756
Bram Moolenaar0f873732019-12-05 20:28:46 +01004757 // Remove a trailing ", ) and )" if they have a match
4758 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004759 if (cmdbase > cmd)
4760 {
4761 p = cmdbase + STRLEN(cmdbase);
4762 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4763 *--p = NUL;
4764 if (p > cmdbase && p[-1] == ')'
4765 && (*cmd =='(' || cmd[1] == '('))
4766 *--p = NUL;
4767 }
4768
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004769 newcmd = cmdbase;
4770 unescape_shellxquote(cmdbase, p_sxe);
4771
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004772 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004773 * If creating new console, arguments are passed to the
4774 * 'cmd.exe' as-is. If it's not, arguments are not treated
4775 * correctly for current 'cmd.exe'. So unescape characters in
4776 * shellxescape except '|' for avoiding to be treated as
4777 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004778 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004779 if (flags != CREATE_NEW_CONSOLE)
4780 {
4781 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004782 char_u *cmd_shell = mch_getenv("COMSPEC");
4783
4784 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004785 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004786
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004787 subcmd = vim_strsave_escaped_ext(cmdbase,
4788 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004789 if (subcmd != NULL)
4790 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004791 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004792 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004793 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004794 if (newcmd != NULL)
4795 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004796 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004797 else
4798 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004799 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004800 }
4801 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004802
4803 /*
4804 * Now, start the command as a process, so that it doesn't
4805 * inherit our handles which causes unpleasant dangling swap
4806 * files if we exit before the spawned process
4807 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004808 if (vim_create_process((char *)newcmd, FALSE, flags,
4809 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004810 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004811 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4812 > (HINSTANCE)32)
4813 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004814 else
4815 {
4816 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004817#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004818# ifdef VIMDLL
4819 if (gui.in_use)
4820# endif
4821 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004822#endif
4823 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004824
4825 if (newcmd != cmdbase)
4826 vim_free(newcmd);
4827
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004828 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004829 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004830 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004831 CloseHandle(si.hStdInput);
4832 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004833 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004834 CloseHandle(pi.hThread);
4835 CloseHandle(pi.hProcess);
4836 }
4837 else
4838 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004839 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004840#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004841 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004842 (!s_dont_use_vimrun && p_stmp ?
4843 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4844 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004846 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004847
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004848 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004849 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004851#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004852 if (
4853# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004854 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004855# endif
4856 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004858 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4859 "External commands will not pause after completion.\n"
4860 "See :help win32-vimrun for more information.");
4861 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004862 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4863 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004864
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004865 if (wmsg != NULL && wtitle != NULL)
4866 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4867 vim_free(wmsg);
4868 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 need_vimrun_warning = FALSE;
4870 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004871 if (
4872# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004873 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004874# endif
4875 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004876 // Use vimrun to execute the command. It opens a console
4877 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004878 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 vimrun_path,
4880 (msg_silent != 0 || (options & SHELL_DOOUT))
4881 ? "-s " : "",
4882 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004883 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004884# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004885 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004886# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004887 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004888 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004889 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4890 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004893 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004894 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004896 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 }
4900
4901 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004902 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar0f873732019-12-05 20:28:46 +01004904 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004906#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004907 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004908 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#endif
4910 )
4911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004912 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 msg_putchar('\n');
4914 }
4915#ifdef FEAT_TITLE
4916 resettitle();
4917#endif
4918
4919 signal(SIGINT, SIG_DFL);
4920#if defined(__GNUC__) && !defined(__MINGW32__)
4921 signal(SIGKILL, SIG_DFL);
4922#else
4923 signal(SIGBREAK, SIG_DFL);
4924#endif
4925 signal(SIGILL, SIG_DFL);
4926 signal(SIGFPE, SIG_DFL);
4927 signal(SIGSEGV, SIG_DFL);
4928 signal(SIGTERM, SIG_DFL);
4929 signal(SIGABRT, SIG_DFL);
4930
4931 return x;
4932}
4933
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004934#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004935 static HANDLE
4936job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004937 char_u *fname,
4938 DWORD dwDesiredAccess,
4939 DWORD dwShareMode,
4940 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4941 DWORD dwCreationDisposition,
4942 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004943{
4944 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004945 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004946
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004947 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004948 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004949 return INVALID_HANDLE_VALUE;
4950
4951 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4952 lpSecurityAttributes, dwCreationDisposition,
4953 dwFlagsAndAttributes, NULL);
4954 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004955 return h;
4956}
4957
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004958/*
4959 * Turn the dictionary "env" into a NUL separated list that can be used as the
4960 * environment argument of vim_create_process().
4961 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004962 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004963win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004964{
4965 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004966 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004967 LPVOID base = GetEnvironmentStringsW();
4968
Bram Moolenaar0f873732019-12-05 20:28:46 +01004969 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004970 if (ga_grow(gap, 1) == FAIL)
4971 return;
4972
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004973 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004974 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004975 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004976 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004977 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004978 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004979 typval_T *item = &dict_lookup(hi)->di_tv;
4980 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004982 --todo;
4983 if (wkey != NULL && wval != NULL)
4984 {
4985 size_t n;
4986 size_t lkey = wcslen(wkey);
4987 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004988
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004989 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4990 continue;
4991 for (n = 0; n < lkey; n++)
4992 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4993 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4994 for (n = 0; n < lval; n++)
4995 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4996 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4997 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004998 vim_free(wkey);
4999 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005000 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005001 }
5002 }
5003
Bram Moolenaar355757a2020-02-10 22:06:32 +01005004 if (base)
5005 {
5006 WCHAR *p = (WCHAR*) base;
5007
5008 // for last \0
5009 if (ga_grow(gap, 1) == FAIL)
5010 return;
5011
5012 while (*p != 0 || *(p + 1) != 0)
5013 {
5014 if (ga_grow(gap, 1) == OK)
5015 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5016 p++;
5017 }
5018 FreeEnvironmentStrings(base);
5019 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5020 }
5021
Bram Moolenaar493359e2018-06-12 20:25:52 +02005022# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005023 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005024# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005025 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005026 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005027# endif
5028# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005029 char_u *version = get_vim_var_str(VV_VERSION);
5030 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005031# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005032 // size of "VIM_SERVERNAME=" and value,
5033 // plus "VIM_TERMINAL=" and value,
5034 // plus two terminating NULs
5035 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005036# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005037 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005038# endif
5039# ifdef FEAT_TERMINAL
5040 + 13 + version_len + 2
5041# endif
5042 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005043
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005044 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005045 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005046# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005047 for (n = 0; n < 15; n++)
5048 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5049 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005050 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005051 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5052 (WCHAR)servername[n];
5053 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005054# endif
5055# ifdef FEAT_TERMINAL
5056 if (is_terminal)
5057 {
5058 for (n = 0; n < 13; n++)
5059 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5060 (WCHAR)"VIM_TERMINAL="[n];
5061 for (n = 0; n < version_len; n++)
5062 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5063 (WCHAR)version[n];
5064 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5065 }
5066# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005067 }
5068 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005069# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005070}
5071
Bram Moolenaarb091f302019-01-19 14:37:00 +01005072/*
5073 * Create a pair of pipes.
5074 * Return TRUE for success, FALSE for failure.
5075 */
5076 static BOOL
5077create_pipe_pair(HANDLE handles[2])
5078{
5079 static LONG s;
5080 char name[64];
5081 SECURITY_ATTRIBUTES sa;
5082
5083 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5084 GetCurrentProcessId(),
5085 InterlockedIncrement(&s));
5086
5087 // Create named pipe. Max size of named pipe is 65535.
5088 handles[1] = CreateNamedPipe(
5089 name,
5090 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5091 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005092 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005093
5094 if (handles[1] == INVALID_HANDLE_VALUE)
5095 return FALSE;
5096
5097 sa.nLength = sizeof(sa);
5098 sa.bInheritHandle = TRUE;
5099 sa.lpSecurityDescriptor = NULL;
5100
5101 handles[0] = CreateFile(name,
5102 FILE_GENERIC_READ,
5103 FILE_SHARE_READ, &sa,
5104 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5105
5106 if (handles[0] == INVALID_HANDLE_VALUE)
5107 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005108 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005109 return FALSE;
5110 }
5111
5112 return TRUE;
5113}
5114
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005115 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005116mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005117{
5118 STARTUPINFO si;
5119 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005120 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005121 SECURITY_ATTRIBUTES saAttr;
5122 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005123 HANDLE ifd[2];
5124 HANDLE ofd[2];
5125 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005126 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005127
5128 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5129 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5130 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5131 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5132 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5133 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5134 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5135
5136 if (use_out_for_err && use_null_for_out)
5137 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005138
5139 ifd[0] = INVALID_HANDLE_VALUE;
5140 ifd[1] = INVALID_HANDLE_VALUE;
5141 ofd[0] = INVALID_HANDLE_VALUE;
5142 ofd[1] = INVALID_HANDLE_VALUE;
5143 efd[0] = INVALID_HANDLE_VALUE;
5144 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005145 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005146
Bram Moolenaar14207f42016-10-27 21:13:10 +02005147 jo = CreateJobObject(NULL, NULL);
5148 if (jo == NULL)
5149 {
5150 job->jv_status = JOB_FAILED;
5151 goto failed;
5152 }
5153
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005154 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005155 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005156
Bram Moolenaar76467df2016-02-12 19:30:26 +01005157 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005158 ZeroMemory(&si, sizeof(si));
5159 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005160 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005161 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005162
Bram Moolenaard8070362016-02-15 21:56:54 +01005163 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5164 saAttr.bInheritHandle = TRUE;
5165 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005166
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005167 if (use_file_for_in)
5168 {
5169 char_u *fname = options->jo_io_name[PART_IN];
5170
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005171 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5172 FILE_SHARE_READ | FILE_SHARE_WRITE,
5173 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5174 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005176 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005177 goto failed;
5178 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005179 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005180 else if (!use_null_for_in
5181 && (!create_pipe_pair(ifd)
5182 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005183 goto failed;
5184
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005185 if (use_file_for_out)
5186 {
5187 char_u *fname = options->jo_io_name[PART_OUT];
5188
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005189 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5190 FILE_SHARE_READ | FILE_SHARE_WRITE,
5191 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5192 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005194 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005195 goto failed;
5196 }
5197 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005198 else if (!use_null_for_out &&
5199 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005200 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005201 goto failed;
5202
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005203 if (use_file_for_err)
5204 {
5205 char_u *fname = options->jo_io_name[PART_ERR];
5206
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005207 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5208 FILE_SHARE_READ | FILE_SHARE_WRITE,
5209 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5210 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005212 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005213 goto failed;
5214 }
5215 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005216 else if (!use_out_for_err && !use_null_for_err &&
5217 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005218 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005219 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005220
Bram Moolenaard8070362016-02-15 21:56:54 +01005221 si.dwFlags |= STARTF_USESTDHANDLES;
5222 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005223 si.hStdOutput = ofd[1];
5224 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5225
5226 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5227 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005228 if (options->jo_set & JO_CHANNEL)
5229 {
5230 channel = options->jo_channel;
5231 if (channel != NULL)
5232 ++channel->ch_refcount;
5233 }
5234 else
5235 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005236 if (channel == NULL)
5237 goto failed;
5238 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005239
5240 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005241 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005242 CREATE_DEFAULT_ERROR_MODE |
5243 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005244 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005245 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005246 &si, &pi,
5247 ga.ga_data,
5248 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005249 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005250 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005251 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005252 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005253 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005254
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005255 ga_clear(&ga);
5256
Bram Moolenaar14207f42016-10-27 21:13:10 +02005257 if (!AssignProcessToJobObject(jo, pi.hProcess))
5258 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005259 // if failing, switch the way to terminate
5260 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005261 CloseHandle(jo);
5262 jo = NULL;
5263 }
5264 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005265 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005266 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005267 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005268 job->jv_status = JOB_STARTED;
5269
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005270 CloseHandle(ifd[0]);
5271 CloseHandle(ofd[1]);
5272 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005273 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005274
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005275 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005276 if (channel != NULL)
5277 {
5278 channel_set_pipes(channel,
5279 use_file_for_in || use_null_for_in
5280 ? INVALID_FD : (sock_T)ifd[1],
5281 use_file_for_out || use_null_for_out
5282 ? INVALID_FD : (sock_T)ofd[0],
5283 use_out_for_err || use_file_for_err || use_null_for_err
5284 ? INVALID_FD : (sock_T)efd[0]);
5285 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005286 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005287 return;
5288
5289failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005290 CloseHandle(ifd[0]);
5291 CloseHandle(ofd[0]);
5292 CloseHandle(efd[0]);
5293 CloseHandle(ifd[1]);
5294 CloseHandle(ofd[1]);
5295 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005296 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005297 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005298}
5299
5300 char *
5301mch_job_status(job_T *job)
5302{
5303 DWORD dwExitCode = 0;
5304
Bram Moolenaar76467df2016-02-12 19:30:26 +01005305 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5306 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005307 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005308 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005309 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005310 {
5311 ch_log(job->jv_channel, "Job ended");
5312 job->jv_status = JOB_ENDED;
5313 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005314 return "dead";
5315 }
5316 return "run";
5317}
5318
Bram Moolenaar97792de2016-10-15 18:36:49 +02005319 job_T *
5320mch_detect_ended_job(job_T *job_list)
5321{
5322 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5323 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5324 job_T *job = job_list;
5325
5326 while (job != NULL)
5327 {
5328 DWORD n;
5329 DWORD result;
5330
5331 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5332 && job != NULL; job = job->jv_next)
5333 {
5334 if (job->jv_status == JOB_STARTED)
5335 {
5336 jobHandles[n] = job->jv_proc_info.hProcess;
5337 jobArray[n] = job;
5338 ++n;
5339 }
5340 }
5341 if (n == 0)
5342 continue;
5343 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5344 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5345 {
5346 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5347
5348 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5349 return wait_job;
5350 }
5351 }
5352 return NULL;
5353}
5354
Bram Moolenaarfb630902016-10-29 14:55:00 +02005355 static BOOL
5356terminate_all(HANDLE process, int code)
5357{
5358 PROCESSENTRY32 pe;
5359 HANDLE h = INVALID_HANDLE_VALUE;
5360 DWORD pid = GetProcessId(process);
5361
5362 if (pid != 0)
5363 {
5364 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5365 if (h != INVALID_HANDLE_VALUE)
5366 {
5367 pe.dwSize = sizeof(PROCESSENTRY32);
5368 if (!Process32First(h, &pe))
5369 goto theend;
5370
5371 do
5372 {
5373 if (pe.th32ParentProcessID == pid)
5374 {
5375 HANDLE ph = OpenProcess(
5376 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5377 if (ph != NULL)
5378 {
5379 terminate_all(ph, code);
5380 CloseHandle(ph);
5381 }
5382 }
5383 } while (Process32Next(h, &pe));
5384
5385 CloseHandle(h);
5386 }
5387 }
5388
5389theend:
5390 return TerminateProcess(process, code);
5391}
5392
5393/*
5394 * Send a (deadly) signal to "job".
5395 * Return FAIL if it didn't work.
5396 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005397 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005398mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005399{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005400 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005401
Bram Moolenaar923d9262016-02-25 20:56:01 +01005402 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005403 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005404 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005405 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005406 {
5407 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5408 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005409 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005410 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005411 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005412 }
5413
5414 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5415 return FAIL;
5416 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005417 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5418 job->jv_proc_info.dwProcessId)
5419 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005420 FreeConsole();
5421 return ret;
5422}
5423
5424/*
5425 * Clear the data related to "job".
5426 */
5427 void
5428mch_clear_job(job_T *job)
5429{
5430 if (job->jv_status != JOB_FAILED)
5431 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005432 if (job->jv_job_object != NULL)
5433 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005434 CloseHandle(job->jv_proc_info.hProcess);
5435 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005436}
5437#endif
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005440#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442/*
5443 * Start termcap mode
5444 */
5445 static void
5446termcap_mode_start(void)
5447{
5448 DWORD cmodein;
5449
5450 if (g_fTermcapMode)
5451 return;
5452
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005453 if (!p_rs && USE_VTP)
5454 vtp_printf("\033[?1049h");
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 SaveConsoleBuffer(&g_cbNonTermcap);
5457
5458 if (g_cbTermcap.IsValid)
5459 {
5460 /*
5461 * We've been in termcap mode before. Restore certain screen
5462 * characteristics, including the buffer size and the window
5463 * size. Since we will be redrawing the screen, we don't need
5464 * to restore the actual contents of the buffer.
5465 */
5466 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005467 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5469 Rows = g_cbTermcap.Info.dwSize.Y;
5470 Columns = g_cbTermcap.Info.dwSize.X;
5471 }
5472 else
5473 {
5474 /*
5475 * This is our first time entering termcap mode. Clear the console
5476 * screen buffer, and resize the buffer to match the current window
5477 * size. We will use this as the size of our editing environment.
5478 */
5479 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005480 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5482 }
5483
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005484# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (g_fMouseActive)
5490 cmodein |= ENABLE_MOUSE_INPUT;
5491 else
5492 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 cmodein |= ENABLE_WINDOW_INPUT;
5494 SetConsoleMode(g_hConIn, cmodein);
5495
5496 redraw_later_clear();
5497 g_fTermcapMode = TRUE;
5498}
5499
5500
5501/*
5502 * End termcap mode
5503 */
5504 static void
5505termcap_mode_end(void)
5506{
5507 DWORD cmodein;
5508 ConsoleBuffer *cb;
5509 COORD coord;
5510 DWORD dwDummy;
5511
5512 if (!g_fTermcapMode)
5513 return;
5514
5515 SaveConsoleBuffer(&g_cbTermcap);
5516
5517 GetConsoleMode(g_hConIn, &cmodein);
5518 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5519 SetConsoleMode(g_hConIn, cmodein);
5520
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005521# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005522 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005523# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005525# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005526 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005527 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 SetConsoleCursorInfo(g_hConOut, &g_cci);
5529
5530 if (p_rs || exiting)
5531 {
5532 /*
5533 * Clear anything that happens to be on the current line.
5534 */
5535 coord.X = 0;
5536 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5537 FillConsoleOutputCharacter(g_hConOut, ' ',
5538 cb->Info.dwSize.X, coord, &dwDummy);
5539 /*
5540 * The following is just for aesthetics. If we are exiting without
5541 * restoring the screen, then we want to have a prompt string
5542 * appear at the bottom line. However, the command interpreter
5543 * seems to always advance the cursor one line before displaying
5544 * the prompt string, which causes the screen to scroll. To
5545 * counter this, move the cursor up one line before exiting.
5546 */
5547 if (exiting && !p_rs)
5548 coord.Y--;
5549 /*
5550 * Position the cursor at the leftmost column of the desired row.
5551 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005552 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005555 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005556 vtp_printf("\033[?1049l");
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 g_fTermcapMode = FALSE;
5559}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005560#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005563#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 void
5565mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005566 char_u *s UNUSED,
5567 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005569 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570}
5571
5572#else
5573
5574/*
5575 * clear `n' chars, starting from `coord'
5576 */
5577 static void
5578clear_chars(
5579 COORD coord,
5580 DWORD n)
5581{
5582 DWORD dwDummy;
5583
5584 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005585
5586 if (!USE_VTP)
5587 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5588 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005589 {
5590 set_console_color_rgb();
5591 gotoxy(coord.X + 1, coord.Y + 1);
5592 vtp_printf("\033[%dX", n);
5593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594}
5595
5596
5597/*
5598 * Clear the screen
5599 */
5600 static void
5601clear_screen(void)
5602{
5603 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005604
5605 if (!USE_VTP)
5606 clear_chars(g_coord, Rows * Columns);
5607 else
5608 {
5609 set_console_color_rgb();
5610 gotoxy(1, 1);
5611 vtp_printf("\033[2J");
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615
5616/*
5617 * Clear to end of display
5618 */
5619 static void
5620clear_to_end_of_display(void)
5621{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005622 COORD save = g_coord;
5623
5624 if (!USE_VTP)
5625 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005627 else
5628 {
5629 set_console_color_rgb();
5630 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5631 vtp_printf("\033[0J");
5632
5633 gotoxy(save.X + 1, save.Y + 1);
5634 g_coord = save;
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
5638
5639/*
5640 * Clear to end of line
5641 */
5642 static void
5643clear_to_end_of_line(void)
5644{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005645 COORD save = g_coord;
5646
5647 if (!USE_VTP)
5648 clear_chars(g_coord, Columns - g_coord.X);
5649 else
5650 {
5651 set_console_color_rgb();
5652 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5653 vtp_printf("\033[0K");
5654
5655 gotoxy(save.X + 1, save.Y + 1);
5656 g_coord = save;
5657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658}
5659
5660
5661/*
5662 * Scroll the scroll region up by `cLines' lines
5663 */
5664 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005665scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
5667 COORD oldcoord = g_coord;
5668
5669 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5670 delete_lines(cLines);
5671
5672 g_coord = oldcoord;
5673}
5674
5675
5676/*
5677 * Set the scroll region
5678 */
5679 static void
5680set_scroll_region(
5681 unsigned left,
5682 unsigned top,
5683 unsigned right,
5684 unsigned bottom)
5685{
5686 if (left >= right
5687 || top >= bottom
5688 || right > (unsigned) Columns - 1
5689 || bottom > (unsigned) Rows - 1)
5690 return;
5691
5692 g_srScrollRegion.Left = left;
5693 g_srScrollRegion.Top = top;
5694 g_srScrollRegion.Right = right;
5695 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005696}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005697
Bram Moolenaar6982f422019-02-16 16:48:01 +01005698 static void
5699set_scroll_region_tb(
5700 unsigned top,
5701 unsigned bottom)
5702{
5703 if (top >= bottom || bottom > (unsigned)Rows - 1)
5704 return;
5705
5706 g_srScrollRegion.Top = top;
5707 g_srScrollRegion.Bottom = bottom;
5708}
5709
5710 static void
5711set_scroll_region_lr(
5712 unsigned left,
5713 unsigned right)
5714{
5715 if (left >= right || right > (unsigned)Columns - 1)
5716 return;
5717
5718 g_srScrollRegion.Left = left;
5719 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
5722
5723/*
5724 * Insert `cLines' lines at the current cursor position
5725 */
5726 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005727insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005729 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 COORD dest;
5731 CHAR_INFO fill;
5732
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005733 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5734
Bram Moolenaar6982f422019-02-16 16:48:01 +01005735 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 dest.Y = g_coord.Y + cLines;
5737
Bram Moolenaar6982f422019-02-16 16:48:01 +01005738 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 source.Top = g_coord.Y;
5740 source.Right = g_srScrollRegion.Right;
5741 source.Bottom = g_srScrollRegion.Bottom - cLines;
5742
Bram Moolenaar6982f422019-02-16 16:48:01 +01005743 clip.Left = g_srScrollRegion.Left;
5744 clip.Top = g_coord.Y;
5745 clip.Right = g_srScrollRegion.Right;
5746 clip.Bottom = g_srScrollRegion.Bottom;
5747
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005748 fill.Char.AsciiChar = ' ';
5749 if (!USE_VTP)
5750 fill.Attributes = g_attrCurrent;
5751 else
5752 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005754 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005755
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005756 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5757
Bram Moolenaar6982f422019-02-16 16:48:01 +01005758 // Here we have to deal with a win32 console flake: If the scroll
5759 // region looks like abc and we scroll c to a and fill with d we get
5760 // cbd... if we scroll block c one line at a time to a, we get cdd...
5761 // vim expects cdd consistently... So we have to deal with that
5762 // here... (this also occurs scrolling the same way in the other
5763 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 if (source.Bottom < dest.Y)
5766 {
5767 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005768 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
Bram Moolenaar6982f422019-02-16 16:48:01 +01005770 coord.X = source.Left;
5771 for (i = clip.Top; i < dest.Y; ++i)
5772 {
5773 coord.Y = i;
5774 clear_chars(coord, source.Right - source.Left + 1);
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
5777}
5778
5779
5780/*
5781 * Delete `cLines' lines at the current cursor position
5782 */
5783 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005784delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005786 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 COORD dest;
5788 CHAR_INFO fill;
5789 int nb;
5790
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005791 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5792
Bram Moolenaar6982f422019-02-16 16:48:01 +01005793 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 dest.Y = g_coord.Y;
5795
Bram Moolenaar6982f422019-02-16 16:48:01 +01005796 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 source.Top = g_coord.Y + cLines;
5798 source.Right = g_srScrollRegion.Right;
5799 source.Bottom = g_srScrollRegion.Bottom;
5800
Bram Moolenaar6982f422019-02-16 16:48:01 +01005801 clip.Left = g_srScrollRegion.Left;
5802 clip.Top = g_coord.Y;
5803 clip.Right = g_srScrollRegion.Right;
5804 clip.Bottom = g_srScrollRegion.Bottom;
5805
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005806 fill.Char.AsciiChar = ' ';
5807 if (!USE_VTP)
5808 fill.Attributes = g_attrCurrent;
5809 else
5810 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005812 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005813
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005814 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5815
Bram Moolenaar6982f422019-02-16 16:48:01 +01005816 // Here we have to deal with a win32 console flake; See insert_lines()
5817 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 nb = dest.Y + (source.Bottom - source.Top) + 1;
5820
5821 if (nb < source.Top)
5822 {
5823 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005824 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaar6982f422019-02-16 16:48:01 +01005826 coord.X = source.Left;
5827 for (i = nb; i < clip.Bottom; ++i)
5828 {
5829 coord.Y = i;
5830 clear_chars(coord, source.Right - source.Left + 1);
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833}
5834
5835
5836/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005837 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 */
5839 static void
5840gotoxy(
5841 unsigned x,
5842 unsigned y)
5843{
5844 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5845 return;
5846
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005847 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005848 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01005849 // There are reports of double-width characters not displayed
5850 // correctly. This workaround should fix it, similar to how it's done
5851 // for VTP.
5852 g_coord.X = 0;
5853 SetConsoleCursorPosition(g_hConOut, g_coord);
5854
Bram Moolenaar2313b612019-09-27 14:14:32 +02005855 // external cursor coords are 1-based; internal are 0-based
5856 g_coord.X = x - 1;
5857 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005858 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005859 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005860 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005861 {
5862 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005863 // destruction. Insider build bug. Always enabled because it's cheap
5864 // and avoids mistakes with recognizing the build.
5865 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005866
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005867 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005868
5869 g_coord.X = x - 1;
5870 g_coord.Y = y - 1;
5871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872}
5873
5874
5875/*
5876 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005877 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 */
5879 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005880textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005882 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 SetConsoleTextAttribute(g_hConOut, wAttr);
5885}
5886
5887
5888 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005889textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005891 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005893 if (!USE_VTP)
5894 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5895 else
5896 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
5899
5900 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005901textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005903 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005905 if (!USE_VTP)
5906 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5907 else
5908 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909}
5910
5911
5912/*
5913 * restore the default text attribute (whatever we started with)
5914 */
5915 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005916normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005918 if (!USE_VTP)
5919 textattr(g_attrDefault);
5920 else
5921 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922}
5923
5924
5925static WORD g_attrPreStandout = 0;
5926
5927/*
5928 * Make the text standout, by brightening it
5929 */
5930 static void
5931standout(void)
5932{
5933 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5936}
5937
5938
5939/*
5940 * Turn off standout mode
5941 */
5942 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005943standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005947
5948 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949}
5950
5951
5952/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005953 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
5955 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005956mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 char_u *p;
5959 int n;
5960
5961 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5962 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005963 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005964# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005965 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005966# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005967 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
5969 p = T_ME + 2;
5970 n = getdigits(&p);
5971 if (*p == 'm' && n > 0)
5972 {
5973 cterm_normal_fg_color = (n & 0xf) + 1;
5974 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5975 }
5976 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005977# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005978 cterm_normal_fg_gui_color = INVALCOLOR;
5979 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981}
5982
5983
5984/*
5985 * visual bell: flash the screen
5986 */
5987 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005988visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 COORD coordOrigin = {0, 0};
5991 WORD attrFlash = ~g_attrCurrent & 0xff;
5992
5993 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005994 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 if (oldattrs == NULL)
5997 return;
5998 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5999 coordOrigin, &dwDummy);
6000 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6001 coordOrigin, &dwDummy);
6002
Bram Moolenaar0f873732019-12-05 20:28:46 +01006003 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006004 if (!USE_VTP)
6005 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 coordOrigin, &dwDummy);
6007 vim_free(oldattrs);
6008}
6009
6010
6011/*
6012 * Make the cursor visible or invisible
6013 */
6014 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006015cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006018
6019 if (USE_VTP)
6020 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6021
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006022# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025}
6026
6027
6028/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006029 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006030 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006032 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006034 char_u *pchBuf,
6035 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006037 COORD coord = g_coord;
6038 DWORD written;
6039 DWORD n, cchwritten, cells;
6040 static WCHAR *unicodebuf = NULL;
6041 static int unibuflen = 0;
6042 int length;
6043 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006045 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6046 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006047 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006048 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006049 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006050 unibuflen = length;
6051 }
6052 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6053 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006055 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006056
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006057 if (!USE_VTP)
6058 {
6059 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6060 coord, &written);
6061 // When writing fails or didn't write a single character, pretend one
6062 // character was written, otherwise we get stuck.
6063 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6064 coord, &cchwritten) == 0
6065 || cchwritten == 0 || cchwritten == (DWORD)-1)
6066 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006067 }
6068 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006069 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006070 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6071 NULL) == 0 || cchwritten == 0)
6072 cchwritten = 1;
6073 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006075 if (cchwritten == length)
6076 {
6077 written = cbToWrite;
6078 g_coord.X += (SHORT)cells;
6079 }
6080 else
6081 {
6082 char_u *p = pchBuf;
6083 for (n = 0; n < cchwritten; n++)
6084 MB_CPTR_ADV(p);
6085 written = p - pchBuf;
6086 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
6089 while (g_coord.X > g_srScrollRegion.Right)
6090 {
6091 g_coord.X -= (SHORT) Columns;
6092 if (g_coord.Y < g_srScrollRegion.Bottom)
6093 g_coord.Y++;
6094 }
6095
6096 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6097
6098 return written;
6099}
6100
6101
6102/*
6103 * mch_write(): write the output buffer to the screen, translating ESC
6104 * sequences into calls to console output routines.
6105 */
6106 void
6107mch_write(
6108 char_u *s,
6109 int len)
6110{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006111# ifdef VIMDLL
6112 if (gui.in_use)
6113 return;
6114# endif
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 s[len] = NUL;
6117
6118 if (!term_console)
6119 {
6120 write(1, s, (unsigned)len);
6121 return;
6122 }
6123
Bram Moolenaar0f873732019-12-05 20:28:46 +01006124 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 while (len--)
6126 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006127 // optimization: use one single write_chars for runs of text,
6128 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006129 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 if (p_wd)
6132 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006133 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (prefix != 0)
6135 prefix = 1;
6136 }
6137
6138 if (prefix != 0)
6139 {
6140 DWORD nWritten;
6141
6142 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006143# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (fdDump)
6145 {
6146 fputc('>', fdDump);
6147 fwrite(s, sizeof(char_u), nWritten, fdDump);
6148 fputs("<\n", fdDump);
6149 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 len -= (nWritten - 1);
6152 s += nWritten;
6153 }
6154 else if (s[0] == '\n')
6155 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006156 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (g_coord.Y == g_srScrollRegion.Bottom)
6158 {
6159 scroll(1);
6160 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6161 }
6162 else
6163 {
6164 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6165 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006166# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (fdDump)
6168 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 s++;
6171 }
6172 else if (s[0] == '\r')
6173 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006174 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006176# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 if (fdDump)
6178 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 s++;
6181 }
6182 else if (s[0] == '\b')
6183 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006184 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (g_coord.X > g_srScrollRegion.Left)
6186 g_coord.X--;
6187 else if (g_coord.Y > g_srScrollRegion.Top)
6188 {
6189 g_coord.X = g_srScrollRegion.Right;
6190 g_coord.Y--;
6191 }
6192 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006193# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (fdDump)
6195 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 s++;
6198 }
6199 else if (s[0] == '\a')
6200 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006201 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006203# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (fdDump)
6205 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 s++;
6208 }
6209 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6210 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006211# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006212 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006213# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006214 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006215 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 switch (s[2])
6218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 case '0': case '1': case '2': case '3': case '4':
6220 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006221 p = s + 1;
6222 do
6223 {
6224 ++p;
6225 args[argc] = getdigits(&p);
6226 argc += (argc < 15) ? 1 : 0;
6227 if (p > s + len)
6228 break;
6229 } while (*p == ';');
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (p > s + len)
6232 break;
6233
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006234 arg1 = args[0];
6235 arg2 = args[1];
6236 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006238 if (argc == 1 && args[0] == 0)
6239 normvideo();
6240 else if (argc == 1)
6241 {
6242 if (USE_VTP)
6243 textcolor((WORD) arg1);
6244 else
6245 textattr((WORD) arg1);
6246 }
6247 else if (USE_VTP)
6248 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006250 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006252 gotoxy(arg2, arg1);
6253 }
6254 else if (argc == 2 && *p == 'r')
6255 {
6256 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6257 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006258 else if (argc == 2 && *p == 'R')
6259 {
6260 set_scroll_region_tb(arg1, arg2);
6261 }
6262 else if (argc == 2 && *p == 'V')
6263 {
6264 set_scroll_region_lr(arg1, arg2);
6265 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006266 else if (argc == 1 && *p == 'A')
6267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 gotoxy(g_coord.X + 1,
6269 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6270 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006271 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 textbackground((WORD) arg1);
6274 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006275 else if (argc == 1 && *p == 'C')
6276 {
6277 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6278 g_coord.Y + 1);
6279 }
6280 else if (argc == 1 && *p == 'f')
6281 {
6282 textcolor((WORD) arg1);
6283 }
6284 else if (argc == 1 && *p == 'H')
6285 {
6286 gotoxy(1, arg1);
6287 }
6288 else if (argc == 1 && *p == 'L')
6289 {
6290 insert_lines(arg1);
6291 }
6292 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 delete_lines(arg1);
6295 }
6296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006297 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 s = p + 1;
6299 break;
6300
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 gotoxy(g_coord.X + 1,
6303 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6304 goto got3;
6305
6306 case 'B':
6307 visual_bell();
6308 goto got3;
6309
6310 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6312 g_coord.Y + 1);
6313 goto got3;
6314
6315 case 'E':
6316 termcap_mode_end();
6317 goto got3;
6318
6319 case 'F':
6320 standout();
6321 goto got3;
6322
6323 case 'f':
6324 standend();
6325 goto got3;
6326
6327 case 'H':
6328 gotoxy(1, 1);
6329 goto got3;
6330
6331 case 'j':
6332 clear_to_end_of_display();
6333 goto got3;
6334
6335 case 'J':
6336 clear_screen();
6337 goto got3;
6338
6339 case 'K':
6340 clear_to_end_of_line();
6341 goto got3;
6342
6343 case 'L':
6344 insert_lines(1);
6345 goto got3;
6346
6347 case 'M':
6348 delete_lines(1);
6349 goto got3;
6350
6351 case 'S':
6352 termcap_mode_start();
6353 goto got3;
6354
6355 case 'V':
6356 cursor_visible(TRUE);
6357 goto got3;
6358
6359 case 'v':
6360 cursor_visible(FALSE);
6361 goto got3;
6362
6363 got3:
6364 s += 3;
6365 len -= 2;
6366 }
6367
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006368# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (fdDump)
6370 {
6371 fputs("ESC | ", fdDump);
6372 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6373 fputc('\n', fdDump);
6374 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377 else
6378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006379 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 DWORD nWritten;
6381
6382 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006383# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (fdDump)
6385 {
6386 fputc('>', fdDump);
6387 fwrite(s, sizeof(char_u), nWritten, fdDump);
6388 fputs("<\n", fdDump);
6389 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392 len -= (nWritten - 1);
6393 s += nWritten;
6394 }
6395 }
6396
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006397# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (fdDump)
6399 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401}
6402
Bram Moolenaar0f873732019-12-05 20:28:46 +01006403#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405
6406/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006407 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 void
6410mch_delay(
6411 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006412 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006414#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 Sleep((int)msec); // never wait for input
6416#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006417# ifdef VIMDLL
6418 if (gui.in_use)
6419 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006420 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006421 return;
6422 }
6423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006425# ifdef FEAT_MZSCHEME
6426 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6427 {
6428 int towait = p_mzq;
6429
Bram Moolenaar0f873732019-12-05 20:28:46 +01006430 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006431 while (msec > 0)
6432 {
6433 mzvim_check_threads();
6434 if (msec < towait)
6435 towait = msec;
6436 Sleep(towait);
6437 msec -= towait;
6438 }
6439 }
6440 else
6441# endif
6442 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006444 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445#endif
6446}
6447
6448
6449/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006450 * This version of remove is not scared by a readonly (backup) file.
6451 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 * Return 0 for success, -1 for failure.
6453 */
6454 int
6455mch_remove(char_u *name)
6456{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006457 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 int n;
6459
Bram Moolenaar203258c2016-01-17 22:15:16 +01006460 /*
6461 * On Windows, deleting a directory's symbolic link is done by
6462 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6463 */
6464 if (mch_isdir(name) && mch_is_symbolic_link(name))
6465 return mch_rmdir(name);
6466
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006467 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6468
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006469 wn = enc_to_utf16(name, NULL);
6470 if (wn == NULL)
6471 return -1;
6472
6473 n = DeleteFileW(wn) ? 0 : -1;
6474 vim_free(wn);
6475 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478
6479/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006480 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 */
6482 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006483mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006485#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6486# ifdef VIMDLL
6487 if (!gui.in_use)
6488# endif
6489 if (g_fCtrlCPressed || g_fCBrkPressed)
6490 {
6491 ctrl_break_was_pressed = g_fCBrkPressed;
6492 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6493 got_int = TRUE;
6494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495#endif
6496}
6497
Bram Moolenaar0f873732019-12-05 20:28:46 +01006498// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006499#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006500
6501/*
6502 * How much main memory in KiB that can be used by VIM.
6503 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006504 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006505mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006506{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006507 MEMORYSTATUSEX ms;
6508
Bram Moolenaar0f873732019-12-05 20:28:46 +01006509 // Need to use GlobalMemoryStatusEx() when there is more memory than
6510 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006511 ms.dwLength = sizeof(MEMORYSTATUSEX);
6512 GlobalMemoryStatusEx(&ms);
6513 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006514 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006515 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006516 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006517 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006518 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006519 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006520 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006521 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006522 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006523 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006524 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006528 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6530 * file whose short file name is "FOO.BAR" (its long file name will
6531 * be correct: "foo.bar~"). Because a file can be accessed by
6532 * either its SFN or its LFN, "foo.bar" has effectively been
6533 * renamed to "foo.bar", which is not at all what was wanted. This
6534 * seems to happen only when renaming files with three-character
6535 * extensions by appending a suffix that does not include ".".
6536 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6537 *
6538 * There is another problem, which isn't really a bug but isn't right either:
6539 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6540 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6541 * service pack 6. Doesn't seem to happen on Windows 98.
6542 *
6543 * Like rename(), returns 0 upon success, non-zero upon failure.
6544 * Should probably set errno appropriately when errors occur.
6545 */
6546 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006547mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006549 WCHAR *p;
6550 int i;
6551 WCHAR szTempFile[_MAX_PATH + 1];
6552 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006555 // No need to play tricks unless the file name contains a "~" as the
6556 // seventh character.
6557 p = wold;
6558 for (i = 0; wold[i] != NUL; ++i)
6559 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6560 && wold[i + 1] != 0)
6561 p = wold + i + 1;
6562 if ((int)(wold + i - p) < 8 || p[6] != '~')
6563 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006565 // Get base path of new file name. Undocumented feature: If pszNewFile is
6566 // a directory, no error is returned and pszFilePart will be NULL.
6567 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006569 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006571 // Get (and create) a unique temporary file name in directory of new file
6572 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return -2;
6574
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006575 // blow the temp file away
6576 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 return -3;
6578
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006579 // rename old file to the temp file
6580 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 return -4;
6582
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006583 // now create an empty file called pszOldFile; this prevents the operating
6584 // system using pszOldFile as an alias (SFN) if we're renaming within the
6585 // same directory. For example, we're editing a file called
6586 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6587 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6588 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6589 // cause all sorts of problems later in buf_write(). So, we create an
6590 // empty file called filena~1.txt and the system will have to find some
6591 // other SFN for filena~1.txt~, such as filena~2.txt
6592 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6594 return -5;
6595 if (!CloseHandle(hf))
6596 return -6;
6597
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006598 // rename the temp file to the new file
6599 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006601 // Renaming failed. Rename the file back to its old name, so that it
6602 // looks like nothing happened.
6603 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 return -7;
6605 }
6606
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006607 // Seems to be left around on Novell filesystems
6608 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006610 // finally, remove the empty old file
6611 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 return -8;
6613
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006614 return 0;
6615}
6616
6617
6618/*
6619 * Converts the filenames to UTF-16, then call mch_wrename().
6620 * Like rename(), returns 0 upon success, non-zero upon failure.
6621 */
6622 int
6623mch_rename(
6624 const char *pszOldFile,
6625 const char *pszNewFile)
6626{
6627 WCHAR *wold = NULL;
6628 WCHAR *wnew = NULL;
6629 int retval = -1;
6630
6631 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6632 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6633 if (wold != NULL && wnew != NULL)
6634 retval = mch_wrename(wold, wnew);
6635 vim_free(wold);
6636 vim_free(wnew);
6637 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638}
6639
6640/*
6641 * Get the default shell for the current hardware platform
6642 */
6643 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006644default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006646 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647}
6648
6649/*
6650 * mch_access() extends access() to do more detailed check on network drives.
6651 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6652 */
6653 int
6654mch_access(char *n, int p)
6655{
6656 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006657 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006658 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006660 wn = enc_to_utf16((char_u *)n, NULL);
6661 if (wn == NULL)
6662 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006664 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
6668 if (p & R_OK)
6669 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006670 // Read check is performed by seeing if we can do a find file on
6671 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006672 int i;
6673 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006675 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6676 TempNameW[i] = wn[i];
6677 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6678 TempNameW[i++] = '\\';
6679 TempNameW[i++] = '*';
6680 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006682 hFile = FindFirstFileW(TempNameW, &d);
6683 if (hFile == INVALID_HANDLE_VALUE)
6684 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006685 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
6689 if (p & W_OK)
6690 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006691 // Trying to create a temporary file in the directory should catch
6692 // directories on read-only network shares. However, in
6693 // directories whose ACL allows writes but denies deletes will end
6694 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006695 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6696 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006697 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006698 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 }
6700 }
6701 else
6702 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006703 // Don't consider a file read-only if another process has opened it.
6704 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6705
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 // Trying to open the file for the required access does ACL, read-only
6707 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006708 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6709 | ((p & R_OK) ? GENERIC_READ : 0);
6710
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006711 hFile = CreateFileW(wn, access_mode, share_mode,
6712 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (hFile == INVALID_HANDLE_VALUE)
6714 goto getout;
6715 CloseHandle(hFile);
6716 }
6717
Bram Moolenaar0f873732019-12-05 20:28:46 +01006718 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 return retval;
6722}
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006725 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 */
6727 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006728mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 WCHAR *wn;
6731 int f;
6732
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006733 wn = enc_to_utf16((char_u *)name, NULL);
6734 if (wn == NULL)
6735 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006737 f = _wopen(wn, flags, mode);
6738 vim_free(wn);
6739 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740}
6741
6742/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006743 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 */
6745 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006746mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 WCHAR *wn, *wm;
6749 FILE *f = NULL;
6750
Bram Moolenaara12a1612019-01-24 16:39:02 +01006751#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006752 // Work around an annoying assertion in the Microsoft debug CRT
6753 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006754 char newMode = mode[strlen(mode) - 1];
6755 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006756
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006757 _get_fmode(&oldMode);
6758 if (newMode == 't')
6759 _set_fmode(_O_TEXT);
6760 else if (newMode == 'b')
6761 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006762#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006763 wn = enc_to_utf16((char_u *)name, NULL);
6764 wm = enc_to_utf16((char_u *)mode, NULL);
6765 if (wn != NULL && wm != NULL)
6766 f = _wfopen(wn, wm);
6767 vim_free(wn);
6768 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006769
Bram Moolenaara12a1612019-01-24 16:39:02 +01006770#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006771 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006772#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006773 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776/*
6777 * SUB STREAM (aka info stream) handling:
6778 *
6779 * NTFS can have sub streams for each file. Normal contents of file is
6780 * stored in the main stream, and extra contents (author information and
6781 * title and so on) can be stored in sub stream. After Windows 2000, user
6782 * can access and store those informations in sub streams via explorer's
6783 * property menuitem in right click menu. Those informations in sub streams
6784 * were lost when copying only the main stream. So we have to copy sub
6785 * streams.
6786 *
6787 * Incomplete explanation:
6788 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6789 * More useful info and an example:
6790 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6791 */
6792
6793/*
6794 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6795 * and write to stream "substream" of file "to".
6796 * Errors are ignored.
6797 */
6798 static void
6799copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6800{
6801 HANDLE hTo;
6802 WCHAR *to_name;
6803
6804 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6805 wcscpy(to_name, to);
6806 wcscat(to_name, substream);
6807
6808 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6809 FILE_ATTRIBUTE_NORMAL, NULL);
6810 if (hTo != INVALID_HANDLE_VALUE)
6811 {
6812 long done;
6813 DWORD todo;
6814 DWORD readcnt, written;
6815 char buf[4096];
6816
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 for (done = 0; done < len; done += written)
6819 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006820 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006821 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6822 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6824 FALSE, FALSE, context)
6825 || readcnt != todo
6826 || !WriteFile(hTo, buf, todo, &written, NULL)
6827 || written != todo)
6828 break;
6829 }
6830 CloseHandle(hTo);
6831 }
6832
6833 free(to_name);
6834}
6835
6836/*
6837 * Copy info streams from file "from" to file "to".
6838 */
6839 static void
6840copy_infostreams(char_u *from, char_u *to)
6841{
6842 WCHAR *fromw;
6843 WCHAR *tow;
6844 HANDLE sh;
6845 WIN32_STREAM_ID sid;
6846 int headersize;
6847 WCHAR streamname[_MAX_PATH];
6848 DWORD readcount;
6849 void *context = NULL;
6850 DWORD lo, hi;
6851 int len;
6852
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006854 fromw = enc_to_utf16(from, NULL);
6855 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (fromw != NULL && tow != NULL)
6857 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006858 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6860 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6861 if (sh != INVALID_HANDLE_VALUE)
6862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006863 // Use BackupRead() to find the info streams. Repeat until we
6864 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 for (;;)
6866 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006867 // Get the header to find the length of the stream name. If
6868 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006870 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6872 &readcount, FALSE, FALSE, &context)
6873 || readcount == 0)
6874 break;
6875
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 // We only deal with streams that have a name. The normal
6877 // file data appears to be without a name, even though docs
6878 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (sid.dwStreamNameSize > 0)
6880 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006881 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (!BackupRead(sh, (LPBYTE)streamname,
6883 sid.dwStreamNameSize,
6884 &readcount, FALSE, FALSE, &context))
6885 break;
6886
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // Copy an info stream with a name ":anything:$DATA".
6888 // Skip "::$DATA", it has no stream name (examples suggest
6889 // it might be used for the normal file contents).
6890 // Note that BackupRead() counts bytes, but the name is in
6891 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 len = readcount / sizeof(WCHAR);
6893 streamname[len] = 0;
6894 if (len > 7 && wcsicmp(streamname + len - 6,
6895 L":$DATA") == 0)
6896 {
6897 streamname[len - 6] = 0;
6898 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006899 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901 }
6902
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 // Advance to the next stream. We might try seeking too far,
6904 // but BackupSeek() doesn't skip over stream borders, thus
6905 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006906 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 &lo, &hi, &context);
6908 }
6909
Bram Moolenaar0f873732019-12-05 20:28:46 +01006910 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6912
6913 CloseHandle(sh);
6914 }
6915 }
6916 vim_free(fromw);
6917 vim_free(tow);
6918}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920/*
6921 * Copy file attributes from file "from" to file "to".
6922 * For Windows NT and later we copy info streams.
6923 * Always returns zero, errors are ignored.
6924 */
6925 int
6926mch_copy_file_attribute(char_u *from, char_u *to)
6927{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006929 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 return 0;
6931}
6932
6933#if defined(MYRESETSTKOFLW) || defined(PROTO)
6934/*
6935 * Recreate a destroyed stack guard page in win32.
6936 * Written by Benjamin Peterson.
6937 */
6938
Bram Moolenaar0f873732019-12-05 20:28:46 +01006939// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006940# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942/*
6943 * This function does the same thing as _resetstkoflw(), which is only
6944 * available in DevStudio .net and later.
6945 * Returns 0 for failure, 1 for success.
6946 */
6947 int
6948myresetstkoflw(void)
6949{
6950 BYTE *pStackPtr;
6951 BYTE *pGuardPage;
6952 BYTE *pStackBase;
6953 BYTE *pLowestPossiblePage;
6954 MEMORY_BASIC_INFORMATION mbi;
6955 SYSTEM_INFO si;
6956 DWORD nPageSize;
6957 DWORD dummy;
6958
Bram Moolenaar0f873732019-12-05 20:28:46 +01006959 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 GetSystemInfo(&si);
6961 nPageSize = si.dwPageSize;
6962
Bram Moolenaar0f873732019-12-05 20:28:46 +01006963 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 pStackPtr = (BYTE*)_alloca(1);
6965
Bram Moolenaar0f873732019-12-05 20:28:46 +01006966 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6968 return 0;
6969 pStackBase = (BYTE*)mbi.AllocationBase;
6970
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006971 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006972 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006973 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 // We want the first committed page in the stack Start at the stack
6977 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 BYTE *pBlock = pStackBase;
6979
Bram Moolenaara466c992005-07-09 21:03:22 +00006980 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
6982 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6983 return 0;
6984
6985 pBlock += mbi.RegionSize;
6986
6987 if (mbi.State & MEM_COMMIT)
6988 break;
6989 }
6990
Bram Moolenaar0f873732019-12-05 20:28:46 +01006991 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (mbi.Protect & PAGE_GUARD)
6993 return 1;
6994
Bram Moolenaar0f873732019-12-05 20:28:46 +01006995 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6997 pGuardPage = pLowestPossiblePage;
6998 else
6999 pGuardPage = (BYTE*)mbi.BaseAddress;
7000
Bram Moolenaar0f873732019-12-05 20:28:46 +01007001 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
7003 return 0;
7004
Bram Moolenaar0f873732019-12-05 20:28:46 +01007005 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
7007 &dummy))
7008 return 0;
7009 }
7010
7011 return 1;
7012}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007015
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007016/*
7017 * The command line arguments in UCS2
7018 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007019static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007020static LPWSTR *ArglistW = NULL;
7021static int global_argc = 0;
7022static char **global_argv;
7023
Bram Moolenaar0f873732019-12-05 20:28:46 +01007024static int used_file_argc = 0; // last argument in global_argv[] used
7025 // for the argument list.
7026static int *used_file_indexes = NULL; // indexes in global_argv[] for
7027 // command line arguments added to
7028 // the argument list
7029static int used_file_count = 0; // nr of entries in used_file_indexes
7030static int used_file_literal = FALSE; // take file names literally
7031static int used_file_full_path = FALSE; // file name was full path
7032static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007033static int used_alist_count = 0;
7034
7035
7036/*
7037 * Get the command line arguments. Unicode version.
7038 * Returns argc. Zero when something fails.
7039 */
7040 int
7041get_cmd_argsW(char ***argvp)
7042{
7043 char **argv = NULL;
7044 int argc = 0;
7045 int i;
7046
Bram Moolenaar14993322014-09-09 12:25:33 +02007047 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007048 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7049 if (ArglistW != NULL)
7050 {
7051 argv = malloc((nArgsW + 1) * sizeof(char *));
7052 if (argv != NULL)
7053 {
7054 argc = nArgsW;
7055 argv[argc] = NULL;
7056 for (i = 0; i < argc; ++i)
7057 {
7058 int len;
7059
Bram Moolenaar0f873732019-12-05 20:28:46 +01007060 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007061 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007062 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007063 (LPSTR *)&argv[i], &len, 0, 0);
7064 if (argv[i] == NULL)
7065 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007066 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007067 while (i > 0)
7068 free(argv[--i]);
7069 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007070 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007071 argc = 0;
7072 }
7073 }
7074 }
7075 }
7076
7077 global_argc = argc;
7078 global_argv = argv;
7079 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007080 {
7081 if (used_file_indexes != NULL)
7082 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007083 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007084 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007085
7086 if (argvp != NULL)
7087 *argvp = argv;
7088 return argc;
7089}
7090
7091 void
7092free_cmd_argsW(void)
7093{
7094 if (ArglistW != NULL)
7095 {
7096 GlobalFree(ArglistW);
7097 ArglistW = NULL;
7098 }
7099}
7100
7101/*
7102 * Remember "name" is an argument that was added to the argument list.
7103 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7104 * is called.
7105 */
7106 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007107used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007108{
7109 int i;
7110
7111 if (used_file_indexes == NULL)
7112 return;
7113 for (i = used_file_argc + 1; i < global_argc; ++i)
7114 if (STRCMP(global_argv[i], name) == 0)
7115 {
7116 used_file_argc = i;
7117 used_file_indexes[used_file_count++] = i;
7118 break;
7119 }
7120 used_file_literal = literal;
7121 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007122 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007123}
7124
7125/*
7126 * Remember the length of the argument list as it was. If it changes then we
7127 * leave it alone when 'encoding' is set.
7128 */
7129 void
7130set_alist_count(void)
7131{
7132 used_alist_count = GARGCOUNT;
7133}
7134
7135/*
7136 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7137 * has been changed while starting up. Use the UCS-2 command line arguments
7138 * and convert them to 'encoding'.
7139 */
7140 void
7141fix_arg_enc(void)
7142{
7143 int i;
7144 int idx;
7145 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007146 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007147
Bram Moolenaar0f873732019-12-05 20:28:46 +01007148 // Safety checks:
7149 // - if argument count differs between the wide and non-wide argument
7150 // list, something must be wrong.
7151 // - the file name arguments must have been located.
7152 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007153 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007154 || ArglistW == NULL
7155 || used_file_indexes == NULL
7156 || used_file_count == 0
7157 || used_alist_count != GARGCOUNT)
7158 return;
7159
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007161 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007162 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007163 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007164 for (i = 0; i < GARGCOUNT; ++i)
7165 fnum_list[i] = GARGLIST[i].ae_fnum;
7166
Bram Moolenaar0f873732019-12-05 20:28:46 +01007167 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007168 alist_clear(&global_alist);
7169 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007170 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007171
7172 for (i = 0; i < used_file_count; ++i)
7173 {
7174 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007175 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007176 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007177 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007178 int literal = used_file_literal;
7179
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007180#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 // When using diff mode may need to concatenate file name to
7182 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007183 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7184 && !mch_isdir(alist_name(&GARGLIST[0])))
7185 {
7186 char_u *r;
7187
7188 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7189 if (r != NULL)
7190 {
7191 vim_free(str);
7192 str = r;
7193 }
7194 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007195#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007196 // Re-use the old buffer by renaming it. When not using literal
7197 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007198 if (used_file_literal)
7199 buf_set_name(fnum_list[i], str);
7200
Bram Moolenaar0f873732019-12-05 20:28:46 +01007201 // Check backtick literal. backtick literal is already expanded in
7202 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007203 if (literal == FALSE)
7204 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007205 size_t len = STRLEN(str);
7206
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007207 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7208 literal = TRUE;
7209 }
7210 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007211 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007212 }
7213
7214 if (!used_file_literal)
7215 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007216 // Now expand wildcards in the arguments.
7217 // Temporarily add '(' and ')' to 'isfname'. These are valid
7218 // filename characters but are excluded from 'isfname' to make
7219 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7220 // Also, unset wildignore to not be influenced by this option.
7221 // The arguments specified in command-line should be kept even if
7222 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007223 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007224 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007225 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007226 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007227 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007228 }
7229
Bram Moolenaar0f873732019-12-05 20:28:46 +01007230 // If wildcard expansion failed, we are editing the first file of the
7231 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007232 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7233 {
7234 do_cmdline_cmd((char_u *)":rewind");
7235 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007236 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007237 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007238
7239 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007240}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007241
7242 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007243mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007244{
7245 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007246 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007247
Bram Moolenaar964b3742019-05-24 18:54:09 +02007248 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007249 if (envbuf == NULL)
7250 return -1;
7251
7252 sprintf((char *)envbuf, "%s=%s", var, value);
7253
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007254 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007255
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007256 vim_free(envbuf);
7257 if (p == NULL)
7258 return -1;
7259 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007260#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007261 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007262#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007263 // Unlike Un*x systems, we can free the string for _wputenv().
7264 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007265
7266 return 0;
7267}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007268
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007269/*
7270 * Support for 256 colors and 24-bit colors was added in Windows 10
7271 * version 1703 (Creators update).
7272 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007273#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7274
7275/*
7276 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007277 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007278 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007279#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007280
7281/*
7282 * ConPTY differences between versions, need different logic.
7283 * version 1903 (May 2019 update).
7284 */
7285#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7286
7287/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007288 * version 1909 (November 2019 update).
7289 */
7290#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7291
7292/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007293 * Stay ahead of the next update, and when it's done, fix this.
7294 * version ? (2020 update, temporarily use the build number of insider preview)
7295 */
7296#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
7297
7298/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007299 * Confirm until this version. Also the logic changes.
7300 * insider preview.
7301 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007302#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007303
7304/*
7305 * Not stable now.
7306 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007307#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007308
7309 static void
7310vtp_flag_init(void)
7311{
7312 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007313#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007314 DWORD mode;
7315 HANDLE out;
7316
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007317# ifdef VIMDLL
7318 if (!gui.in_use)
7319# endif
7320 {
7321 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007322
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007323 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7324 GetConsoleMode(out, &mode);
7325 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7326 if (SetConsoleMode(out, mode) == 0)
7327 vtp_working = 0;
7328 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007329#endif
7330
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007331 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007332 conpty_working = 1;
7333 if (ver >= CONPTY_STABLE_BUILD)
7334 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007335
Bram Moolenaar57da6982019-09-13 22:30:11 +02007336 if (ver <= CONPTY_INSIDER_BUILD)
7337 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007338 if (ver <= CONPTY_1909_BUILD)
7339 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007340 if (ver <= CONPTY_1903_BUILD)
7341 conpty_type = 2;
7342 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7343 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007344
7345 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
7346 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007347}
7348
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007349#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007350
7351 static void
7352vtp_init(void)
7353{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007354 HMODULE hKerneldll;
7355 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007356# ifdef FEAT_TERMGUICOLORS
7357 COLORREF fg, bg;
7358# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007359
Bram Moolenaar0f873732019-12-05 20:28:46 +01007360 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007361 hKerneldll = GetModuleHandle("kernel32.dll");
7362 if (hKerneldll != NULL)
7363 {
7364 pGetConsoleScreenBufferInfoEx =
7365 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7366 hKerneldll, "GetConsoleScreenBufferInfoEx");
7367 pSetConsoleScreenBufferInfoEx =
7368 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7369 hKerneldll, "SetConsoleScreenBufferInfoEx");
7370 if (pGetConsoleScreenBufferInfoEx != NULL
7371 && pSetConsoleScreenBufferInfoEx != NULL)
7372 has_csbiex = TRUE;
7373 }
7374
7375 csbi.cbSize = sizeof(csbi);
7376 if (has_csbiex)
7377 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007378 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7379 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007380 store_console_bg_rgb = save_console_bg_rgb;
7381 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007382
7383# ifdef FEAT_TERMGUICOLORS
7384 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7385 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7386 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7387 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7388 default_console_color_bg = bg;
7389 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007390# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007391
7392 set_console_color_rgb();
7393}
7394
7395 static void
7396vtp_exit(void)
7397{
Bram Moolenaardf543822020-01-30 11:53:59 +01007398 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007399}
7400
7401 static int
7402vtp_printf(
7403 char *format,
7404 ...)
7405{
7406 char_u buf[100];
7407 va_list list;
7408 DWORD result;
7409
7410 va_start(list, format);
7411 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7412 va_end(list);
7413 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7414 return (int)result;
7415}
7416
7417 static void
7418vtp_sgr_bulk(
7419 int arg)
7420{
7421 int args[1];
7422
7423 args[0] = arg;
7424 vtp_sgr_bulks(1, args);
7425}
7426
7427 static void
7428vtp_sgr_bulks(
7429 int argc,
7430 int *args
7431)
7432{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007433 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007434 char_u buf[2 + (4 * 16) + 1];
7435 char_u *p;
7436 int i;
7437
7438 p = buf;
7439 *p++ = '\033';
7440 *p++ = '[';
7441
7442 for (i = 0; i < argc; ++i)
7443 {
7444 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7445 *p++ = ';';
7446 }
7447 p--;
7448 *p++ = 'm';
7449 *p = NUL;
7450 vtp_printf((char *)buf);
7451}
7452
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007453# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007454 static int
7455ctermtoxterm(
7456 int cterm)
7457{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007458 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007459
7460 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7461 return (((int)r << 16) | ((int)g << 8) | (int)b);
7462}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007463# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007464
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007465 static void
7466set_console_color_rgb(void)
7467{
7468# ifdef FEAT_TERMGUICOLORS
7469 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007470 guicolor_T fg, bg;
7471 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007472
7473 if (!USE_VTP)
7474 return;
7475
Bram Moolenaara050b942019-12-02 21:35:31 +01007476 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7477
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007478 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7479 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7480
7481 csbi.cbSize = sizeof(csbi);
7482 if (has_csbiex)
7483 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7484
7485 csbi.cbSize = sizeof(csbi);
7486 csbi.srWindow.Right += 1;
7487 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007488 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7489 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007490 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7491 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007492 if (has_csbiex)
7493 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7494# endif
7495}
7496
Bram Moolenaara050b942019-12-02 21:35:31 +01007497# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7498 void
7499get_default_console_color(
7500 int *cterm_fg,
7501 int *cterm_bg,
7502 guicolor_T *gui_fg,
7503 guicolor_T *gui_bg)
7504{
7505 int id;
7506 guicolor_T guifg = INVALCOLOR;
7507 guicolor_T guibg = INVALCOLOR;
7508 int ctermfg = 0;
7509 int ctermbg = 0;
7510
7511 id = syn_name2id((char_u *)"Normal");
7512 if (id > 0 && p_tgc)
7513 syn_id2colors(id, &guifg, &guibg);
7514 if (guifg == INVALCOLOR)
7515 {
7516 ctermfg = -1;
7517 if (id > 0)
7518 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7519 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7520 : default_console_color_fg;
7521 cterm_normal_fg_gui_color = guifg;
7522 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7523 }
7524 if (guibg == INVALCOLOR)
7525 {
7526 ctermbg = -1;
7527 if (id > 0)
7528 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7529 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7530 : default_console_color_bg;
7531 cterm_normal_bg_gui_color = guibg;
7532 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7533 }
7534
7535 *cterm_fg = ctermfg;
7536 *cterm_bg = ctermbg;
7537 *gui_fg = guifg;
7538 *gui_bg = guibg;
7539}
7540# endif
7541
Bram Moolenaardf543822020-01-30 11:53:59 +01007542/*
7543 * Set the console colors to the original colors or the last set colors.
7544 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007545 static void
7546reset_console_color_rgb(void)
7547{
7548# ifdef FEAT_TERMGUICOLORS
7549 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7550
7551 csbi.cbSize = sizeof(csbi);
7552 if (has_csbiex)
7553 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7554
7555 csbi.cbSize = sizeof(csbi);
7556 csbi.srWindow.Right += 1;
7557 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007558 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7559 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7560 if (has_csbiex)
7561 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7562# endif
7563}
7564
7565/*
7566 * Set the console colors to the original colors.
7567 */
7568 static void
7569restore_console_color_rgb(void)
7570{
7571# ifdef FEAT_TERMGUICOLORS
7572 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7573
7574 csbi.cbSize = sizeof(csbi);
7575 if (has_csbiex)
7576 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7577
7578 csbi.cbSize = sizeof(csbi);
7579 csbi.srWindow.Right += 1;
7580 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007581 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7582 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007583 if (has_csbiex)
7584 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7585# endif
7586}
7587
7588 void
7589control_console_color_rgb(void)
7590{
7591 if (USE_VTP)
7592 set_console_color_rgb();
7593 else
7594 reset_console_color_rgb();
7595}
7596
7597 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007598use_vtp(void)
7599{
7600 return USE_VTP;
7601}
7602
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007603 int
7604is_term_win32(void)
7605{
7606 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7607}
7608
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007609 int
7610has_vtp_working(void)
7611{
7612 return vtp_working;
7613}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007614
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007615#endif
7616
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007617 int
7618has_conpty_working(void)
7619{
7620 return conpty_working;
7621}
7622
7623 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007624get_conpty_type(void)
7625{
7626 return conpty_type;
7627}
7628
7629 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007630is_conpty_stable(void)
7631{
7632 return conpty_stable;
7633}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007634
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02007635 int
7636get_conpty_fix_type(void)
7637{
7638 return conpty_fix_type;
7639}
7640
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007641#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007642 void
7643resize_console_buf(void)
7644{
7645 CONSOLE_SCREEN_BUFFER_INFO csbi;
7646 COORD coord;
7647 SMALL_RECT newsize;
7648
7649 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7650 {
7651 coord.X = SRWIDTH(csbi.srWindow);
7652 coord.Y = SRHEIGHT(csbi.srWindow);
7653 SetConsoleScreenBufferSize(g_hConOut, coord);
7654
7655 newsize.Left = 0;
7656 newsize.Top = 0;
7657 newsize.Right = coord.X - 1;
7658 newsize.Bottom = coord.Y - 1;
7659 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7660
7661 SetConsoleScreenBufferSize(g_hConOut, coord);
7662 }
7663}
7664#endif