blob: f32d486a52d3fe11765f24fede619c13b8800f6f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * os_win32.c
11 *
12 * Used for both the console version and the Win32 GUI. A lot of code is for
Bram Moolenaar4f974752019-02-17 17:44:42 +010013 * the console version only, so there is a lot of "#ifndef FEAT_GUI_MSWIN".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * Win32 (Windows NT and Windows 95) system-dependent routines.
16 * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
17 * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
18 *
19 * George V. Reilly <george@reilly.org> wrote most of this.
20 * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0.
21 */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
Bram Moolenaar325b7a22004-07-05 15:58:32 +000025#ifdef FEAT_MZSCHEME
26# include "if_mzsch.h"
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#include <signal.h>
31#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010032
Bram Moolenaar0f873732019-12-05 20:28:46 +010033// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# include <process.h>
Bram Moolenaare7bebc42021-02-01 20:50:37 +010036# include <winternl.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39#undef chdir
40#ifdef __GNUC__
41# ifndef __MINGW32__
42# include <dirent.h>
43# endif
44#else
45# include <direct.h>
46#endif
47
Bram Moolenaar82881492012-11-20 16:53:39 +010048#ifndef PROTO
Bram Moolenaar651fca82021-11-29 20:39:38 +000049# if !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010050# include <shellapi.h>
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052#endif
53
Bram Moolenaarfb630902016-10-29 14:55:00 +020054#ifdef FEAT_JOB_CHANNEL
55# include <tlhelp32.h>
56#endif
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef __MINGW32__
59# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
60# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
61# endif
62# ifndef RIGHTMOST_BUTTON_PRESSED
63# define RIGHTMOST_BUTTON_PRESSED 0x0002
64# endif
65# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
66# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
67# endif
68# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
69# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
70# endif
71# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
72# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
73# endif
74
75/*
76 * EventFlags
77 */
78# ifndef MOUSE_MOVED
79# define MOUSE_MOVED 0x0001
80# endif
81# ifndef DOUBLE_CLICK
82# define DOUBLE_CLICK 0x0002
83# endif
84#endif
85
Bram Moolenaar0f873732019-12-05 20:28:46 +010086// Record all output and all keyboard & mouse input
87// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89#ifdef MCH_WRITE_DUMP
90FILE* fdDump = NULL;
91#endif
92
93/*
94 * When generating prototypes for Win32 on Unix, these lines make the syntax
95 * errors disappear. They do not need to be correct.
96 */
97#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010098# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000099typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000100typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101typedef int ACCESS_MASK;
102typedef int BOOL;
Paul Ollis65745772022-06-05 16:55:54 +0100103typedef int BOOLEAN;
104typedef int CALLBACK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105typedef int COLORREF;
106typedef int CONSOLE_CURSOR_INFO;
107typedef int COORD;
108typedef int DWORD;
109typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100110typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111typedef int HDC;
112typedef int HFONT;
113typedef int HICON;
114typedef int HINSTANCE;
115typedef int HWND;
116typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200117typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118typedef int KEY_EVENT_RECORD;
119typedef int LOGFONT;
120typedef int LPBOOL;
121typedef int LPCTSTR;
122typedef int LPDWORD;
123typedef int LPSTR;
124typedef int LPTSTR;
125typedef int LPVOID;
126typedef int MOUSE_EVENT_RECORD;
127typedef int PACL;
128typedef int PDWORD;
129typedef int PHANDLE;
130typedef int PRINTDLG;
131typedef int PSECURITY_DESCRIPTOR;
132typedef int PSID;
133typedef int SECURITY_INFORMATION;
134typedef int SHORT;
135typedef int SMALL_RECT;
136typedef int TEXTMETRIC;
137typedef int TOKEN_INFORMATION_CLASS;
138typedef int TRUSTEE;
139typedef int WORD;
140typedef int WCHAR;
141typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100142typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200143typedef int SE_OBJECT_TYPE;
144typedef int PSNSECINFO;
145typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100146typedef int STARTUPINFO;
147typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200148typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100149# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
151
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200152#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100153// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
155static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
156
Bram Moolenaar0f873732019-12-05 20:28:46 +0100157// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100159static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Bram Moolenaar0f873732019-12-05 20:28:46 +0100161// The attribute of the screen when the editor was started
162static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static WORD g_attrCurrent;
164
Bram Moolenaar0f873732019-12-05 20:28:46 +0100165static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
166static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
167static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static void scroll(unsigned cLines);
170static void set_scroll_region(unsigned left, unsigned top,
171 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100172static void set_scroll_region_tb(unsigned top, unsigned bottom);
173static void set_scroll_region_lr(unsigned left, unsigned right);
174static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void delete_lines(unsigned cLines);
176static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178static int s_cursor_visible = TRUE;
179static int did_create_conin = FALSE;
Christopher Plewright20b795e2022-12-20 20:01:58 +0000180// The 'input_record_buffer' is an internal dynamic fifo queue of MS-Windows
181// console INPUT_RECORD events that are normally read from the console input
182// buffer. This provides an injection point for testing the low-level handling
183// of INPUT_RECORDs.
184typedef struct input_record_buffer_node_S
185{
186 INPUT_RECORD ir;
187 struct input_record_buffer_node_S *next;
188} input_record_buffer_node_T;
189typedef struct input_record_buffer_S
190{
191 input_record_buffer_node_T *head;
192 input_record_buffer_node_T *tail;
193 int length;
194} input_record_buffer_T;
195static input_record_buffer_T input_record_buffer;
196static int peek_input_record_buffer(INPUT_RECORD* irEvents, int nMaxLength);
197static int read_input_record_buffer(INPUT_RECORD* irEvents, int nMaxLength);
198static int write_input_record_buffer(INPUT_RECORD* irEvents, int nLength);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200199#endif
200#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201static int s_dont_use_vimrun = TRUE;
202static int need_vimrun_warning = FALSE;
203static char *vimrun_path = "vimrun ";
204#endif
205
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200206static int win32_getattrs(char_u *name);
207static int win32_setattrs(char_u *name, int attrs);
208static int win32_set_archive(char_u *name);
209
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100210static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200211static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100212static int conpty_stable = 0;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +0200213static int conpty_fix_type = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100214static void vtp_flag_init();
215
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200216#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100217static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100218static void vtp_init();
219static void vtp_exit();
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100220static void vtp_sgr_bulk(int arg);
221static void vtp_sgr_bulks(int argc, int *argv);
222
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200223static int wt_working = 0;
Christopher Plewright1140b512022-11-12 18:46:05 +0000224static void wt_init(void);
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200225
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200226static int g_color_index_bg = 0;
227static int g_color_index_fg = 7;
228
229# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +0000230static guicolor_T save_console_bg_rgb;
231static guicolor_T save_console_fg_rgb;
232static guicolor_T store_console_bg_rgb;
233static guicolor_T store_console_fg_rgb;
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +0100234static int default_console_color_bg = 0x000000; // black
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200235static int default_console_color_fg = 0xc0c0c0; // white
Christopher Plewright38804d62022-11-09 23:55:52 +0000236# define USE_VTP (vtp_working && is_term_win32())
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200237# define USE_WT (wt_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100238# else
239# define USE_VTP 0
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200240# define USE_WT 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100241# endif
242
243static void set_console_color_rgb(void);
244static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100245static void restore_console_color_rgb(void);
Christopher Plewright20b795e2022-12-20 20:01:58 +0000246#endif // !FEAT_GUI_MSWIN || VIMDLL
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100247
Bram Moolenaar0f873732019-12-05 20:28:46 +0100248// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100249#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
250# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
251#endif
252
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200253#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100254static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#endif
256
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200257static char_u *exe_path = NULL;
258
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100259static BOOL win8_or_later = FALSE;
Christopher Plewright1140b512022-11-12 18:46:05 +0000260static BOOL win11_or_later = FALSE;
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100261
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100262/*
263 * Get version number including build number
264 */
265typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100266#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100267 (((major) << 24) | ((minor) << 16) | (build))
268
269 static DWORD
270get_build_number(void)
271{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100272 OSVERSIONINFOW osver;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100273 HMODULE hNtdll;
274 PfnRtlGetVersion pRtlGetVersion;
275 DWORD ver = MAKE_VER(0, 0, 0);
276
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100277 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100278 hNtdll = GetModuleHandle("ntdll.dll");
279 if (hNtdll != NULL)
280 {
281 pRtlGetVersion =
282 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
283 pRtlGetVersion(&osver);
284 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
285 min(osver.dwMinorVersion, 255),
286 min(osver.dwBuildNumber, 32767));
287 }
288 return ver;
289}
290
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200291#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200292 static BOOL
293is_ambiwidth_event(
294 INPUT_RECORD *ir)
295{
296 return ir->EventType == KEY_EVENT
297 && ir->Event.KeyEvent.bKeyDown
298 && ir->Event.KeyEvent.wRepeatCount == 1
299 && ir->Event.KeyEvent.wVirtualKeyCode == 0x12
300 && ir->Event.KeyEvent.wVirtualScanCode == 0x38
K.Takata972db232022-02-04 10:45:38 +0000301 && ir->Event.KeyEvent.uChar.UnicodeChar == 0
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200302 && ir->Event.KeyEvent.dwControlKeyState == 2;
303}
304
305 static void
306make_ambiwidth_event(
307 INPUT_RECORD *down,
308 INPUT_RECORD *up)
309{
310 down->Event.KeyEvent.wVirtualKeyCode = 0;
311 down->Event.KeyEvent.wVirtualScanCode = 0;
K.Takata972db232022-02-04 10:45:38 +0000312 down->Event.KeyEvent.uChar.UnicodeChar
313 = up->Event.KeyEvent.uChar.UnicodeChar;
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200314 down->Event.KeyEvent.dwControlKeyState = 0;
315}
316
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100317/*
318 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100319 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100320 */
321 static BOOL
322read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100323 HANDLE hInput,
324 INPUT_RECORD *lpBuffer,
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100325 int nLength,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100326 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100327{
328 enum
329 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100330 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100331 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100332 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100333 static DWORD s_dwIndex = 0;
334 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100335 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100336 int head;
337 int tail;
338 int i;
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200339 static INPUT_RECORD s_irPseudo;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100340
Christopher Plewright20b795e2022-12-20 20:01:58 +0000341 if (s_dwMax == 0 && input_record_buffer.length > 0)
342 {
343 dwEvents = read_input_record_buffer(s_irCache, IRSIZE);
344 s_dwIndex = 0;
345 s_dwMax = dwEvents;
346 }
347
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200348 if (nLength == -2)
349 return (s_dwMax > 0) ? TRUE : FALSE;
350
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100351 if (!win8_or_later)
352 {
353 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200354 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
355 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100356 }
357
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100358 if (s_dwMax == 0)
359 {
Christopher Plewright38804d62022-11-09 23:55:52 +0000360 if (!vtp_working && nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200361 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200362 GetNumberOfConsoleInputEvents(hInput, &dwEvents);
363 if (dwEvents == 0 && nLength == -1)
364 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
365 ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents);
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100366 s_dwIndex = 0;
367 s_dwMax = dwEvents;
368 if (dwEvents == 0)
369 {
370 *lpEvents = 0;
371 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100372 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100373
Bram Moolenaar06b7b582020-05-30 17:49:25 +0200374 for (i = s_dwIndex; i < (int)s_dwMax - 1; ++i)
375 if (is_ambiwidth_event(&s_irCache[i]))
376 make_ambiwidth_event(&s_irCache[i], &s_irCache[i + 1]);
377
Bram Moolenaardd415a62014-02-05 14:02:27 +0100378 if (s_dwMax > 1)
379 {
380 head = 0;
381 tail = s_dwMax - 1;
382 while (head != tail)
383 {
384 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
385 && s_irCache[head + 1].EventType
386 == WINDOW_BUFFER_SIZE_EVENT)
387 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100388 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100389 for (i = head; i < tail; ++i)
390 s_irCache[i] = s_irCache[i + 1];
391 --tail;
392 continue;
393 }
394 head++;
395 }
396 s_dwMax = tail + 1;
397 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100398 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100399
Bram Moolenaarbc970da2020-04-26 19:00:07 +0200400 if (s_irCache[s_dwIndex].EventType == KEY_EVENT)
401 {
402 if (s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount > 1)
403 {
404 s_irPseudo = s_irCache[s_dwIndex];
405 s_irPseudo.Event.KeyEvent.wRepeatCount = 1;
406 s_irCache[s_dwIndex].Event.KeyEvent.wRepeatCount--;
407 *lpBuffer = s_irPseudo;
408 *lpEvents = 1;
409 return TRUE;
410 }
411 }
412
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100413 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200414 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100415 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100416 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100417 return TRUE;
418}
419
420/*
421 * Version of PeekConsoleInput() that works with IME.
422 */
423 static BOOL
424peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100425 HANDLE hInput,
426 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200427 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100428 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100429{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100430 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100431}
432
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100433# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200434 static DWORD
435msg_wait_for_multiple_objects(
436 DWORD nCount,
437 LPHANDLE pHandles,
438 BOOL fWaitAll,
439 DWORD dwMilliseconds,
440 DWORD dwWakeMask)
441{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100442 if (read_console_input(NULL, NULL, -2, NULL))
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200443 return WAIT_OBJECT_0;
444 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
445 dwMilliseconds, dwWakeMask);
446}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100447# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200448
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100449# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200450 static DWORD
451wait_for_single_object(
452 HANDLE hHandle,
453 DWORD dwMilliseconds)
454{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100455 if (read_console_input(NULL, NULL, -2, NULL))
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200456 return WAIT_OBJECT_0;
457 return WaitForSingleObject(hHandle, dwMilliseconds);
458}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100459# endif
Christopher Plewright20b795e2022-12-20 20:01:58 +0000460#endif // !FEAT_GUI_MSWIN || VIMDLL
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 static void
463get_exe_name(void)
464{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100465 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
466 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100467#define MAX_ENV_PATH_LEN 8192
468 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200469 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471 if (exe_name == NULL)
472 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100473 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100474 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (*temp != NUL)
476 exe_name = FullName_save((char_u *)temp, FALSE);
477 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000478
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000480 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200481 exe_path = vim_strnsave(exe_name, gettail_sep(exe_name) - exe_name);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200482 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // Append our starting directory to $PATH, so that when doing
485 // "!xxd" it's found in our starting directory. Needed because
486 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200487 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100488 if (p == NULL
489 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200490 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100491 if (p == NULL || *p == NUL)
492 temp[0] = NUL;
493 else
494 {
495 STRCPY(temp, p);
496 STRCAT(temp, ";");
497 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200498 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100499 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200500 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200505/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100506 * Unescape characters in "p" that appear in "escaped".
507 */
508 static void
509unescape_shellxquote(char_u *p, char_u *escaped)
510{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100511 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100512 int n;
513
514 while (*p != NUL)
515 {
516 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
517 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100518 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100519 p += n;
520 l -= n;
521 }
522}
523
524/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200525 * Load library "name".
526 */
527 HINSTANCE
K.Takatad68b2fc2022-02-12 11:18:37 +0000528vimLoadLib(const char *name)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200529{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200530 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200531
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200532 // No need to load any library when registering OLE.
533 if (found_register_arg)
534 return dll;
535
Bram Moolenaar0f873732019-12-05 20:28:46 +0100536 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
537 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200538 if (exe_path == NULL)
539 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200540 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200541 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200542 WCHAR old_dirw[MAXPATHL];
543
544 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
545 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100546 // Change directory to where the executable is, both to make
547 // sure we find a .dll there and to avoid looking for a .dll
548 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100549 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200550 dll = LoadLibrary(name);
551 SetCurrentDirectoryW(old_dirw);
552 return dll;
553 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200554 }
555 return dll;
556}
557
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200558#if defined(VIMDLL) || defined(PROTO)
559/*
560 * Check if the current executable file is for the GUI subsystem.
561 */
562 int
563mch_is_gui_executable(void)
564{
565 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
566 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
567 PIMAGE_NT_HEADERS pPE;
568
569 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
570 return FALSE;
571 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
572 if (pPE->Signature != IMAGE_NT_SIGNATURE)
573 return FALSE;
574 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
575 return TRUE;
576 return FALSE;
577}
578#endif
579
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000580#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) \
581 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100582/*
583 * Get related information about 'funcname' which is imported by 'hInst'.
584 * If 'info' is 0, return the function address.
585 * If 'info' is 1, return the module name which the function is imported from.
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000586 * If 'info' is 2, hook the function with 'ptr', and return the original
587 * function address.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100588 */
589 static void *
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000590get_imported_func_info(HINSTANCE hInst, const char *funcname, int info,
591 const void *ptr)
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100592{
593 PBYTE pImage = (PBYTE)hInst;
594 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
595 PIMAGE_NT_HEADERS pPE;
596 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100597 PIMAGE_THUNK_DATA pIAT; // Import Address Table
598 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100599 PIMAGE_IMPORT_BY_NAME pImpName;
600
601 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
602 return NULL;
603 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
604 if (pPE->Signature != IMAGE_NT_SIGNATURE)
605 return NULL;
606 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
607 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
608 .VirtualAddress);
609 for (; pImpDesc->FirstThunk; ++pImpDesc)
610 {
611 if (!pImpDesc->OriginalFirstThunk)
612 continue;
613 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
614 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
615 for (; pIAT->u1.Function; ++pIAT, ++pINT)
616 {
617 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
618 continue;
619 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
620 + (UINT_PTR)(pINT->u1.AddressOfData));
621 if (strcmp((char *)pImpName->Name, funcname) == 0)
622 {
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000623 void *original;
624 DWORD old, new = PAGE_READWRITE;
625
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100626 switch (info)
627 {
628 case 0:
629 return (void *)pIAT->u1.Function;
630 case 1:
631 return (void *)(pImage + pImpDesc->Name);
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000632 case 2:
633 original = (void *)pIAT->u1.Function;
634 VirtualProtect(&pIAT->u1.Function, sizeof(void *),
635 new, &old);
636 pIAT->u1.Function = (UINT_PTR)ptr;
637 VirtualProtect(&pIAT->u1.Function, sizeof(void *),
638 old, &new);
639 return original;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100640 default:
641 return NULL;
642 }
643 }
644 }
645 }
646 return NULL;
647}
648
649/*
650 * Get the module handle which 'funcname' in 'hInst' is imported from.
651 */
652 HINSTANCE
653find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
654{
655 char *modulename;
656
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000657 modulename = (char *)get_imported_func_info(hInst, funcname, 1, NULL);
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100658 if (modulename != NULL)
659 return GetModuleHandleA(modulename);
660 return NULL;
661}
662
663/*
664 * Get the address of 'funcname' which is imported by 'hInst' DLL.
665 */
666 void *
667get_dll_import_func(HINSTANCE hInst, const char *funcname)
668{
Bram Moolenaar63ff72a2022-02-07 13:54:01 +0000669 return get_imported_func_info(hInst, funcname, 0, NULL);
670}
671
672/*
673 * Hook the function named 'funcname' which is imported by 'hInst' DLL,
674 * and return the original function address.
675 */
676 void *
677hook_dll_import_func(HINSTANCE hInst, const char *funcname, const void *hook)
678{
679 return get_imported_func_info(hInst, funcname, 2, hook);
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100680}
681#endif
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
684# ifndef GETTEXT_DLL
685# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200686# define GETTEXT_DLL_ALT1 "libintl-8.dll"
687# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100689// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000690static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200691static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000692static char *null_libintl_textdomain(const char *);
693static char *null_libintl_bindtextdomain(const char *, const char *);
694static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100695static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200697static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000698char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200699char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
700 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000701char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
702char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000704char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
705 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100706int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100709dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 int i;
712 static struct
713 {
714 char *name;
715 FARPROC *ptr;
716 } libintl_entry[] =
717 {
718 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200719 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
721 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
722 {NULL, NULL}
723 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100724 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaar7554c542018-10-06 15:03:15 +0200726 // No need to initialize twice.
727 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200729 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100730 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100731# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100732 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200733 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100734# endif
735# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200736 if (!hLibintlDLL)
737 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100738# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100739 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200741 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200743 verbose_enter();
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000744 semsg(_(e_could_not_load_library_str_str), GETTEXT_DLL, GetWin32Error());
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200745 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200747 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 for (i = 0; libintl_entry[i].name != NULL
750 && libintl_entry[i].ptr != NULL; ++i)
751 {
K.Takata54119102022-02-03 13:33:03 +0000752 if ((*libintl_entry[i].ptr = GetProcAddress(hLibintlDLL,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 libintl_entry[i].name)) == NULL)
754 {
755 dyn_libintl_end();
756 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000757 {
758 verbose_enter();
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000759 semsg(_(e_could_not_load_library_function_str), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000760 verbose_leave();
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 return 0;
763 }
764 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000765
Bram Moolenaar0f873732019-12-05 20:28:46 +0100766 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100767 dyn_libintl_bind_textdomain_codeset = (char *(*)(const char *, const char *))
768 GetProcAddress(hLibintlDLL, "bind_textdomain_codeset");
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000769 if (dyn_libintl_bind_textdomain_codeset == NULL)
770 dyn_libintl_bind_textdomain_codeset =
771 null_libintl_bind_textdomain_codeset;
772
Bram Moolenaar0f873732019-12-05 20:28:46 +0100773 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100774 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
775 if (hmsvcrt != NULL)
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100776 dyn_libintl_wputenv = (int (*)(const wchar_t *))
777 GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100778 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
779 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 return 1;
782}
783
784 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100785dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
787 if (hLibintlDLL)
788 FreeLibrary(hLibintlDLL);
789 hLibintlDLL = NULL;
790 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200791 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 dyn_libintl_textdomain = null_libintl_textdomain;
793 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000794 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100795 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000799null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 return (char*)msgid;
802}
803
804 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200805null_libintl_ngettext(
806 const char *msgid,
807 const char *msgid_plural,
808 unsigned long n)
809{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200810 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200811}
812
Bram Moolenaaree695f72016-08-03 22:08:45 +0200813 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100814null_libintl_bindtextdomain(
815 const char *domainname UNUSED,
816 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 return NULL;
819}
820
821 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100822null_libintl_bind_textdomain_codeset(
823 const char *domainname UNUSED,
824 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000825{
826 return NULL;
827}
828
829 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100830null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 return NULL;
833}
834
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200835 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100836null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100837{
838 return 0;
839}
840
Bram Moolenaar0f873732019-12-05 20:28:46 +0100841#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar0f873732019-12-05 20:28:46 +0100843// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845#ifndef VER_PLATFORM_WIN32_WINDOWS
846# define VER_PLATFORM_WIN32_WINDOWS 1
847#endif
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100850# ifndef PROTO
851# include <aclapi.h>
852# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200853# ifndef PROTECTED_DACL_SECURITY_INFORMATION
854# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
Bram Moolenaar27515922013-06-29 15:36:26 +0200858#ifdef HAVE_ACL
859/*
860 * Enables or disables the specified privilege.
861 */
862 static BOOL
863win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
864{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100865 BOOL bResult;
866 LUID luid;
867 HANDLE hToken;
868 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200869
870 if (!OpenProcessToken(GetCurrentProcess(),
871 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
872 return FALSE;
873
874 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
875 {
876 CloseHandle(hToken);
877 return FALSE;
878 }
879
Bram Moolenaar45500912014-07-09 20:51:07 +0200880 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200881 tokenPrivileges.Privileges[0].Luid = luid;
882 tokenPrivileges.Privileges[0].Attributes = bEnable ?
883 SE_PRIVILEGE_ENABLED : 0;
884
885 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
886 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
887
888 CloseHandle(hToken);
889
890 return bResult && GetLastError() == ERROR_SUCCESS;
891}
892#endif
893
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100894#ifdef _MSC_VER
895// Suppress the deprecation warning for using GetVersionEx().
896// It is needed for implementing "windowsversion()".
897# pragma warning(push)
898# pragma warning(disable: 4996)
899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200901 * Set "win8_or_later" and fill in "windowsVersion" if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 */
903 void
904PlatformId(void)
905{
906 static int done = FALSE;
907
908 if (!done)
909 {
910 OSVERSIONINFO ovi;
911
912 ovi.dwOSVersionInfoSize = sizeof(ovi);
913 GetVersionEx(&ovi);
914
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200915#ifdef FEAT_EVAL
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100916 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
917 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar1e1d2e82020-05-18 20:17:02 +0200918#endif
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100919 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
920 || ovi.dwMajorVersion > 6)
921 win8_or_later = TRUE;
922
Christopher Plewright1140b512022-11-12 18:46:05 +0000923 if ((ovi.dwMajorVersion == 10 && ovi.dwBuildNumber >= 22000)
924 || ovi.dwMajorVersion > 10)
925 win11_or_later = TRUE;
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100928 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200929 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931 done = TRUE;
932 }
933}
Bram Moolenaar29d2f452020-12-04 19:42:52 +0100934#ifdef _MSC_VER
935# pragma warning(pop)
936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200938#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100940# define SHIFT (SHIFT_PRESSED)
941# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
942# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
943# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945
Bram Moolenaar0f873732019-12-05 20:28:46 +0100946// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
947// We map function keys to their ANSI terminal equivalents, as produced
948// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
949// ANSI key with a value >= '\300' is nonstandard, but provided anyway
950// so that the user can have access to all SHIFT-, CTRL-, and ALT-
951// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000953static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
955 WORD wVirtKey;
956 BOOL fAnsiKey;
957 int chAlone;
958 int chShift;
959 int chCtrl;
960 int chAlt;
961} VirtKeyMap[] =
962{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200963// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
965
966 { VK_F1, TRUE, ';', 'T', '^', 'h', },
967 { VK_F2, TRUE, '<', 'U', '_', 'i', },
968 { VK_F3, TRUE, '=', 'V', '`', 'j', },
969 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
970 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
971 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
972 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
973 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
974 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
975 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200976 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
977 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200979 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
980 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
981 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
982 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
983 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
984 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
985 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
986 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
987 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
988 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100989 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200991 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100993# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200994 // Most people don't have F13-F20, but what the hell...
995 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
996 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
997 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
998 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
999 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
1000 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
1001 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
1002 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001003# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001004 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
1005 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
1006 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
1007 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001009 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
1010 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
1011 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
1012 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
1013 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
1014 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
1015 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
1016 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
1017 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
1018 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +01001019 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020};
1021
1022
Bram Moolenaar0f873732019-12-05 20:28:46 +01001023/*
1024 * The return code indicates key code size.
1025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001028 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 UINT uMods = pker->dwControlKeyState;
1031 static int s_iIsDead = 0;
1032 static WORD awAnsiCode[2];
1033 static BYTE abKeystate[256];
1034
1035
1036 if (s_iIsDead == 2)
1037 {
K.Takata972db232022-02-04 10:45:38 +00001038 pker->uChar.UnicodeChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 s_iIsDead = 0;
1040 return 1;
1041 }
1042
Christopher Plewright20b795e2022-12-20 20:01:58 +00001043 if (pker->uChar.UnicodeChar > 0 && pker->uChar.UnicodeChar < 0xfffd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 return 1;
1045
Bram Moolenaara80faa82020-04-12 19:37:17 +02001046 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar0f873732019-12-05 20:28:46 +01001048 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001049 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 if (uMods & SHIFT_PRESSED)
1052 abKeystate[VK_SHIFT] = 0x80;
1053 if (uMods & CAPSLOCK_ON)
1054 abKeystate[VK_CAPITAL] = 1;
1055
1056 if ((uMods & ALT_GR) == ALT_GR)
1057 {
1058 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
1059 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
1060 }
1061
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001062 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
1063 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 if (s_iIsDead > 0)
K.Takata972db232022-02-04 10:45:38 +00001066 pker->uChar.UnicodeChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 return s_iIsDead;
1069}
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071static BOOL g_fJustGotFocus = FALSE;
1072
1073/*
1074 * Decode a KEY_EVENT into one or two keystrokes
1075 */
1076 static BOOL
1077decode_key_event(
1078 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001079 WCHAR *pch,
1080 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001082 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 int i;
1085 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
1086
1087 *pch = *pch2 = NUL;
1088 g_fJustGotFocus = FALSE;
1089
Bram Moolenaar0f873732019-12-05 20:28:46 +01001090 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (!pker->bKeyDown)
1092 return FALSE;
1093
Bram Moolenaar0f873732019-12-05 20:28:46 +01001094 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 switch (pker->wVirtualKeyCode)
1096 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001097 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case VK_SHIFT:
1099 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001100 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 return FALSE;
1102
1103 default:
1104 break;
1105 }
1106
Bram Moolenaar0f873732019-12-05 20:28:46 +01001107 // special cases
K.Takata972db232022-02-04 10:45:38 +00001108 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0
Christopher Plewright20b795e2022-12-20 20:01:58 +00001109 && (pker->uChar.UnicodeChar == NUL
1110 || pker->uChar.UnicodeChar == 0xfffd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001112 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (pker->wVirtualKeyCode == '6')
1114 {
1115 *pch = Ctrl_HAT;
1116 return TRUE;
1117 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001118 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 else if (pker->wVirtualKeyCode == '2')
1120 {
1121 *pch = NUL;
1122 return TRUE;
1123 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001124 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else if (pker->wVirtualKeyCode == 0xBD)
1126 {
1127 *pch = Ctrl__;
1128 return TRUE;
1129 }
1130 }
1131
Bram Moolenaar0f873732019-12-05 20:28:46 +01001132 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1134 {
1135 *pch = K_NUL;
1136 *pch2 = '\017';
1137 return TRUE;
1138 }
1139
K.Takataeeec2542021-06-02 13:28:16 +02001140 for (i = ARRAY_LENGTH(VirtKeyMap); --i >= 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1143 {
1144 if (nModifs == 0)
1145 *pch = VirtKeyMap[i].chAlone;
1146 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1147 *pch = VirtKeyMap[i].chShift;
1148 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1149 *pch = VirtKeyMap[i].chCtrl;
1150 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1151 *pch = VirtKeyMap[i].chAlt;
1152
1153 if (*pch != 0)
1154 {
1155 if (VirtKeyMap[i].fAnsiKey)
1156 {
1157 *pch2 = *pch;
1158 *pch = K_NUL;
1159 }
1160
1161 return TRUE;
1162 }
1163 }
1164 }
1165
1166 i = win32_kbd_patch_key(pker);
1167
1168 if (i < 0)
1169 *pch = NUL;
1170 else
1171 {
K.Takata972db232022-02-04 10:45:38 +00001172 *pch = (i > 0) ? pker->uChar.UnicodeChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 if (pmodifiers != NULL)
1175 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001176 // Pass on the ALT key as a modifier, but only when not combined
1177 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1179 *pmodifiers |= MOD_MASK_ALT;
1180
Bram Moolenaar0f873732019-12-05 20:28:46 +01001181 // Pass on SHIFT only for special keys, because we don't know when
1182 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1184 *pmodifiers |= MOD_MASK_SHIFT;
1185
Bram Moolenaar0f873732019-12-05 20:28:46 +01001186 // Pass on CTRL only for non-special keys, because we don't know
1187 // when it's already included with the character. And not when
1188 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1190 && *pch >= 0x20 && *pch < 0x80)
1191 *pmodifiers |= MOD_MASK_CTRL;
1192 }
1193 }
1194
1195 return (*pch != NUL);
1196}
1197
Christopher Plewright20b795e2022-12-20 20:01:58 +00001198# if defined(FEAT_EVAL)
1199 static int
1200encode_key_event(dict_T *args, INPUT_RECORD *ir)
1201{
1202 static int s_dwMods = 0;
1203
1204 char_u *event = dict_get_string(args, "event", TRUE);
1205 if (event && (STRICMP(event, "keydown") == 0
1206 || STRICMP(event, "keyup") == 0))
1207 {
1208 WORD vkCode = dict_get_number_def(args, "keycode", 0);
1209 if (vkCode <= 0 || vkCode >= 0xFF)
1210 {
1211 semsg(_(e_invalid_argument_nr), (long)vkCode);
1212 return FALSE;
1213 }
1214
1215 ir->EventType = KEY_EVENT;
1216 KEY_EVENT_RECORD ker;
1217 ZeroMemory(&ker, sizeof(ker));
1218 ker.bKeyDown = STRICMP(event, "keydown") == 0;
1219 ker.wRepeatCount = 1;
1220 ker.wVirtualScanCode = 0;
1221 ker.dwControlKeyState = 0;
1222 int mods = (int)dict_get_number(args, "modifiers");
1223 // Encode the win32 console key modifiers from Vim keyboard modifiers.
1224 if (mods)
1225 {
1226 // If "modifiers" is explicitly set in the args, then we reset any
1227 // remembered modifer key state that may have been set from earlier
1228 // mod-key-down events, even if they are not yet unset by earlier
1229 // mod-key-up events.
1230 s_dwMods = 0;
1231 if (mods & MOD_MASK_SHIFT)
1232 ker.dwControlKeyState |= SHIFT_PRESSED;
1233 if (mods & MOD_MASK_CTRL)
1234 ker.dwControlKeyState |= LEFT_CTRL_PRESSED;
1235 if (mods & MOD_MASK_ALT)
1236 ker.dwControlKeyState |= LEFT_ALT_PRESSED;
1237 }
1238
1239 if (vkCode == VK_LSHIFT || vkCode == VK_RSHIFT || vkCode == VK_SHIFT)
1240 {
1241 if (STRICMP(event, "keydown") == 0)
1242 s_dwMods |= SHIFT_PRESSED;
1243 else
1244 s_dwMods &= ~SHIFT_PRESSED;
1245 }
1246 else if (vkCode == VK_LCONTROL || vkCode == VK_CONTROL)
1247 {
1248 if (STRICMP(event, "keydown") == 0)
1249 s_dwMods |= LEFT_CTRL_PRESSED;
1250 else
1251 s_dwMods &= ~LEFT_CTRL_PRESSED;
1252 }
1253 else if (vkCode == VK_RCONTROL)
1254 {
1255 if (STRICMP(event, "keydown") == 0)
1256 s_dwMods |= RIGHT_CTRL_PRESSED;
1257 else
1258 s_dwMods &= ~RIGHT_CTRL_PRESSED;
1259 }
1260 else if (vkCode == VK_LMENU || vkCode == VK_MENU)
1261 {
1262 if (STRICMP(event, "keydown") == 0)
1263 s_dwMods |= LEFT_ALT_PRESSED;
1264 else
1265 s_dwMods &= ~LEFT_ALT_PRESSED;
1266 }
1267 else if (vkCode == VK_RMENU)
1268 {
1269 if (STRICMP(event, "keydown") == 0)
1270 s_dwMods |= RIGHT_ALT_PRESSED;
1271 else
1272 s_dwMods &= ~RIGHT_ALT_PRESSED;
1273 }
1274 ker.dwControlKeyState |= s_dwMods;
1275 ker.wVirtualKeyCode = vkCode;
1276 win32_kbd_patch_key(&ker);
1277
1278 for (int i = ARRAY_LENGTH(VirtKeyMap);
1279 --i >= 0 && !ker.uChar.UnicodeChar; )
1280 {
1281 if (VirtKeyMap[i].wVirtKey == vkCode)
1282 ker.uChar.UnicodeChar = 0xfffd; // REPLACEMENT CHARACTER
1283 }
1284
1285 ir->Event.KeyEvent = ker;
1286 vim_free(event);
1287 }
1288 else
1289 {
1290 if (event == NULL)
1291 {
1292 semsg(_(e_missing_argument_str), "event");
1293 }
1294 else
1295 {
1296 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
1297 vim_free(event);
1298 }
1299 return FALSE;
1300 }
1301 return TRUE;
1302}
1303# endif // FEAT_EVAL
1304#endif // !FEAT_GUI_MSWIN || VIMDLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307/*
1308 * For the GUI the mouse handling is in gui_w32.c.
1309 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001310#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001312mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314}
Christopher Plewright20b795e2022-12-20 20:01:58 +00001315#else // !FEAT_GUI_MSWIN || VIMDLL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001316static int g_fMouseAvail = FALSE; // mouse present
1317static int g_fMouseActive = FALSE; // mouse enabled
1318static int g_nMouseClick = -1; // mouse status
1319static int g_xMouse; // mouse x coordinate
1320static int g_yMouse; // mouse y coordinate
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001321static DWORD g_cmodein = 0; // Original console input mode
1322static DWORD g_cmodeout = 0; // Original console output mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324/*
1325 * Enable or disable mouse input
1326 */
1327 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001328mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 DWORD cmodein;
1331
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001332# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001333 if (gui.in_use)
1334 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (!g_fMouseAvail)
1337 return;
1338
1339 g_fMouseActive = on;
1340 GetConsoleMode(g_hConIn, &cmodein);
1341
1342 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001345 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
1346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 else
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 cmodein &= ~ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001350 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Wez Furlong6ef5ab52021-05-30 19:29:41 +02001353 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
Bram Moolenaar157d8132018-03-06 17:09:20 +01001356
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001357# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001358/*
1359 * Called when 'balloonevalterm' changed.
1360 */
1361 void
1362mch_bevalterm_changed(void)
1363{
1364 mch_setmouse(g_fMouseActive);
1365}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001366# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001367
Christopher Plewright2a46f812022-10-16 19:47:45 +01001368/*
1369 * Win32 console mouse scroll event handler.
Christopher Plewright20b795e2022-12-20 20:01:58 +00001370 * Console version of the _OnMouseWheel() function in gui_w32.c
Christopher Plewright2a46f812022-10-16 19:47:45 +01001371 *
1372 * This encodes the mouse scroll direction and keyboard modifiers into
1373 * g_nMouseClick, and the mouse position into g_xMouse and g_yMouse
1374 *
1375 * The direction of the scroll is decoded from two fields of the win32 console
1376 * mouse event record;
Christopher Plewright20b795e2022-12-20 20:01:58 +00001377 * 1. The orientation - vertical or horizontal flag - from dwEventFlags
Christopher Plewright2a46f812022-10-16 19:47:45 +01001378 * 2. The sign - positive or negative (aka delta flag) - from dwButtonState
1379 *
Christopher Plewright20b795e2022-12-20 20:01:58 +00001380 * When scroll orientation is HORIZONTAL
Christopher Plewright2a46f812022-10-16 19:47:45 +01001381 * - If the high word of the dwButtonState member contains a positive
1382 * value, the wheel was rotated to the right.
1383 * - Otherwise, the wheel was rotated to the left.
Christopher Plewright20b795e2022-12-20 20:01:58 +00001384 * When scroll orientation is VERTICAL
Christopher Plewright2a46f812022-10-16 19:47:45 +01001385 * - If the high word of the dwButtonState member contains a positive value,
1386 * the wheel was rotated forward, away from the user.
1387 * - Otherwise, the wheel was rotated backward, toward the user.
1388 */
1389 static void
1390decode_mouse_wheel(MOUSE_EVENT_RECORD *pmer)
1391{
Christopher Plewright2a46f812022-10-16 19:47:45 +01001392 int horizontal = (pmer->dwEventFlags == MOUSE_HWHEELED);
1393 int zDelta = pmer->dwButtonState;
1394
1395 g_xMouse = pmer->dwMousePosition.X;
1396 g_yMouse = pmer->dwMousePosition.Y;
1397
K.Takata161b6ac2022-11-14 15:31:07 +00001398# ifdef FEAT_PROP_POPUP
Christopher Plewright605d02a2022-10-19 11:54:46 +01001399 int lcol = g_xMouse;
1400 int lrow = g_yMouse;
Christopher Plewright38804d62022-11-09 23:55:52 +00001401 win_T *wp = mouse_find_win(&lrow, &lcol, FIND_POPUP);
Christopher Plewright2a46f812022-10-16 19:47:45 +01001402 if (wp != NULL && popup_is_popup(wp))
1403 {
1404 g_nMouseClick = -1;
1405 cmdarg_T cap;
1406 oparg_T oa;
1407 CLEAR_FIELD(cap);
1408 clear_oparg(&oa);
1409 cap.oap = &oa;
1410 if (horizontal)
1411 {
1412 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
1413 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
1414 }
1415 else
1416 {
1417 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
1418 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
1419 }
1420
1421 // Mouse hovers over popup window, scroll it if possible.
1422 mouse_row = wp->w_winrow;
1423 mouse_col = wp->w_wincol;
1424 nv_mousescroll(&cap);
1425 update_screen(0);
1426 setcursor();
1427 out_flush();
1428 return;
1429 }
K.Takata161b6ac2022-11-14 15:31:07 +00001430# endif
Christopher Plewright2a46f812022-10-16 19:47:45 +01001431 mouse_col = g_xMouse;
1432 mouse_row = g_yMouse;
1433
1434 char_u modifiers = 0;
1435 char_u direction = 0;
1436
1437 // Decode the direction into an event that Vim can process
1438 if (horizontal)
1439 direction = zDelta >= 0 ? KE_MOUSELEFT : KE_MOUSERIGHT;
1440 else
1441 direction = zDelta >= 0 ? KE_MOUSEDOWN : KE_MOUSEUP;
1442
dundargocc57b5bc2022-11-02 13:30:51 +00001443 // Decode the win32 console key modifiers into Vim mouse modifiers.
Christopher Plewright2a46f812022-10-16 19:47:45 +01001444 if (pmer->dwControlKeyState & SHIFT_PRESSED)
Christopher Plewright605d02a2022-10-19 11:54:46 +01001445 modifiers |= MOD_MASK_SHIFT; // MOUSE_SHIFT;
Christopher Plewright2a46f812022-10-16 19:47:45 +01001446 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
Christopher Plewright605d02a2022-10-19 11:54:46 +01001447 modifiers |= MOD_MASK_CTRL; // MOUSE_CTRL;
Christopher Plewright2a46f812022-10-16 19:47:45 +01001448 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1449 modifiers |= MOD_MASK_ALT; // MOUSE_ALT;
1450
1451 // add (bitwise or) the scroll direction and the key modifier chars
1452 // together.
1453 g_nMouseClick = ((direction << 8) | modifiers);
1454
1455 return;
1456}
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458/*
1459 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1460 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1461 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1462 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1463 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1464 * and we return the mouse position in g_xMouse and g_yMouse.
1465 *
1466 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1467 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1468 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1469 *
1470 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1471 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1472 *
1473 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1474 * moves, even if it stays within the same character cell. We ignore
1475 * all MOUSE_MOVED messages if the position hasn't really changed, and
1476 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1477 * we're only interested in MOUSE_DRAG).
1478 *
1479 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1480 * 2-button mouses by pressing the left & right buttons simultaneously.
1481 * In practice, it's almost impossible to click both at the same time,
1482 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1483 * in such cases, if the user is clicking quickly.
1484 */
1485 static BOOL
1486decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001487 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 static int s_nOldButton = -1;
1490 static int s_nOldMouseClick = -1;
1491 static int s_xOldMouse = -1;
1492 static int s_yOldMouse = -1;
1493 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001494# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 static int s_cClicks = 1;
1498 static BOOL s_fReleased = TRUE;
1499 static DWORD s_dwLastClickTime = 0;
1500 static BOOL s_fNextIsMiddle = FALSE;
1501
Bram Moolenaar0f873732019-12-05 20:28:46 +01001502 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1505 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1506 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1507 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1508
1509 int nButton;
1510
1511 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1512 cButtons = 2;
1513
1514 if (!g_fMouseAvail || !g_fMouseActive)
1515 {
1516 g_nMouseClick = -1;
1517 return FALSE;
1518 }
1519
Bram Moolenaar0f873732019-12-05 20:28:46 +01001520 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (g_fJustGotFocus)
1522 {
1523 g_fJustGotFocus = FALSE;
1524 return FALSE;
1525 }
1526
Christopher Plewright605d02a2022-10-19 11:54:46 +01001527 // If there is an unprocessed mouse click drop this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (g_nMouseClick != -1)
1529 return TRUE;
Christopher Plewright605d02a2022-10-19 11:54:46 +01001530
Christopher Plewright2a46f812022-10-16 19:47:45 +01001531 if (pmer->dwEventFlags == MOUSE_WHEELED
1532 || pmer->dwEventFlags == MOUSE_HWHEELED)
1533 {
1534 decode_mouse_wheel(pmer);
1535 return TRUE; // we now should have a mouse scroll in g_nMouseClick
1536 }
Christopher Plewright605d02a2022-10-19 11:54:46 +01001537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 nButton = -1;
1539 g_xMouse = pmer->dwMousePosition.X;
1540 g_yMouse = pmer->dwMousePosition.Y;
1541
1542 if (pmer->dwEventFlags == MOUSE_MOVED)
1543 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001544 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1545 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1547 return FALSE;
1548 }
1549
Bram Moolenaar0f873732019-12-05 20:28:46 +01001550 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1552 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001553 nButton = MOUSE_RELEASE;
1554
Bram Moolenaar0f873732019-12-05 20:28:46 +01001555 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001557 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001558# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001559 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001560 if (p_bevalterm)
1561 nButton = MOUSE_DRAG;
1562 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001563# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001564 return FALSE;
1565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 s_fReleased = TRUE;
1568 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001569 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001571 // on a 2-button mouse, hold down left and right buttons
1572 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1575 {
1576 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1577
Bram Moolenaar0f873732019-12-05 20:28:46 +01001578 // if either left or right button only is pressed, see if the
1579 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (dwLR == LEFT || dwLR == RIGHT)
1581 {
1582 for (;;)
1583 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001584 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1586 != WAIT_OBJECT_0)
1587 break;
1588 else
1589 {
1590 DWORD cRecords = 0;
1591 INPUT_RECORD ir;
1592 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1593
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001594 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1597 || !(pmer2->dwButtonState & LEFT_RIGHT))
1598 break;
1599 else
1600 {
1601 if (pmer2->dwEventFlags != MOUSE_MOVED)
1602 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001603 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605 return decode_mouse_event(pmer2);
1606 }
1607 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1608 s_yOldMouse == pmer2->dwMousePosition.Y)
1609 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001610 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001611 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar0f873732019-12-05 20:28:46 +01001613 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001614 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1617 break;
1618 }
1619 else
1620 break;
1621 }
1622 }
1623 }
1624 }
1625 }
1626
1627 if (s_fNextIsMiddle)
1628 {
1629 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1630 ? MOUSE_DRAG : MOUSE_MIDDLE;
1631 s_fNextIsMiddle = FALSE;
1632 }
1633 else if (cButtons == 2 &&
1634 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1635 {
1636 nButton = MOUSE_MIDDLE;
1637
1638 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1639 {
1640 s_fNextIsMiddle = TRUE;
1641 nButton = MOUSE_RELEASE;
1642 }
1643 }
1644 else if ((pmer->dwButtonState & LEFT) == LEFT)
1645 nButton = MOUSE_LEFT;
1646 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1647 nButton = MOUSE_MIDDLE;
1648 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1649 nButton = MOUSE_RIGHT;
1650
1651 if (! s_fReleased && ! s_fNextIsMiddle
1652 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1653 return FALSE;
1654
1655 s_fReleased = s_fNextIsMiddle;
1656 }
1657
1658 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001660 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (nButton != -1 && nButton != MOUSE_RELEASE)
1662 {
1663 DWORD dwCurrentTime = GetTickCount();
1664
1665 if (s_xOldMouse != g_xMouse
1666 || s_yOldMouse != g_yMouse
1667 || s_nOldButton != nButton
1668 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001669# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1673 {
1674 s_cClicks = 1;
1675 }
1676 else if (++s_cClicks > 4)
1677 {
1678 s_cClicks = 1;
1679 }
1680
1681 s_dwLastClickTime = dwCurrentTime;
1682 }
1683 }
1684 else if (pmer->dwEventFlags == MOUSE_MOVED)
1685 {
1686 if (nButton != -1 && nButton != MOUSE_RELEASE)
1687 nButton = MOUSE_DRAG;
1688
1689 s_cClicks = 1;
1690 }
1691
1692 if (nButton == -1)
1693 return FALSE;
1694
1695 if (nButton != MOUSE_RELEASE)
1696 s_nOldButton = nButton;
1697
1698 g_nMouseClick = nButton;
1699
1700 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1701 g_nMouseClick |= MOUSE_SHIFT;
1702 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1703 g_nMouseClick |= MOUSE_CTRL;
1704 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1705 g_nMouseClick |= MOUSE_ALT;
1706
1707 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1708 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1709
Bram Moolenaar0f873732019-12-05 20:28:46 +01001710 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (s_xOldMouse == g_xMouse
1712 && s_yOldMouse == g_yMouse
1713 && s_nOldMouseClick == g_nMouseClick)
1714 {
1715 g_nMouseClick = -1;
1716 return FALSE;
1717 }
1718
1719 s_xOldMouse = g_xMouse;
1720 s_yOldMouse = g_yMouse;
1721 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001722# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 s_nOldMouseClick = g_nMouseClick;
1726
1727 return TRUE;
1728}
1729
Christopher Plewright20b795e2022-12-20 20:01:58 +00001730# ifdef FEAT_EVAL
1731 static int
1732encode_mouse_event(dict_T *args, INPUT_RECORD *ir)
1733{
1734 int button;
1735 int row;
1736 int col;
1737 int repeated_click;
1738 int_u mods;
1739 int move;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Christopher Plewright20b795e2022-12-20 20:01:58 +00001741 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
1742 return FALSE;
1743
1744 // Note: "move" is optional, requires fewer arguments
1745 move = (int)dict_get_bool(args, "move", FALSE);
1746 if (!move && (!dict_has_key(args, "button")
1747 || !dict_has_key(args, "multiclick")
1748 || !dict_has_key(args, "modifiers")))
1749 return FALSE;
1750
1751 row = (int)dict_get_number(args, "row") - 1;
1752 col = (int)dict_get_number(args, "col") - 1;
1753
1754 ir->EventType = MOUSE_EVENT;
1755 MOUSE_EVENT_RECORD mer;
1756 ZeroMemory(&mer, sizeof(mer));
1757 mer.dwMousePosition.X = col;
1758 mer.dwMousePosition.Y = row;
1759
1760 if (move)
1761 {
1762 mer.dwButtonState = 0;
1763 mer.dwEventFlags = MOUSE_MOVED;
1764 }
1765 else
1766 {
1767 button = (int)dict_get_number(args, "button");
1768 repeated_click = (int)dict_get_number(args, "multiclick");
1769 mods = (int)dict_get_number(args, "modifiers");
1770 // Reset the scroll values to known values.
1771 // XXX: Remove this when/if the scroll step is made configurable.
1772 mouse_set_hor_scroll_step(6);
1773 mouse_set_vert_scroll_step(3);
1774
1775 switch (button)
1776 {
1777 case MOUSE_LEFT:
1778 mer.dwButtonState = FROM_LEFT_1ST_BUTTON_PRESSED;
1779 mer.dwEventFlags = repeated_click == 1 ? DOUBLE_CLICK : 0;
1780 break;
1781 case MOUSE_MIDDLE:
1782 mer.dwButtonState = FROM_LEFT_2ND_BUTTON_PRESSED;
1783 mer.dwEventFlags = repeated_click == 1 ? DOUBLE_CLICK : 0;
1784 break;
1785 case MOUSE_RIGHT:
1786 mer.dwButtonState = RIGHTMOST_BUTTON_PRESSED;
1787 mer.dwEventFlags = repeated_click == 1 ? DOUBLE_CLICK : 0;
1788 break;
1789 case MOUSE_RELEASE:
1790 // umm? Assume Left Release?
1791 mer.dwEventFlags = 0;
1792
1793 case MOUSE_MOVE:
1794 mer.dwButtonState = 0;
1795 mer.dwEventFlags = MOUSE_MOVED;
1796 break;
1797 case MOUSE_X1:
1798 mer.dwButtonState = FROM_LEFT_3RD_BUTTON_PRESSED;
1799 break;
1800 case MOUSE_X2:
1801 mer.dwButtonState = FROM_LEFT_4TH_BUTTON_PRESSED;
1802 break;
1803 case MOUSE_4: // KE_MOUSEDOWN;
1804 mer.dwButtonState = -1;
1805 mer.dwEventFlags = MOUSE_WHEELED;
1806 break;
1807 case MOUSE_5: // KE_MOUSEUP;
1808 mer.dwButtonState = +1;
1809 mer.dwEventFlags = MOUSE_WHEELED;
1810 break;
1811 case MOUSE_6: // KE_MOUSELEFT;
1812 mer.dwButtonState = -1;
1813 mer.dwEventFlags = MOUSE_HWHEELED;
1814 break;
1815 case MOUSE_7: // KE_MOUSERIGHT;
1816 mer.dwButtonState = +1;
1817 mer.dwEventFlags = MOUSE_HWHEELED;
1818 break;
1819 default:
1820 semsg(_(e_invalid_argument_str), "button");
1821 return FALSE;
1822 }
1823 }
1824
1825 mer.dwControlKeyState = 0;
1826 if (mods != 0)
1827 {
1828 // Encode the win32 console key modifiers from Vim MOUSE modifiers.
1829 if (mods & MOUSE_SHIFT)
1830 mer.dwControlKeyState |= SHIFT_PRESSED;
1831 if (mods & MOUSE_CTRL)
1832 mer.dwControlKeyState |= LEFT_CTRL_PRESSED;
1833 if (mods & MOUSE_ALT)
1834 mer.dwControlKeyState |= LEFT_ALT_PRESSED;
1835 }
1836 ir->Event.MouseEvent = mer;
1837 return TRUE;
1838}
1839# endif // FEAT_EVAL
1840
1841 static int
1842write_input_record_buffer(INPUT_RECORD* irEvents, int nLength)
1843{
1844 int nCount = 0;
1845 while (nCount < nLength)
1846 {
1847 input_record_buffer.length++;
1848 input_record_buffer_node_T *event_node =
1849 malloc(sizeof(input_record_buffer_node_T));
1850 event_node->ir = irEvents[nCount++];
1851 event_node->next = NULL;
1852 if (input_record_buffer.tail == NULL)
1853 {
1854 input_record_buffer.head = event_node;
1855 input_record_buffer.tail = event_node;
1856 }
1857 else
1858 {
1859 input_record_buffer.tail->next = event_node;
1860 input_record_buffer.tail = input_record_buffer.tail->next;
1861 }
1862 }
1863 return nCount;
1864}
1865
1866 static int
1867read_input_record_buffer(INPUT_RECORD* irEvents, int nMaxLength)
1868{
1869 int nCount = 0;
1870 while (nCount < nMaxLength && input_record_buffer.head != NULL)
1871 {
1872 input_record_buffer.length--;
1873 input_record_buffer_node_T *pop_head = input_record_buffer.head;
1874 irEvents[nCount++] = pop_head->ir;
1875 input_record_buffer.head = pop_head->next;
1876 vim_free(pop_head);
1877 if (input_record_buffer.length == 0)
1878 input_record_buffer.tail = NULL;
1879 }
1880 return nCount;
1881}
1882 static int
1883peek_input_record_buffer(INPUT_RECORD* irEvents, int nMaxLength)
1884{
1885 int nCount = 0;
1886 input_record_buffer_node_T *temp = input_record_buffer.head;
1887 while (nCount < nMaxLength && temp != NULL)
1888 {
1889 irEvents[nCount++] = temp->ir;
1890 temp = temp->next;
1891 }
1892 return nCount;
1893}
1894#endif // !FEAT_GUI_MSWIN || VIMDLL
1895
1896#ifdef FEAT_EVAL
1897/*
1898 * The 'test_mswin_event' function is for testing Vim's low-level handling of
1899 * user input events. ie, this manages the encoding of INPUT_RECORD events
1900 * so that we have a way to test how Vim decodes INPUT_RECORD events in Windows
1901 * consoles.
1902 *
1903 * The 'test_mswin_event' function is based on 'test_gui_event'. In fact, when
1904 * the Windows GUI is running, the arguments; 'event' and 'args', are the same.
1905 * So, it acts as an alias for 'test_gui_event' for the Windows GUI.
1906 *
1907 * When the Windows console is running, the arguments; 'event' and 'args', are
1908 * a subset of what 'test_gui_event' handles, ie, only "key" and "mouse"
1909 * events are encoded as INPUT_RECORD events.
1910 *
1911 * Note: INPUT_RECORDs are only used by the Windows console, not the GUI. The
1912 * GUI sends MSG structs instead.
1913 */
1914 int
1915test_mswin_event(char_u *event, dict_T *args)
1916{
1917 int lpEventsWritten = 0;
1918
1919# if defined(VIMDLL) || defined(FEAT_GUI_MSWIN)
1920 if (gui.in_use)
1921 return test_gui_w32_sendevent(event, args);
1922# endif
1923
1924# if defined(VIMDLL) || !defined(FEAT_GUI_MSWIN)
1925
1926// Currently implemented event record types are; KEY_EVENT and MOUSE_EVENT
1927// Potentially could also implement: FOCUS_EVENT and WINDOW_BUFFER_SIZE_EVENT
1928// Maybe also: MENU_EVENT
1929
1930 INPUT_RECORD ir;
1931 BOOL input_encoded = FALSE;
1932 if (STRCMP(event, "key") == 0)
1933 input_encoded = encode_key_event(args, &ir);
1934 else if (STRCMP(event, "mouse") == 0)
1935 input_encoded = encode_mouse_event(args, &ir);
1936 else
1937 {
1938 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
1939 return FALSE;
1940 }
1941
1942 // Ideally, WriteConsoleInput would be used to inject these low-level
1943 // events. But, this doesnt work well in the CI test environment. So
1944 // implementing an input_record_buffer instead.
1945 if (input_encoded)
1946 lpEventsWritten = write_input_record_buffer(&ir, 1);
1947
1948 if (STRCMP(event, "mouse") == 0)
1949 exec_normal(TRUE, TRUE, TRUE);
1950
1951# endif
1952 return lpEventsWritten;
1953}
1954#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956#ifdef MCH_CURSOR_SHAPE
1957/*
1958 * Set the shape of the cursor.
1959 * 'thickness' can be from 1 (thin) to 99 (block)
1960 */
1961 static void
1962mch_set_cursor_shape(int thickness)
1963{
Christopher Plewright38804d62022-11-09 23:55:52 +00001964 if (vtp_working)
K.Takatadf5320c2022-09-01 13:20:16 +01001965 {
1966 if (*T_CSI == NUL)
1967 {
1968 // If 't_SI' is not set, use the default cursor styles.
1969 if (thickness < 50)
1970 vtp_printf("\033[3 q"); // underline
1971 else
1972 vtp_printf("\033[0 q"); // default
1973 }
1974 }
1975 else
1976 {
1977 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1978 ConsoleCursorInfo.dwSize = thickness;
1979 ConsoleCursorInfo.bVisible = s_cursor_visible;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
K.Takatadf5320c2022-09-01 13:20:16 +01001981 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1982 if (s_cursor_visible)
1983 SetConsoleCursorPosition(g_hConOut, g_coord);
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985}
1986
1987 void
1988mch_update_cursor(void)
1989{
1990 int idx;
1991 int thickness;
1992
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001993# ifdef VIMDLL
1994 if (gui.in_use)
1995 return;
1996# endif
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 /*
1999 * How the cursor is drawn depends on the current mode.
2000 */
2001 idx = get_shape_idx(FALSE);
2002
2003 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002004 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 else
2006 thickness = shape_table[idx].percentage;
2007 mch_set_cursor_shape(thickness);
2008}
2009#endif
2010
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002011#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012/*
2013 * Handle FOCUS_EVENT.
2014 */
2015 static void
2016handle_focus_event(INPUT_RECORD ir)
2017{
2018 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
2019 ui_focus_change((int)g_fJustGotFocus);
2020}
2021
Bram Moolenaar78d21da2019-02-17 15:00:52 +01002022static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024/*
2025 * Wait until console input from keyboard or mouse is available,
2026 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002027 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 * Return TRUE if something is available FALSE if not.
2029 */
2030 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002031WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 DWORD dwNow = 0, dwEndTime = 0;
2034 INPUT_RECORD ir;
2035 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002036 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002037# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002038 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002042 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 dwEndTime = GetTickCount() + msec;
2044 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002045 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 dwEndTime = INFINITE;
2047
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002048 // We need to loop until the end of the time period, because
2049 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 for (;;)
2051 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02002052 // Only process messages when waiting.
2053 if (msec != 0)
2054 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002055# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02002056 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002057# endif
2058# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02002059 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002060# endif
2061# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02002062 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002063# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02002064 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01002065
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002066 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002067# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002068 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 )
2071 return TRUE;
2072
2073 if (msec > 0)
2074 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002075 // If the specified wait time has passed, return. Beware that
2076 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02002078 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 break;
2080 }
2081 if (msec != 0)
2082 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002083 DWORD dwWaitTime = dwEndTime - dwNow;
2084
Bram Moolenaarc478ee32020-12-01 21:27:51 +01002085 // Don't wait for more than 11 msec to avoid dropping characters,
2086 // check channel while waiting for input and handle a callback from
2087 // 'balloonexpr'.
2088 if (dwWaitTime > 11)
2089 dwWaitTime = 11;
2090
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002091# ifdef FEAT_MZSCHEME
Bram Moolenaarc478ee32020-12-01 21:27:51 +01002092 if (mzthreads_allowed() && p_mzq > 0 && (long)dwWaitTime > p_mzq)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002093 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002094# endif
2095# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002096 // When waiting very briefly don't trigger timers.
2097 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01002098 {
2099 long due_time;
2100
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002101 // Trigger timers and then get the time in msec until the next
2102 // one is due. Wait up to that time.
2103 due_time = check_due_timer();
2104 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01002105 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002106 // timer may have used feedkeys().
2107 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01002108 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002109 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
2110 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01002111 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002112# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002113 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002114# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002115 // Wait for either an event on the console input or a
2116 // message in the client-server window.
2117 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
2118 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002119# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002120 wait_for_single_object(g_hConIn, dwWaitTime)
2121 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002122# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01002123 )
2124 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126
2127 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01002128 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002130# ifdef FEAT_MBYTE_IME
matveyt4eb19142021-05-20 11:54:10 +02002131 // May have to redraw if the cursor ends up in the wrong place.
2132 // Only when not peeking.
Bram Moolenaar24959102022-05-07 20:01:16 +01002133 if (State == MODE_CMDLINE && msg_row == Rows - 1 && msec != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
2135 CONSOLE_SCREEN_BUFFER_INFO csbi;
2136
2137 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2138 {
2139 if (csbi.dwCursorPosition.Y != msg_row)
2140 {
matveyte08795e2021-05-07 15:00:17 +02002141 // The screen is now messed up, must redraw the command
2142 // line and later all the windows.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002143 redraw_all_later(UPD_CLEAR);
matveyte08795e2021-05-07 15:00:17 +02002144 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 redrawcmd();
2146 }
2147 }
2148 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151 if (cRecords > 0)
2152 {
2153 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
2154 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01002156 // Windows IME sends two '\n's with only one 'ENTER'. First:
2157 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
K.Takata972db232022-02-04 10:45:38 +00002158 if (ir.Event.KeyEvent.uChar.UnicodeChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
2160 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01002161 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 continue;
2163 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
2166 NULL, FALSE))
2167 return TRUE;
2168 }
2169
Bram Moolenaar3a69e112014-01-10 13:51:42 +01002170 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172 if (ir.EventType == FOCUS_EVENT)
2173 handle_focus_event(ir);
2174 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01002175 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01002176 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
2177
Bram Moolenaar36698e32019-12-11 22:57:40 +01002178 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002179 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01002180 if (dwSize.X != Columns || dwSize.Y != Rows)
2181 {
2182 CONSOLE_SCREEN_BUFFER_INFO csbi;
2183 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01002184 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01002185 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01002186 if (dwSize.X != Columns || dwSize.Y != Rows)
2187 {
2188 ResizeConBuf(g_hConOut, dwSize);
2189 shell_resized();
2190 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01002191 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01002192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else if (ir.EventType == MOUSE_EVENT
2194 && decode_mouse_event(&ir.Event.MouseEvent))
2195 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 else if (msec == 0)
2198 break;
2199 }
2200
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002201# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01002202 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (input_available())
2204 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002205# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return FALSE;
2208}
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210/*
2211 * return non-zero if a character is available
2212 */
2213 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002214mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002216# ifdef VIMDLL
2217 if (gui.in_use)
2218 return TRUE;
2219# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002220 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002222
2223# if defined(FEAT_TERMINAL) || defined(PROTO)
2224/*
2225 * Check for any pending input or messages.
2226 */
2227 int
2228mch_check_messages(void)
2229{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002230# ifdef VIMDLL
2231 if (gui.in_use)
2232 return TRUE;
2233# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002234 return WaitForChar(0L, TRUE);
2235}
2236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238/*
2239 * Create the console input. Used when reading stdin doesn't work.
2240 */
2241 static void
2242create_conin(void)
2243{
2244 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
2245 FILE_SHARE_READ|FILE_SHARE_WRITE,
2246 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002247 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 did_create_conin = TRUE;
2249}
2250
2251/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01002252 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002254 static WCHAR
2255tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002257 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 for (;;)
2260 {
2261 INPUT_RECORD ir;
2262 DWORD cRecords = 0;
2263
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002264# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002265 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (input_available())
2267 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (g_nMouseClick != -1)
2269 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002270# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01002271 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (did_create_conin)
2274 read_error_exit();
2275 create_conin();
2276 continue;
2277 }
2278
2279 if (ir.EventType == KEY_EVENT)
2280 {
2281 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
2282 pmodifiers, TRUE))
2283 return ch;
2284 }
2285 else if (ir.EventType == FOCUS_EVENT)
2286 handle_focus_event(ir);
2287 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
2288 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 else if (ir.EventType == MOUSE_EVENT)
2290 {
2291 if (decode_mouse_event(&ir.Event.MouseEvent))
2292 return 0;
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002296#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298
2299/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02002300 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Get one or more characters from the keyboard or the mouse.
2302 * If time == 0, do not wait for characters.
2303 * If time == n, wait a short time for characters.
2304 * If time == -1, wait forever for characters.
2305 * Returns the number of characters read into buf.
2306 */
2307 int
2308mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002309 char_u *buf UNUSED,
2310 int maxlen UNUSED,
2311 long time UNUSED,
2312 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002314#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316 int len;
2317 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002318# ifdef VIMDLL
2319// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
2320# define TYPEAHEADSPACE 6
2321# else
2322# define TYPEAHEADSPACE 0
2323# endif
2324# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002325 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 static int typeaheadlen = 0;
2327
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002328# ifdef VIMDLL
2329 if (gui.in_use)
2330 return 0;
2331# endif
2332
Bram Moolenaar0f873732019-12-05 20:28:46 +01002333 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (typeaheadlen > 0)
2335 goto theend;
2336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (time >= 0)
2338 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002339 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01002342 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar3918c952005-03-15 22:34:55 +00002346 /*
2347 * If there is no character available within 2 seconds (default)
2348 * write the autoscript file to disk. Or cause the CursorHold event
2349 * to be triggered.
2350 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002351 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00002353 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002355 buf[0] = K_SPECIAL;
2356 buf[1] = KS_EXTRA;
2357 buf[2] = (int)KE_CURSORHOLD;
2358 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00002360 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362 }
2363
2364 /*
2365 * Try to read as many characters as there are, until the buffer is full.
2366 */
2367
Bram Moolenaar0f873732019-12-05 20:28:46 +01002368 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 g_fCBrkPressed = FALSE;
2370
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002371# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (fdDump)
2373 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
Bram Moolenaar0f873732019-12-05 20:28:46 +01002376 // Keep looping until there is something in the typeahead buffer and more
2377 // to get and still room in the buffer (up to two bytes for a char and
2378 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002379 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01002380 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 if (typebuf_changed(tb_change_cnt))
2383 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002384 // "buf" may be invalid now if a client put something in the
2385 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 typeaheadlen = 0;
2387 break;
2388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (g_nMouseClick != -1)
2390 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002391# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (fdDump)
2393 fprintf(fdDump, "{%02x @ %d, %d}",
2394 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002395# endif
Christopher Plewright2a46f812022-10-16 19:47:45 +01002396 char_u modifiers = ((char_u *)(&g_nMouseClick))[0];
2397 char_u scroll_dir = ((char_u *)(&g_nMouseClick))[1];
2398
2399 if (scroll_dir == KE_MOUSEDOWN
2400 || scroll_dir == KE_MOUSEUP
2401 || scroll_dir == KE_MOUSELEFT
2402 || scroll_dir == KE_MOUSERIGHT)
2403 {
2404 if (modifiers > 0)
2405 {
Christopher Plewright03193062022-11-22 12:58:27 +00002406 // use K_SPECIAL instead of CSI to make mappings work
2407 typeahead[typeaheadlen++] = K_SPECIAL;
Christopher Plewright2a46f812022-10-16 19:47:45 +01002408 typeahead[typeaheadlen++] = KS_MODIFIER;
2409 typeahead[typeaheadlen++] = modifiers;
2410 }
2411 typeahead[typeaheadlen++] = CSI;
2412 typeahead[typeaheadlen++] = KS_EXTRA;
2413 typeahead[typeaheadlen++] = scroll_dir;
Christopher Plewright2a46f812022-10-16 19:47:45 +01002414 }
2415 else
2416 {
2417 typeahead[typeaheadlen++] = ESC + 128;
2418 typeahead[typeaheadlen++] = 'M';
2419 typeahead[typeaheadlen++] = g_nMouseClick;
Christopher Plewright2a46f812022-10-16 19:47:45 +01002420 }
Christopher Plewright36446bb2022-11-23 22:28:08 +00002421
2422 // Pass the pointer coordinates of the mouse event in 2 bytes,
2423 // allowing for > 223 columns. Both for click and scroll events.
2424 // This is the same as what is used for the GUI.
2425 typeahead[typeaheadlen++] = (char_u)(g_xMouse / 128 + ' ' + 1);
2426 typeahead[typeaheadlen++] = (char_u)(g_xMouse % 128 + ' ' + 1);
2427 typeahead[typeaheadlen++] = (char_u)(g_yMouse / 128 + ' ' + 1);
2428 typeahead[typeaheadlen++] = (char_u)(g_yMouse % 128 + ' ' + 1);
2429
2430 g_nMouseClick = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002434 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int modifiers = 0;
2436
2437 c = tgetch(&modifiers, &ch2);
2438
2439 if (typebuf_changed(tb_change_cnt))
2440 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002441 // "buf" may be invalid now if a client put something in the
2442 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 typeaheadlen = 0;
2444 break;
2445 }
2446
2447 if (c == Ctrl_C && ctrl_c_interrupts)
2448 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002449# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 got_int = TRUE;
2453 }
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 int n = 1;
2458
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002459 if (ch2 == NUL)
2460 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002461 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002462 char_u *p;
2463 WCHAR ch[2];
2464
2465 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002466 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002467 {
2468 ch[1] = tgetch(&modifiers, &ch2);
2469 n++;
2470 }
2471 p = utf16_to_enc(ch, &n);
2472 if (p != NULL)
2473 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002474 for (i = 0, j = 0; i < n; i++)
2475 {
2476 typeahead[typeaheadlen + j++] = p[i];
2477# ifdef VIMDLL
2478 if (p[i] == CSI)
2479 {
2480 typeahead[typeaheadlen + j++] = KS_EXTRA;
2481 typeahead[typeaheadlen + j++] = KE_CSI;
2482 }
2483# endif
2484 }
2485 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002486 vim_free(p);
2487 }
2488 }
2489 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002490 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02002491 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002492# ifdef VIMDLL
2493 if (c == CSI)
2494 {
2495 typeahead[typeaheadlen + 1] = KS_EXTRA;
2496 typeahead[typeaheadlen + 2] = KE_CSI;
2497 n = 3;
2498 }
2499# endif
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (ch2 != NUL)
2502 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02002503 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002504 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02002505 switch (ch2)
2506 {
2507 case (WCHAR)'\324': // SHIFT+Insert
2508 case (WCHAR)'\325': // CTRL+Insert
2509 case (WCHAR)'\327': // SHIFT+Delete
2510 case (WCHAR)'\330': // CTRL+Delete
2511 typeahead[typeaheadlen + n] = (char_u)ch2;
2512 n++;
2513 break;
2514
2515 default:
2516 typeahead[typeaheadlen + n] = 3;
2517 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2518 n += 2;
2519 break;
2520 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01002521 }
2522 else
2523 {
2524 typeahead[typeaheadlen + n] = 3;
2525 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
2526 n += 2;
2527 }
Bram Moolenaar45500912014-07-09 20:51:07 +02002528 }
2529
Bram Moolenaar0f873732019-12-05 20:28:46 +01002530 // Use the ALT key to set the 8th bit of the character
2531 // when it's one byte, the 8th bit isn't set yet and not
2532 // using a double-byte encoding (would become a lead
2533 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if ((modifiers & MOD_MASK_ALT)
2535 && n == 1
2536 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 )
2539 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00002540 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
2541 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 modifiers &= ~MOD_MASK_ALT;
2543 }
2544
2545 if (modifiers != 0)
2546 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002547 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 mch_memmove(typeahead + typeaheadlen + 3,
2549 typeahead + typeaheadlen, n);
2550 typeahead[typeaheadlen++] = K_SPECIAL;
2551 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
2552 typeahead[typeaheadlen++] = modifiers;
2553 }
2554
2555 typeaheadlen += n;
2556
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002557# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (fdDump)
2559 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 }
2563 }
2564
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002565# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (fdDump)
2567 {
2568 fputs("]\n", fdDump);
2569 fflush(fdDump);
2570 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 len = 0;
2576 while (len < maxlen && typeaheadlen > 0)
2577 {
2578 buf[len++] = typeahead[0];
2579 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2580 }
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002581# ifdef FEAT_EVAL
Bram Moolenaar7ca86fe2020-09-03 19:25:11 +02002582 if (len > 0)
2583 {
2584 buf[len] = NUL;
2585 ch_log(NULL, "raw key input: \"%s\"", buf);
2586 }
K.Takata6e1d31e2022-02-03 13:05:32 +00002587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 return len;
2589
Bram Moolenaar0f873732019-12-05 20:28:46 +01002590#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002592#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593}
2594
Bram Moolenaar82881492012-11-20 16:53:39 +01002595#ifndef PROTO
2596# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002597# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#endif
2600
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002601/*
Bram Moolenaar95da1362020-05-30 18:37:55 +02002602 * Return TRUE if "name" is an executable file, FALSE if not or it doesn't exist.
Bram Moolenaar43663192017-03-05 14:29:12 +01002603 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2604 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002605 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 static int
Bram Moolenaar95da1362020-05-30 18:37:55 +02002608executable_file(char *name, char_u **path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
LemonBoy40fd7e62022-05-05 20:18:16 +01002610 int attrs = win32_getattrs((char_u *)name);
2611
2612 // The file doesn't exist or is a folder.
2613 if (attrs == -1 || (attrs & FILE_ATTRIBUTE_DIRECTORY))
2614 return FALSE;
2615 // Check if the file is an AppExecLink, a special alias used by Windows
2616 // Store for its apps.
2617 if (attrs & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar43663192017-03-05 14:29:12 +01002618 {
LemonBoy40fd7e62022-05-05 20:18:16 +01002619 char_u *res = resolve_appexeclink((char_u *)name);
2620 if (res == NULL)
2621 return FALSE;
2622 // The path is already absolute.
Bram Moolenaar95da1362020-05-30 18:37:55 +02002623 if (path != NULL)
LemonBoy40fd7e62022-05-05 20:18:16 +01002624 *path = res;
2625 else
2626 vim_free(res);
Bram Moolenaar95da1362020-05-30 18:37:55 +02002627 }
LemonBoy40fd7e62022-05-05 20:18:16 +01002628 else if (path != NULL)
2629 *path = FullName_save((char_u *)name, FALSE);
2630 return TRUE;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002631}
2632
2633/*
2634 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2635 * If "use_path" is FALSE: Return TRUE if "name" exists.
2636 * If "use_pathext" is TRUE search "name" with extensions in $PATHEXT.
2637 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2638 * the allocated memory.
2639 */
2640 static int
2641executable_exists(char *name, char_u **path, int use_path, int use_pathext)
2642{
2643 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
2644 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
2645 // UTF-8.
2646 char_u buf[_MAX_PATH * 3];
2647 size_t len = STRLEN(name);
2648 size_t tmplen;
2649 char_u *p, *e, *e2;
2650 char_u *pathbuf = NULL;
2651 char_u *pathext = NULL;
2652 char_u *pathextbuf = NULL;
Mike Williamsa3d1b292021-06-30 20:56:00 +02002653 char_u *shname = NULL;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002654 int noext = FALSE;
2655 int retval = FALSE;
2656
2657 if (len >= sizeof(buf)) // safety check
Bram Moolenaar43663192017-03-05 14:29:12 +01002658 return FALSE;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002659
2660 // Using the name directly when a Unix-shell like 'shell'.
Mike Williamsa3d1b292021-06-30 20:56:00 +02002661 shname = gettail(p_sh);
2662 if (strstr((char *)shname, "sh") != NULL &&
2663 !(strstr((char *)shname, "powershell") != NULL
2664 || strstr((char *)shname, "pwsh") != NULL))
Bram Moolenaar95da1362020-05-30 18:37:55 +02002665 noext = TRUE;
2666
2667 if (use_pathext)
2668 {
2669 pathext = mch_getenv("PATHEXT");
2670 if (pathext == NULL)
2671 pathext = (char_u *)".com;.exe;.bat;.cmd";
2672
2673 if (noext == FALSE)
2674 {
2675 /*
2676 * Loop over all extensions in $PATHEXT.
2677 * Check "name" ends with extension.
2678 */
2679 p = pathext;
2680 while (*p)
2681 {
2682 if (p[0] == ';'
2683 || (p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2684 {
2685 // Skip empty or single ".".
2686 ++p;
2687 continue;
2688 }
2689 e = vim_strchr(p, ';');
2690 if (e == NULL)
2691 e = p + STRLEN(p);
2692 tmplen = e - p;
2693
2694 if (_strnicoll(name + len - tmplen, (char *)p, tmplen) == 0)
2695 {
2696 noext = TRUE;
2697 break;
2698 }
2699
2700 p = e;
2701 }
2702 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002703 }
2704
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002705 // Prepend single "." to pathext, it means no extension added.
Bram Moolenaar95da1362020-05-30 18:37:55 +02002706 if (pathext == NULL)
2707 pathext = (char_u *)".";
2708 else if (noext == TRUE)
2709 {
2710 if (pathextbuf == NULL)
2711 pathextbuf = alloc(STRLEN(pathext) + 3);
2712 if (pathextbuf == NULL)
2713 {
2714 retval = FALSE;
2715 goto theend;
2716 }
2717 STRCPY(pathextbuf, ".;");
2718 STRCAT(pathextbuf, pathext);
2719 pathext = pathextbuf;
2720 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002721
Bram Moolenaar95da1362020-05-30 18:37:55 +02002722 // Use $PATH when "use_path" is TRUE and "name" is basename.
2723 if (use_path && gettail((char_u *)name) == (char_u *)name)
2724 {
2725 p = mch_getenv("PATH");
2726 if (p != NULL)
2727 {
2728 pathbuf = alloc(STRLEN(p) + 3);
2729 if (pathbuf == NULL)
2730 {
2731 retval = FALSE;
2732 goto theend;
2733 }
Yasuhiro Matsumoto05cf63e2022-05-03 11:02:28 +01002734
2735 if (mch_getenv("NoDefaultCurrentDirectoryInExePath") == NULL)
2736 STRCPY(pathbuf, ".;");
2737 else
2738 *pathbuf = NUL;
Bram Moolenaar95da1362020-05-30 18:37:55 +02002739 STRCAT(pathbuf, p);
2740 }
2741 }
2742
2743 /*
2744 * Walk through all entries in $PATH to check if "name" exists there and
2745 * is an executable file.
2746 */
2747 p = (pathbuf != NULL) ? pathbuf : (char_u *)".";
2748 while (*p)
2749 {
2750 if (*p == ';') // Skip empty entry
2751 {
2752 ++p;
2753 continue;
2754 }
2755 e = vim_strchr(p, ';');
2756 if (e == NULL)
2757 e = p + STRLEN(p);
2758
2759 if (e - p + len + 2 > sizeof(buf))
2760 {
2761 retval = FALSE;
2762 goto theend;
2763 }
2764 // A single "." that means current dir.
2765 if (e - p == 1 && *p == '.')
2766 STRCPY(buf, name);
2767 else
2768 {
2769 vim_strncpy(buf, p, e - p);
2770 add_pathsep(buf);
2771 STRCAT(buf, name);
2772 }
2773 tmplen = STRLEN(buf);
2774
2775 /*
2776 * Loop over all extensions in $PATHEXT.
2777 * Check "name" with extension added.
2778 */
2779 p = pathext;
2780 while (*p)
2781 {
2782 if (*p == ';')
2783 {
2784 // Skip empty entry
2785 ++p;
2786 continue;
2787 }
2788 e2 = vim_strchr(p, (int)';');
2789 if (e2 == NULL)
2790 e2 = p + STRLEN(p);
2791
2792 if (!(p[0] == '.' && (p[1] == NUL || p[1] == ';')))
2793 {
2794 // Not a single "." that means no extension is added.
2795 if (e2 - p + tmplen + 1 > sizeof(buf))
2796 {
2797 retval = FALSE;
2798 goto theend;
2799 }
2800 vim_strncpy(buf + tmplen, p, e2 - p);
2801 }
2802 if (executable_file((char *)buf, path))
2803 {
2804 retval = TRUE;
2805 goto theend;
2806 }
2807
2808 p = e2;
2809 }
2810
2811 p = e;
2812 }
2813
2814theend:
2815 free(pathextbuf);
2816 free(pathbuf);
2817 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818}
2819
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002820#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2821 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002822/*
2823 * Bad parameter handler.
2824 *
2825 * Certain MS CRT functions will intentionally crash when passed invalid
2826 * parameters to highlight possible security holes. Setting this function as
2827 * the bad parameter handler will prevent the crash.
2828 *
2829 * In debug builds the parameters contain CRT information that might help track
2830 * down the source of a problem, but in non-debug builds the arguments are all
2831 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2832 * worth allowing these to make debugging of issues easier.
2833 */
2834 static void
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01002835bad_param_handler(const wchar_t *expression UNUSED,
2836 const wchar_t *function UNUSED,
2837 const wchar_t *file UNUSED,
2838 unsigned int line UNUSED,
2839 uintptr_t pReserved UNUSED)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002840{
2841}
2842
2843# define SET_INVALID_PARAM_HANDLER \
2844 ((void)_set_invalid_parameter_handler(bad_param_handler))
2845#else
2846# define SET_INVALID_PARAM_HANDLER
2847#endif
2848
Bram Moolenaar4f974752019-02-17 17:44:42 +01002849#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851/*
2852 * GUI version of mch_init().
2853 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002854 static void
2855mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002857# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaar0f873732019-12-05 20:28:46 +01002861 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002862 SET_INVALID_PARAM_HANDLER;
2863
Bram Moolenaar0f873732019-12-05 20:28:46 +01002864 // Let critical errors result in a failure, not in a dialog box. Required
2865 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 SetErrorMode(SEM_FAILCRITICALERRORS);
2867
Bram Moolenaar0f873732019-12-05 20:28:46 +01002868 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar0f873732019-12-05 20:28:46 +01002870 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 Rows = 25;
2872 Columns = 80;
2873
Bram Moolenaar0f873732019-12-05 20:28:46 +01002874 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 char_u vimrun_location[_MAX_PATH + 4];
2877
Bram Moolenaar0f873732019-12-05 20:28:46 +01002878 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 STRCPY(vimrun_location, exe_name);
2880 STRCPY(gettail(vimrun_location), "vimrun.exe");
2881 if (mch_getperm(vimrun_location) >= 0)
2882 {
2883 if (*skiptowhite(vimrun_location) != NUL)
2884 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002885 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 mch_memmove(vimrun_location + 1, vimrun_location,
2887 STRLEN(vimrun_location) + 1);
2888 *vimrun_location = '"';
2889 STRCPY(gettail(vimrun_location), "vimrun\" ");
2890 }
2891 else
2892 STRCPY(gettail(vimrun_location), "vimrun ");
2893
2894 vimrun_path = (char *)vim_strsave(vimrun_location);
2895 s_dont_use_vimrun = FALSE;
2896 }
Bram Moolenaar95da1362020-05-30 18:37:55 +02002897 else if (executable_exists("vimrun.exe", NULL, TRUE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 s_dont_use_vimrun = FALSE;
2899
Bram Moolenaar0f873732019-12-05 20:28:46 +01002900 // Don't give the warning for a missing vimrun.exe right now, but only
2901 // when vimrun was supposed to be used. Don't bother people that do
2902 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (s_dont_use_vimrun)
2904 need_vimrun_warning = TRUE;
2905 }
2906
2907 /*
2908 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2909 * Otherwise the default "findstr /n" is used.
2910 */
Bram Moolenaar95da1362020-05-30 18:37:55 +02002911 if (!executable_exists("findstr.exe", NULL, TRUE, FALSE))
Bram Moolenaar75ab5902022-04-18 15:36:40 +01002912 set_option_value_give_err((char_u *)"grepprg",
2913 0, (char_u *)"grep -n", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002915# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002916 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002917# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002918
2919 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922
Bram Moolenaar0f873732019-12-05 20:28:46 +01002923#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002924
2925#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002927# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2928# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930/*
2931 * ClearConsoleBuffer()
2932 * Description:
2933 * Clears the entire contents of the console screen buffer, using the
2934 * specified attribute.
2935 * Returns:
2936 * TRUE on success
2937 */
2938 static BOOL
2939ClearConsoleBuffer(WORD wAttribute)
2940{
2941 CONSOLE_SCREEN_BUFFER_INFO csbi;
2942 COORD coord;
2943 DWORD NumCells, dummy;
2944
2945 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2946 return FALSE;
2947
2948 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2949 coord.X = 0;
2950 coord.Y = 0;
2951 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2952 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2955 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 return TRUE;
2959}
2960
2961/*
2962 * FitConsoleWindow()
2963 * Description:
2964 * Checks if the console window will fit within given buffer dimensions.
2965 * Also, if requested, will shrink the window to fit.
2966 * Returns:
2967 * TRUE on success
2968 */
2969 static BOOL
2970FitConsoleWindow(
2971 COORD dwBufferSize,
2972 BOOL WantAdjust)
2973{
2974 CONSOLE_SCREEN_BUFFER_INFO csbi;
2975 COORD dwWindowSize;
2976 BOOL NeedAdjust = FALSE;
2977
2978 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2979 {
2980 /*
2981 * A buffer resize will fail if the current console window does
2982 * not lie completely within that buffer. To avoid this, we might
2983 * have to move and possibly shrink the window.
2984 */
2985 if (csbi.srWindow.Right >= dwBufferSize.X)
2986 {
2987 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2988 if (dwWindowSize.X > dwBufferSize.X)
2989 dwWindowSize.X = dwBufferSize.X;
2990 csbi.srWindow.Right = dwBufferSize.X - 1;
2991 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2992 NeedAdjust = TRUE;
2993 }
2994 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2995 {
2996 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2997 if (dwWindowSize.Y > dwBufferSize.Y)
2998 dwWindowSize.Y = dwBufferSize.Y;
2999 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
3000 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
3001 NeedAdjust = TRUE;
3002 }
3003 if (NeedAdjust && WantAdjust)
3004 {
3005 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
3006 return FALSE;
3007 }
3008 return TRUE;
3009 }
3010
3011 return FALSE;
3012}
3013
3014typedef struct ConsoleBufferStruct
3015{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003016 BOOL IsValid;
3017 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003018 PCHAR_INFO Buffer;
3019 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02003020 PSMALL_RECT Regions;
3021 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022} ConsoleBuffer;
3023
3024/*
3025 * SaveConsoleBuffer()
3026 * Description:
3027 * Saves important information about the console buffer, including the
3028 * actual buffer contents. The saved information is suitable for later
3029 * restoration by RestoreConsoleBuffer().
3030 * Returns:
3031 * TRUE if all information was saved; FALSE otherwise
3032 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
3033 */
3034 static BOOL
3035SaveConsoleBuffer(
3036 ConsoleBuffer *cb)
3037{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003038 DWORD NumCells;
3039 COORD BufferCoord;
3040 SMALL_RECT ReadRegion;
3041 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02003042 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (cb == NULL)
3045 return FALSE;
3046
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003047 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
3049 cb->IsValid = FALSE;
3050 return FALSE;
3051 }
3052 cb->IsValid = TRUE;
3053
Christopher Plewright1140b512022-11-12 18:46:05 +00003054 // VTP uses alternate screen buffer.
3055 // No need to save buffer contents for restoration.
3056 if (win11_or_later && vtp_working)
3057 return TRUE;
3058
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003059 /*
3060 * Allocate a buffer large enough to hold the entire console screen
3061 * buffer. If this ConsoleBuffer structure has already been initialized
3062 * with a buffer of the correct size, then just use that one.
3063 */
3064 if (!cb->IsValid || cb->Buffer == NULL ||
3065 cb->BufferSize.X != cb->Info.dwSize.X ||
3066 cb->BufferSize.Y != cb->Info.dwSize.Y)
3067 {
3068 cb->BufferSize.X = cb->Info.dwSize.X;
3069 cb->BufferSize.Y = cb->Info.dwSize.Y;
3070 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
3071 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003072 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003073 if (cb->Buffer == NULL)
3074 return FALSE;
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003078 * We will now copy the console screen buffer into our buffer.
3079 * ReadConsoleOutput() seems to be limited as far as how much you
3080 * can read at a time. Empirically, this number seems to be about
3081 * 12000 cells (rows * columns). Start at position (0, 0) and copy
3082 * in chunks until it is all copied. The chunks will all have the
3083 * same horizontal characteristics, so initialize them now. The
3084 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
Bram Moolenaar61594242015-09-01 20:23:37 +02003086 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003087 ReadRegion.Left = 0;
3088 ReadRegion.Right = cb->Info.dwSize.X - 1;
3089 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02003090
3091 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
3092 if (cb->Regions == NULL || numregions != cb->NumRegions)
3093 {
3094 cb->NumRegions = numregions;
3095 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003096 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02003097 if (cb->Regions == NULL)
3098 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003099 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02003100 return FALSE;
3101 }
3102 }
3103
3104 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003106 /*
3107 * Read into position (0, Y) in our buffer.
3108 */
3109 BufferCoord.Y = Y;
3110 /*
3111 * Read the region whose top left corner is (0, Y) and whose bottom
3112 * right corner is (width - 1, Y + Y_incr - 1). This should define
3113 * a region of size width by Y_incr. Don't worry if this region is
3114 * too large for the remaining buffer; it will be cropped.
3115 */
3116 ReadRegion.Top = Y;
3117 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003118 if (!ReadConsoleOutputW(g_hConOut, // output handle
3119 cb->Buffer, // our buffer
3120 cb->BufferSize, // dimensions of our buffer
3121 BufferCoord, // offset in our buffer
3122 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003123 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(cb->Buffer);
3125 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003126 return FALSE;
3127 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02003128 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130
3131 return TRUE;
3132}
3133
3134/*
3135 * RestoreConsoleBuffer()
3136 * Description:
3137 * Restores important information about the console buffer, including the
3138 * actual buffer contents, if desired. The information to restore is in
3139 * the same format used by SaveConsoleBuffer().
3140 * Returns:
3141 * TRUE on success
3142 */
3143 static BOOL
3144RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003145 ConsoleBuffer *cb,
3146 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003148 COORD BufferCoord;
3149 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02003150 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Christopher Plewright1140b512022-11-12 18:46:05 +00003152 // VTP uses alternate screen buffer.
3153 // No need to restore buffer contents.
3154 if (win11_or_later && vtp_working)
3155 return TRUE;
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (cb == NULL || !cb->IsValid)
3158 return FALSE;
3159
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003160 /*
3161 * Before restoring the buffer contents, clear the current buffer, and
3162 * restore the cursor position and window information. Doing this now
3163 * prevents old buffer contents from "flashing" onto the screen.
3164 */
3165 if (RestoreScreen)
3166 ClearConsoleBuffer(cb->Info.wAttributes);
3167
3168 FitConsoleWindow(cb->Info.dwSize, TRUE);
3169 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
3170 return FALSE;
3171 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
3172 return FALSE;
3173
3174 if (!RestoreScreen)
3175 {
3176 /*
3177 * No need to restore the screen buffer contents, so we're done.
3178 */
3179 return TRUE;
3180 }
3181
3182 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
3183 return FALSE;
3184 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
3185 return FALSE;
3186
3187 /*
3188 * Restore the screen buffer contents.
3189 */
3190 if (cb->Buffer != NULL)
3191 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02003192 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003193 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02003194 BufferCoord.X = cb->Regions[i].Left;
3195 BufferCoord.Y = cb->Regions[i].Top;
3196 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01003197 if (!WriteConsoleOutputW(g_hConOut, // output handle
3198 cb->Buffer, // our buffer
3199 cb->BufferSize, // dimensions of our buffer
3200 BufferCoord, // offset in our buffer
3201 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02003202 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003203 }
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 return TRUE;
3207}
3208
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003209# define FEAT_RESTORE_ORIG_SCREEN
3210# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003211static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213static ConsoleBuffer g_cbNonTermcap = { 0 };
3214static ConsoleBuffer g_cbTermcap = { 0 };
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01003217HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218static HICON g_hOrigIconSmall = NULL;
3219static HICON g_hOrigIcon = NULL;
3220static HICON g_hVimIcon = NULL;
3221static BOOL g_fCanChangeIcon = FALSE;
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
3224 * GetConsoleIcon()
3225 * Description:
3226 * Attempts to retrieve the small icon and/or the big icon currently in
3227 * use by a given window.
3228 * Returns:
3229 * TRUE on success
3230 */
3231 static BOOL
3232GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003233 HWND hWnd,
3234 HICON *phIconSmall,
3235 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 if (hWnd == NULL)
3238 return FALSE;
3239
3240 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003241 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
3242 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003244 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
3245 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 return TRUE;
3247}
3248
3249/*
3250 * SetConsoleIcon()
3251 * Description:
3252 * Attempts to change the small icon and/or the big icon currently in
3253 * use by a given window.
3254 * Returns:
3255 * TRUE on success
3256 */
3257 static BOOL
3258SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003259 HWND hWnd,
3260 HICON hIconSmall,
3261 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (hWnd == NULL)
3264 return FALSE;
3265
3266 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003267 SendMessage(hWnd, WM_SETICON,
3268 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003270 SendMessage(hWnd, WM_SETICON,
3271 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return TRUE;
3273}
3274
3275/*
3276 * SaveConsoleTitleAndIcon()
3277 * Description:
3278 * Saves the current console window title in g_szOrigTitle, for later
3279 * restoration. Also, attempts to obtain a handle to the console window,
3280 * and use it to save the small and big icons currently in use by the
3281 * console window. This is not always possible on some versions of Windows;
3282 * nor is it possible when running Vim remotely using Telnet (since the
3283 * console window the user sees is owned by a remote process).
3284 */
3285 static void
3286SaveConsoleTitleAndIcon(void)
3287{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003288 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
3290 return;
3291
3292 /*
3293 * Obtain a handle to the console window using GetConsoleWindow() from
3294 * KERNEL32.DLL; we need to handle in order to change the window icon.
3295 * This function only exists on NT-based Windows, starting with Windows
3296 * 2000. On older operating systems, we can't change the window icon
3297 * anyway.
3298 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003299 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (g_hWnd == NULL)
3301 return;
3302
Bram Moolenaar0f873732019-12-05 20:28:46 +01003303 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
3305 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
3306 return;
3307
Bram Moolenaar0f873732019-12-05 20:28:46 +01003308 // Extract the first icon contained in the Vim executable.
K.Takata2da11a42022-09-10 13:03:12 +01003309 if (
3310# ifdef FEAT_LIBCALL
3311 mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL ||
3312# endif
3313 g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003314 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (g_hVimIcon != NULL)
3316 g_fCanChangeIcon = TRUE;
3317}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319static int g_fWindInitCalled = FALSE;
3320static int g_fTermcapMode = FALSE;
3321static CONSOLE_CURSOR_INFO g_cci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323/*
3324 * non-GUI version of mch_init().
3325 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003326 static void
3327mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003329# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003330 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003331# endif
3332# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaar0f873732019-12-05 20:28:46 +01003336 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02003337 SET_INVALID_PARAM_HANDLER;
3338
Bram Moolenaar0f873732019-12-05 20:28:46 +01003339 // Let critical errors result in a failure, not in a dialog box. Required
3340 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 SetErrorMode(SEM_FAILCRITICALERRORS);
3342
Bram Moolenaar0f873732019-12-05 20:28:46 +01003343 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 out_flush();
3345
Bram Moolenaar0f873732019-12-05 20:28:46 +01003346 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (read_cmd_fd == 0)
3348 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
3349 else
3350 create_conin();
3351 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
3352
Christopher Plewright38804d62022-11-09 23:55:52 +00003353 wt_init();
3354 vtp_flag_init();
3355 vtp_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003356# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003357 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003358 SaveConsoleBuffer(&g_cbOrig);
3359 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003360# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003361 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01003362 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
3363 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (cterm_normal_fg_color == 0)
3366 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
3367 if (cterm_normal_bg_color == 0)
3368 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
3369
Bram Moolenaarbdace832019-03-02 10:13:42 +01003370 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02003371 g_color_index_fg = g_attrDefault & 0xf;
3372 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
3373
Bram Moolenaar0f873732019-12-05 20:28:46 +01003374 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 update_tcap(g_attrCurrent);
3376
3377 GetConsoleCursorInfo(g_hConOut, &g_cci);
3378 GetConsoleMode(g_hConIn, &g_cmodein);
3379 GetConsoleMode(g_hConOut, &g_cmodeout);
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 SaveConsoleTitleAndIcon();
3382 /*
3383 * Set both the small and big icons of the console window to Vim's icon.
3384 * Note that Vim presently only has one size of icon (32x32), but it
3385 * automatically gets scaled down to 16x16 when setting the small icon.
3386 */
3387 if (g_fCanChangeIcon)
3388 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 ui_get_shellsize();
3391
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003392# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 fdDump = fopen("dump", "wt");
3394
3395 if (fdDump)
3396 {
3397 time_t t;
3398
3399 time(&t);
3400 fputs(ctime(&t), fdDump);
3401 fflush(fdDump);
3402 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
3405 g_fWindInitCalled = TRUE;
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003409# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01003410 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412}
3413
3414/*
3415 * non-GUI version of mch_exit().
3416 * Shut down and exit with status `r'
3417 * Careful: mch_exit() may be called before mch_init()!
3418 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003419 static void
3420mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaar955f1982017-02-05 15:10:51 +01003422 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003424 vtp_exit();
3425
Bram Moolenaar955f1982017-02-05 15:10:51 +01003426 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (g_fWindInitCalled)
3428 settmode(TMODE_COOK);
3429
Bram Moolenaar0f873732019-12-05 20:28:46 +01003430 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 if (g_fWindInitCalled)
3433 {
Bram Moolenaar40385db2018-08-07 22:31:44 +02003434 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 /*
3436 * Restore both the small and big icons of the console window to
3437 * what they were at startup. Don't do this when the window is
3438 * closed, Vim would hang here.
3439 */
3440 if (g_fCanChangeIcon && !g_fForceExit)
3441 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003443# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (fdDump)
3445 {
3446 time_t t;
3447
3448 time(&t);
3449 fputs(ctime(&t), fdDump);
3450 fclose(fdDump);
3451 }
3452 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455
3456 SetConsoleCursorInfo(g_hConOut, &g_cci);
Wez Furlong6ef5ab52021-05-30 19:29:41 +02003457 SetConsoleMode(g_hConIn, g_cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 SetConsoleMode(g_hConOut, g_cmodeout);
3459
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003460# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 exit(r);
3465}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003466#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003468 void
3469mch_init(void)
3470{
3471#ifdef VIMDLL
3472 if (gui.starting)
3473 mch_init_g();
3474 else
3475 mch_init_c();
3476#elif defined(FEAT_GUI_MSWIN)
3477 mch_init_g();
3478#else
3479 mch_init_c();
3480#endif
3481}
3482
3483 void
3484mch_exit(int r)
3485{
Bram Moolenaar173d8412020-04-19 14:02:26 +02003486#ifdef FEAT_NETBEANS_INTG
3487 netbeans_send_disconnect();
3488#endif
3489
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003490#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02003491 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003492 mch_exit_g(r);
3493 else
3494 mch_exit_c(r);
3495#elif defined(FEAT_GUI_MSWIN)
3496 mch_exit_g(r);
3497#else
3498 mch_exit_c(r);
3499#endif
3500}
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502/*
3503 * Do we have an interactive window?
3504 */
3505 int
3506mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003507 int argc UNUSED,
3508 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 get_exe_name();
3511
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003512#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003513 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003515# ifdef VIMDLL
3516 if (gui.in_use)
3517 return OK;
3518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (isatty(1))
3520 return OK;
3521 return FAIL;
3522#endif
3523}
3524
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003525/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003526 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 * When "len" is > 0, also expand short to long filenames.
3528 */
3529 void
3530fname_case(
3531 char_u *name,
3532 int len)
3533{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003534 int flen;
3535 WCHAR *p;
3536 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00003538 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003539 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return;
3541
3542 slash_adjust(name);
3543
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003544 p = enc_to_utf16(name, NULL);
3545 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02003546 return;
3547
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003548 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003550 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003552 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003554 if (len > 0 || flen >= (int)STRLEN(q))
3555 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
3556 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01003559 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562
3563/*
3564 * Insert user name in s[len].
3565 */
3566 int
3567mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003568 char_u *s,
3569 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003571 WCHAR wszUserName[256 + 1]; // UNLEN is 256
K.Takataeeec2542021-06-02 13:28:16 +02003572 DWORD wcch = ARRAY_LENGTH(wszUserName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003574 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003575 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003576 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003577
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003578 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003579 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003580 vim_strncpy(s, p, len - 1);
3581 vim_free(p);
3582 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003583 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01003584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 s[0] = NUL;
3586 return FAIL;
3587}
3588
3589
3590/*
3591 * Insert host name in s[len].
3592 */
3593 void
3594mch_get_host_name(
3595 char_u *s,
3596 int len)
3597{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003598 WCHAR wszHostName[256 + 1];
K.Takataeeec2542021-06-02 13:28:16 +02003599 DWORD wcch = ARRAY_LENGTH(wszHostName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003601 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003602 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003603 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003604
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003605 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003606 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003607 vim_strncpy(s, p, len - 1);
3608 vim_free(p);
3609 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003610 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01003611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612}
3613
3614
3615/*
3616 * return process ID
3617 */
3618 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003619mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 return (long)GetCurrentProcessId();
3622}
3623
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003624/*
3625 * return TRUE if process "pid" is still running
3626 */
3627 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02003628mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02003629{
3630 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
3631 DWORD status = 0;
3632 int ret = FALSE;
3633
3634 if (hProcess == NULL)
3635 return FALSE; // might not have access
3636 if (GetExitCodeProcess(hProcess, &status) )
3637 ret = status == STILL_ACTIVE;
3638 CloseHandle(hProcess);
3639 return ret;
3640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642/*
3643 * Get name of current directory into buffer 'buf' of length 'len' bytes.
3644 * Return OK for success, FAIL for failure.
3645 */
3646 int
3647mch_dirname(
3648 char_u *buf,
3649 int len)
3650{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003651 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 /*
3654 * Originally this was:
3655 * return (getcwd(buf, len) != NULL ? OK : FAIL);
3656 * But the Win32s known bug list says that getcwd() doesn't work
3657 * so use the Win32 system call instead. <Negri>
3658 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003659 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003661 WCHAR wcbuf[_MAX_PATH + 1];
3662 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003664 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003666 p = utf16_to_enc(wcbuf, NULL);
3667 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02003668 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003669 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003671 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003674 if (p == NULL)
3675 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02003676
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003677 if (p != NULL)
3678 {
3679 vim_strncpy(buf, p, len - 1);
3680 vim_free(p);
3681 return OK;
3682 }
3683 }
3684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685}
3686
3687/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01003688 * Get file permissions for "name".
3689 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 */
3691 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003692mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003694 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01003695 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003697 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01003698 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699}
3700
3701
3702/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003703 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003704 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003705 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
3707 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003708mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003710 long n;
3711 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02003712
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003713 p = enc_to_utf16(name, NULL);
3714 if (p == NULL)
3715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003717 n = _wchmod(p, perm);
3718 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003719 if (n == -1)
3720 return FAIL;
3721
3722 win32_set_archive(name);
3723
3724 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725}
3726
3727/*
3728 * Set hidden flag for "name".
3729 */
3730 void
3731mch_hide(char_u *name)
3732{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003733 int attrs = win32_getattrs(name);
3734 if (attrs == -1)
3735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003737 attrs |= FILE_ATTRIBUTE_HIDDEN;
3738 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739}
3740
3741/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003742 * Return TRUE if file "name" exists and is hidden.
3743 */
3744 int
3745mch_ishidden(char_u *name)
3746{
3747 int f = win32_getattrs(name);
3748
3749 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003750 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003751
3752 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3753}
3754
3755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 * return TRUE if "name" is a directory
3757 * return FALSE if "name" is not a directory or upon error
3758 */
3759 int
3760mch_isdir(char_u *name)
3761{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003762 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003765 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3768}
3769
3770/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003771 * return TRUE if "name" is a directory, NOT a symlink to a directory
3772 * return FALSE if "name" is not a directory
3773 * return FALSE for error
3774 */
3775 int
3776mch_isrealdir(char_u *name)
3777{
3778 return mch_isdir(name) && !mch_is_symbolic_link(name);
3779}
3780
3781/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003782 * Create directory "name".
3783 * Return 0 on success, -1 on error.
3784 */
3785 int
3786mch_mkdir(char_u *name)
3787{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003788 WCHAR *p;
3789 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003790
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003791 p = enc_to_utf16(name, NULL);
3792 if (p == NULL)
3793 return -1;
3794 retval = _wmkdir(p);
3795 vim_free(p);
3796 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003797}
3798
3799/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003800 * Delete directory "name".
3801 * Return 0 on success, -1 on error.
3802 */
3803 int
3804mch_rmdir(char_u *name)
3805{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003806 WCHAR *p;
3807 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003808
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003809 p = enc_to_utf16(name, NULL);
3810 if (p == NULL)
3811 return -1;
3812 retval = _wrmdir(p);
3813 vim_free(p);
3814 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003815}
3816
3817/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003818 * Return TRUE if file "fname" has more than one link.
3819 */
3820 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003821mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003822{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003823 BY_HANDLE_FILE_INFORMATION info;
3824
3825 return win32_fileinfo(fname, &info) == FILEINFO_OK
3826 && info.nNumberOfLinks > 1;
3827}
3828
3829/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003830 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003831 */
3832 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003833mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003834{
3835 HANDLE hFind;
3836 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003837 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003838 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003839 WIN32_FIND_DATAW findDataW;
3840
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003841 wn = enc_to_utf16(name, NULL);
3842 if (wn == NULL)
3843 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003844
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003845 hFind = FindFirstFileW(wn, &findDataW);
3846 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003847 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003848 {
3849 fileFlags = findDataW.dwFileAttributes;
3850 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003851 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003852 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003853
3854 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003855 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3856 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003857 res = TRUE;
3858
3859 return res;
3860}
3861
3862/*
3863 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3864 * link.
3865 */
3866 int
3867mch_is_linked(char_u *fname)
3868{
3869 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3870 return TRUE;
3871 return FALSE;
3872}
3873
3874/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003875 * Get the by-handle-file-information for "fname".
3876 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003877 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003878 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3879 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3880 */
3881 int
3882win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3883{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003884 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003885 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003886 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003887
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003888 wn = enc_to_utf16(fname, NULL);
3889 if (wn == NULL)
3890 return FILEINFO_ENC_FAIL;
3891
3892 hFile = CreateFileW(wn, // file name
3893 GENERIC_READ, // access mode
3894 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3895 NULL, // security descriptor
3896 OPEN_EXISTING, // creation disposition
3897 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3898 NULL); // handle to template file
3899 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003900
3901 if (hFile != INVALID_HANDLE_VALUE)
3902 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003903 if (GetFileInformationByHandle(hFile, info) != 0)
3904 res = FILEINFO_OK;
3905 else
3906 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003907 CloseHandle(hFile);
3908 }
3909
Bram Moolenaar03f48552006-02-28 23:52:23 +00003910 return res;
3911}
3912
3913/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003914 * get file attributes for `name'
3915 * -1 : error
3916 * else FILE_ATTRIBUTE_* defined in winnt.h
3917 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003918 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003919win32_getattrs(char_u *name)
3920{
3921 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003922 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003923
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003924 p = enc_to_utf16(name, NULL);
3925 if (p == NULL)
3926 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003927
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003928 attr = GetFileAttributesW(p);
3929 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003930
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003931 return attr;
3932}
3933
3934/*
3935 * set file attributes for `name' to `attrs'
3936 *
3937 * return -1 for failure, 0 otherwise
3938 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003939 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003940win32_setattrs(char_u *name, int attrs)
3941{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003942 int res;
3943 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003944
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003945 p = enc_to_utf16(name, NULL);
3946 if (p == NULL)
3947 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003948
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003949 res = SetFileAttributesW(p, attrs);
3950 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003951
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003952 return res ? 0 : -1;
3953}
3954
3955/*
3956 * Set archive flag for "name".
3957 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003958 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003959win32_set_archive(char_u *name)
3960{
3961 int attrs = win32_getattrs(name);
3962 if (attrs == -1)
3963 return -1;
3964
3965 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3966 return win32_setattrs(name, attrs);
3967}
3968
3969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * Return TRUE if file or directory "name" is writable (not readonly).
3971 * Strange semantics of Win32: a readonly directory is writable, but you can't
3972 * delete a file. Let's say this means it is writable.
3973 */
3974 int
3975mch_writable(char_u *name)
3976{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003977 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003979 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3980 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003984 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003985 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003986 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3987 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 */
3989 int
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01003990mch_can_exe(char_u *name, char_u **path, int use_path UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar95da1362020-05-30 18:37:55 +02003992 return executable_exists((char *)name, path, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995/*
3996 * Check what "name" is:
3997 * NODE_NORMAL: file or directory (or doesn't exist)
3998 * NODE_WRITABLE: writable device, socket, fifo, etc.
3999 * NODE_OTHER: non-writable things
4000 */
4001 int
4002mch_nodetype(char_u *name)
4003{
4004 HANDLE hFile;
4005 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004006 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Bram Moolenaar0f873732019-12-05 20:28:46 +01004008 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
4009 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
4010 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00004011 if (STRNCMP(name, "\\\\.\\", 4) == 0)
4012 return NODE_WRITABLE;
4013
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004014 wn = enc_to_utf16(name, NULL);
4015 if (wn == NULL)
4016 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004017
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004018 hFile = CreateFileW(wn, // file name
4019 GENERIC_WRITE, // access mode
4020 0, // share mode
4021 NULL, // security descriptor
4022 OPEN_EXISTING, // creation disposition
4023 0, // file attributes
4024 NULL); // handle to template file
4025 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (hFile == INVALID_HANDLE_VALUE)
4027 return NODE_NORMAL;
4028
4029 type = GetFileType(hFile);
4030 CloseHandle(hFile);
4031 if (type == FILE_TYPE_CHAR)
4032 return NODE_WRITABLE;
4033 if (type == FILE_TYPE_DISK)
4034 return NODE_NORMAL;
4035 return NODE_OTHER;
4036}
4037
4038#ifdef HAVE_ACL
4039struct my_acl
4040{
4041 PSECURITY_DESCRIPTOR pSecurityDescriptor;
4042 PSID pSidOwner;
4043 PSID pSidGroup;
4044 PACL pDacl;
4045 PACL pSacl;
4046};
4047#endif
4048
4049/*
4050 * Return a pointer to the ACL of file "fname" in allocated memory.
4051 * Return NULL if the ACL is not available for whatever reason.
4052 */
4053 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004054mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
4056#ifndef HAVE_ACL
4057 return (vim_acl_T)NULL;
4058#else
4059 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02004060 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004062 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004063 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004065 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02004066
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004067 wn = enc_to_utf16(fname, NULL);
4068 if (wn == NULL)
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01004069 {
4070 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004071 return NULL;
Bram Moolenaarbbf9f342020-11-10 22:03:40 +01004072 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004073
4074 // Try to retrieve the entire security descriptor.
4075 err = GetNamedSecurityInfoW(
4076 wn, // Abstract filename
4077 SE_FILE_OBJECT, // File Object
4078 OWNER_SECURITY_INFORMATION |
4079 GROUP_SECURITY_INFORMATION |
4080 DACL_SECURITY_INFORMATION |
4081 SACL_SECURITY_INFORMATION,
4082 &p->pSidOwner, // Ownership information.
4083 &p->pSidGroup, // Group membership.
4084 &p->pDacl, // Discretionary information.
4085 &p->pSacl, // For auditing purposes.
4086 &p->pSecurityDescriptor);
4087 if (err == ERROR_ACCESS_DENIED ||
4088 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004089 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004090 // Retrieve only DACL.
4091 (void)GetNamedSecurityInfoW(
4092 wn,
4093 SE_FILE_OBJECT,
4094 DACL_SECURITY_INFORMATION,
4095 NULL,
4096 NULL,
4097 &p->pDacl,
4098 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004099 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004100 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004101 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004102 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004103 mch_free_acl((vim_acl_T)p);
4104 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004106 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 return (vim_acl_T)p;
4110#endif
4111}
4112
Bram Moolenaar27515922013-06-29 15:36:26 +02004113#ifdef HAVE_ACL
4114/*
4115 * Check if "acl" contains inherited ACE.
4116 */
4117 static BOOL
4118is_acl_inherited(PACL acl)
4119{
4120 DWORD i;
4121 ACL_SIZE_INFORMATION acl_info;
4122 PACCESS_ALLOWED_ACE ace;
4123
4124 acl_info.AceCount = 0;
4125 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
4126 for (i = 0; i < acl_info.AceCount; i++)
4127 {
4128 GetAce(acl, i, (LPVOID *)&ace);
4129 if (ace->Header.AceFlags & INHERITED_ACE)
4130 return TRUE;
4131 }
4132 return FALSE;
4133}
4134#endif
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136/*
4137 * Set the ACL of file "fname" to "acl" (unless it's NULL).
4138 * Errors are ignored.
4139 * This must only be called with "acl" equal to what mch_get_acl() returned.
4140 */
4141 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004142mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144#ifdef HAVE_ACL
4145 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02004146 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004147 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004149 if (p == NULL)
4150 return;
4151
4152 wn = enc_to_utf16(fname, NULL);
4153 if (wn == NULL)
4154 return;
4155
4156 // Set security flags
4157 if (p->pSidOwner)
4158 sec_info |= OWNER_SECURITY_INFORMATION;
4159 if (p->pSidGroup)
4160 sec_info |= GROUP_SECURITY_INFORMATION;
4161 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02004162 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004163 sec_info |= DACL_SECURITY_INFORMATION;
4164 // Do not inherit its parent's DACL.
4165 // If the DACL is inherited, Cygwin permissions would be changed.
4166 if (!is_acl_inherited(p->pDacl))
4167 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02004168 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004169 if (p->pSacl)
4170 sec_info |= SACL_SECURITY_INFORMATION;
4171
4172 (void)SetNamedSecurityInfoW(
4173 wn, // Abstract filename
4174 SE_FILE_OBJECT, // File Object
4175 sec_info,
4176 p->pSidOwner, // Ownership information.
4177 p->pSidGroup, // Group membership.
4178 p->pDacl, // Discretionary information.
4179 p->pSacl // For auditing purposes.
4180 );
4181 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#endif
4183}
4184
4185 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004186mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188#ifdef HAVE_ACL
4189 struct my_acl *p = (struct my_acl *)acl;
4190
4191 if (p != NULL)
4192 {
4193 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
4194 vim_free(p);
4195 }
4196#endif
4197}
4198
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004199#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201/*
4202 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
4203 */
4204 static BOOL WINAPI
4205handler_routine(
4206 DWORD dwCtrlType)
4207{
Bram Moolenaar589b1102017-08-12 16:39:05 +02004208 INPUT_RECORD ir;
4209 DWORD out;
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 switch (dwCtrlType)
4212 {
4213 case CTRL_C_EVENT:
4214 if (ctrl_c_interrupts)
4215 g_fCtrlCPressed = TRUE;
4216 return TRUE;
4217
4218 case CTRL_BREAK_EVENT:
4219 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02004220 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004221 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02004222 ir.EventType = KEY_EVENT;
4223 ir.Event.KeyEvent.bKeyDown = TRUE;
4224 ir.Event.KeyEvent.wRepeatCount = 1;
4225 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
4226 ir.Event.KeyEvent.wVirtualScanCode = 0;
4227 ir.Event.KeyEvent.dwControlKeyState = 0;
4228 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
4229 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return TRUE;
4231
Bram Moolenaar0f873732019-12-05 20:28:46 +01004232 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 case CTRL_CLOSE_EVENT:
4234 case CTRL_LOGOFF_EVENT:
4235 case CTRL_SHUTDOWN_EVENT:
4236 windgoto((int)Rows - 1, 0);
4237 g_fForceExit = TRUE;
4238
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004239 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 (dwCtrlType == CTRL_CLOSE_EVENT
4241 ? _("close")
4242 : dwCtrlType == CTRL_LOGOFF_EVENT
4243 ? _("logoff")
4244 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004245# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
Bram Moolenaar0f873732019-12-05 20:28:46 +01004249 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar0f873732019-12-05 20:28:46 +01004251 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 default:
4254 return FALSE;
4255 }
4256}
4257
4258
4259/*
4260 * set the tty in (raw) ? "raw" : "cooked" mode
4261 */
4262 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02004263mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
4265 DWORD cmodein;
4266 DWORD cmodeout;
4267 BOOL bEnableHandler;
4268
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004269# ifdef VIMDLL
4270 if (gui.in_use)
4271 return;
4272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 GetConsoleMode(g_hConIn, &cmodein);
4274 GetConsoleMode(g_hConOut, &cmodeout);
4275 if (tmode == TMODE_RAW)
4276 {
4277 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
4278 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02004280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02004282 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
4283 }
4284 else
4285 {
4286 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
4287 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01004288 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004289# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01004290 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
4291 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01004292 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004293# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01004294 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004295# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01004296 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 bEnableHandler = TRUE;
4298 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004299 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
4302 ENABLE_ECHO_INPUT);
4303 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
4304 bEnableHandler = FALSE;
4305 }
Wez Furlong6ef5ab52021-05-30 19:29:41 +02004306 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 SetConsoleMode(g_hConOut, cmodeout);
4308 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
4309
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004310# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (fdDump)
4312 {
4313 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
4314 tmode == TMODE_RAW ? "raw" :
4315 tmode == TMODE_COOK ? "cooked" : "normal",
4316 cmodein, cmodeout);
4317 fflush(fdDump);
4318 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322
4323/*
4324 * Get the size of the current window in `Rows' and `Columns'
4325 * Return OK when size could be determined, FAIL otherwise.
4326 */
4327 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004328mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 CONSOLE_SCREEN_BUFFER_INFO csbi;
4331
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004332# ifdef VIMDLL
4333 if (gui.in_use)
4334 return OK;
4335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (!g_fTermcapMode && g_cbTermcap.IsValid)
4337 {
4338 /*
4339 * For some reason, we are trying to get the screen dimensions
4340 * even though we are not in termcap mode. The 'Rows' and 'Columns'
4341 * variables are really intended to mean the size of Vim screen
4342 * while in termcap mode.
4343 */
4344 Rows = g_cbTermcap.Info.dwSize.Y;
4345 Columns = g_cbTermcap.Info.dwSize.X;
4346 }
4347 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
4348 {
4349 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
4350 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
4351 }
4352 else
4353 {
4354 Rows = 25;
4355 Columns = 80;
4356 }
4357 return OK;
4358}
4359
4360/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004361 * Resize console buffer to 'COORD'
4362 */
4363 static void
4364ResizeConBuf(
4365 HANDLE hConsole,
4366 COORD coordScreen)
4367{
4368 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
4369 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004370# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004371 if (fdDump)
4372 {
4373 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
4374 GetLastError());
4375 fflush(fdDump);
4376 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004377# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004378 }
4379}
4380
4381/*
4382 * Resize console window size to 'srWindowRect'
4383 */
4384 static void
4385ResizeWindow(
4386 HANDLE hConsole,
4387 SMALL_RECT srWindowRect)
4388{
4389 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
4390 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004391# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004392 if (fdDump)
4393 {
4394 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
4395 GetLastError());
4396 fflush(fdDump);
4397 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004398# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004399 }
4400}
4401
4402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 * Set a console window to `xSize' * `ySize'
4404 */
4405 static void
4406ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004407 HANDLE hConsole,
4408 int xSize,
4409 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004411 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
4412 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02004414 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02004415 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004417# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (fdDump)
4419 {
4420 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
4421 fflush(fdDump);
4422 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
Bram Moolenaar0f873732019-12-05 20:28:46 +01004425 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 coordScreen = GetLargestConsoleWindowSize(hConsole);
4427
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
4430 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
4431 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
4432
4433 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
4434 {
4435 int sx, sy;
4436
4437 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
4438 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
4439 if (sy < ySize || sx < xSize)
4440 {
4441 /*
4442 * Increasing number of lines/columns, do buffer first.
4443 * Use the maximal size in x and y direction.
4444 */
4445 if (sy < ySize)
4446 coordScreen.Y = ySize;
4447 else
4448 coordScreen.Y = sy;
4449 if (sx < xSize)
4450 coordScreen.X = xSize;
4451 else
4452 coordScreen.X = sx;
4453 SetConsoleScreenBufferSize(hConsole, coordScreen);
4454 }
4455 }
4456
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004457 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 coordScreen.X = xSize;
4459 coordScreen.Y = ySize;
4460
Bram Moolenaar2551c032018-08-23 22:38:31 +02004461 // In the new console call API, only the first time in reverse order
4462 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004464 ResizeWindow(hConsole, srWindowRect);
4465 ResizeConBuf(hConsole, coordScreen);
4466 }
4467 else
4468 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02004469 // Workaround for a Windows 10 bug
4470 cursor.X = srWindowRect.Left;
4471 cursor.Y = srWindowRect.Top;
4472 SetConsoleCursorPosition(hConsole, cursor);
4473
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02004474 ResizeConBuf(hConsole, coordScreen);
4475 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02004476 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478}
4479
4480
4481/*
4482 * Set the console window to `Rows' * `Columns'
4483 */
4484 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004485mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 COORD coordScreen;
4488
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004489# ifdef VIMDLL
4490 if (gui.in_use)
4491 return;
4492# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004493 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (suppress_winsize != 0)
4495 {
4496 suppress_winsize = 2;
4497 return;
4498 }
4499
4500 if (term_console)
4501 {
4502 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
4503
Bram Moolenaar0f873732019-12-05 20:28:46 +01004504 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (Rows > coordScreen.Y)
4506 Rows = coordScreen.Y;
4507 if (Columns > coordScreen.X)
4508 Columns = coordScreen.X;
4509
4510 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
4511 }
4512}
4513
4514/*
4515 * Rows and/or Columns has changed.
4516 */
4517 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004518mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004520# ifdef VIMDLL
4521 if (gui.in_use)
4522 return;
4523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 set_scroll_region(0, 0, Columns - 1, Rows - 1);
4525}
4526
4527
4528/*
4529 * Called when started up, to set the winsize that was delayed.
4530 */
4531 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004532mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533{
4534 if (suppress_winsize == 2)
4535 {
4536 suppress_winsize = 0;
4537 mch_set_shellsize();
4538 shell_resized();
4539 }
4540 suppress_winsize = 0;
4541}
Bram Moolenaar0f873732019-12-05 20:28:46 +01004542#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004544 static BOOL
4545vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01004546 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004547 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01004548 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004549 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004550 PROCESS_INFORMATION *pi,
4551 LPVOID *env,
4552 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004553{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004554 BOOL ret = FALSE;
4555 WCHAR *wcmd, *wcwd = NULL;
4556
4557 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4558 if (wcmd == NULL)
4559 return FALSE;
4560 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004561 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004562 wcwd = enc_to_utf16((char_u *)cwd, NULL);
4563 if (wcwd == NULL)
4564 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004565 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004566
4567 ret = CreateProcessW(
4568 NULL, // Executable name
4569 wcmd, // Command to execute
4570 NULL, // Process security attributes
4571 NULL, // Thread security attributes
4572 inherit_handles, // Inherit handles
4573 flags, // Creation flags
4574 env, // Environment
4575 wcwd, // Current directory
4576 (LPSTARTUPINFOW)si, // Startup information
4577 pi); // Process information
4578theend:
4579 vim_free(wcmd);
4580 vim_free(wcwd);
4581 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004585 static HINSTANCE
4586vim_shell_execute(
4587 char *cmd,
4588 INT n_show_cmd)
4589{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004590 HINSTANCE ret;
4591 WCHAR *wcmd;
4592
4593 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4594 if (wcmd == NULL)
4595 return (HINSTANCE) 0;
4596
4597 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
4598 vim_free(wcmd);
4599 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004600}
4601
4602
Bram Moolenaar4f974752019-02-17 17:44:42 +01004603#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605/*
4606 * Specialised version of system() for Win32 GUI mode.
4607 * This version proceeds as follows:
4608 * 1. Create a console window for use by the subprocess
4609 * 2. Run the subprocess (it gets the allocated console by default)
4610 * 3. Wait for the subprocess to terminate and get its exit code
4611 * 4. Prompt the user to press a key to close the console window
4612 */
4613 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004614mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 STARTUPINFO si;
4617 PROCESS_INFORMATION pi;
4618 DWORD ret = 0;
4619 HWND hwnd = GetFocus();
4620
4621 si.cb = sizeof(si);
4622 si.lpReserved = NULL;
4623 si.lpDesktop = NULL;
4624 si.lpTitle = NULL;
4625 si.dwFlags = STARTF_USESHOWWINDOW;
4626 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004627 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004628 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004630 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01004631 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 else
4633 si.wShowWindow = SW_SHOWNORMAL;
4634 si.cbReserved2 = 0;
4635 si.lpReserved2 = NULL;
4636
Bram Moolenaar0f873732019-12-05 20:28:46 +01004637 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004638 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004639 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
4640 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar0f873732019-12-05 20:28:46 +01004642 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004644# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 int delay = 1;
4646
Bram Moolenaar0f873732019-12-05 20:28:46 +01004647 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 for (;;)
4649 {
4650 MSG msg;
4651
K.Takatab7057bd2022-01-21 11:37:07 +00004652 if (PeekMessageW(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00004655 DispatchMessageW(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02004656 delay = 1;
4657 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4660 break;
4661
Bram Moolenaar0f873732019-12-05 20:28:46 +01004662 // We start waiting for a very short time and then increase it, so
4663 // that we respond quickly when the process is quick, and don't
4664 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (delay < 50)
4666 delay += 10;
4667 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004668# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 GetExitCodeProcess(pi.hProcess, &ret);
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
Bram Moolenaar0f873732019-12-05 20:28:46 +01004676 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 CloseHandle(pi.hThread);
4678 CloseHandle(pi.hProcess);
4679
Bram Moolenaar0f873732019-12-05 20:28:46 +01004680 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4682
4683 return ret;
4684}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004685
4686/*
4687 * Thread launched by the gui to send the current buffer data to the
4688 * process. This way avoid to hang up vim totally if the children
4689 * process take a long time to process the lines.
4690 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004691 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004692sub_process_writer(LPVOID param)
4693{
4694 HANDLE g_hChildStd_IN_Wr = param;
4695 linenr_T lnum = curbuf->b_op_start.lnum;
4696 DWORD len = 0;
4697 DWORD l;
4698 char_u *lp = ml_get(lnum);
4699 char_u *s;
4700 int written = 0;
4701
4702 for (;;)
4703 {
4704 l = (DWORD)STRLEN(lp + written);
4705 if (l == 0)
4706 len = 0;
4707 else if (lp[written] == NL)
4708 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004709 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004710 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4711 }
4712 else
4713 {
4714 s = vim_strchr(lp + written, NL);
4715 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4716 s == NULL ? l : (DWORD)(s - (lp + written)),
4717 &len, NULL);
4718 }
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004719 if (len == l)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004720 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004721 // Finished a line, add a NL, unless this line should not have
4722 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004723 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004724 || (!curbuf->b_p_bin
4725 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004726 || (lnum != curbuf->b_no_eol_lnum
4727 && (lnum != curbuf->b_ml.ml_line_count
4728 || curbuf->b_p_eol)))
4729 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004730 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4731 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004732 }
4733
4734 ++lnum;
4735 if (lnum > curbuf->b_op_end.lnum)
4736 break;
4737
4738 lp = ml_get(lnum);
4739 written = 0;
4740 }
4741 else if (len > 0)
4742 written += len;
4743 }
4744
Bram Moolenaar0f873732019-12-05 20:28:46 +01004745 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004746 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004747 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004748}
4749
4750
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004752
4753/*
4754 * This function read from the children's stdout and write the
4755 * data on screen or in the buffer accordingly.
4756 */
4757 static void
4758dump_pipe(int options,
4759 HANDLE g_hChildStd_OUT_Rd,
4760 garray_T *ga,
4761 char_u buffer[],
4762 DWORD *buffer_off)
4763{
4764 DWORD availableBytes = 0;
4765 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004766 int ret;
4767 DWORD len;
4768 DWORD toRead;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004769
Bram Moolenaar0f873732019-12-05 20:28:46 +01004770 // we query the pipe to see if there is any data to read
4771 // to avoid to perform a blocking read
4772 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4773 NULL, // optional buffer
4774 0, // buffer size
4775 NULL, // number of read bytes
4776 &availableBytes, // available bytes total
4777 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004778
Bram Moolenaar0f873732019-12-05 20:28:46 +01004779 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004780 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004781 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01004782 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004783 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004784 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004785
Bram Moolenaar0f873732019-12-05 20:28:46 +01004786 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004787 if (len == 0)
4788 break;
4789
4790 availableBytes -= len;
4791
4792 if (options & SHELL_READ)
4793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004794 // Do NUL -> NL translation, append NL separated
4795 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004796 for (i = 0; i < len; ++i)
4797 {
4798 if (buffer[i] == NL)
4799 append_ga_line(ga);
4800 else if (buffer[i] == NUL)
4801 ga_append(ga, NL);
4802 else
4803 ga_append(ga, buffer[i]);
4804 }
4805 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004806 else if (has_mbyte)
4807 {
4808 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004809 int c;
4810 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004811
4812 len += *buffer_off;
4813 buffer[len] = NUL;
4814
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 // Check if the last character in buffer[] is
4816 // incomplete, keep these bytes for the next
4817 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004818 for (p = buffer; p < buffer + len; p += l)
4819 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004820 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004821 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004822 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004823 else if (MB_BYTE2LEN(*p) != l)
4824 break;
4825 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004826 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004828 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004829 if (len >= 12)
4830 ++p;
4831 else
4832 {
4833 *buffer_off = len;
4834 return;
4835 }
4836 }
4837 c = *p;
4838 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004839 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004840 if (p < buffer + len)
4841 {
4842 *p = c;
4843 *buffer_off = (DWORD)((buffer + len) - p);
4844 mch_memmove(buffer, p, *buffer_off);
4845 return;
4846 }
4847 *buffer_off = 0;
4848 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004849 else
4850 {
4851 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004852 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004853 }
4854
4855 windgoto(msg_row, msg_col);
4856 cursor_on();
4857 out_flush();
4858 }
4859}
4860
4861/*
4862 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4863 * for communication and doesn't open any new window.
4864 */
4865 static int
4866mch_system_piped(char *cmd, int options)
4867{
4868 STARTUPINFO si;
4869 PROCESS_INFORMATION pi;
4870 DWORD ret = 0;
4871
4872 HANDLE g_hChildStd_IN_Rd = NULL;
4873 HANDLE g_hChildStd_IN_Wr = NULL;
4874 HANDLE g_hChildStd_OUT_Rd = NULL;
4875 HANDLE g_hChildStd_OUT_Wr = NULL;
4876
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004878 DWORD len;
4879
Bram Moolenaar0f873732019-12-05 20:28:46 +01004880 // buffer used to receive keys
4881 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4882 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004883
4884 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004885 int noread_cnt = 0;
4886 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004887 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004888 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004889 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004890
4891 SECURITY_ATTRIBUTES saAttr;
4892
Bram Moolenaar0f873732019-12-05 20:28:46 +01004893 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004894 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4895 saAttr.bInheritHandle = TRUE;
4896 saAttr.lpSecurityDescriptor = NULL;
4897
4898 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004899 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004900 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004901 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004902 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004903 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004904 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004905 {
4906 CloseHandle(g_hChildStd_IN_Rd);
4907 CloseHandle(g_hChildStd_IN_Wr);
4908 CloseHandle(g_hChildStd_OUT_Rd);
4909 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004910 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004911 }
4912
4913 si.cb = sizeof(si);
4914 si.lpReserved = NULL;
4915 si.lpDesktop = NULL;
4916 si.lpTitle = NULL;
4917 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4918
Bram Moolenaar0f873732019-12-05 20:28:46 +01004919 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004920 si.hStdError = g_hChildStd_OUT_Wr;
4921 si.hStdOutput = g_hChildStd_OUT_Wr;
4922 si.hStdInput = g_hChildStd_IN_Rd;
4923 si.wShowWindow = SW_HIDE;
4924 si.cbReserved2 = 0;
4925 si.lpReserved2 = NULL;
4926
4927 if (options & SHELL_READ)
4928 ga_init2(&ga, 1, BUFLEN);
4929
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004930 if (cmd != NULL)
4931 {
4932 p = (char *)vim_strsave((char_u *)cmd);
4933 if (p != NULL)
4934 unescape_shellxquote((char_u *)p, p_sxe);
4935 else
4936 p = cmd;
4937 }
4938
Bram Moolenaar0f873732019-12-05 20:28:46 +01004939 // Now, run the command.
4940 // About "Inherit handles" being TRUE: this command can be litigious,
4941 // handle inheritance was deactivated for pending temp file, but, if we
4942 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004943 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4944 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004945
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004946 if (p != cmd)
4947 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004948
Bram Moolenaar0f873732019-12-05 20:28:46 +01004949 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004950 CloseHandle(g_hChildStd_IN_Rd);
4951 CloseHandle(g_hChildStd_OUT_Wr);
4952
4953 if (options & SHELL_WRITE)
4954 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004955 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004956 _beginthreadex(NULL, // security attributes
4957 0, // default stack size
4958 sub_process_writer, // function to be executed
4959 g_hChildStd_IN_Wr, // parameter
4960 0, // creation flag, start immediately
4961 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004962 CloseHandle(thread);
4963 g_hChildStd_IN_Wr = NULL;
4964 }
4965
Bram Moolenaar0f873732019-12-05 20:28:46 +01004966 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004967 for (;;)
4968 {
4969 MSG msg;
4970
K.Takatab7057bd2022-01-21 11:37:07 +00004971 if (PeekMessageW(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004972 {
4973 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00004974 DispatchMessageW(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004975 }
4976
Bram Moolenaar0f873732019-12-05 20:28:46 +01004977 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004978 if ((options & (SHELL_READ|SHELL_WRITE))
4979# ifdef FEAT_GUI
4980 || gui.in_use
4981# endif
4982 )
4983 {
4984 len = 0;
4985 if (!(options & SHELL_EXPAND)
4986 && ((options &
4987 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4988 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4989# ifdef FEAT_GUI
4990 || gui.in_use
4991# endif
4992 )
4993 && (ta_len > 0 || noread_cnt > 4))
4994 {
4995 if (ta_len == 0)
4996 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004997 // Get extra characters when we don't have any. Reset the
4998 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004999 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005000 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5001 }
5002 if (ta_len > 0 || len > 0)
5003 {
5004 /*
5005 * For pipes: Check for CTRL-C: send interrupt signal to
5006 * child. Check for CTRL-D: EOF, close pipe to child.
5007 */
5008 if (len == 1 && cmd != NULL)
5009 {
5010 if (ta_buf[ta_len] == Ctrl_C)
5011 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005012 // Learn what exit code is expected, for
5013 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005014 TerminateProcess(pi.hProcess, 9);
5015 }
5016 if (ta_buf[ta_len] == Ctrl_D)
5017 {
5018 CloseHandle(g_hChildStd_IN_Wr);
5019 g_hChildStd_IN_Wr = NULL;
5020 }
5021 }
5022
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01005023 len = term_replace_keycodes(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005024
5025 /*
5026 * For pipes: echo the typed characters. For a pty this
5027 * does not seem to work.
5028 */
5029 for (i = ta_len; i < ta_len + len; ++i)
5030 {
5031 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5032 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005033 else if (has_mbyte)
5034 {
5035 int l = (*mb_ptr2len)(ta_buf + i);
5036
5037 msg_outtrans_len(ta_buf + i, l);
5038 i += l - 1;
5039 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005040 else
5041 msg_outtrans_len(ta_buf + i, 1);
5042 }
5043 windgoto(msg_row, msg_col);
5044 out_flush();
5045
5046 ta_len += len;
5047
5048 /*
5049 * Write the characters to the child, unless EOF has been
5050 * typed for pipes. Write one character at a time, to
5051 * avoid losing too much typeahead. When writing buffer
5052 * lines, drop the typed characters (only check for
5053 * CTRL-C).
5054 */
5055 if (options & SHELL_WRITE)
5056 ta_len = 0;
5057 else if (g_hChildStd_IN_Wr != NULL)
5058 {
5059 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
5060 1, &len, NULL);
5061 // if we are typing in, we want to keep things reactive
5062 delay = 1;
5063 if (len > 0)
5064 {
5065 ta_len -= len;
5066 mch_memmove(ta_buf, ta_buf + len, ta_len);
5067 }
5068 }
5069 }
5070 }
5071 }
5072
5073 if (ta_len)
5074 ui_inchar_undo(ta_buf, ta_len);
5075
5076 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
5077 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02005078 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005079 break;
5080 }
5081
5082 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02005083 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005084
Bram Moolenaar0f873732019-12-05 20:28:46 +01005085 // We start waiting for a very short time and then increase it, so
5086 // that we respond quickly when the process is quick, and don't
5087 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005088 if (delay < 50)
5089 delay += 10;
5090 }
5091
Bram Moolenaar0f873732019-12-05 20:28:46 +01005092 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005093 CloseHandle(g_hChildStd_OUT_Rd);
5094 if (g_hChildStd_IN_Wr != NULL)
5095 CloseHandle(g_hChildStd_IN_Wr);
5096
5097 WaitForSingleObject(pi.hProcess, INFINITE);
5098
Bram Moolenaar0f873732019-12-05 20:28:46 +01005099 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005100 GetExitCodeProcess(pi.hProcess, &ret);
5101
5102 if (options & SHELL_READ)
5103 {
5104 if (ga.ga_len > 0)
5105 {
5106 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005107 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005108 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
5109 }
5110 else
5111 curbuf->b_no_eol_lnum = 0;
5112 ga_clear(&ga);
5113 }
5114
Bram Moolenaar0f873732019-12-05 20:28:46 +01005115 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005116 CloseHandle(pi.hThread);
5117 CloseHandle(pi.hProcess);
5118
5119 return ret;
5120}
5121
5122 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005123mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005124{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005125 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005126 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02005127 return mch_system_piped(cmd, options);
5128 else
5129 return mch_system_classic(cmd, options);
5130}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005133#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01005134 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02005135mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01005136{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005137 int ret;
5138 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02005139 char_u *buf;
5140 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005141
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02005142 // If the command starts and ends with double quotes, enclose the command
5143 // in parentheses.
5144 len = STRLEN(cmd);
5145 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
5146 {
5147 len += 3;
5148 buf = alloc(len);
5149 if (buf == NULL)
5150 return -1;
5151 vim_snprintf((char *)buf, len, "(%s)", cmd);
5152 wcmd = enc_to_utf16(buf, NULL);
5153 free(buf);
5154 }
5155 else
5156 wcmd = enc_to_utf16((char_u *)cmd, NULL);
5157
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005158 if (wcmd == NULL)
5159 return -1;
5160
5161 ret = _wsystem(wcmd);
5162 vim_free(wcmd);
5163 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01005164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166#endif
5167
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005168 static int
5169mch_system(char *cmd, int options)
5170{
5171#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005172 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005173 return mch_system_g(cmd, options);
5174 else
5175 return mch_system_c(cmd, options);
5176#elif defined(FEAT_GUI_MSWIN)
5177 return mch_system_g(cmd, options);
5178#else
5179 return mch_system_c(cmd, options);
5180#endif
5181}
5182
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005183#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5184/*
5185 * Use a terminal window to run a shell command in.
5186 */
5187 static int
5188mch_call_shell_terminal(
5189 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005190 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005191{
5192 jobopt_T opt;
5193 char_u *newcmd = NULL;
5194 typval_T argvar[2];
5195 long_u cmdlen;
5196 int retval = -1;
5197 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02005198 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005199 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005200 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005201
Bram Moolenaar42f652f2018-03-19 21:44:37 +01005202 if (cmd == NULL)
5203 cmdlen = STRLEN(p_sh) + 1;
5204 else
5205 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02005206 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005207 if (newcmd == NULL)
5208 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01005209 if (cmd == NULL)
5210 {
5211 STRCPY(newcmd, p_sh);
5212 ch_log(NULL, "starting terminal to run a shell");
5213 }
5214 else
5215 {
5216 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
5217 ch_log(NULL, "starting terminal for system command '%s'", cmd);
5218 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005219
5220 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005221
5222 argvar[0].v_type = VAR_STRING;
5223 argvar[0].vval.v_string = newcmd;
5224 argvar[1].v_type = VAR_UNKNOWN;
5225 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01005226 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01005227 {
5228 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01005229 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01005230 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005231
Bram Moolenaarf9c38832018-06-19 19:59:20 +02005232 job = term_getjob(buf->b_term);
5233 ++job->jv_refcount;
5234
Bram Moolenaar0f873732019-12-05 20:28:46 +01005235 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005236 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00005237 if (curbuf == buf)
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005238 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00005239 // Only do this when a window was found for "buf".
5240 clear_oparg(&oa);
5241 while (term_use_loop())
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005242 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00005243 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
5244 {
5245 // If terminal_loop() returns OK we got a key that is handled
5246 // in Normal model. We don't do redrawing anyway.
5247 if (terminal_loop(TRUE) == OK)
5248 normal_cmd(&oa, TRUE);
5249 }
5250 else
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005251 normal_cmd(&oa, TRUE);
5252 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00005253 retval = job->jv_exitval;
5254 ch_log(NULL, "system command finished");
5255
5256 job_unref(job);
5257
5258 // restore curwin/curbuf and a few other things
5259 aucmd_restbuf(&aco);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005260 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005261
5262 wait_return(TRUE);
5263 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
5264
5265 vim_free(newcmd);
5266 return retval;
5267}
5268#endif
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270/*
5271 * Either execute a command by calling the shell or start a new shell
5272 */
5273 int
5274mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005275 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005276 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 int x = 0;
5279 int tmode = cur_tmode;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005280 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02005281
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00005282#ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01005283 ch_log(NULL, "executing shell command: %s", cmd);
5284#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005285 // Change the title to reflect that we are in a subshell.
K.Takataeeec2542021-06-02 13:28:16 +02005286 if (GetConsoleTitleW(szShellTitle, ARRAY_LENGTH(szShellTitle) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02005287 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005288 if (cmd == NULL)
5289 wcscat(szShellTitle, L" :sh");
5290 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02005291 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02005293
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005294 if (wn != NULL)
5295 {
5296 wcscat(szShellTitle, L" - !");
5297 if ((wcslen(szShellTitle) + wcslen(wn) <
K.Takataeeec2542021-06-02 13:28:16 +02005298 ARRAY_LENGTH(szShellTitle)))
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005299 wcscat(szShellTitle, wn);
5300 SetConsoleTitleW(szShellTitle);
5301 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02005302 }
5303 }
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 out_flush();
5307
5308#ifdef MCH_WRITE_DUMP
5309 if (fdDump)
5310 {
5311 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
5312 fflush(fdDump);
5313 }
5314#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005315#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005316 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005317 if (
5318# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005319 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005320# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005321 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005322 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
5323 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005324 char_u *cmdbase = cmd;
5325
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02005326 if (cmdbase != NULL)
5327 // Skip a leading quote and (.
5328 while (*cmdbase == '"' || *cmdbase == '(')
5329 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005330
5331 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01005332 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
5333 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005334 {
5335 // Use a terminal window to run the command in.
5336 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005337 resettitle();
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02005338 return x;
5339 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01005340 }
5341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 /*
5344 * Catch all deadly signals while running the external command, because a
5345 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
5346 */
5347 signal(SIGINT, SIG_IGN);
5348#if defined(__GNUC__) && !defined(__MINGW32__)
5349 signal(SIGKILL, SIG_IGN);
5350#else
5351 signal(SIGBREAK, SIG_IGN);
5352#endif
5353 signal(SIGILL, SIG_IGN);
5354 signal(SIGFPE, SIG_IGN);
5355 signal(SIGSEGV, SIG_IGN);
5356 signal(SIGTERM, SIG_IGN);
5357 signal(SIGABRT, SIG_IGN);
5358
5359 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005360 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
5362 if (cmd == NULL)
5363 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01005364 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366 else
5367 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005368 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005369 char_u *newcmd = NULL;
5370 char_u *cmdbase = cmd;
5371 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005372
Bram Moolenaar0f873732019-12-05 20:28:46 +01005373 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005374 if (*cmdbase == '"' )
5375 ++cmdbase;
5376 if (*cmdbase == '(')
5377 ++cmdbase;
5378
Bram Moolenaar1c465442017-03-12 20:10:05 +01005379 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005380 {
5381 STARTUPINFO si;
5382 PROCESS_INFORMATION pi;
5383 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01005384 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005385 char_u *p;
5386
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01005387 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005388 si.cb = sizeof(si);
5389 si.lpReserved = NULL;
5390 si.lpDesktop = NULL;
5391 si.lpTitle = NULL;
5392 si.dwFlags = 0;
5393 si.cbReserved2 = 0;
5394 si.lpReserved2 = NULL;
5395
5396 cmdbase = skipwhite(cmdbase + 5);
5397 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01005398 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005399 {
5400 cmdbase = skipwhite(cmdbase + 4);
5401 si.dwFlags = STARTF_USESHOWWINDOW;
5402 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01005403 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005404 }
5405 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01005406 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005407 {
5408 cmdbase = skipwhite(cmdbase + 2);
5409 flags = CREATE_NO_WINDOW;
5410 si.dwFlags = STARTF_USESTDHANDLES;
5411 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005412 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005413 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005414 NULL, // Security att.
5415 OPEN_EXISTING, // Open flags
5416 FILE_ATTRIBUTE_NORMAL, // File att.
5417 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005418 si.hStdOutput = si.hStdInput;
5419 si.hStdError = si.hStdInput;
5420 }
5421
Bram Moolenaar0f873732019-12-05 20:28:46 +01005422 // Remove a trailing ", ) and )" if they have a match
5423 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005424 if (cmdbase > cmd)
5425 {
5426 p = cmdbase + STRLEN(cmdbase);
5427 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
5428 *--p = NUL;
5429 if (p > cmdbase && p[-1] == ')'
5430 && (*cmd =='(' || cmd[1] == '('))
5431 *--p = NUL;
5432 }
5433
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005434 newcmd = cmdbase;
5435 unescape_shellxquote(cmdbase, p_sxe);
5436
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005437 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005438 * If creating new console, arguments are passed to the
5439 * 'cmd.exe' as-is. If it's not, arguments are not treated
5440 * correctly for current 'cmd.exe'. So unescape characters in
5441 * shellxescape except '|' for avoiding to be treated as
5442 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005443 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005444 if (flags != CREATE_NEW_CONSOLE)
5445 {
5446 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01005447 char_u *cmd_shell = mch_getenv("COMSPEC");
5448
5449 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01005450 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005451
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01005452 subcmd = vim_strsave_escaped_ext(cmdbase,
5453 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005454 if (subcmd != NULL)
5455 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005456 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005457 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02005458 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005459 if (newcmd != NULL)
5460 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01005461 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005462 else
5463 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01005464 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005465 }
5466 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005467
5468 /*
5469 * Now, start the command as a process, so that it doesn't
5470 * inherit our handles which causes unpleasant dangling swap
5471 * files if we exit before the spawned process
5472 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005473 if (vim_create_process((char *)newcmd, FALSE, flags,
5474 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005475 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01005476 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
5477 > (HINSTANCE)32)
5478 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005479 else
5480 {
5481 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01005482#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005483# ifdef VIMDLL
5484 if (gui.in_use)
5485# endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005486 emsg(_(e_command_not_found));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005487#endif
5488 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01005489
5490 if (newcmd != cmdbase)
5491 vim_free(newcmd);
5492
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01005493 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005495 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005496 CloseHandle(si.hStdInput);
5497 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005498 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005499 CloseHandle(pi.hThread);
5500 CloseHandle(pi.hProcess);
5501 }
5502 else
5503 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005504 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01005505#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005506 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005507 (!s_dont_use_vimrun && p_stmp ?
5508 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
5509 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005511 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00005512
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02005513 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005514 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01005516#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005517 if (
5518# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005519 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005520# endif
5521 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01005523 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
5524 "External commands will not pause after completion.\n"
5525 "See :help win32-vimrun for more information.");
5526 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005527 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
5528 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01005529
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005530 if (wmsg != NULL && wtitle != NULL)
5531 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
5532 vim_free(wmsg);
5533 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 need_vimrun_warning = FALSE;
5535 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005536 if (
5537# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005538 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005539# endif
5540 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005541 // Use vimrun to execute the command. It opens a console
5542 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005543 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 vimrun_path,
5545 (msg_silent != 0 || (options & SHELL_DOOUT))
5546 ? "-s " : "",
5547 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005548 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005549# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005550 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005551# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02005552 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02005553 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005554 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
5555 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02005556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005558 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00005559 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01005561 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 }
5565
5566 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005567 {
5568 // The shell may have messed with the mode, always set it.
5569 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005570 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02005571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01005575#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02005576 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005577 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#endif
5579 )
5580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005581 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 msg_putchar('\n');
5583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 resettitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 signal(SIGINT, SIG_DFL);
5587#if defined(__GNUC__) && !defined(__MINGW32__)
5588 signal(SIGKILL, SIG_DFL);
5589#else
5590 signal(SIGBREAK, SIG_DFL);
5591#endif
5592 signal(SIGILL, SIG_DFL);
5593 signal(SIGFPE, SIG_DFL);
5594 signal(SIGSEGV, SIG_DFL);
5595 signal(SIGTERM, SIG_DFL);
5596 signal(SIGABRT, SIG_DFL);
5597
5598 return x;
5599}
5600
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005601#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005602 static HANDLE
5603job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01005604 char_u *fname,
5605 DWORD dwDesiredAccess,
5606 DWORD dwShareMode,
5607 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
5608 DWORD dwCreationDisposition,
5609 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005610{
5611 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005612 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005613
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005614 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005615 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005616 return INVALID_HANDLE_VALUE;
5617
5618 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
5619 lpSecurityAttributes, dwCreationDisposition,
5620 dwFlagsAndAttributes, NULL);
5621 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005622 return h;
5623}
5624
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005625/*
5626 * Turn the dictionary "env" into a NUL separated list that can be used as the
5627 * environment argument of vim_create_process().
5628 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005629 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005630win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005631{
5632 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005633 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005634 LPVOID base = GetEnvironmentStringsW();
5635
Bram Moolenaar0f873732019-12-05 20:28:46 +01005636 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005637 if (ga_grow(gap, 1) == FAIL)
5638 return;
5639
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005640 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005641 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005642 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005643 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005645 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005646 typval_T *item = &dict_lookup(hi)->di_tv;
5647 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005648 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005649 --todo;
5650 if (wkey != NULL && wval != NULL)
5651 {
5652 size_t n;
5653 size_t lkey = wcslen(wkey);
5654 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02005655
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005656 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
5657 continue;
5658 for (n = 0; n < lkey; n++)
5659 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
5660 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
5661 for (n = 0; n < lval; n++)
5662 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
5663 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5664 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01005665 vim_free(wkey);
5666 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005667 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005668 }
5669 }
5670
Bram Moolenaar355757a2020-02-10 22:06:32 +01005671 if (base)
5672 {
5673 WCHAR *p = (WCHAR*) base;
5674
5675 // for last \0
5676 if (ga_grow(gap, 1) == FAIL)
5677 return;
5678
5679 while (*p != 0 || *(p + 1) != 0)
5680 {
5681 if (ga_grow(gap, 1) == OK)
5682 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5683 p++;
5684 }
5685 FreeEnvironmentStrings(base);
5686 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5687 }
5688
Bram Moolenaar493359e2018-06-12 20:25:52 +02005689# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005690 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005691# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005692 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005693 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005694# endif
5695# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005696 char_u *version = get_vim_var_str(VV_VERSION);
5697 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005698# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005699 // size of "VIM_SERVERNAME=" and value,
5700 // plus "VIM_TERMINAL=" and value,
5701 // plus two terminating NULs
5702 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005703# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005704 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005705# endif
5706# ifdef FEAT_TERMINAL
5707 + 13 + version_len + 2
5708# endif
5709 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005710
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005711 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005712 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005713# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005714 for (n = 0; n < 15; n++)
5715 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5716 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005717 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005718 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5719 (WCHAR)servername[n];
5720 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005721# endif
5722# ifdef FEAT_TERMINAL
5723 if (is_terminal)
5724 {
5725 for (n = 0; n < 13; n++)
5726 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5727 (WCHAR)"VIM_TERMINAL="[n];
5728 for (n = 0; n < version_len; n++)
5729 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5730 (WCHAR)version[n];
5731 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5732 }
5733# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005734 }
5735 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005736# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005737}
5738
Bram Moolenaarb091f302019-01-19 14:37:00 +01005739/*
5740 * Create a pair of pipes.
5741 * Return TRUE for success, FALSE for failure.
5742 */
5743 static BOOL
5744create_pipe_pair(HANDLE handles[2])
5745{
5746 static LONG s;
5747 char name[64];
5748 SECURITY_ATTRIBUTES sa;
5749
5750 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5751 GetCurrentProcessId(),
5752 InterlockedIncrement(&s));
5753
5754 // Create named pipe. Max size of named pipe is 65535.
5755 handles[1] = CreateNamedPipe(
5756 name,
5757 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5758 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005759 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005760
5761 if (handles[1] == INVALID_HANDLE_VALUE)
5762 return FALSE;
5763
5764 sa.nLength = sizeof(sa);
5765 sa.bInheritHandle = TRUE;
5766 sa.lpSecurityDescriptor = NULL;
5767
5768 handles[0] = CreateFile(name,
5769 FILE_GENERIC_READ,
5770 FILE_SHARE_READ, &sa,
5771 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5772
5773 if (handles[0] == INVALID_HANDLE_VALUE)
5774 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005775 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005776 return FALSE;
5777 }
5778
5779 return TRUE;
5780}
5781
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005782 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005783mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005784{
5785 STARTUPINFO si;
5786 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005787 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005788 SECURITY_ATTRIBUTES saAttr;
5789 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005790 HANDLE ifd[2];
5791 HANDLE ofd[2];
5792 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005793 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005794
5795 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5796 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5797 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5798 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5799 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5800 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5801 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5802
5803 if (use_out_for_err && use_null_for_out)
5804 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005805
5806 ifd[0] = INVALID_HANDLE_VALUE;
5807 ifd[1] = INVALID_HANDLE_VALUE;
5808 ofd[0] = INVALID_HANDLE_VALUE;
5809 ofd[1] = INVALID_HANDLE_VALUE;
5810 efd[0] = INVALID_HANDLE_VALUE;
5811 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005812 ga_init2(&ga, sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005813
Bram Moolenaar14207f42016-10-27 21:13:10 +02005814 jo = CreateJobObject(NULL, NULL);
5815 if (jo == NULL)
5816 {
5817 job->jv_status = JOB_FAILED;
5818 goto failed;
5819 }
5820
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005821 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005822 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005823
Bram Moolenaar76467df2016-02-12 19:30:26 +01005824 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005825 ZeroMemory(&si, sizeof(si));
5826 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005827 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005828 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005829
Bram Moolenaard8070362016-02-15 21:56:54 +01005830 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5831 saAttr.bInheritHandle = TRUE;
5832 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005833
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005834 if (use_file_for_in)
5835 {
5836 char_u *fname = options->jo_io_name[PART_IN];
5837
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005838 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5839 FILE_SHARE_READ | FILE_SHARE_WRITE,
5840 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5841 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005842 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005843 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005844 goto failed;
5845 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005846 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005847 else if (!use_null_for_in
5848 && (!create_pipe_pair(ifd)
5849 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005850 goto failed;
5851
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005852 if (use_file_for_out)
5853 {
5854 char_u *fname = options->jo_io_name[PART_OUT];
5855
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005856 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5857 FILE_SHARE_READ | FILE_SHARE_WRITE,
5858 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5859 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005860 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005861 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005862 goto failed;
5863 }
5864 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005865 else if (!use_null_for_out &&
5866 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005867 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005868 goto failed;
5869
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005870 if (use_file_for_err)
5871 {
5872 char_u *fname = options->jo_io_name[PART_ERR];
5873
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005874 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5875 FILE_SHARE_READ | FILE_SHARE_WRITE,
5876 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5877 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005878 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005879 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005880 goto failed;
5881 }
5882 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005883 else if (!use_out_for_err && !use_null_for_err &&
5884 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005885 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005886 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005887
Bram Moolenaard8070362016-02-15 21:56:54 +01005888 si.dwFlags |= STARTF_USESTDHANDLES;
5889 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005890 si.hStdOutput = ofd[1];
5891 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5892
5893 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5894 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005895 if (options->jo_set & JO_CHANNEL)
5896 {
5897 channel = options->jo_channel;
5898 if (channel != NULL)
5899 ++channel->ch_refcount;
5900 }
5901 else
5902 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005903 if (channel == NULL)
5904 goto failed;
5905 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005906
5907 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005908 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005909 CREATE_DEFAULT_ERROR_MODE |
5910 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005911 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005912 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005913 &si, &pi,
5914 ga.ga_data,
5915 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005916 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005917 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005918 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005919 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005920 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005921
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005922 ga_clear(&ga);
5923
Bram Moolenaar14207f42016-10-27 21:13:10 +02005924 if (!AssignProcessToJobObject(jo, pi.hProcess))
5925 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005926 // if failing, switch the way to terminate
5927 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005928 CloseHandle(jo);
5929 jo = NULL;
5930 }
5931 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005932 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005933 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005934 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005935 job->jv_status = JOB_STARTED;
5936
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005937 CloseHandle(ifd[0]);
5938 CloseHandle(ofd[1]);
5939 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005940 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005941
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005942 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005943 if (channel != NULL)
5944 {
5945 channel_set_pipes(channel,
5946 use_file_for_in || use_null_for_in
5947 ? INVALID_FD : (sock_T)ifd[1],
5948 use_file_for_out || use_null_for_out
5949 ? INVALID_FD : (sock_T)ofd[0],
5950 use_out_for_err || use_file_for_err || use_null_for_err
5951 ? INVALID_FD : (sock_T)efd[0]);
5952 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005953 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005954 return;
5955
5956failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005957 CloseHandle(ifd[0]);
5958 CloseHandle(ofd[0]);
5959 CloseHandle(efd[0]);
5960 CloseHandle(ifd[1]);
5961 CloseHandle(ofd[1]);
5962 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005963 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005964 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005965}
5966
5967 char *
5968mch_job_status(job_T *job)
5969{
5970 DWORD dwExitCode = 0;
5971
Bram Moolenaar76467df2016-02-12 19:30:26 +01005972 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5973 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005974 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005975 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005976 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005977 {
5978 ch_log(job->jv_channel, "Job ended");
5979 job->jv_status = JOB_ENDED;
5980 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005981 return "dead";
5982 }
5983 return "run";
5984}
5985
Bram Moolenaar97792de2016-10-15 18:36:49 +02005986 job_T *
5987mch_detect_ended_job(job_T *job_list)
5988{
5989 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5990 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5991 job_T *job = job_list;
5992
5993 while (job != NULL)
5994 {
5995 DWORD n;
5996 DWORD result;
5997
5998 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5999 && job != NULL; job = job->jv_next)
6000 {
6001 if (job->jv_status == JOB_STARTED)
6002 {
6003 jobHandles[n] = job->jv_proc_info.hProcess;
6004 jobArray[n] = job;
6005 ++n;
6006 }
6007 }
6008 if (n == 0)
6009 continue;
6010 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
6011 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
6012 {
6013 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
6014
6015 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
6016 return wait_job;
6017 }
6018 }
6019 return NULL;
6020}
6021
Bram Moolenaarfb630902016-10-29 14:55:00 +02006022 static BOOL
6023terminate_all(HANDLE process, int code)
6024{
6025 PROCESSENTRY32 pe;
6026 HANDLE h = INVALID_HANDLE_VALUE;
6027 DWORD pid = GetProcessId(process);
6028
6029 if (pid != 0)
6030 {
6031 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6032 if (h != INVALID_HANDLE_VALUE)
6033 {
6034 pe.dwSize = sizeof(PROCESSENTRY32);
6035 if (!Process32First(h, &pe))
6036 goto theend;
6037
6038 do
6039 {
6040 if (pe.th32ParentProcessID == pid)
6041 {
6042 HANDLE ph = OpenProcess(
6043 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
6044 if (ph != NULL)
6045 {
6046 terminate_all(ph, code);
6047 CloseHandle(ph);
6048 }
6049 }
6050 } while (Process32Next(h, &pe));
6051
6052 CloseHandle(h);
6053 }
6054 }
6055
6056theend:
6057 return TerminateProcess(process, code);
6058}
6059
6060/*
6061 * Send a (deadly) signal to "job".
6062 * Return FAIL if it didn't work.
6063 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01006064 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006065mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01006066{
Bram Moolenaar923d9262016-02-25 20:56:01 +01006067 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006068
Bram Moolenaar923d9262016-02-25 20:56:01 +01006069 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01006070 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006071 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02006072 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006073 {
6074 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
6075 job->jv_channel->ch_killing = TRUE;
K.Takata135e1522022-01-29 15:27:58 +00006076 return TerminateJobObject(job->jv_job_object, (UINT)-1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006077 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01006078 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006079 }
6080
6081 if (!AttachConsole(job->jv_proc_info.dwProcessId))
6082 return FAIL;
6083 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01006084 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
6085 job->jv_proc_info.dwProcessId)
6086 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01006087 FreeConsole();
6088 return ret;
6089}
6090
6091/*
6092 * Clear the data related to "job".
6093 */
6094 void
6095mch_clear_job(job_T *job)
6096{
6097 if (job->jv_status != JOB_FAILED)
6098 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02006099 if (job->jv_job_object != NULL)
6100 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01006101 CloseHandle(job->jv_proc_info.hProcess);
6102 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01006103}
6104#endif
6105
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006107#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109/*
6110 * Start termcap mode
6111 */
6112 static void
6113termcap_mode_start(void)
6114{
6115 DWORD cmodein;
6116
6117 if (g_fTermcapMode)
6118 return;
6119
Christopher Plewright1140b512022-11-12 18:46:05 +00006120 // VTP uses alternate screen buffer.
6121 // Switch to a new alternate screen buffer.
6122 if (win11_or_later && p_rs && vtp_working)
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02006123 vtp_printf("\033[?1049h");
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 SaveConsoleBuffer(&g_cbNonTermcap);
6126
6127 if (g_cbTermcap.IsValid)
6128 {
6129 /*
6130 * We've been in termcap mode before. Restore certain screen
6131 * characteristics, including the buffer size and the window
6132 * size. Since we will be redrawing the screen, we don't need
6133 * to restore the actual contents of the buffer.
6134 */
6135 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006136 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
6138 Rows = g_cbTermcap.Info.dwSize.Y;
6139 Columns = g_cbTermcap.Info.dwSize.X;
6140 }
6141 else
6142 {
6143 /*
6144 * This is our first time entering termcap mode. Clear the console
6145 * screen buffer, and resize the buffer to match the current window
6146 * size. We will use this as the size of our editing environment.
6147 */
6148 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006149 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
6151 }
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 resettitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154
6155 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (g_fMouseActive)
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 cmodein |= ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006159 cmodein &= ~ENABLE_QUICK_EDIT_MODE;
6160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 else
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 cmodein &= ~ENABLE_MOUSE_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006164 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
6165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 cmodein |= ENABLE_WINDOW_INPUT;
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006167 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169 redraw_later_clear();
6170 g_fTermcapMode = TRUE;
6171}
6172
6173
6174/*
6175 * End termcap mode
6176 */
6177 static void
6178termcap_mode_end(void)
6179{
6180 DWORD cmodein;
6181 ConsoleBuffer *cb;
6182 COORD coord;
6183 DWORD dwDummy;
6184
6185 if (!g_fTermcapMode)
6186 return;
6187
6188 SaveConsoleBuffer(&g_cbTermcap);
6189
6190 GetConsoleMode(g_hConIn, &cmodein);
6191 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
Wez Furlong6ef5ab52021-05-30 19:29:41 +02006192 cmodein |= g_cmodein & ENABLE_QUICK_EDIT_MODE;
6193 SetConsoleMode(g_hConIn, cmodein | ENABLE_EXTENDED_FLAGS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006195# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01006196 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006199# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02006200 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01006201 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Christopher Plewright1140b512022-11-12 18:46:05 +00006203 // VTP uses alternate screen buffer.
6204 // Switch back to main screen buffer.
6205 if (exiting && win11_or_later && p_rs && vtp_working)
6206 vtp_printf("\033[?1049l");
6207
6208 if (!USE_WT && (p_rs || exiting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210 /*
6211 * Clear anything that happens to be on the current line.
6212 */
6213 coord.X = 0;
6214 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
Christopher Plewright1140b512022-11-12 18:46:05 +00006215 if (!vtp_working)
6216 FillConsoleOutputCharacter(g_hConOut, ' ',
6217 cb->Info.dwSize.X, coord, &dwDummy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 /*
6219 * The following is just for aesthetics. If we are exiting without
6220 * restoring the screen, then we want to have a prompt string
6221 * appear at the bottom line. However, the command interpreter
6222 * seems to always advance the cursor one line before displaying
6223 * the prompt string, which causes the screen to scroll. To
6224 * counter this, move the cursor up one line before exiting.
6225 */
6226 if (exiting && !p_rs)
6227 coord.Y--;
6228 /*
6229 * Position the cursor at the leftmost column of the desired row.
6230 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02006231 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
Christopher Plewright1140b512022-11-12 18:46:05 +00006233 SetConsoleCursorInfo(g_hConOut, &g_cci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 g_fTermcapMode = FALSE;
6235}
Christopher Plewright38804d62022-11-09 23:55:52 +00006236#endif // !FEAT_GUI_MSWIN || VIMDLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006239#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 void
6241mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01006242 char_u *s UNUSED,
6243 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006245 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248#else
6249
6250/*
6251 * clear `n' chars, starting from `coord'
6252 */
6253 static void
6254clear_chars(
6255 COORD coord,
6256 DWORD n)
6257{
Christopher Plewright38804d62022-11-09 23:55:52 +00006258 if (!vtp_working)
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006259 {
6260 DWORD dwDummy;
6261
6262 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
6263 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord,
6264 &dwDummy);
6265 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006266 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006267 {
6268 set_console_color_rgb();
6269 gotoxy(coord.X + 1, coord.Y + 1);
6270 vtp_printf("\033[%dX", n);
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272}
6273
6274
6275/*
6276 * Clear the screen
6277 */
6278 static void
6279clear_screen(void)
6280{
6281 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006282
Christopher Plewright38804d62022-11-09 23:55:52 +00006283 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006284 clear_chars(g_coord, Rows * Columns);
6285 else
6286 {
6287 set_console_color_rgb();
6288 gotoxy(1, 1);
6289 vtp_printf("\033[2J");
6290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291}
6292
6293
6294/*
6295 * Clear to end of display
6296 */
6297 static void
6298clear_to_end_of_display(void)
6299{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006300 COORD save = g_coord;
6301
Christopher Plewright38804d62022-11-09 23:55:52 +00006302 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006303 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006305 else
6306 {
6307 set_console_color_rgb();
6308 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6309 vtp_printf("\033[0J");
6310
6311 gotoxy(save.X + 1, save.Y + 1);
6312 g_coord = save;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316
6317/*
6318 * Clear to end of line
6319 */
6320 static void
6321clear_to_end_of_line(void)
6322{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006323 COORD save = g_coord;
6324
Christopher Plewright38804d62022-11-09 23:55:52 +00006325 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006326 clear_chars(g_coord, Columns - g_coord.X);
6327 else
6328 {
6329 set_console_color_rgb();
6330 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6331 vtp_printf("\033[0K");
6332
6333 gotoxy(save.X + 1, save.Y + 1);
6334 g_coord = save;
6335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
6338
6339/*
6340 * Scroll the scroll region up by `cLines' lines
6341 */
6342 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006343scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 COORD oldcoord = g_coord;
6346
6347 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
6348 delete_lines(cLines);
6349
6350 g_coord = oldcoord;
6351}
6352
6353
6354/*
6355 * Set the scroll region
6356 */
6357 static void
6358set_scroll_region(
6359 unsigned left,
6360 unsigned top,
6361 unsigned right,
6362 unsigned bottom)
6363{
6364 if (left >= right
6365 || top >= bottom
6366 || right > (unsigned) Columns - 1
6367 || bottom > (unsigned) Rows - 1)
6368 return;
6369
6370 g_srScrollRegion.Left = left;
6371 g_srScrollRegion.Top = top;
6372 g_srScrollRegion.Right = right;
6373 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01006374}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006375
Bram Moolenaar6982f422019-02-16 16:48:01 +01006376 static void
6377set_scroll_region_tb(
6378 unsigned top,
6379 unsigned bottom)
6380{
6381 if (top >= bottom || bottom > (unsigned)Rows - 1)
6382 return;
6383
6384 g_srScrollRegion.Top = top;
6385 g_srScrollRegion.Bottom = bottom;
6386}
6387
6388 static void
6389set_scroll_region_lr(
6390 unsigned left,
6391 unsigned right)
6392{
6393 if (left >= right || right > (unsigned)Columns - 1)
6394 return;
6395
6396 g_srScrollRegion.Left = left;
6397 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398}
6399
6400
6401/*
6402 * Insert `cLines' lines at the current cursor position
6403 */
6404 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006405insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
Bram Moolenaar6982f422019-02-16 16:48:01 +01006407 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 COORD dest;
6409 CHAR_INFO fill;
6410
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006411 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
6412
Bram Moolenaar6982f422019-02-16 16:48:01 +01006413 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 dest.Y = g_coord.Y + cLines;
6415
Bram Moolenaar6982f422019-02-16 16:48:01 +01006416 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 source.Top = g_coord.Y;
6418 source.Right = g_srScrollRegion.Right;
6419 source.Bottom = g_srScrollRegion.Bottom - cLines;
6420
Bram Moolenaar6982f422019-02-16 16:48:01 +01006421 clip.Left = g_srScrollRegion.Left;
6422 clip.Top = g_coord.Y;
6423 clip.Right = g_srScrollRegion.Right;
6424 clip.Bottom = g_srScrollRegion.Bottom;
6425
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006426 fill.Char.AsciiChar = ' ';
Christopher Plewright38804d62022-11-09 23:55:52 +00006427 if (!(vtp_working
K.Takata161b6ac2022-11-14 15:31:07 +00006428# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00006429 && (p_tgc || t_colors >= 256)
K.Takata161b6ac2022-11-14 15:31:07 +00006430# endif
Christopher Plewright38804d62022-11-09 23:55:52 +00006431 ))
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006432 fill.Attributes = g_attrCurrent;
6433 else
6434 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006436 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006437
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006438 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
6439
Bram Moolenaar6982f422019-02-16 16:48:01 +01006440 // Here we have to deal with a win32 console flake: If the scroll
6441 // region looks like abc and we scroll c to a and fill with d we get
6442 // cbd... if we scroll block c one line at a time to a, we get cdd...
6443 // vim expects cdd consistently... So we have to deal with that
6444 // here... (this also occurs scrolling the same way in the other
6445 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447 if (source.Bottom < dest.Y)
6448 {
6449 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01006450 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
Bram Moolenaar6982f422019-02-16 16:48:01 +01006452 coord.X = source.Left;
6453 for (i = clip.Top; i < dest.Y; ++i)
6454 {
6455 coord.Y = i;
6456 clear_chars(coord, source.Right - source.Left + 1);
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02006459
Christopher Plewright38804d62022-11-09 23:55:52 +00006460 if (vtp_working)
Bram Moolenaar06b7b582020-05-30 17:49:25 +02006461 {
6462 COORD coord;
6463 int i;
6464
6465 coord.X = source.Left;
6466 for (i = source.Top; i < dest.Y; ++i)
6467 {
6468 coord.Y = i;
6469 clear_chars(coord, source.Right - source.Left + 1);
6470 }
6471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472}
6473
6474
6475/*
6476 * Delete `cLines' lines at the current cursor position
6477 */
6478 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006479delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
Bram Moolenaar6982f422019-02-16 16:48:01 +01006481 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 COORD dest;
6483 CHAR_INFO fill;
6484 int nb;
6485
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006486 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
6487
Bram Moolenaar6982f422019-02-16 16:48:01 +01006488 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 dest.Y = g_coord.Y;
6490
Bram Moolenaar6982f422019-02-16 16:48:01 +01006491 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 source.Top = g_coord.Y + cLines;
6493 source.Right = g_srScrollRegion.Right;
6494 source.Bottom = g_srScrollRegion.Bottom;
6495
Bram Moolenaar6982f422019-02-16 16:48:01 +01006496 clip.Left = g_srScrollRegion.Left;
6497 clip.Top = g_coord.Y;
6498 clip.Right = g_srScrollRegion.Right;
6499 clip.Bottom = g_srScrollRegion.Bottom;
6500
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006501 fill.Char.AsciiChar = ' ';
Christopher Plewright38804d62022-11-09 23:55:52 +00006502 if (!vtp_working)
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006503 fill.Attributes = g_attrCurrent;
6504 else
6505 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006507 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006508
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01006509 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
6510
Bram Moolenaar6982f422019-02-16 16:48:01 +01006511 // Here we have to deal with a win32 console flake; See insert_lines()
6512 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514 nb = dest.Y + (source.Bottom - source.Top) + 1;
6515
6516 if (nb < source.Top)
6517 {
6518 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01006519 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar6982f422019-02-16 16:48:01 +01006521 coord.X = source.Left;
6522 for (i = nb; i < clip.Bottom; ++i)
6523 {
6524 coord.Y = i;
6525 clear_chars(coord, source.Right - source.Left + 1);
6526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
Bram Moolenaar06b7b582020-05-30 17:49:25 +02006528
Christopher Plewright38804d62022-11-09 23:55:52 +00006529 if (vtp_working)
Bram Moolenaar06b7b582020-05-30 17:49:25 +02006530 {
6531 COORD coord;
6532 int i;
6533
6534 coord.X = source.Left;
6535 for (i = nb; i <= source.Bottom; ++i)
6536 {
6537 coord.Y = i;
6538 clear_chars(coord, source.Right - source.Left + 1);
6539 }
6540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
6542
6543
6544/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02006545 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 */
6547 static void
6548gotoxy(
6549 unsigned x,
6550 unsigned y)
6551{
6552 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
6553 return;
6554
Christopher Plewright38804d62022-11-09 23:55:52 +00006555 if (!(vtp_working
K.Takata161b6ac2022-11-14 15:31:07 +00006556# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00006557 && (p_tgc || t_colors >= 256)
K.Takata161b6ac2022-11-14 15:31:07 +00006558# endif
Christopher Plewright38804d62022-11-09 23:55:52 +00006559 ))
Bram Moolenaar2313b612019-09-27 14:14:32 +02006560 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01006561 // There are reports of double-width characters not displayed
6562 // correctly. This workaround should fix it, similar to how it's done
6563 // for VTP.
6564 g_coord.X = 0;
6565 SetConsoleCursorPosition(g_hConOut, g_coord);
6566
Bram Moolenaar2313b612019-09-27 14:14:32 +02006567 // external cursor coords are 1-based; internal are 0-based
6568 g_coord.X = x - 1;
6569 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006570 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006571 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006572 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02006573 {
6574 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02006575 // destruction. Insider build bug. Always enabled because it's cheap
6576 // and avoids mistakes with recognizing the build.
6577 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006578
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006579 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02006580
6581 g_coord.X = x - 1;
6582 g_coord.Y = y - 1;
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
6586
6587/*
6588 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006589 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 */
6591 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006592textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006594 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 SetConsoleTextAttribute(g_hConOut, wAttr);
6597}
6598
6599
6600 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006601textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006603 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Christopher Plewright38804d62022-11-09 23:55:52 +00006605 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006606 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6607 else
6608 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
6611
6612 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006613textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
Bram Moolenaar6383b922015-03-24 17:12:19 +01006615 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
Christopher Plewright38804d62022-11-09 23:55:52 +00006617 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006618 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
6619 else
6620 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621}
6622
6623
6624/*
6625 * restore the default text attribute (whatever we started with)
6626 */
6627 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006628normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
Christopher Plewright38804d62022-11-09 23:55:52 +00006630 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006631 textattr(g_attrDefault);
6632 else
6633 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634}
6635
6636
6637static WORD g_attrPreStandout = 0;
6638
6639/*
6640 * Make the text standout, by brightening it
6641 */
6642 static void
6643standout(void)
6644{
6645 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
6648}
6649
6650
6651/*
6652 * Turn off standout mode
6653 */
6654 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006655standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659
6660 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
6662
6663
6664/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00006665 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 */
6667 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006668mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 char_u *p;
6671 int n;
6672
6673 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
6674 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006675 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006677 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006678# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006679 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 p = T_ME + 2;
6682 n = getdigits(&p);
6683 if (*p == 'm' && n > 0)
6684 {
6685 cterm_normal_fg_color = (n & 0xf) + 1;
6686 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
6687 }
6688 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006689# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006690 cterm_normal_fg_gui_color = INVALCOLOR;
6691 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
6695
6696/*
6697 * visual bell: flash the screen
6698 */
6699 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006700visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 COORD coordOrigin = {0, 0};
6703 WORD attrFlash = ~g_attrCurrent & 0xff;
6704
6705 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006706 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 if (oldattrs == NULL)
6709 return;
6710 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
6711 coordOrigin, &dwDummy);
6712 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
6713 coordOrigin, &dwDummy);
6714
Bram Moolenaar0f873732019-12-05 20:28:46 +01006715 Sleep(15); // wait for 15 msec
Christopher Plewright38804d62022-11-09 23:55:52 +00006716 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006717 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 coordOrigin, &dwDummy);
6719 vim_free(oldattrs);
6720}
6721
6722
6723/*
6724 * Make the cursor visible or invisible
6725 */
6726 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006727cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006730
Christopher Plewright38804d62022-11-09 23:55:52 +00006731 if (vtp_working)
Bram Moolenaar2695de62020-04-17 21:13:01 +02006732 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6733
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006734# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737}
6738
6739
6740/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006741 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006742 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006744 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006746 char_u *pchBuf,
6747 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006749 COORD coord = g_coord;
6750 DWORD written;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006751 DWORD n, cchwritten;
6752 static DWORD cells;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006753 static WCHAR *unicodebuf = NULL;
6754 static int unibuflen = 0;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006755 static int length;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006756 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006757 static WCHAR *utf8spbuf = NULL;
6758 static int utf8splength;
6759 static DWORD utf8spcells;
6760 static WCHAR **utf8usingbuf = &unicodebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006762 if (cbToWrite != 1 || *pchBuf != ' ' || !enc_utf8)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006763 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006764 utf8usingbuf = &unicodebuf;
6765 do
6766 {
6767 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6768 unicodebuf, unibuflen);
6769 if (length && length <= unibuflen)
6770 break;
6771 vim_free(unicodebuf);
6772 unicodebuf = length ? LALLOC_MULT(WCHAR, length) : NULL;
6773 unibuflen = unibuflen ? 0 : length;
6774 } while(1);
6775 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006776 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006777 else // cbToWrite == 1 && *pchBuf == ' ' && enc_utf8
6778 {
6779 if (utf8usingbuf != &utf8spbuf)
6780 {
6781 if (utf8spbuf == NULL)
6782 {
6783 cells = mb_string2cells((char_u *)" ", 1);
6784 length = MultiByteToWideChar(CP_UTF8, 0, " ", 1, NULL, 0);
6785 utf8spbuf = LALLOC_MULT(WCHAR, length);
6786 if (utf8spbuf != NULL)
6787 {
6788 MultiByteToWideChar(CP_UTF8, 0, " ", 1, utf8spbuf, length);
6789 utf8usingbuf = &utf8spbuf;
6790 utf8splength = length;
6791 utf8spcells = cells;
6792 }
6793 }
6794 else
6795 {
6796 utf8usingbuf = &utf8spbuf;
6797 length = utf8splength;
6798 cells = utf8spcells;
6799 }
6800 }
6801 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006802
Christopher Plewright38804d62022-11-09 23:55:52 +00006803 if (!(vtp_working
K.Takata161b6ac2022-11-14 15:31:07 +00006804# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00006805 && (p_tgc || t_colors >= 256)
K.Takata161b6ac2022-11-14 15:31:07 +00006806# endif
Christopher Plewright38804d62022-11-09 23:55:52 +00006807 ))
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006808 {
6809 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6810 coord, &written);
6811 // When writing fails or didn't write a single character, pretend one
6812 // character was written, otherwise we get stuck.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006813 if (WriteConsoleOutputCharacterW(g_hConOut, *utf8usingbuf, length,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006814 coord, &cchwritten) == 0
6815 || cchwritten == 0 || cchwritten == (DWORD)-1)
6816 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006817 }
6818 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006819 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006820 if (WriteConsoleW(g_hConOut, *utf8usingbuf, length, &cchwritten,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006821 NULL) == 0 || cchwritten == 0)
6822 cchwritten = 1;
6823 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006824
K.Takata135e1522022-01-29 15:27:58 +00006825 if (cchwritten == (DWORD)length)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006826 {
6827 written = cbToWrite;
6828 g_coord.X += (SHORT)cells;
6829 }
6830 else
6831 {
6832 char_u *p = pchBuf;
6833 for (n = 0; n < cchwritten; n++)
6834 MB_CPTR_ADV(p);
6835 written = p - pchBuf;
6836 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 while (g_coord.X > g_srScrollRegion.Right)
6840 {
6841 g_coord.X -= (SHORT) Columns;
6842 if (g_coord.Y < g_srScrollRegion.Bottom)
6843 g_coord.Y++;
6844 }
6845
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006846 // Cursor under VTP is always in the correct position, no need to reset.
Christopher Plewright38804d62022-11-09 23:55:52 +00006847 if (!(vtp_working
K.Takata161b6ac2022-11-14 15:31:07 +00006848# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00006849 && (p_tgc || t_colors >= 256)
K.Takata161b6ac2022-11-14 15:31:07 +00006850# endif
Christopher Plewright38804d62022-11-09 23:55:52 +00006851 ))
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006852 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854 return written;
6855}
6856
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006857 static char_u *
6858get_seq(
6859 int *args,
6860 int *count,
6861 char_u *head)
6862{
6863 int argc;
6864 char_u *p;
6865
6866 if (head == NULL || *head != '\033')
6867 return NULL;
6868
6869 argc = 0;
6870 p = head;
6871 ++p;
6872 do
6873 {
6874 ++p;
6875 args[argc] = getdigits(&p);
6876 argc += (argc < 15) ? 1 : 0;
6877 } while (*p == ';');
6878 *count = argc;
6879
6880 return p;
6881}
6882
6883 static char_u *
6884get_sgr(
6885 int *args,
6886 int *count,
6887 char_u *head)
6888{
6889 char_u *p = get_seq(args, count, head);
6890
6891 return (p && *p == 'm') ? ++p : NULL;
6892}
6893
6894/*
6895 * Pointer to next if SGR (^[[n;2;*;*;*m), NULL otherwise.
6896 */
6897 static char_u *
6898sgrn2(
6899 char_u *head,
6900 int n)
6901{
6902 int argc;
6903 int args[16];
6904 char_u *p = get_sgr(args, &argc, head);
6905
6906 return p && argc == 5 && args[0] == n && args[1] == 2 ? p : NULL;
6907}
6908
6909/*
6910 * Pointer to next if SGR(^[[nm)<space>ESC, NULL otherwise.
6911 */
6912 static char_u *
6913sgrnc(
6914 char_u *head,
6915 int n)
6916{
6917 int argc;
6918 int args[16];
6919 char_u *p = get_sgr(args, &argc, head);
6920
6921 return p && argc == 1 && args[0] == n && (p = skipwhite(p)) && *p == '\033'
6922 ? p : NULL;
6923}
6924
6925 static char_u *
6926skipblank(char_u *q)
6927{
6928 char_u *p = q;
6929
6930 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
6931 ++p;
6932 return p;
6933}
6934
6935/*
6936 * Pointer to the next if any whitespace that may follow SGR is ESC, otherwise
6937 * NULL.
6938 */
6939 static char_u *
6940sgrn2c(
6941 char_u *head,
6942 int n)
6943{
6944 char_u *p = sgrn2(head, n);
6945
6946 return p && *p != NUL && (p = skipblank(p)) && *p == '\033' ? p : NULL;
6947}
6948
6949/*
6950 * If there is only a newline between the sequence immediately following it,
6951 * a pointer to the character following the newline is returned.
6952 * Otherwise NULL.
6953 */
6954 static char_u *
6955sgrn2cn(
6956 char_u *head,
6957 int n)
6958{
6959 char_u *p = sgrn2(head, n);
6960
6961 return p && p[0] == 0x0a && p[1] == '\033' ? ++p : NULL;
6962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964/*
6965 * mch_write(): write the output buffer to the screen, translating ESC
6966 * sequences into calls to console output routines.
6967 */
6968 void
6969mch_write(
6970 char_u *s,
6971 int len)
6972{
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02006973 char_u *end = s + len;
6974
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006975# ifdef VIMDLL
6976 if (gui.in_use)
6977 return;
6978# endif
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (!term_console)
6981 {
6982 write(1, s, (unsigned)len);
6983 return;
6984 }
6985
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 while (len--)
6988 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006989 int prefix = -1;
6990 char_u ch;
6991
6992 // While processing a sequence, on rare occasions it seems that another
6993 // sequence may be inserted asynchronously.
6994 if (len < 0)
6995 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006996 redraw_all_later(UPD_CLEAR);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02006997 return;
6998 }
6999
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02007000 while (s + ++prefix < end)
7001 {
7002 ch = s[prefix];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007003 if (ch <= 0x1e && !(ch != '\n' && ch != '\r' && ch != '\b'
7004 && ch != '\a' && ch != '\033'))
7005 break;
Bram Moolenaard23f8bd2021-04-21 11:30:48 +02007006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008 if (p_wd)
7009 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02007010 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (prefix != 0)
7012 prefix = 1;
7013 }
7014
7015 if (prefix != 0)
7016 {
7017 DWORD nWritten;
7018
7019 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007020# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (fdDump)
7022 {
7023 fputc('>', fdDump);
7024 fwrite(s, sizeof(char_u), nWritten, fdDump);
7025 fputs("<\n", fdDump);
7026 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 len -= (nWritten - 1);
7029 s += nWritten;
7030 }
7031 else if (s[0] == '\n')
7032 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007033 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (g_coord.Y == g_srScrollRegion.Bottom)
7035 {
7036 scroll(1);
7037 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
7038 }
7039 else
7040 {
7041 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
7042 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007043# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (fdDump)
7045 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 s++;
7048 }
7049 else if (s[0] == '\r')
7050 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007053# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (fdDump)
7055 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 s++;
7058 }
7059 else if (s[0] == '\b')
7060 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007061 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 if (g_coord.X > g_srScrollRegion.Left)
7063 g_coord.X--;
7064 else if (g_coord.Y > g_srScrollRegion.Top)
7065 {
7066 g_coord.X = g_srScrollRegion.Right;
7067 g_coord.Y--;
7068 }
7069 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007070# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (fdDump)
7072 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 s++;
7075 }
7076 else if (s[0] == '\a')
7077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007078 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007080# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 if (fdDump)
7082 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 s++;
7085 }
7086 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
7087 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007088# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007089 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007090# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007091 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007092 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007093 char_u *sp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095 switch (s[2])
7096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 case '0': case '1': case '2': case '3': case '4':
7098 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007099 if (*(p = get_seq(args, &argc, s)) != 'm')
7100 goto notsgr;
7101
7102 p = s;
7103
7104 // Handling frequent optional sequences. Output to the screen
7105 // takes too long, so do not output as much as possible.
7106
7107 // If resetFG,FG,BG,<cr>,BG,FG are connected, the preceding
7108 // resetFG,FG,BG are omitted.
7109 if (sgrn2(sgrn2(sgrn2cn(sgrn2(sgrnc(p, 39), 38), 48), 48), 38))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007110 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007111 p = sgrn2(sgrn2(sgrnc(p, 39), 38), 48);
7112 len = len + 1 - (int)(p - s);
7113 s = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 break;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007117 // If FG,BG,BG,FG of SGR are connected, the first FG can be
7118 // omitted.
7119 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 48), 38))
7120 p = sp;
7121
7122 // If FG,BG,FG,BG of SGR are connected, the first FG can be
7123 // omitted.
7124 if (sgrn2(sgrn2(sgrn2c((sp = sgrn2(p, 38)), 48), 38), 48))
7125 p = sp;
7126
7127 // If BG,BG of SGR are connected, the first BG can be omitted.
7128 if (sgrn2((sp = sgrn2(p, 48)), 48))
7129 p = sp;
7130
7131 // If restoreFG and FG are connected, the restoreFG can be
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01007132 // omitted.
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02007133 if (sgrn2((sp = sgrnc(p, 39)), 38))
7134 p = sp;
7135
7136 p = get_seq(args, &argc, p);
7137
7138notsgr:
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007139 arg1 = args[0];
7140 arg2 = args[1];
7141 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007143 if (argc == 1 && args[0] == 0)
7144 normvideo();
7145 else if (argc == 1)
7146 {
Christopher Plewright38804d62022-11-09 23:55:52 +00007147 if (vtp_working
7148# ifdef FEAT_TERMGUICOLORS
7149 && (p_tgc || t_colors >= 256)
7150# endif
7151 )
7152 textcolor((WORD)arg1);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007153 else
Christopher Plewright38804d62022-11-09 23:55:52 +00007154 textattr((WORD)arg1);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007155 }
Christopher Plewright38804d62022-11-09 23:55:52 +00007156 else if (vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007157 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007159 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007161 gotoxy(arg2, arg1);
7162 }
7163 else if (argc == 2 && *p == 'r')
7164 {
7165 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
7166 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01007167 else if (argc == 2 && *p == 'R')
7168 {
7169 set_scroll_region_tb(arg1, arg2);
7170 }
7171 else if (argc == 2 && *p == 'V')
7172 {
7173 set_scroll_region_lr(arg1, arg2);
7174 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007175 else if (argc == 1 && *p == 'A')
7176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 gotoxy(g_coord.X + 1,
7178 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
7179 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007180 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
7182 textbackground((WORD) arg1);
7183 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007184 else if (argc == 1 && *p == 'C')
7185 {
7186 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
7187 g_coord.Y + 1);
7188 }
7189 else if (argc == 1 && *p == 'f')
7190 {
7191 textcolor((WORD) arg1);
7192 }
7193 else if (argc == 1 && *p == 'H')
7194 {
7195 gotoxy(1, arg1);
7196 }
7197 else if (argc == 1 && *p == 'L')
7198 {
7199 insert_lines(arg1);
7200 }
7201 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 delete_lines(arg1);
7204 }
7205
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007206 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 s = p + 1;
7208 break;
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 gotoxy(g_coord.X + 1,
7212 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
7213 goto got3;
7214
7215 case 'B':
7216 visual_bell();
7217 goto got3;
7218
7219 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
7221 g_coord.Y + 1);
7222 goto got3;
7223
7224 case 'E':
7225 termcap_mode_end();
7226 goto got3;
7227
7228 case 'F':
7229 standout();
7230 goto got3;
7231
7232 case 'f':
7233 standend();
7234 goto got3;
7235
7236 case 'H':
7237 gotoxy(1, 1);
7238 goto got3;
7239
7240 case 'j':
7241 clear_to_end_of_display();
7242 goto got3;
7243
7244 case 'J':
7245 clear_screen();
7246 goto got3;
7247
7248 case 'K':
7249 clear_to_end_of_line();
7250 goto got3;
7251
7252 case 'L':
7253 insert_lines(1);
7254 goto got3;
7255
7256 case 'M':
7257 delete_lines(1);
7258 goto got3;
7259
7260 case 'S':
7261 termcap_mode_start();
7262 goto got3;
7263
7264 case 'V':
7265 cursor_visible(TRUE);
7266 goto got3;
7267
7268 case 'v':
7269 cursor_visible(FALSE);
7270 goto got3;
7271
7272 got3:
7273 s += 3;
7274 len -= 2;
7275 }
7276
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007277# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (fdDump)
7279 {
7280 fputs("ESC | ", fdDump);
7281 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
7282 fputc('\n', fdDump);
7283 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
K.Takatadf5320c2022-09-01 13:20:16 +01007286 else if (s[0] == ESC && len >= 3-1 && s[1] == '[')
7287 {
7288 int l = 2;
7289
7290 if (isdigit(s[l]))
7291 l++;
7292 if (s[l] == ' ' && s[l + 1] == 'q')
7293 {
7294 // DECSCUSR (cursor style) sequences
Christopher Plewright38804d62022-11-09 23:55:52 +00007295 if (vtp_working)
K.Takatadf5320c2022-09-01 13:20:16 +01007296 vtp_printf("%.*s", l + 2, s); // Pass through
7297 s += l + 2;
7298 len -= l + 1;
7299 }
7300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 else
7302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007303 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 DWORD nWritten;
7305
7306 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007307# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (fdDump)
7309 {
7310 fputc('>', fdDump);
7311 fwrite(s, sizeof(char_u), nWritten, fdDump);
7312 fputs("<\n", fdDump);
7313 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
7316 len -= (nWritten - 1);
7317 s += nWritten;
7318 }
7319 }
7320
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007321# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 if (fdDump)
7323 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
Bram Moolenaar0f873732019-12-05 20:28:46 +01007327#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329
7330/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01007331 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 */
7333 void
7334mch_delay(
7335 long msec,
Bram Moolenaar0981c872020-08-23 14:28:37 +02007336 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007338#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007339 Sleep((int)msec); // never wait for input
7340#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007341# ifdef VIMDLL
7342 if (gui.in_use)
7343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007344 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007345 return;
7346 }
7347# endif
Bram Moolenaar0981c872020-08-23 14:28:37 +02007348 if (flags & MCH_DELAY_IGNOREINPUT)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007349# ifdef FEAT_MZSCHEME
7350 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
7351 {
7352 int towait = p_mzq;
7353
Bram Moolenaar0f873732019-12-05 20:28:46 +01007354 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007355 while (msec > 0)
7356 {
7357 mzvim_check_threads();
7358 if (msec < towait)
7359 towait = msec;
7360 Sleep(towait);
7361 msec -= towait;
7362 }
7363 }
7364 else
7365# endif
7366 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02007368 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370}
7371
7372
7373/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01007374 * This version of remove is not scared by a readonly (backup) file.
7375 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 * Return 0 for success, -1 for failure.
7377 */
7378 int
7379mch_remove(char_u *name)
7380{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007381 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int n;
7383
Bram Moolenaar203258c2016-01-17 22:15:16 +01007384 /*
7385 * On Windows, deleting a directory's symbolic link is done by
7386 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
7387 */
7388 if (mch_isdir(name) && mch_is_symbolic_link(name))
7389 return mch_rmdir(name);
7390
Bram Moolenaar12b559e2013-06-12 22:41:37 +02007391 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
7392
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007393 wn = enc_to_utf16(name, NULL);
7394 if (wn == NULL)
7395 return -1;
7396
7397 n = DeleteFileW(wn) ? 0 : -1;
7398 vim_free(wn);
7399 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400}
7401
7402
7403/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007404 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 */
7406 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007407mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007409#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
7410# ifdef VIMDLL
7411 if (!gui.in_use)
7412# endif
7413 if (g_fCtrlCPressed || g_fCBrkPressed)
7414 {
7415 ctrl_break_was_pressed = g_fCBrkPressed;
7416 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
7417 got_int = TRUE;
7418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#endif
7420}
7421
Bram Moolenaar0f873732019-12-05 20:28:46 +01007422// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01007423#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01007424
7425/*
7426 * How much main memory in KiB that can be used by VIM.
7427 */
Bram Moolenaaree273972016-01-02 21:11:51 +01007428 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01007429mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01007430{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007431 MEMORYSTATUSEX ms;
7432
Bram Moolenaar0f873732019-12-05 20:28:46 +01007433 // Need to use GlobalMemoryStatusEx() when there is more memory than
7434 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007435 ms.dwLength = sizeof(MEMORYSTATUSEX);
7436 GlobalMemoryStatusEx(&ms);
7437 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01007438 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007439 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007440 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01007441 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007442 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01007443 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007444 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007445 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01007446 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007447 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007448 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01007449}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007452 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
7454 * file whose short file name is "FOO.BAR" (its long file name will
7455 * be correct: "foo.bar~"). Because a file can be accessed by
7456 * either its SFN or its LFN, "foo.bar" has effectively been
7457 * renamed to "foo.bar", which is not at all what was wanted. This
7458 * seems to happen only when renaming files with three-character
7459 * extensions by appending a suffix that does not include ".".
7460 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
7461 *
7462 * There is another problem, which isn't really a bug but isn't right either:
7463 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
7464 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
7465 * service pack 6. Doesn't seem to happen on Windows 98.
7466 *
7467 * Like rename(), returns 0 upon success, non-zero upon failure.
7468 * Should probably set errno appropriately when errors occur.
7469 */
7470 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007471mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007473 WCHAR *p;
7474 int i;
7475 WCHAR szTempFile[_MAX_PATH + 1];
7476 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007479 // No need to play tricks unless the file name contains a "~" as the
7480 // seventh character.
7481 p = wold;
7482 for (i = 0; wold[i] != NUL; ++i)
7483 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
7484 && wold[i + 1] != 0)
7485 p = wold + i + 1;
7486 if ((int)(wold + i - p) < 8 || p[6] != '~')
7487 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007489 // Get base path of new file name. Undocumented feature: If pszNewFile is
7490 // a directory, no error is returned and pszFilePart will be NULL.
7491 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007493 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007495 // Get (and create) a unique temporary file name in directory of new file
7496 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return -2;
7498
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007499 // blow the temp file away
7500 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 return -3;
7502
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007503 // rename old file to the temp file
7504 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 return -4;
7506
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007507 // now create an empty file called pszOldFile; this prevents the operating
7508 // system using pszOldFile as an alias (SFN) if we're renaming within the
7509 // same directory. For example, we're editing a file called
7510 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
7511 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
7512 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
7513 // cause all sorts of problems later in buf_write(). So, we create an
7514 // empty file called filena~1.txt and the system will have to find some
7515 // other SFN for filena~1.txt~, such as filena~2.txt
7516 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
7518 return -5;
7519 if (!CloseHandle(hf))
7520 return -6;
7521
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007522 // rename the temp file to the new file
7523 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007525 // Renaming failed. Rename the file back to its old name, so that it
7526 // looks like nothing happened.
7527 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 return -7;
7529 }
7530
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007531 // Seems to be left around on Novell filesystems
7532 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007534 // finally, remove the empty old file
7535 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 return -8;
7537
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007538 return 0;
7539}
7540
7541
7542/*
7543 * Converts the filenames to UTF-16, then call mch_wrename().
7544 * Like rename(), returns 0 upon success, non-zero upon failure.
7545 */
7546 int
7547mch_rename(
7548 const char *pszOldFile,
7549 const char *pszNewFile)
7550{
7551 WCHAR *wold = NULL;
7552 WCHAR *wnew = NULL;
7553 int retval = -1;
7554
7555 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
7556 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
7557 if (wold != NULL && wnew != NULL)
7558 retval = mch_wrename(wold, wnew);
7559 vim_free(wold);
7560 vim_free(wnew);
7561 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562}
7563
7564/*
7565 * Get the default shell for the current hardware platform
7566 */
7567 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007568default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007570 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572
7573/*
7574 * mch_access() extends access() to do more detailed check on network drives.
7575 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
7576 */
7577 int
7578mch_access(char *n, int p)
7579{
7580 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007581 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007582 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007584 wn = enc_to_utf16((char_u *)n, NULL);
7585 if (wn == NULL)
7586 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01007588 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
7592 if (p & R_OK)
7593 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007594 // Read check is performed by seeing if we can do a find file on
7595 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007596 int i;
7597 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007599 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
7600 TempNameW[i] = wn[i];
7601 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
7602 TempNameW[i++] = '\\';
7603 TempNameW[i++] = '*';
7604 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007606 hFile = FindFirstFileW(TempNameW, &d);
7607 if (hFile == INVALID_HANDLE_VALUE)
7608 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612
7613 if (p & W_OK)
7614 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007615 // Trying to create a temporary file in the directory should catch
7616 // directories on read-only network shares. However, in
7617 // directories whose ACL allows writes but denies deletes will end
7618 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007619 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
7620 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007621 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007622 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 }
7625 else
7626 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007627 // Don't consider a file read-only if another process has opened it.
7628 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
7629
Bram Moolenaar0f873732019-12-05 20:28:46 +01007630 // Trying to open the file for the required access does ACL, read-only
7631 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02007632 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
7633 | ((p & R_OK) ? GENERIC_READ : 0);
7634
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007635 hFile = CreateFileW(wn, access_mode, share_mode,
7636 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 if (hFile == INVALID_HANDLE_VALUE)
7638 goto getout;
7639 CloseHandle(hFile);
7640 }
7641
Bram Moolenaar0f873732019-12-05 20:28:46 +01007642 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return retval;
7646}
7647
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007649 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 */
7651 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007652mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 WCHAR *wn;
7655 int f;
7656
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007657 wn = enc_to_utf16((char_u *)name, NULL);
7658 if (wn == NULL)
7659 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007661 f = _wopen(wn, flags, mode);
7662 vim_free(wn);
7663 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
7665
7666/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007667 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
7669 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02007670mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 WCHAR *wn, *wm;
7673 FILE *f = NULL;
7674
Bram Moolenaara12a1612019-01-24 16:39:02 +01007675#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01007676 // Work around an annoying assertion in the Microsoft debug CRT
7677 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007678 char newMode = mode[strlen(mode) - 1];
7679 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007680
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007681 _get_fmode(&oldMode);
7682 if (newMode == 't')
7683 _set_fmode(_O_TEXT);
7684 else if (newMode == 'b')
7685 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007686#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007687 wn = enc_to_utf16((char_u *)name, NULL);
7688 wm = enc_to_utf16((char_u *)mode, NULL);
7689 if (wn != NULL && wm != NULL)
7690 f = _wfopen(wn, wm);
7691 vim_free(wn);
7692 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00007693
Bram Moolenaara12a1612019-01-24 16:39:02 +01007694#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007695 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007696#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007697 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700/*
7701 * SUB STREAM (aka info stream) handling:
7702 *
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007703 * NTFS can have sub streams for each file. The normal contents of a file is
7704 * stored in the main stream, and extra contents (author information, title and
7705 * so on) can be stored in a sub stream. After Windows 2000, the user can
7706 * access and store this information in sub streams via an explorer's property
7707 * menu item in the right click menu. This information in sub streams was lost
7708 * when copying only the main stream. Therefore we have to copy sub streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 *
7710 * Incomplete explanation:
7711 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
7712 * More useful info and an example:
7713 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
7714 */
7715
7716/*
7717 * Copy info stream data "substream". Read from the file with BackupRead(sh)
7718 * and write to stream "substream" of file "to".
7719 * Errors are ignored.
7720 */
7721 static void
7722copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
7723{
7724 HANDLE hTo;
7725 WCHAR *to_name;
7726
7727 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
7728 wcscpy(to_name, to);
7729 wcscat(to_name, substream);
7730
7731 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
7732 FILE_ATTRIBUTE_NORMAL, NULL);
7733 if (hTo != INVALID_HANDLE_VALUE)
7734 {
7735 long done;
7736 DWORD todo;
7737 DWORD readcnt, written;
7738 char buf[4096];
7739
Bram Moolenaar0f873732019-12-05 20:28:46 +01007740 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 for (done = 0; done < len; done += written)
7742 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007743 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007744 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
7745 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
7747 FALSE, FALSE, context)
7748 || readcnt != todo
7749 || !WriteFile(hTo, buf, todo, &written, NULL)
7750 || written != todo)
7751 break;
7752 }
7753 CloseHandle(hTo);
7754 }
7755
7756 free(to_name);
7757}
7758
7759/*
7760 * Copy info streams from file "from" to file "to".
7761 */
7762 static void
7763copy_infostreams(char_u *from, char_u *to)
7764{
7765 WCHAR *fromw;
7766 WCHAR *tow;
7767 HANDLE sh;
7768 WIN32_STREAM_ID sid;
7769 int headersize;
7770 WCHAR streamname[_MAX_PATH];
7771 DWORD readcount;
7772 void *context = NULL;
7773 DWORD lo, hi;
7774 int len;
7775
Bram Moolenaar0f873732019-12-05 20:28:46 +01007776 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007777 fromw = enc_to_utf16(from, NULL);
7778 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (fromw != NULL && tow != NULL)
7780 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007781 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
7783 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
7784 if (sh != INVALID_HANDLE_VALUE)
7785 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007786 // Use BackupRead() to find the info streams. Repeat until we
7787 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 for (;;)
7789 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007790 // Get the header to find the length of the stream name. If
7791 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007793 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
7795 &readcount, FALSE, FALSE, &context)
7796 || readcount == 0)
7797 break;
7798
Bram Moolenaar0f873732019-12-05 20:28:46 +01007799 // We only deal with streams that have a name. The normal
7800 // file data appears to be without a name, even though docs
7801 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (sid.dwStreamNameSize > 0)
7803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007804 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (!BackupRead(sh, (LPBYTE)streamname,
7806 sid.dwStreamNameSize,
7807 &readcount, FALSE, FALSE, &context))
7808 break;
7809
Bram Moolenaar0f873732019-12-05 20:28:46 +01007810 // Copy an info stream with a name ":anything:$DATA".
7811 // Skip "::$DATA", it has no stream name (examples suggest
7812 // it might be used for the normal file contents).
7813 // Note that BackupRead() counts bytes, but the name is in
7814 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 len = readcount / sizeof(WCHAR);
7816 streamname[len] = 0;
7817 if (len > 7 && wcsicmp(streamname + len - 6,
7818 L":$DATA") == 0)
7819 {
7820 streamname[len - 6] = 0;
7821 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007822 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
7824 }
7825
Bram Moolenaar0f873732019-12-05 20:28:46 +01007826 // Advance to the next stream. We might try seeking too far,
7827 // but BackupSeek() doesn't skip over stream borders, thus
7828 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007829 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 &lo, &hi, &context);
7831 }
7832
Bram Moolenaar0f873732019-12-05 20:28:46 +01007833 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
7835
7836 CloseHandle(sh);
7837 }
7838 }
7839 vim_free(fromw);
7840 vim_free(tow);
7841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
7843/*
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007844 * ntdll.dll definitions
7845 */
7846#define FileEaInformation 7
7847#ifndef STATUS_SUCCESS
7848# define STATUS_SUCCESS ((NTSTATUS) 0x00000000L)
7849#endif
7850
7851typedef struct _FILE_FULL_EA_INFORMATION_ {
7852 ULONG NextEntryOffset;
7853 UCHAR Flags;
7854 UCHAR EaNameLength;
7855 USHORT EaValueLength;
7856 CHAR EaName[1];
7857} FILE_FULL_EA_INFORMATION_, *PFILE_FULL_EA_INFORMATION_;
7858
7859typedef struct _FILE_EA_INFORMATION_ {
7860 ULONG EaSize;
7861} FILE_EA_INFORMATION_, *PFILE_EA_INFORMATION_;
7862
Paul Ollis65745772022-06-05 16:55:54 +01007863#ifndef PROTO
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007864typedef NTSTATUS (NTAPI *PfnNtOpenFile)(
7865 PHANDLE FileHandle,
7866 ACCESS_MASK DesiredAccess,
7867 POBJECT_ATTRIBUTES ObjectAttributes,
7868 PIO_STATUS_BLOCK IoStatusBlock,
7869 ULONG ShareAccess,
7870 ULONG OpenOptions);
7871typedef NTSTATUS (NTAPI *PfnNtClose)(
7872 HANDLE Handle);
7873typedef NTSTATUS (NTAPI *PfnNtSetEaFile)(
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01007874 HANDLE FileHandle,
7875 PIO_STATUS_BLOCK IoStatusBlock,
7876 PVOID Buffer,
7877 ULONG Length);
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007878typedef NTSTATUS (NTAPI *PfnNtQueryEaFile)(
7879 HANDLE FileHandle,
7880 PIO_STATUS_BLOCK IoStatusBlock,
7881 PVOID Buffer,
7882 ULONG Length,
7883 BOOLEAN ReturnSingleEntry,
7884 PVOID EaList,
7885 ULONG EaListLength,
7886 PULONG EaIndex,
7887 BOOLEAN RestartScan);
7888typedef NTSTATUS (NTAPI *PfnNtQueryInformationFile)(
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01007889 HANDLE FileHandle,
7890 PIO_STATUS_BLOCK IoStatusBlock,
7891 PVOID FileInformation,
7892 ULONG Length,
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007893 FILE_INFORMATION_CLASS FileInformationClass);
7894typedef VOID (NTAPI *PfnRtlInitUnicodeString)(
7895 PUNICODE_STRING DestinationString,
7896 PCWSTR SourceString);
7897
7898PfnNtOpenFile pNtOpenFile = NULL;
7899PfnNtClose pNtClose = NULL;
7900PfnNtSetEaFile pNtSetEaFile = NULL;
7901PfnNtQueryEaFile pNtQueryEaFile = NULL;
7902PfnNtQueryInformationFile pNtQueryInformationFile = NULL;
7903PfnRtlInitUnicodeString pRtlInitUnicodeString = NULL;
Paul Ollis65745772022-06-05 16:55:54 +01007904#endif
Bram Moolenaare7bebc42021-02-01 20:50:37 +01007905
7906/*
7907 * Load ntdll.dll functions.
7908 */
7909 static BOOL
7910load_ntdll(void)
7911{
7912 static int loaded = -1;
7913
7914 if (loaded == -1)
7915 {
7916 HMODULE hNtdll = GetModuleHandle("ntdll.dll");
7917 if (hNtdll != NULL)
7918 {
7919 pNtOpenFile = (PfnNtOpenFile) GetProcAddress(hNtdll, "NtOpenFile");
7920 pNtClose = (PfnNtClose) GetProcAddress(hNtdll, "NtClose");
7921 pNtSetEaFile = (PfnNtSetEaFile)
7922 GetProcAddress(hNtdll, "NtSetEaFile");
7923 pNtQueryEaFile = (PfnNtQueryEaFile)
7924 GetProcAddress(hNtdll, "NtQueryEaFile");
7925 pNtQueryInformationFile = (PfnNtQueryInformationFile)
7926 GetProcAddress(hNtdll, "NtQueryInformationFile");
7927 pRtlInitUnicodeString = (PfnRtlInitUnicodeString)
7928 GetProcAddress(hNtdll, "RtlInitUnicodeString");
7929 }
7930 if (pNtOpenFile == NULL
7931 || pNtClose == NULL
7932 || pNtSetEaFile == NULL
7933 || pNtQueryEaFile == NULL
7934 || pNtQueryInformationFile == NULL
7935 || pRtlInitUnicodeString == NULL)
7936 loaded = FALSE;
7937 else
7938 loaded = TRUE;
7939 }
7940 return (BOOL) loaded;
7941}
7942
7943/*
7944 * Copy extended attributes (EA) from file "from" to file "to".
7945 */
7946 static void
7947copy_extattr(char_u *from, char_u *to)
7948{
7949 char_u *fromf = NULL;
7950 char_u *tof = NULL;
7951 WCHAR *fromw = NULL;
7952 WCHAR *tow = NULL;
7953 UNICODE_STRING u;
7954 HANDLE h;
7955 OBJECT_ATTRIBUTES oa;
7956 IO_STATUS_BLOCK iosb;
7957 FILE_EA_INFORMATION_ eainfo = {0};
7958 void *ea = NULL;
7959
7960 if (!load_ntdll())
7961 return;
7962
7963 // Convert the file names to the fully qualified object names.
7964 fromf = alloc(STRLEN(from) + 5);
7965 tof = alloc(STRLEN(to) + 5);
7966 if (fromf == NULL || tof == NULL)
7967 goto theend;
7968 STRCPY(fromf, "\\??\\");
7969 STRCAT(fromf, from);
7970 STRCPY(tof, "\\??\\");
7971 STRCAT(tof, to);
7972
7973 // Convert the names to wide characters.
7974 fromw = enc_to_utf16(fromf, NULL);
7975 tow = enc_to_utf16(tof, NULL);
7976 if (fromw == NULL || tow == NULL)
7977 goto theend;
7978
7979 // Get the EA.
7980 pRtlInitUnicodeString(&u, fromw);
7981 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
7982 if (pNtOpenFile(&h, FILE_READ_EA, &oa, &iosb, 0,
7983 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
7984 goto theend;
7985 pNtQueryInformationFile(h, &iosb, &eainfo, sizeof(eainfo),
7986 FileEaInformation);
7987 if (eainfo.EaSize != 0)
7988 {
7989 ea = alloc(eainfo.EaSize);
7990 if (ea != NULL)
7991 {
7992 if (pNtQueryEaFile(h, &iosb, ea, eainfo.EaSize, FALSE,
7993 NULL, 0, NULL, TRUE) != STATUS_SUCCESS)
7994 {
7995 vim_free(ea);
7996 ea = NULL;
7997 }
7998 }
7999 }
8000 pNtClose(h);
8001
8002 // Set the EA.
8003 if (ea != NULL)
8004 {
8005 pRtlInitUnicodeString(&u, tow);
8006 InitializeObjectAttributes(&oa, &u, 0, NULL, NULL);
8007 if (pNtOpenFile(&h, FILE_WRITE_EA, &oa, &iosb, 0,
8008 FILE_NON_DIRECTORY_FILE) != STATUS_SUCCESS)
8009 goto theend;
8010
8011 pNtSetEaFile(h, &iosb, ea, eainfo.EaSize);
8012 pNtClose(h);
8013 }
8014
8015theend:
8016 vim_free(fromf);
8017 vim_free(tof);
8018 vim_free(fromw);
8019 vim_free(tow);
8020 vim_free(ea);
8021}
8022
8023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 * Copy file attributes from file "from" to file "to".
8025 * For Windows NT and later we copy info streams.
8026 * Always returns zero, errors are ignored.
8027 */
8028 int
8029mch_copy_file_attribute(char_u *from, char_u *to)
8030{
Bram Moolenaar0f873732019-12-05 20:28:46 +01008031 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008032 copy_infostreams(from, to);
Bram Moolenaare7bebc42021-02-01 20:50:37 +01008033 copy_extattr(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 return 0;
8035}
8036
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008037
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008038/*
K.Takatadc73b4b2021-06-08 18:32:36 +02008039 * The command line arguments in UTF-16
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008040 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008041static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008042static LPWSTR *ArglistW = NULL;
8043static int global_argc = 0;
8044static char **global_argv;
8045
Bram Moolenaar0f873732019-12-05 20:28:46 +01008046static int used_file_argc = 0; // last argument in global_argv[] used
8047 // for the argument list.
8048static int *used_file_indexes = NULL; // indexes in global_argv[] for
8049 // command line arguments added to
8050 // the argument list
8051static int used_file_count = 0; // nr of entries in used_file_indexes
8052static int used_file_literal = FALSE; // take file names literally
8053static int used_file_full_path = FALSE; // file name was full path
8054static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008055static int used_alist_count = 0;
8056
8057
8058/*
8059 * Get the command line arguments. Unicode version.
8060 * Returns argc. Zero when something fails.
8061 */
8062 int
8063get_cmd_argsW(char ***argvp)
8064{
8065 char **argv = NULL;
8066 int argc = 0;
8067 int i;
8068
Bram Moolenaar14993322014-09-09 12:25:33 +02008069 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008070 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
8071 if (ArglistW != NULL)
8072 {
8073 argv = malloc((nArgsW + 1) * sizeof(char *));
8074 if (argv != NULL)
8075 {
8076 argc = nArgsW;
8077 argv[argc] = NULL;
8078 for (i = 0; i < argc; ++i)
8079 {
8080 int len;
8081
K.Takatadc73b4b2021-06-08 18:32:36 +02008082 // Convert each Unicode argument to UTF-8.
8083 WideCharToMultiByte_alloc(CP_UTF8, 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008084 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008085 (LPSTR *)&argv[i], &len, 0, 0);
8086 if (argv[i] == NULL)
8087 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008088 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008089 while (i > 0)
8090 free(argv[--i]);
8091 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01008092 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008093 argc = 0;
8094 }
8095 }
8096 }
8097 }
8098
8099 global_argc = argc;
8100 global_argv = argv;
8101 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02008102 {
8103 if (used_file_indexes != NULL)
8104 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008105 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02008106 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008107
8108 if (argvp != NULL)
8109 *argvp = argv;
8110 return argc;
8111}
8112
8113 void
8114free_cmd_argsW(void)
8115{
8116 if (ArglistW != NULL)
8117 {
8118 GlobalFree(ArglistW);
8119 ArglistW = NULL;
8120 }
8121}
8122
8123/*
8124 * Remember "name" is an argument that was added to the argument list.
8125 * This avoids that we have to re-parse the argument list when fix_arg_enc()
8126 * is called.
8127 */
8128 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008129used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008130{
8131 int i;
8132
8133 if (used_file_indexes == NULL)
8134 return;
8135 for (i = used_file_argc + 1; i < global_argc; ++i)
8136 if (STRCMP(global_argv[i], name) == 0)
8137 {
8138 used_file_argc = i;
8139 used_file_indexes[used_file_count++] = i;
8140 break;
8141 }
8142 used_file_literal = literal;
8143 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008144 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008145}
8146
8147/*
8148 * Remember the length of the argument list as it was. If it changes then we
8149 * leave it alone when 'encoding' is set.
8150 */
8151 void
8152set_alist_count(void)
8153{
8154 used_alist_count = GARGCOUNT;
8155}
8156
8157/*
8158 * Fix the encoding of the command line arguments. Invoked when 'encoding'
K.Takatadc73b4b2021-06-08 18:32:36 +02008159 * has been changed while starting up. Use the UTF-16 command line arguments
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008160 * and convert them to 'encoding'.
8161 */
8162 void
8163fix_arg_enc(void)
8164{
8165 int i;
8166 int idx;
8167 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008168 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008169
Bram Moolenaar0f873732019-12-05 20:28:46 +01008170 // Safety checks:
8171 // - if argument count differs between the wide and non-wide argument
8172 // list, something must be wrong.
8173 // - the file name arguments must have been located.
8174 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008175 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008176 || ArglistW == NULL
8177 || used_file_indexes == NULL
8178 || used_file_count == 0
8179 || used_alist_count != GARGCOUNT)
8180 return;
8181
Bram Moolenaar0f873732019-12-05 20:28:46 +01008182 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008183 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00008184 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008185 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00008186 for (i = 0; i < GARGCOUNT; ++i)
8187 fnum_list[i] = GARGLIST[i].ae_fnum;
8188
Bram Moolenaar0f873732019-12-05 20:28:46 +01008189 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008190 alist_clear(&global_alist);
8191 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01008192 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008193
8194 for (i = 0; i < used_file_count; ++i)
8195 {
8196 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00008197 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008198 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008199 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02008200 int literal = used_file_literal;
8201
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008202#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01008203 // When using diff mode may need to concatenate file name to
8204 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008205 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
8206 && !mch_isdir(alist_name(&GARGLIST[0])))
8207 {
8208 char_u *r;
8209
8210 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
8211 if (r != NULL)
8212 {
8213 vim_free(str);
8214 str = r;
8215 }
8216 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008217#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01008218 // Re-use the old buffer by renaming it. When not using literal
8219 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008220 if (used_file_literal)
8221 buf_set_name(fnum_list[i], str);
8222
Bram Moolenaar0f873732019-12-05 20:28:46 +01008223 // Check backtick literal. backtick literal is already expanded in
8224 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02008225 if (literal == FALSE)
8226 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02008227 size_t len = STRLEN(str);
8228
Bram Moolenaar39d21e32017-08-05 23:09:31 +02008229 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
8230 literal = TRUE;
8231 }
8232 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00008233 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008234 }
8235
8236 if (!used_file_literal)
8237 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01008238 // Now expand wildcards in the arguments.
8239 // Temporarily add '(' and ')' to 'isfname'. These are valid
8240 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00008241 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaar0f873732019-12-05 20:28:46 +01008242 // Also, unset wildignore to not be influenced by this option.
8243 // The arguments specified in command-line should be kept even if
8244 // encoding options were changed.
Christian Brabandtfd916d62021-11-01 22:44:33 +00008245 // Use :legacy so that it also works when in Vim9 script.
8246 do_cmdline_cmd((char_u *)":legacy let g:SaVe_ISF = &isf|set isf+=(,)");
8247 do_cmdline_cmd((char_u *)":legacy let g:SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00008248 alist_expand(fnum_list, used_alist_count);
Christian Brabandtfd916d62021-11-01 22:44:33 +00008249 do_cmdline_cmd(
8250 (char_u *)":legacy let &isf = g:SaVe_ISF|unlet g:SaVe_ISF");
8251 do_cmdline_cmd(
8252 (char_u *)":legacy let &wig = g:SaVe_WIG|unlet g:SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008253 }
8254
Bram Moolenaar0f873732019-12-05 20:28:46 +01008255 // If wildcard expansion failed, we are editing the first file of the
8256 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008257 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
8258 {
8259 do_cmdline_cmd((char_u *)":rewind");
Bram Moolenaar05268152021-11-18 18:53:45 +00008260 if (GARGCOUNT == 1 && used_file_full_path
8261 && vim_chdirfile(alist_name(&GARGLIST[0]), "drop") == OK)
8262 last_chdir_reason = "drop";
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008263 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008264
8265 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008266}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008267
8268 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008269mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008270{
8271 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008272 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008273
Bram Moolenaar964b3742019-05-24 18:54:09 +02008274 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008275 if (envbuf == NULL)
8276 return -1;
8277
8278 sprintf((char *)envbuf, "%s=%s", var, value);
8279
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008280 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008281
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008282 vim_free(envbuf);
8283 if (p == NULL)
8284 return -1;
8285 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01008286#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008287 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01008288#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008289 // Unlike Un*x systems, we can free the string for _wputenv().
8290 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01008291
8292 return 0;
8293}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008294
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008295/*
8296 * Support for 256 colors and 24-bit colors was added in Windows 10
8297 * version 1703 (Creators update).
8298 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008299#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
8300
8301/*
8302 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02008303 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008304 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008305#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02008306
8307/*
8308 * ConPTY differences between versions, need different logic.
8309 * version 1903 (May 2019 update).
8310 */
8311#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
8312
8313/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01008314 * version 1909 (November 2019 update).
8315 */
8316#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
8317
8318/*
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008319 * Stay ahead of the next update, and when it's done, fix this.
8320 * version ? (2020 update, temporarily use the build number of insider preview)
8321 */
8322#define CONPTY_NEXT_UPDATE_BUILD MAKE_VER(10, 0, 19587)
8323
8324/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02008325 * Confirm until this version. Also the logic changes.
8326 * insider preview.
8327 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02008328#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02008329
8330/*
8331 * Not stable now.
8332 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008333#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Christopher Plewright1140b512022-11-12 18:46:05 +00008334// Notes:
8335// Win 10 22H2 Final is build 19045, it's conpty is widely used.
8336// Strangely, 19045 is newer but is a lower build number than the 2020 insider
8337// preview which had a build 19587. And, not sure how stable that was?
8338// Win Server 2022 (May 10, 2022) is build 20348, its conpty is widely used.
8339// Win 11 starts from build 22000, even though the major version says 10!
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008340
8341 static void
8342vtp_flag_init(void)
8343{
8344 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008345#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008346 DWORD mode;
8347 HANDLE out;
8348
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008349# ifdef VIMDLL
8350 if (!gui.in_use)
8351# endif
8352 {
8353 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008354
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008355 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
8356 GetConsoleMode(out, &mode);
8357 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
8358 if (SetConsoleMode(out, mode) == 0)
8359 vtp_working = 0;
8360 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008361#endif
8362
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008363 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008364 conpty_working = 1;
8365 if (ver >= CONPTY_STABLE_BUILD)
8366 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008367
Bram Moolenaar57da6982019-09-13 22:30:11 +02008368 if (ver <= CONPTY_INSIDER_BUILD)
8369 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01008370 if (ver <= CONPTY_1909_BUILD)
8371 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02008372 if (ver <= CONPTY_1903_BUILD)
8373 conpty_type = 2;
8374 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
8375 conpty_type = 1;
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008376
8377 if (ver >= CONPTY_NEXT_UPDATE_BUILD)
8378 conpty_fix_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008379}
8380
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008381#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008382
8383 static void
8384vtp_init(void)
8385{
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008386# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00008387 if (!vtp_working)
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +01008388 {
Christopher Plewright38804d62022-11-09 23:55:52 +00008389 CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8390 csbi.cbSize = sizeof(csbi);
8391 GetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8392 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
8393 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
8394 store_console_bg_rgb = save_console_bg_rgb;
8395 store_console_fg_rgb = save_console_fg_rgb;
8396
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +01008397 COLORREF bg;
8398 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
8399 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
8400 default_console_color_bg = bg;
Christopher Plewright38804d62022-11-09 23:55:52 +00008401
8402 COLORREF fg;
8403 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
8404 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
8405 default_console_color_fg = fg;
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +01008406 }
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01008407# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008408
8409 set_console_color_rgb();
8410}
8411
8412 static void
8413vtp_exit(void)
8414{
Bram Moolenaardf543822020-01-30 11:53:59 +01008415 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008416}
8417
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008418 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008419vtp_printf(
8420 char *format,
8421 ...)
8422{
8423 char_u buf[100];
8424 va_list list;
8425 DWORD result;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008426 int len;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008427
8428 va_start(list, format);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008429 len = vim_vsnprintf((char *)buf, 100, (char *)format, list);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008430 va_end(list);
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008431 WriteConsoleA(g_hConOut, buf, (DWORD)len, &result, NULL);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008432 return (int)result;
8433}
8434
8435 static void
8436vtp_sgr_bulk(
8437 int arg)
8438{
8439 int args[1];
8440
8441 args[0] = arg;
8442 vtp_sgr_bulks(1, args);
8443}
8444
K.Takata6e1d31e2022-02-03 13:05:32 +00008445# define FAST256(x) \
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008446 if ((*p-- = "0123456789"[(n = x % 10)]) \
8447 && x >= 10 && (*p-- = "0123456789"[((m = x % 100) - n) / 10]) \
8448 && x >= 100 && (*p-- = "012"[((x & 0xff) - m) / 100]));
8449
K.Takata6e1d31e2022-02-03 13:05:32 +00008450# define FAST256CASE(x) \
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008451 case x: \
8452 FAST256(newargs[x - 1]);
8453
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008454 static void
8455vtp_sgr_bulks(
8456 int argc,
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008457 int *args)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008458{
K.Takata6e1d31e2022-02-03 13:05:32 +00008459# define MAXSGR 16
8460# define SGRBUFSIZE 2 + 4 * MAXSGR + 1 // '\033[' + SGR + 'm'
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008461 char_u buf[SGRBUFSIZE];
8462 char_u *p;
8463 int in, out;
8464 int newargs[16];
8465 static int sgrfgr = -1, sgrfgg, sgrfgb;
8466 static int sgrbgr = -1, sgrbgg, sgrbgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008467
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008468 if (argc == 0)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008469 {
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008470 sgrfgr = sgrbgr = -1;
K.Takatadf5320c2022-09-01 13:20:16 +01008471 vtp_printf("\033[m");
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008472 return;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008473 }
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02008474
8475 in = out = 0;
8476 while (in < argc)
8477 {
8478 int s = args[in];
8479 int copylen = 1;
8480
8481 if (s == 38)
8482 {
8483 if (argc - in >= 5 && args[in + 1] == 2)
8484 {
8485 if (sgrfgr == args[in + 2] && sgrfgg == args[in + 3]
8486 && sgrfgb == args[in + 4])
8487 {
8488 in += 5;
8489 copylen = 0;
8490 }
8491 else
8492 {
8493 sgrfgr = args[in + 2];
8494 sgrfgg = args[in + 3];
8495 sgrfgb = args[in + 4];
8496 copylen = 5;
8497 }
8498 }
8499 else if (argc - in >= 3 && args[in + 1] == 5)
8500 {
8501 sgrfgr = -1;
8502 copylen = 3;
8503 }
8504 }
8505 else if (s == 48)
8506 {
8507 if (argc - in >= 5 && args[in + 1] == 2)
8508 {
8509 if (sgrbgr == args[in + 2] && sgrbgg == args[in + 3]
8510 && sgrbgb == args[in + 4])
8511 {
8512 in += 5;
8513 copylen = 0;
8514 }
8515 else
8516 {
8517 sgrbgr = args[in + 2];
8518 sgrbgg = args[in + 3];
8519 sgrbgb = args[in + 4];
8520 copylen = 5;
8521 }
8522 }
8523 else if (argc - in >= 3 && args[in + 1] == 5)
8524 {
8525 sgrbgr = -1;
8526 copylen = 3;
8527 }
8528 }
8529 else if (30 <= s && s <= 39)
8530 sgrfgr = -1;
8531 else if (90 <= s && s <= 97)
8532 sgrfgr = -1;
8533 else if (40 <= s && s <= 49)
8534 sgrbgr = -1;
8535 else if (100 <= s && s <= 107)
8536 sgrbgr = -1;
8537 else if (s == 0)
8538 sgrfgr = sgrbgr = -1;
8539
8540 while (copylen--)
8541 newargs[out++] = args[in++];
8542 }
8543
8544 p = &buf[sizeof(buf) - 1];
8545 *p-- = 'm';
8546
8547 switch (out)
8548 {
8549 int n, m;
8550 DWORD r;
8551
8552 FAST256CASE(16);
8553 *p-- = ';';
8554 FAST256CASE(15);
8555 *p-- = ';';
8556 FAST256CASE(14);
8557 *p-- = ';';
8558 FAST256CASE(13);
8559 *p-- = ';';
8560 FAST256CASE(12);
8561 *p-- = ';';
8562 FAST256CASE(11);
8563 *p-- = ';';
8564 FAST256CASE(10);
8565 *p-- = ';';
8566 FAST256CASE(9);
8567 *p-- = ';';
8568 FAST256CASE(8);
8569 *p-- = ';';
8570 FAST256CASE(7);
8571 *p-- = ';';
8572 FAST256CASE(6);
8573 *p-- = ';';
8574 FAST256CASE(5);
8575 *p-- = ';';
8576 FAST256CASE(4);
8577 *p-- = ';';
8578 FAST256CASE(3);
8579 *p-- = ';';
8580 FAST256CASE(2);
8581 *p-- = ';';
8582 FAST256CASE(1);
8583 *p-- = '[';
8584 *p = '\033';
8585 WriteConsoleA(g_hConOut, p, (DWORD)(&buf[SGRBUFSIZE] - p), &r, NULL);
8586 default:
8587 break;
8588 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008589}
8590
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008591 static void
8592wt_init(void)
8593{
Christopher Plewright1140b512022-11-12 18:46:05 +00008594 wt_working = mch_getenv("WT_SESSION") != NULL;
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008595}
8596
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008597# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008598 static int
8599ctermtoxterm(
8600 int cterm)
8601{
Bram Moolenaar9894e392018-05-05 14:29:06 +02008602 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008603
8604 cterm_color2rgb(cterm, &r, &g, &b, &idx);
8605 return (((int)r << 16) | ((int)g << 8) | (int)b);
8606}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02008607# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008608
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008609 static void
8610set_console_color_rgb(void)
8611{
8612# ifdef FEAT_TERMGUICOLORS
K.Takata27b53be2022-09-18 12:25:49 +01008613 CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01008614 guicolor_T fg, bg;
8615 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008616
Christopher Plewright38804d62022-11-09 23:55:52 +00008617 if (!vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008618 return;
8619
Bram Moolenaara050b942019-12-02 21:35:31 +01008620 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
8621
Christopher Plewright38804d62022-11-09 23:55:52 +00008622 if (p_tgc || t_colors >= 256)
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008623 {
8624 term_fg_rgb_color(fg);
8625 term_bg_rgb_color(bg);
8626 return;
8627 }
8628
Christopher Plewright38804d62022-11-09 23:55:52 +00008629 if (!conpty_working)
8630 {
8631 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
8632 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008633
Christopher Plewright38804d62022-11-09 23:55:52 +00008634 csbi.cbSize = sizeof(csbi);
8635 GetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008636
Christopher Plewright38804d62022-11-09 23:55:52 +00008637 csbi.cbSize = sizeof(csbi);
8638 csbi.srWindow.Right += 1;
8639 csbi.srWindow.Bottom += 1;
8640 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
8641 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
8642 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
8643 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
8644 SetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
8645 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008646# endif
8647}
8648
Bram Moolenaara050b942019-12-02 21:35:31 +01008649# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
8650 void
8651get_default_console_color(
8652 int *cterm_fg,
8653 int *cterm_bg,
8654 guicolor_T *gui_fg,
8655 guicolor_T *gui_bg)
8656{
8657 int id;
8658 guicolor_T guifg = INVALCOLOR;
8659 guicolor_T guibg = INVALCOLOR;
8660 int ctermfg = 0;
8661 int ctermbg = 0;
Christopher Plewright38804d62022-11-09 23:55:52 +00008662 int dummynull = 0;
Bram Moolenaara050b942019-12-02 21:35:31 +01008663
8664 id = syn_name2id((char_u *)"Normal");
8665 if (id > 0 && p_tgc)
8666 syn_id2colors(id, &guifg, &guibg);
8667 if (guifg == INVALCOLOR)
8668 {
8669 ctermfg = -1;
8670 if (id > 0)
Christopher Plewright38804d62022-11-09 23:55:52 +00008671 syn_id2cterm_bg(id, &ctermfg, &dummynull);
8672 if (vtp_working)
8673 {
8674 cterm_normal_fg_gui_color = guifg =
8675 ctermfg != -1 ? ctermtoxterm(ctermfg) : INVALCOLOR;
8676 ctermfg = ctermfg < 0 ? 0 : ctermfg;
8677 }
8678 else
8679 {
8680 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
Bram Moolenaara050b942019-12-02 21:35:31 +01008681 : default_console_color_fg;
Christopher Plewright38804d62022-11-09 23:55:52 +00008682 cterm_normal_fg_gui_color = guifg;
8683 ctermfg = ctermfg < 0 ? 0 : ctermfg;
8684 }
Bram Moolenaara050b942019-12-02 21:35:31 +01008685 }
8686 if (guibg == INVALCOLOR)
8687 {
8688 ctermbg = -1;
8689 if (id > 0)
Christopher Plewright38804d62022-11-09 23:55:52 +00008690 syn_id2cterm_bg(id, &dummynull, &ctermbg);
8691 if (vtp_working)
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +01008692 {
8693 cterm_normal_bg_gui_color = guibg =
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01008694 ctermbg != -1 ? ctermtoxterm(ctermbg) : INVALCOLOR;
Yasuhiro Matsumotoe42c8da2022-09-01 11:31:45 +01008695 if (ctermbg < 0)
8696 ctermbg = 0;
8697 }
K.Takatae53a0d42022-09-05 21:45:11 +01008698 else
8699 {
8700 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
8701 : default_console_color_bg;
8702 cterm_normal_bg_gui_color = guibg;
8703 ctermbg = ctermbg < 0 ? 0 : ctermbg;
8704 }
Bram Moolenaara050b942019-12-02 21:35:31 +01008705 }
8706
8707 *cterm_fg = ctermfg;
8708 *cterm_bg = ctermbg;
8709 *gui_fg = guifg;
8710 *gui_bg = guibg;
8711}
8712# endif
8713
Bram Moolenaardf543822020-01-30 11:53:59 +01008714/*
8715 * Set the console colors to the original colors or the last set colors.
8716 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008717 static void
8718reset_console_color_rgb(void)
8719{
8720# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008721
Christopher Plewright38804d62022-11-09 23:55:52 +00008722 if (vtp_working)
Bram Moolenaar06b7b582020-05-30 17:49:25 +02008723 return;
8724
Christopher Plewright38804d62022-11-09 23:55:52 +00008725 CONSOLE_SCREEN_BUFFER_INFOEX csbi;
8726
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008727 csbi.cbSize = sizeof(csbi);
K.Takata27b53be2022-09-18 12:25:49 +01008728 GetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008729
8730 csbi.cbSize = sizeof(csbi);
8731 csbi.srWindow.Right += 1;
8732 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01008733 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
8734 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
K.Takata27b53be2022-09-18 12:25:49 +01008735 SetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaardf543822020-01-30 11:53:59 +01008736# endif
8737}
8738
8739/*
8740 * Set the console colors to the original colors.
8741 */
8742 static void
8743restore_console_color_rgb(void)
8744{
8745# ifdef FEAT_TERMGUICOLORS
Christopher Plewright38804d62022-11-09 23:55:52 +00008746 if (vtp_working)
8747 return;
8748
K.Takata27b53be2022-09-18 12:25:49 +01008749 CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaardf543822020-01-30 11:53:59 +01008750
8751 csbi.cbSize = sizeof(csbi);
K.Takata27b53be2022-09-18 12:25:49 +01008752 GetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaardf543822020-01-30 11:53:59 +01008753
8754 csbi.cbSize = sizeof(csbi);
8755 csbi.srWindow.Right += 1;
8756 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02008757 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
8758 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
K.Takata27b53be2022-09-18 12:25:49 +01008759 SetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008760# endif
8761}
8762
8763 void
8764control_console_color_rgb(void)
8765{
Christopher Plewright38804d62022-11-09 23:55:52 +00008766 if (vtp_working)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008767 set_console_color_rgb();
8768 else
8769 reset_console_color_rgb();
8770}
8771
8772 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01008773use_vtp(void)
8774{
8775 return USE_VTP;
8776}
8777
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02008778 int
8779is_term_win32(void)
8780{
8781 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
8782}
8783
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01008784 int
8785has_vtp_working(void)
8786{
8787 return vtp_working;
8788}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008789
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01008790#endif
8791
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008792 int
8793has_conpty_working(void)
8794{
8795 return conpty_working;
8796}
8797
8798 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02008799get_conpty_type(void)
8800{
8801 return conpty_type;
8802}
8803
8804 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01008805is_conpty_stable(void)
8806{
8807 return conpty_stable;
8808}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008809
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02008810 int
8811get_conpty_fix_type(void)
8812{
8813 return conpty_fix_type;
8814}
8815
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008816#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008817 void
8818resize_console_buf(void)
8819{
8820 CONSOLE_SCREEN_BUFFER_INFO csbi;
8821 COORD coord;
8822 SMALL_RECT newsize;
8823
8824 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
8825 {
8826 coord.X = SRWIDTH(csbi.srWindow);
8827 coord.Y = SRHEIGHT(csbi.srWindow);
8828 SetConsoleScreenBufferSize(g_hConOut, coord);
8829
8830 newsize.Left = 0;
8831 newsize.Top = 0;
8832 newsize.Right = coord.X - 1;
8833 newsize.Bottom = coord.Y - 1;
8834 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
8835
8836 SetConsoleScreenBufferSize(g_hConOut, coord);
8837 }
8838}
8839#endif
Martin Tournoij1a3e5742021-07-24 13:57:29 +02008840
8841 char *
8842GetWin32Error(void)
8843{
K.Takatad68b2fc2022-02-12 11:18:37 +00008844 static char *oldmsg = NULL;
Martin Tournoij1a3e5742021-07-24 13:57:29 +02008845 char *msg = NULL;
K.Takatad68b2fc2022-02-12 11:18:37 +00008846
Martin Tournoij1a3e5742021-07-24 13:57:29 +02008847 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
8848 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
K.Takatad68b2fc2022-02-12 11:18:37 +00008849 if (oldmsg != NULL)
8850 LocalFree(oldmsg);
Martin Tournoij1a3e5742021-07-24 13:57:29 +02008851 if (msg != NULL)
8852 {
8853 // remove trailing \r\n
8854 char *pcrlf = strstr(msg, "\r\n");
8855 if (pcrlf != NULL)
8856 *pcrlf = '\0';
K.Takatad68b2fc2022-02-12 11:18:37 +00008857 oldmsg = msg;
Martin Tournoij1a3e5742021-07-24 13:57:29 +02008858 }
8859 return msg;
8860}
Paul Ollis65745772022-06-05 16:55:54 +01008861
8862#if defined(FEAT_RELTIME) || defined(PROTO)
8863static HANDLE timer_handle;
8864static int timer_active = FALSE;
8865
8866/*
8867 * Calls to start_timeout alternate the return value pointer between the two
8868 * entries in timeout_flags. If the previously active timeout is very close to
8869 * expiring when start_timeout() is called then a race condition means that the
8870 * set_flag() function may still be invoked after the previous timer is
8871 * deleted. Ping-ponging between the two flags prevents this causing 'fake'
8872 * timeouts.
8873 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008874static sig_atomic_t timeout_flags[2];
8875static int timeout_flag_idx = 0;
8876static sig_atomic_t *timeout_flag = &timeout_flags[0];
Paul Ollis65745772022-06-05 16:55:54 +01008877
8878
8879 static void CALLBACK
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008880set_flag(void *param, BOOLEAN unused2 UNUSED)
Paul Ollis65745772022-06-05 16:55:54 +01008881{
8882 int *timeout_flag = (int *)param;
8883
8884 *timeout_flag = TRUE;
8885}
8886
8887/*
8888 * Stop any active timeout.
8889 */
8890 void
8891stop_timeout(void)
8892{
8893 if (timer_active)
8894 {
Christopher Plewright2a46f812022-10-16 19:47:45 +01008895 BOOL ret = DeleteTimerQueueTimer(NULL, timer_handle, NULL);
Paul Ollis65745772022-06-05 16:55:54 +01008896 timer_active = FALSE;
8897 if (!ret && GetLastError() != ERROR_IO_PENDING)
8898 {
8899 semsg(_(e_could_not_clear_timeout_str), GetWin32Error());
8900 }
8901 }
8902 *timeout_flag = FALSE;
8903}
8904
8905/*
8906 * Start the timeout timer.
8907 *
8908 * The period is defined in milliseconds.
8909 *
8910 * The return value is a pointer to a flag that is initialised to 0. If the
8911 * timeout expires, the flag is set to 1. This will only return pointers to
8912 * static memory; i.e. any pointer returned by this function may always be
8913 * safely dereferenced.
8914 *
8915 * This function is not expected to fail, but if it does it still returns a
8916 * valid flag pointer; the flag will remain stuck at zero.
8917 */
Bram Moolenaar155f2d12022-06-20 13:38:33 +01008918 volatile sig_atomic_t *
Paul Ollis65745772022-06-05 16:55:54 +01008919start_timeout(long msec)
8920{
Paul Ollis65745772022-06-05 16:55:54 +01008921 BOOL ret;
8922
Bram Moolenaar1f30caf2022-06-19 14:36:35 +01008923 timeout_flag = &timeout_flags[timeout_flag_idx];
Paul Ollis65745772022-06-05 16:55:54 +01008924
8925 stop_timeout();
8926 ret = CreateTimerQueueTimer(
8927 &timer_handle, NULL, set_flag, timeout_flag,
8928 (DWORD)msec, 0, WT_EXECUTEDEFAULT);
8929 if (!ret)
8930 {
8931 semsg(_(e_could_not_set_timeout_str), GetWin32Error());
8932 }
8933 else
8934 {
Bram Moolenaar1f30caf2022-06-19 14:36:35 +01008935 timeout_flag_idx = (timeout_flag_idx + 1) % 2;
Paul Ollis65745772022-06-05 16:55:54 +01008936 timer_active = TRUE;
8937 *timeout_flag = FALSE;
8938 }
8939 return timeout_flag;
8940}
8941#endif