blob: 6bbd492055039f5f6abf4dd157441a34079a81d6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * os_win32.c
11 *
12 * Used for both the console version and the Win32 GUI. A lot of code is for
Bram Moolenaar4f974752019-02-17 17:44:42 +010013 * the console version only, so there is a lot of "#ifndef FEAT_GUI_MSWIN".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * Win32 (Windows NT and Windows 95) system-dependent routines.
16 * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code,
17 * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5.
18 *
19 * George V. Reilly <george@reilly.org> wrote most of this.
20 * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0.
21 */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
Bram Moolenaar325b7a22004-07-05 15:58:32 +000025#ifdef FEAT_MZSCHEME
26# include "if_mzsch.h"
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#include <signal.h>
31#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010032
Bram Moolenaar0f873732019-12-05 20:28:46 +010033// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# include <process.h>
36#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38#undef chdir
39#ifdef __GNUC__
40# ifndef __MINGW32__
41# include <dirent.h>
42# endif
43#else
44# include <direct.h>
45#endif
46
Bram Moolenaar82881492012-11-20 16:53:39 +010047#ifndef PROTO
Bram Moolenaar4f974752019-02-17 17:44:42 +010048# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010049# include <shellapi.h>
50# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
52
Bram Moolenaarfb630902016-10-29 14:55:00 +020053#ifdef FEAT_JOB_CHANNEL
54# include <tlhelp32.h>
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef __MINGW32__
58# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
59# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
60# endif
61# ifndef RIGHTMOST_BUTTON_PRESSED
62# define RIGHTMOST_BUTTON_PRESSED 0x0002
63# endif
64# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
65# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
66# endif
67# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
68# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
69# endif
70# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
71# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
72# endif
73
74/*
75 * EventFlags
76 */
77# ifndef MOUSE_MOVED
78# define MOUSE_MOVED 0x0001
79# endif
80# ifndef DOUBLE_CLICK
81# define DOUBLE_CLICK 0x0002
82# endif
83#endif
84
Bram Moolenaar0f873732019-12-05 20:28:46 +010085// Record all output and all keyboard & mouse input
86// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88#ifdef MCH_WRITE_DUMP
89FILE* fdDump = NULL;
90#endif
91
92/*
93 * When generating prototypes for Win32 on Unix, these lines make the syntax
94 * errors disappear. They do not need to be correct.
95 */
96#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010097# define WINAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef char * LPCSTR;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000099typedef char * LPWSTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100typedef int ACCESS_MASK;
101typedef int BOOL;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int HANDLE;
Bram Moolenaaref269542016-01-19 13:22:12 +0100107typedef int LPHANDLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HINSTANCE;
112typedef int HWND;
113typedef int INPUT_RECORD;
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200114typedef int INT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115typedef int KEY_EVENT_RECORD;
116typedef int LOGFONT;
117typedef int LPBOOL;
118typedef int LPCTSTR;
119typedef int LPDWORD;
120typedef int LPSTR;
121typedef int LPTSTR;
122typedef int LPVOID;
123typedef int MOUSE_EVENT_RECORD;
124typedef int PACL;
125typedef int PDWORD;
126typedef int PHANDLE;
127typedef int PRINTDLG;
128typedef int PSECURITY_DESCRIPTOR;
129typedef int PSID;
130typedef int SECURITY_INFORMATION;
131typedef int SHORT;
132typedef int SMALL_RECT;
133typedef int TEXTMETRIC;
134typedef int TOKEN_INFORMATION_CLASS;
135typedef int TRUSTEE;
136typedef int WORD;
137typedef int WCHAR;
138typedef void VOID;
Bram Moolenaar82881492012-11-20 16:53:39 +0100139typedef int BY_HANDLE_FILE_INFORMATION;
Bram Moolenaar32ac8cd2013-07-03 18:49:17 +0200140typedef int SE_OBJECT_TYPE;
141typedef int PSNSECINFO;
142typedef int PSNSECINFOW;
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +0100143typedef int STARTUPINFO;
144typedef int PROCESS_INFORMATION;
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200145typedef int LPSECURITY_ATTRIBUTES;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146# define __stdcall // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
148
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200149#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100150// Win32 Console handles for input and output
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static HANDLE g_hConIn = INVALID_HANDLE_VALUE;
152static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
153
Bram Moolenaar0f873732019-12-05 20:28:46 +0100154// Win32 Screen buffer,coordinate,console I/O information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static SMALL_RECT g_srScrollRegion;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100156static COORD g_coord; // 0-based, but external coords are 1-based
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar0f873732019-12-05 20:28:46 +0100158// The attribute of the screen when the editor was started
159static WORD g_attrDefault = 7; // lightgray text on black background
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static WORD g_attrCurrent;
161
Bram Moolenaar0f873732019-12-05 20:28:46 +0100162static int g_fCBrkPressed = FALSE; // set by ctrl-break interrupt
163static int g_fCtrlCPressed = FALSE; // set when ctrl-C or ctrl-break detected
164static int g_fForceExit = FALSE; // set when forcefully exiting
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static void scroll(unsigned cLines);
167static void set_scroll_region(unsigned left, unsigned top,
168 unsigned right, unsigned bottom);
Bram Moolenaar6982f422019-02-16 16:48:01 +0100169static void set_scroll_region_tb(unsigned top, unsigned bottom);
170static void set_scroll_region_lr(unsigned left, unsigned right);
171static void insert_lines(unsigned cLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void delete_lines(unsigned cLines);
173static void gotoxy(unsigned x, unsigned y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void standout(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int s_cursor_visible = TRUE;
176static int did_create_conin = FALSE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200177#endif
178#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static int s_dont_use_vimrun = TRUE;
180static int need_vimrun_warning = FALSE;
181static char *vimrun_path = "vimrun ";
182#endif
183
Bram Moolenaar12b559e2013-06-12 22:41:37 +0200184static int win32_getattrs(char_u *name);
185static int win32_setattrs(char_u *name, int attrs);
186static int win32_set_archive(char_u *name);
187
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100188static int conpty_working = 0;
Bram Moolenaar57da6982019-09-13 22:30:11 +0200189static int conpty_type = 0;
Bram Moolenaard9ef1b82019-02-13 19:23:10 +0100190static int conpty_stable = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100191static void vtp_flag_init();
192
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200193#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar6902c0e2019-02-16 14:07:37 +0100194static int vtp_working = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100195static void vtp_init();
196static void vtp_exit();
197static int vtp_printf(char *format, ...);
198static void vtp_sgr_bulk(int arg);
199static void vtp_sgr_bulks(int argc, int *argv);
200
201static guicolor_T save_console_bg_rgb;
202static guicolor_T save_console_fg_rgb;
203
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200204static int g_color_index_bg = 0;
205static int g_color_index_fg = 7;
206
207# ifdef FEAT_TERMGUICOLORS
208static int default_console_color_bg = 0x000000; // black
209static int default_console_color_fg = 0xc0c0c0; // white
210# endif
211
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100212# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200213# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100214# else
215# define USE_VTP 0
216# endif
217
218static void set_console_color_rgb(void);
219static void reset_console_color_rgb(void);
220#endif
221
Bram Moolenaar0f873732019-12-05 20:28:46 +0100222// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100223#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
224# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
225#endif
226
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200227#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100228static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
230
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200231static char_u *exe_path = NULL;
232
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100233static BOOL win8_or_later = FALSE;
234
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200235#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100236// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100237typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
238{
239 ULONG cbSize;
240 COORD dwSize;
241 COORD dwCursorPosition;
242 WORD wAttributes;
243 SMALL_RECT srWindow;
244 COORD dwMaximumWindowSize;
245 WORD wPopupAttributes;
246 BOOL bFullscreenSupported;
247 COLORREF ColorTable[16];
248} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
249typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
250static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
251typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
252static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
253static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100254#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100255
256/*
257 * Get version number including build number
258 */
259typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100260#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100261 (((major) << 24) | ((minor) << 16) | (build))
262
263 static DWORD
264get_build_number(void)
265{
266 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
267 HMODULE hNtdll;
268 PfnRtlGetVersion pRtlGetVersion;
269 DWORD ver = MAKE_VER(0, 0, 0);
270
271 hNtdll = GetModuleHandle("ntdll.dll");
272 if (hNtdll != NULL)
273 {
274 pRtlGetVersion =
275 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
276 pRtlGetVersion(&osver);
277 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
278 min(osver.dwMinorVersion, 255),
279 min(osver.dwBuildNumber, 32767));
280 }
281 return ver;
282}
283
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200284#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100285/*
286 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100287 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100288 */
289 static BOOL
290read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100291 HANDLE hInput,
292 INPUT_RECORD *lpBuffer,
293 DWORD nLength,
294 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100295{
296 enum
297 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100298 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100299 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100300 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100301 static DWORD s_dwIndex = 0;
302 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100303 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100304 int head;
305 int tail;
306 int i;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100307
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200308 if (nLength == -2)
309 return (s_dwMax > 0) ? TRUE : FALSE;
310
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100311 if (!win8_or_later)
312 {
313 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200314 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
315 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100316 }
317
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100318 if (s_dwMax == 0)
319 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100320 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200321 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
322 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100323 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100324 s_dwIndex = 0;
325 s_dwMax = dwEvents;
326 if (dwEvents == 0)
327 {
328 *lpEvents = 0;
329 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100330 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100331
332 if (s_dwMax > 1)
333 {
334 head = 0;
335 tail = s_dwMax - 1;
336 while (head != tail)
337 {
338 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
339 && s_irCache[head + 1].EventType
340 == WINDOW_BUFFER_SIZE_EVENT)
341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100342 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100343 for (i = head; i < tail; ++i)
344 s_irCache[i] = s_irCache[i + 1];
345 --tail;
346 continue;
347 }
348 head++;
349 }
350 s_dwMax = tail + 1;
351 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100352 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100353
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100354 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200355 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100356 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100357 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100358 return TRUE;
359}
360
361/*
362 * Version of PeekConsoleInput() that works with IME.
363 */
364 static BOOL
365peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100366 HANDLE hInput,
367 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200368 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100369 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100370{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100371 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100372}
373
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100374# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200375 static DWORD
376msg_wait_for_multiple_objects(
377 DWORD nCount,
378 LPHANDLE pHandles,
379 BOOL fWaitAll,
380 DWORD dwMilliseconds,
381 DWORD dwWakeMask)
382{
383 if (read_console_input(NULL, NULL, -2, NULL))
384 return WAIT_OBJECT_0;
385 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
386 dwMilliseconds, dwWakeMask);
387}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100388# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200389
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100390# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200391 static DWORD
392wait_for_single_object(
393 HANDLE hHandle,
394 DWORD dwMilliseconds)
395{
396 if (read_console_input(NULL, NULL, -2, NULL))
397 return WAIT_OBJECT_0;
398 return WaitForSingleObject(hHandle, dwMilliseconds);
399}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100400# endif
401#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 static void
404get_exe_name(void)
405{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100406 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
407 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100408#define MAX_ENV_PATH_LEN 8192
409 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200410 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 if (exe_name == NULL)
413 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100414 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100415 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (*temp != NUL)
417 exe_name = FullName_save((char_u *)temp, FALSE);
418 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000419
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200420 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000421 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200422 exe_path = vim_strnsave(exe_name,
423 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200424 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000425 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100426 // Append our starting directory to $PATH, so that when doing
427 // "!xxd" it's found in our starting directory. Needed because
428 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200429 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100430 if (p == NULL
431 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200432 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 if (p == NULL || *p == NUL)
434 temp[0] = NUL;
435 else
436 {
437 STRCPY(temp, p);
438 STRCAT(temp, ";");
439 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200440 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100441 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200442 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000443 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445}
446
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200447/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100448 * Unescape characters in "p" that appear in "escaped".
449 */
450 static void
451unescape_shellxquote(char_u *p, char_u *escaped)
452{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100453 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100454 int n;
455
456 while (*p != NUL)
457 {
458 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
459 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100460 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100461 p += n;
462 l -= n;
463 }
464}
465
466/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200467 * Load library "name".
468 */
469 HINSTANCE
470vimLoadLib(char *name)
471{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200472 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200473
Bram Moolenaar0f873732019-12-05 20:28:46 +0100474 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
475 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200476 if (exe_path == NULL)
477 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200478 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200480 WCHAR old_dirw[MAXPATHL];
481
482 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
483 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // Change directory to where the executable is, both to make
485 // sure we find a .dll there and to avoid looking for a .dll
486 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100487 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200488 dll = LoadLibrary(name);
489 SetCurrentDirectoryW(old_dirw);
490 return dll;
491 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200492 }
493 return dll;
494}
495
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200496#if defined(VIMDLL) || defined(PROTO)
497/*
498 * Check if the current executable file is for the GUI subsystem.
499 */
500 int
501mch_is_gui_executable(void)
502{
503 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
504 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
505 PIMAGE_NT_HEADERS pPE;
506
507 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
508 return FALSE;
509 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
510 if (pPE->Signature != IMAGE_NT_SIGNATURE)
511 return FALSE;
512 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
513 return TRUE;
514 return FALSE;
515}
516#endif
517
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100518#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
519/*
520 * Get related information about 'funcname' which is imported by 'hInst'.
521 * If 'info' is 0, return the function address.
522 * If 'info' is 1, return the module name which the function is imported from.
523 */
524 static void *
525get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
526{
527 PBYTE pImage = (PBYTE)hInst;
528 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
529 PIMAGE_NT_HEADERS pPE;
530 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100531 PIMAGE_THUNK_DATA pIAT; // Import Address Table
532 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100533 PIMAGE_IMPORT_BY_NAME pImpName;
534
535 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
536 return NULL;
537 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
538 if (pPE->Signature != IMAGE_NT_SIGNATURE)
539 return NULL;
540 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
541 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
542 .VirtualAddress);
543 for (; pImpDesc->FirstThunk; ++pImpDesc)
544 {
545 if (!pImpDesc->OriginalFirstThunk)
546 continue;
547 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
548 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
549 for (; pIAT->u1.Function; ++pIAT, ++pINT)
550 {
551 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
552 continue;
553 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
554 + (UINT_PTR)(pINT->u1.AddressOfData));
555 if (strcmp((char *)pImpName->Name, funcname) == 0)
556 {
557 switch (info)
558 {
559 case 0:
560 return (void *)pIAT->u1.Function;
561 case 1:
562 return (void *)(pImage + pImpDesc->Name);
563 default:
564 return NULL;
565 }
566 }
567 }
568 }
569 return NULL;
570}
571
572/*
573 * Get the module handle which 'funcname' in 'hInst' is imported from.
574 */
575 HINSTANCE
576find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
577{
578 char *modulename;
579
580 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
581 if (modulename != NULL)
582 return GetModuleHandleA(modulename);
583 return NULL;
584}
585
586/*
587 * Get the address of 'funcname' which is imported by 'hInst' DLL.
588 */
589 void *
590get_dll_import_func(HINSTANCE hInst, const char *funcname)
591{
592 return get_imported_func_info(hInst, funcname, 0);
593}
594#endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
597# ifndef GETTEXT_DLL
598# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200599# define GETTEXT_DLL_ALT1 "libintl-8.dll"
600# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000603static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200604static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000605static char *null_libintl_textdomain(const char *);
606static char *null_libintl_bindtextdomain(const char *, const char *);
607static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100608static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200610static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000611char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200612char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
613 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000614char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
615char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000617char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
618 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100619int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100622dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int i;
625 static struct
626 {
627 char *name;
628 FARPROC *ptr;
629 } libintl_entry[] =
630 {
631 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200632 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
634 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
635 {NULL, NULL}
636 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100637 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar7554c542018-10-06 15:03:15 +0200639 // No need to initialize twice.
640 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200642 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100643 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100644# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100645 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200646 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100647# endif
648# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200649 if (!hLibintlDLL)
650 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100651# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100652 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200654 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200656 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100657 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200658 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200660 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 for (i = 0; libintl_entry[i].name != NULL
663 && libintl_entry[i].ptr != NULL; ++i)
664 {
665 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
666 libintl_entry[i].name)) == NULL)
667 {
668 dyn_libintl_end();
669 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000670 {
671 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000673 verbose_leave();
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return 0;
676 }
677 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000678
Bram Moolenaar0f873732019-12-05 20:28:46 +0100679 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000681 "bind_textdomain_codeset");
682 if (dyn_libintl_bind_textdomain_codeset == NULL)
683 dyn_libintl_bind_textdomain_codeset =
684 null_libintl_bind_textdomain_codeset;
685
Bram Moolenaar0f873732019-12-05 20:28:46 +0100686 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100687 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
688 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100689 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100690 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
691 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 return 1;
694}
695
696 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100697dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 if (hLibintlDLL)
700 FreeLibrary(hLibintlDLL);
701 hLibintlDLL = NULL;
702 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200703 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 dyn_libintl_textdomain = null_libintl_textdomain;
705 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000706 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100707 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000711null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 return (char*)msgid;
714}
715
716 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200717null_libintl_ngettext(
718 const char *msgid,
719 const char *msgid_plural,
720 unsigned long n)
721{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200722 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200723}
724
Bram Moolenaaree695f72016-08-03 22:08:45 +0200725 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100726null_libintl_bindtextdomain(
727 const char *domainname UNUSED,
728 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 return NULL;
731}
732
733 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100734null_libintl_bind_textdomain_codeset(
735 const char *domainname UNUSED,
736 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000737{
738 return NULL;
739}
740
741 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100742null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 return NULL;
745}
746
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200747 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100748null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100749{
750 return 0;
751}
752
Bram Moolenaar0f873732019-12-05 20:28:46 +0100753#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar0f873732019-12-05 20:28:46 +0100755// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757#ifndef VER_PLATFORM_WIN32_WINDOWS
758# define VER_PLATFORM_WIN32_WINDOWS 1
759#endif
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100762# ifndef PROTO
763# include <aclapi.h>
764# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200765# ifndef PROTECTED_DACL_SECURITY_INFORMATION
766# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#endif
769
Bram Moolenaar27515922013-06-29 15:36:26 +0200770#ifdef HAVE_ACL
771/*
772 * Enables or disables the specified privilege.
773 */
774 static BOOL
775win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
776{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100777 BOOL bResult;
778 LUID luid;
779 HANDLE hToken;
780 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200781
782 if (!OpenProcessToken(GetCurrentProcess(),
783 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
784 return FALSE;
785
786 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
787 {
788 CloseHandle(hToken);
789 return FALSE;
790 }
791
Bram Moolenaar45500912014-07-09 20:51:07 +0200792 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200793 tokenPrivileges.Privileges[0].Luid = luid;
794 tokenPrivileges.Privileges[0].Attributes = bEnable ?
795 SE_PRIVILEGE_ENABLED : 0;
796
797 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
798 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
799
800 CloseHandle(hToken);
801
802 return bResult && GetLastError() == ERROR_SUCCESS;
803}
804#endif
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100807 * Set "win8_or_later" and fill in "windowsVersion".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 */
809 void
810PlatformId(void)
811{
812 static int done = FALSE;
813
814 if (!done)
815 {
816 OSVERSIONINFO ovi;
817
818 ovi.dwOSVersionInfoSize = sizeof(ovi);
819 GetVersionEx(&ovi);
820
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100821 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
822 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100824 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
825 || ovi.dwMajorVersion > 6)
826 win8_or_later = TRUE;
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100829 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200830 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#endif
832 done = TRUE;
833 }
834}
835
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200836#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100838# define SHIFT (SHIFT_PRESSED)
839# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
840# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
841# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
845// We map function keys to their ANSI terminal equivalents, as produced
846// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
847// ANSI key with a value >= '\300' is nonstandard, but provided anyway
848// so that the user can have access to all SHIFT-, CTRL-, and ALT-
849// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000851static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 WORD wVirtKey;
854 BOOL fAnsiKey;
855 int chAlone;
856 int chShift;
857 int chCtrl;
858 int chAlt;
859} VirtKeyMap[] =
860{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200861// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
863
864 { VK_F1, TRUE, ';', 'T', '^', 'h', },
865 { VK_F2, TRUE, '<', 'U', '_', 'i', },
866 { VK_F3, TRUE, '=', 'V', '`', 'j', },
867 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
868 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
869 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
870 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
871 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
872 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
873 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200874 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
875 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200877 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
878 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
879 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
880 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
881 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
882 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
883 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
884 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
885 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
886 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100887 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200889 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100891# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200892 // Most people don't have F13-F20, but what the hell...
893 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
894 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
895 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
896 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
897 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
898 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
899 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
900 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100901# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200902 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
903 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
904 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
905 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200907 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
908 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
909 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
910 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
911 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
912 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
913 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
914 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
915 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
916 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100917 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918};
919
920
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100921# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
922# define UChar UnicodeChar
923# else
924# define UChar uChar.UnicodeChar
925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar0f873732019-12-05 20:28:46 +0100927/*
928 * The return code indicates key code size.
929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000932 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 UINT uMods = pker->dwControlKeyState;
935 static int s_iIsDead = 0;
936 static WORD awAnsiCode[2];
937 static BYTE abKeystate[256];
938
939
940 if (s_iIsDead == 2)
941 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200942 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 s_iIsDead = 0;
944 return 1;
945 }
946
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200947 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 return 1;
949
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200950 vim_memset(abKeystate, 0, sizeof (abKeystate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
Bram Moolenaar0f873732019-12-05 20:28:46 +0100952 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200953 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 if (uMods & SHIFT_PRESSED)
956 abKeystate[VK_SHIFT] = 0x80;
957 if (uMods & CAPSLOCK_ON)
958 abKeystate[VK_CAPITAL] = 1;
959
960 if ((uMods & ALT_GR) == ALT_GR)
961 {
962 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
963 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
964 }
965
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200966 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
967 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200970 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 return s_iIsDead;
973}
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975static BOOL g_fJustGotFocus = FALSE;
976
977/*
978 * Decode a KEY_EVENT into one or two keystrokes
979 */
980 static BOOL
981decode_key_event(
982 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200983 WCHAR *pch,
984 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200986 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 int i;
989 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
990
991 *pch = *pch2 = NUL;
992 g_fJustGotFocus = FALSE;
993
Bram Moolenaar0f873732019-12-05 20:28:46 +0100994 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (!pker->bKeyDown)
996 return FALSE;
997
Bram Moolenaar0f873732019-12-05 20:28:46 +0100998 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 switch (pker->wVirtualKeyCode)
1000 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001001 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case VK_SHIFT:
1003 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001004 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 return FALSE;
1006
1007 default:
1008 break;
1009 }
1010
Bram Moolenaar0f873732019-12-05 20:28:46 +01001011 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001012 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001014 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (pker->wVirtualKeyCode == '6')
1016 {
1017 *pch = Ctrl_HAT;
1018 return TRUE;
1019 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001020 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 else if (pker->wVirtualKeyCode == '2')
1022 {
1023 *pch = NUL;
1024 return TRUE;
1025 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else if (pker->wVirtualKeyCode == 0xBD)
1028 {
1029 *pch = Ctrl__;
1030 return TRUE;
1031 }
1032 }
1033
Bram Moolenaar0f873732019-12-05 20:28:46 +01001034 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1036 {
1037 *pch = K_NUL;
1038 *pch2 = '\017';
1039 return TRUE;
1040 }
1041
1042 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1043 {
1044 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1045 {
1046 if (nModifs == 0)
1047 *pch = VirtKeyMap[i].chAlone;
1048 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1049 *pch = VirtKeyMap[i].chShift;
1050 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1051 *pch = VirtKeyMap[i].chCtrl;
1052 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1053 *pch = VirtKeyMap[i].chAlt;
1054
1055 if (*pch != 0)
1056 {
1057 if (VirtKeyMap[i].fAnsiKey)
1058 {
1059 *pch2 = *pch;
1060 *pch = K_NUL;
1061 }
1062
1063 return TRUE;
1064 }
1065 }
1066 }
1067
1068 i = win32_kbd_patch_key(pker);
1069
1070 if (i < 0)
1071 *pch = NUL;
1072 else
1073 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001074 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (pmodifiers != NULL)
1077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001078 // Pass on the ALT key as a modifier, but only when not combined
1079 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1081 *pmodifiers |= MOD_MASK_ALT;
1082
Bram Moolenaar0f873732019-12-05 20:28:46 +01001083 // Pass on SHIFT only for special keys, because we don't know when
1084 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1086 *pmodifiers |= MOD_MASK_SHIFT;
1087
Bram Moolenaar0f873732019-12-05 20:28:46 +01001088 // Pass on CTRL only for non-special keys, because we don't know
1089 // when it's already included with the character. And not when
1090 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1092 && *pch >= 0x20 && *pch < 0x80)
1093 *pmodifiers |= MOD_MASK_CTRL;
1094 }
1095 }
1096
1097 return (*pch != NUL);
1098}
1099
Bram Moolenaar0f873732019-12-05 20:28:46 +01001100#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * For the GUI the mouse handling is in gui_w32.c.
1105 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001106#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001108mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001111#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001112static int g_fMouseAvail = FALSE; // mouse present
1113static int g_fMouseActive = FALSE; // mouse enabled
1114static int g_nMouseClick = -1; // mouse status
1115static int g_xMouse; // mouse x coordinate
1116static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118/*
1119 * Enable or disable mouse input
1120 */
1121 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001122mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 DWORD cmodein;
1125
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001126# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001127 if (gui.in_use)
1128 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (!g_fMouseAvail)
1131 return;
1132
1133 g_fMouseActive = on;
1134 GetConsoleMode(g_hConIn, &cmodein);
1135
1136 if (g_fMouseActive)
1137 cmodein |= ENABLE_MOUSE_INPUT;
1138 else
1139 cmodein &= ~ENABLE_MOUSE_INPUT;
1140
1141 SetConsoleMode(g_hConIn, cmodein);
1142}
1143
Bram Moolenaar157d8132018-03-06 17:09:20 +01001144
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001145# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001146/*
1147 * Called when 'balloonevalterm' changed.
1148 */
1149 void
1150mch_bevalterm_changed(void)
1151{
1152 mch_setmouse(g_fMouseActive);
1153}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001154# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*
1157 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1158 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1159 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1160 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1161 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1162 * and we return the mouse position in g_xMouse and g_yMouse.
1163 *
1164 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1165 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1166 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1167 *
1168 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1169 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1170 *
1171 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1172 * moves, even if it stays within the same character cell. We ignore
1173 * all MOUSE_MOVED messages if the position hasn't really changed, and
1174 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1175 * we're only interested in MOUSE_DRAG).
1176 *
1177 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1178 * 2-button mouses by pressing the left & right buttons simultaneously.
1179 * In practice, it's almost impossible to click both at the same time,
1180 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1181 * in such cases, if the user is clicking quickly.
1182 */
1183 static BOOL
1184decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001185 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 static int s_nOldButton = -1;
1188 static int s_nOldMouseClick = -1;
1189 static int s_xOldMouse = -1;
1190 static int s_yOldMouse = -1;
1191 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001192# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 static int s_cClicks = 1;
1196 static BOOL s_fReleased = TRUE;
1197 static DWORD s_dwLastClickTime = 0;
1198 static BOOL s_fNextIsMiddle = FALSE;
1199
Bram Moolenaar0f873732019-12-05 20:28:46 +01001200 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1203 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1204 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1205 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1206
1207 int nButton;
1208
1209 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1210 cButtons = 2;
1211
1212 if (!g_fMouseAvail || !g_fMouseActive)
1213 {
1214 g_nMouseClick = -1;
1215 return FALSE;
1216 }
1217
Bram Moolenaar0f873732019-12-05 20:28:46 +01001218 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (g_fJustGotFocus)
1220 {
1221 g_fJustGotFocus = FALSE;
1222 return FALSE;
1223 }
1224
Bram Moolenaar0f873732019-12-05 20:28:46 +01001225 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (g_nMouseClick != -1)
1227 return TRUE;
1228
1229 nButton = -1;
1230 g_xMouse = pmer->dwMousePosition.X;
1231 g_yMouse = pmer->dwMousePosition.Y;
1232
1233 if (pmer->dwEventFlags == MOUSE_MOVED)
1234 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001235 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1236 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1238 return FALSE;
1239 }
1240
Bram Moolenaar0f873732019-12-05 20:28:46 +01001241 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1243 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001244 nButton = MOUSE_RELEASE;
1245
Bram Moolenaar0f873732019-12-05 20:28:46 +01001246 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001248 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001249# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001250 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001251 if (p_bevalterm)
1252 nButton = MOUSE_DRAG;
1253 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001254# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001255 return FALSE;
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 s_fReleased = TRUE;
1259 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001260 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001262 // on a 2-button mouse, hold down left and right buttons
1263 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1266 {
1267 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1268
Bram Moolenaar0f873732019-12-05 20:28:46 +01001269 // if either left or right button only is pressed, see if the
1270 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (dwLR == LEFT || dwLR == RIGHT)
1272 {
1273 for (;;)
1274 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001275 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1277 != WAIT_OBJECT_0)
1278 break;
1279 else
1280 {
1281 DWORD cRecords = 0;
1282 INPUT_RECORD ir;
1283 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1284
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001285 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1288 || !(pmer2->dwButtonState & LEFT_RIGHT))
1289 break;
1290 else
1291 {
1292 if (pmer2->dwEventFlags != MOUSE_MOVED)
1293 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001294 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
1296 return decode_mouse_event(pmer2);
1297 }
1298 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1299 s_yOldMouse == pmer2->dwMousePosition.Y)
1300 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001301 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001302 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Bram Moolenaar0f873732019-12-05 20:28:46 +01001304 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001305 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
1307 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1308 break;
1309 }
1310 else
1311 break;
1312 }
1313 }
1314 }
1315 }
1316 }
1317
1318 if (s_fNextIsMiddle)
1319 {
1320 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1321 ? MOUSE_DRAG : MOUSE_MIDDLE;
1322 s_fNextIsMiddle = FALSE;
1323 }
1324 else if (cButtons == 2 &&
1325 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1326 {
1327 nButton = MOUSE_MIDDLE;
1328
1329 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1330 {
1331 s_fNextIsMiddle = TRUE;
1332 nButton = MOUSE_RELEASE;
1333 }
1334 }
1335 else if ((pmer->dwButtonState & LEFT) == LEFT)
1336 nButton = MOUSE_LEFT;
1337 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1338 nButton = MOUSE_MIDDLE;
1339 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1340 nButton = MOUSE_RIGHT;
1341
1342 if (! s_fReleased && ! s_fNextIsMiddle
1343 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1344 return FALSE;
1345
1346 s_fReleased = s_fNextIsMiddle;
1347 }
1348
1349 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1350 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001351 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (nButton != -1 && nButton != MOUSE_RELEASE)
1353 {
1354 DWORD dwCurrentTime = GetTickCount();
1355
1356 if (s_xOldMouse != g_xMouse
1357 || s_yOldMouse != g_yMouse
1358 || s_nOldButton != nButton
1359 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001360# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1364 {
1365 s_cClicks = 1;
1366 }
1367 else if (++s_cClicks > 4)
1368 {
1369 s_cClicks = 1;
1370 }
1371
1372 s_dwLastClickTime = dwCurrentTime;
1373 }
1374 }
1375 else if (pmer->dwEventFlags == MOUSE_MOVED)
1376 {
1377 if (nButton != -1 && nButton != MOUSE_RELEASE)
1378 nButton = MOUSE_DRAG;
1379
1380 s_cClicks = 1;
1381 }
1382
1383 if (nButton == -1)
1384 return FALSE;
1385
1386 if (nButton != MOUSE_RELEASE)
1387 s_nOldButton = nButton;
1388
1389 g_nMouseClick = nButton;
1390
1391 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1392 g_nMouseClick |= MOUSE_SHIFT;
1393 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1394 g_nMouseClick |= MOUSE_CTRL;
1395 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1396 g_nMouseClick |= MOUSE_ALT;
1397
1398 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1399 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01001401 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (s_xOldMouse == g_xMouse
1403 && s_yOldMouse == g_yMouse
1404 && s_nOldMouseClick == g_nMouseClick)
1405 {
1406 g_nMouseClick = -1;
1407 return FALSE;
1408 }
1409
1410 s_xOldMouse = g_xMouse;
1411 s_yOldMouse = g_yMouse;
1412 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001413# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 s_nOldMouseClick = g_nMouseClick;
1417
1418 return TRUE;
1419}
1420
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001421#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423
1424#ifdef MCH_CURSOR_SHAPE
1425/*
1426 * Set the shape of the cursor.
1427 * 'thickness' can be from 1 (thin) to 99 (block)
1428 */
1429 static void
1430mch_set_cursor_shape(int thickness)
1431{
1432 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1433 ConsoleCursorInfo.dwSize = thickness;
1434 ConsoleCursorInfo.bVisible = s_cursor_visible;
1435
1436 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1437 if (s_cursor_visible)
1438 SetConsoleCursorPosition(g_hConOut, g_coord);
1439}
1440
1441 void
1442mch_update_cursor(void)
1443{
1444 int idx;
1445 int thickness;
1446
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001447# ifdef VIMDLL
1448 if (gui.in_use)
1449 return;
1450# endif
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /*
1453 * How the cursor is drawn depends on the current mode.
1454 */
1455 idx = get_shape_idx(FALSE);
1456
1457 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001458 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 else
1460 thickness = shape_table[idx].percentage;
1461 mch_set_cursor_shape(thickness);
1462}
1463#endif
1464
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001465#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466/*
1467 * Handle FOCUS_EVENT.
1468 */
1469 static void
1470handle_focus_event(INPUT_RECORD ir)
1471{
1472 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1473 ui_focus_change((int)g_fJustGotFocus);
1474}
1475
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001476static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Wait until console input from keyboard or mouse is available,
1480 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001481 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 * Return TRUE if something is available FALSE if not.
1483 */
1484 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001485WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 DWORD dwNow = 0, dwEndTime = 0;
1488 INPUT_RECORD ir;
1489 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001490 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001491# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001492 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001496 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 dwEndTime = GetTickCount() + msec;
1498 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001499 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 dwEndTime = INFINITE;
1501
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001502 // We need to loop until the end of the time period, because
1503 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 for (;;)
1505 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001506 // Only process messages when waiting.
1507 if (msec != 0)
1508 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001509# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001510 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001511# endif
1512# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001513 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001514# endif
1515# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001516 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001517# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001518 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001519
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001520 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001521# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001522 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 )
1525 return TRUE;
1526
1527 if (msec > 0)
1528 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001529 // If the specified wait time has passed, return. Beware that
1530 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001532 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 break;
1534 }
1535 if (msec != 0)
1536 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001537 DWORD dwWaitTime = dwEndTime - dwNow;
1538
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001539# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001540 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001541 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001542 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001543 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001544 // If there is readahead then parse_queued_messages() timed out
1545 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001546 if (channel_any_readahead())
1547 dwWaitTime = 10;
1548 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001549# endif
1550# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001551 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001552 // The 'balloonexpr' may indirectly invoke a callback while
1553 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001554 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001555# endif
1556# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001557 if (mzthreads_allowed() && p_mzq > 0
1558 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001559 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001560# endif
1561# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001562 // When waiting very briefly don't trigger timers.
1563 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001564 {
1565 long due_time;
1566
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001567 // Trigger timers and then get the time in msec until the next
1568 // one is due. Wait up to that time.
1569 due_time = check_due_timer();
1570 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001571 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001572 // timer may have used feedkeys().
1573 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001574 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001575 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1576 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001577 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001578# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001579 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001580# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001581 // Wait for either an event on the console input or a
1582 // message in the client-server window.
1583 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1584 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001585# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001586 wait_for_single_object(g_hConIn, dwWaitTime)
1587 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001588# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001589 )
1590 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592
1593 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001594 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001596# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (State & CMDLINE && msg_row == Rows - 1)
1598 {
1599 CONSOLE_SCREEN_BUFFER_INFO csbi;
1600
1601 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1602 {
1603 if (csbi.dwCursorPosition.Y != msg_row)
1604 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001605 // The screen is now messed up, must redraw the
1606 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 redraw_all_later(CLEAR);
1608 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1609 redrawcmd();
1610 }
1611 }
1612 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 if (cRecords > 0)
1616 {
1617 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1618 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001619# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001620 // Windows IME sends two '\n's with only one 'ENTER'. First:
1621 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001622 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1624 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001625 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 continue;
1627 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1630 NULL, FALSE))
1631 return TRUE;
1632 }
1633
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001634 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 if (ir.EventType == FOCUS_EVENT)
1637 handle_focus_event(ir);
1638 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001639 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001640 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1641
Bram Moolenaar36698e32019-12-11 22:57:40 +01001642 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001643 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001644 if (dwSize.X != Columns || dwSize.Y != Rows)
1645 {
1646 CONSOLE_SCREEN_BUFFER_INFO csbi;
1647 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001648 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001649 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001650 if (dwSize.X != Columns || dwSize.Y != Rows)
1651 {
1652 ResizeConBuf(g_hConOut, dwSize);
1653 shell_resized();
1654 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001655 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else if (ir.EventType == MOUSE_EVENT
1658 && decode_mouse_event(&ir.Event.MouseEvent))
1659 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else if (msec == 0)
1662 break;
1663 }
1664
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001665# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001666 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (input_available())
1668 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001669# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 return FALSE;
1672}
1673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674/*
1675 * return non-zero if a character is available
1676 */
1677 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001678mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001680# ifdef VIMDLL
1681 if (gui.in_use)
1682 return TRUE;
1683# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001684 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001686
1687# if defined(FEAT_TERMINAL) || defined(PROTO)
1688/*
1689 * Check for any pending input or messages.
1690 */
1691 int
1692mch_check_messages(void)
1693{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001694# ifdef VIMDLL
1695 if (gui.in_use)
1696 return TRUE;
1697# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001698 return WaitForChar(0L, TRUE);
1699}
1700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702/*
1703 * Create the console input. Used when reading stdin doesn't work.
1704 */
1705 static void
1706create_conin(void)
1707{
1708 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1709 FILE_SHARE_READ|FILE_SHARE_WRITE,
1710 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001711 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 did_create_conin = TRUE;
1713}
1714
1715/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001716 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001718 static WCHAR
1719tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001721 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 for (;;)
1724 {
1725 INPUT_RECORD ir;
1726 DWORD cRecords = 0;
1727
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001728# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001729 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (input_available())
1731 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (g_nMouseClick != -1)
1733 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001734# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001735 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 if (did_create_conin)
1738 read_error_exit();
1739 create_conin();
1740 continue;
1741 }
1742
1743 if (ir.EventType == KEY_EVENT)
1744 {
1745 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1746 pmodifiers, TRUE))
1747 return ch;
1748 }
1749 else if (ir.EventType == FOCUS_EVENT)
1750 handle_focus_event(ir);
1751 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1752 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 else if (ir.EventType == MOUSE_EVENT)
1754 {
1755 if (decode_mouse_event(&ir.Event.MouseEvent))
1756 return 0;
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001760#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762
1763/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001764 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * Get one or more characters from the keyboard or the mouse.
1766 * If time == 0, do not wait for characters.
1767 * If time == n, wait a short time for characters.
1768 * If time == -1, wait forever for characters.
1769 * Returns the number of characters read into buf.
1770 */
1771 int
1772mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001773 char_u *buf UNUSED,
1774 int maxlen UNUSED,
1775 long time UNUSED,
1776 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001778#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 int len;
1781 int c;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001782# define TYPEAHEADLEN 20
Bram Moolenaar0f873732019-12-05 20:28:46 +01001783 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 static int typeaheadlen = 0;
1785
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001786# ifdef VIMDLL
1787 if (gui.in_use)
1788 return 0;
1789# endif
1790
Bram Moolenaar0f873732019-12-05 20:28:46 +01001791 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (typeaheadlen > 0)
1793 goto theend;
1794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (time >= 0)
1796 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001797 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001802 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaar3918c952005-03-15 22:34:55 +00001804 /*
1805 * If there is no character available within 2 seconds (default)
1806 * write the autoscript file to disk. Or cause the CursorHold event
1807 * to be triggered.
1808 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001809 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001811 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001813 buf[0] = K_SPECIAL;
1814 buf[1] = KS_EXTRA;
1815 buf[2] = (int)KE_CURSORHOLD;
1816 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001818 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 }
1821
1822 /*
1823 * Try to read as many characters as there are, until the buffer is full.
1824 */
1825
Bram Moolenaar0f873732019-12-05 20:28:46 +01001826 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 g_fCBrkPressed = FALSE;
1828
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001829# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (fdDump)
1831 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar0f873732019-12-05 20:28:46 +01001834 // Keep looping until there is something in the typeahead buffer and more
1835 // to get and still room in the buffer (up to two bytes for a char and
1836 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001837 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 && typeaheadlen + 5 <= TYPEAHEADLEN)
1839 {
1840 if (typebuf_changed(tb_change_cnt))
1841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001842 // "buf" may be invalid now if a client put something in the
1843 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 typeaheadlen = 0;
1845 break;
1846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (g_nMouseClick != -1)
1848 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001849# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (fdDump)
1851 fprintf(fdDump, "{%02x @ %d, %d}",
1852 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 typeahead[typeaheadlen++] = ESC + 128;
1855 typeahead[typeaheadlen++] = 'M';
1856 typeahead[typeaheadlen++] = g_nMouseClick;
1857 typeahead[typeaheadlen++] = g_xMouse + '!';
1858 typeahead[typeaheadlen++] = g_yMouse + '!';
1859 g_nMouseClick = -1;
1860 }
1861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001863 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 int modifiers = 0;
1865
1866 c = tgetch(&modifiers, &ch2);
1867
1868 if (typebuf_changed(tb_change_cnt))
1869 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001870 // "buf" may be invalid now if a client put something in the
1871 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 typeaheadlen = 0;
1873 break;
1874 }
1875
1876 if (c == Ctrl_C && ctrl_c_interrupts)
1877 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001878# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 got_int = TRUE;
1882 }
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
1886 int n = 1;
1887
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001888 if (ch2 == NUL)
1889 {
1890 int i;
1891 char_u *p;
1892 WCHAR ch[2];
1893
1894 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001895 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001896 {
1897 ch[1] = tgetch(&modifiers, &ch2);
1898 n++;
1899 }
1900 p = utf16_to_enc(ch, &n);
1901 if (p != NULL)
1902 {
1903 for (i = 0; i < n; i++)
1904 typeahead[typeaheadlen + i] = p[i];
1905 vim_free(p);
1906 }
1907 }
1908 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001909 typeahead[typeaheadlen] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (ch2 != NUL)
1911 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001912 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001913 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001914 switch (ch2)
1915 {
1916 case (WCHAR)'\324': // SHIFT+Insert
1917 case (WCHAR)'\325': // CTRL+Insert
1918 case (WCHAR)'\327': // SHIFT+Delete
1919 case (WCHAR)'\330': // CTRL+Delete
1920 typeahead[typeaheadlen + n] = (char_u)ch2;
1921 n++;
1922 break;
1923
1924 default:
1925 typeahead[typeaheadlen + n] = 3;
1926 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1927 n += 2;
1928 break;
1929 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001930 }
1931 else
1932 {
1933 typeahead[typeaheadlen + n] = 3;
1934 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1935 n += 2;
1936 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001937 }
1938
Bram Moolenaar0f873732019-12-05 20:28:46 +01001939 // Use the ALT key to set the 8th bit of the character
1940 // when it's one byte, the 8th bit isn't set yet and not
1941 // using a double-byte encoding (would become a lead
1942 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if ((modifiers & MOD_MASK_ALT)
1944 && n == 1
1945 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 )
1948 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001949 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1950 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 modifiers &= ~MOD_MASK_ALT;
1952 }
1953
1954 if (modifiers != 0)
1955 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001956 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 mch_memmove(typeahead + typeaheadlen + 3,
1958 typeahead + typeaheadlen, n);
1959 typeahead[typeaheadlen++] = K_SPECIAL;
1960 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1961 typeahead[typeaheadlen++] = modifiers;
1962 }
1963
1964 typeaheadlen += n;
1965
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001966# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (fdDump)
1968 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
1972 }
1973
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001974# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (fdDump)
1976 {
1977 fputs("]\n", fdDump);
1978 fflush(fdDump);
1979 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001983 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 len = 0;
1985 while (len < maxlen && typeaheadlen > 0)
1986 {
1987 buf[len++] = typeahead[0];
1988 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
1989 }
1990 return len;
1991
Bram Moolenaar0f873732019-12-05 20:28:46 +01001992#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001994#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995}
1996
Bram Moolenaar82881492012-11-20 16:53:39 +01001997#ifndef PROTO
1998# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001999# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
2002
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002003/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002004 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2005 * If "use_path" is FALSE: Return TRUE if "name" exists.
2006 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2007 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002008 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002011executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002013 WCHAR *p;
2014 WCHAR fnamew[_MAX_PATH];
2015 WCHAR *dumw;
2016 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002017 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
Bram Moolenaar43663192017-03-05 14:29:12 +01002019 if (!use_path)
2020 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002021 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002022 {
2023 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002024 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002025 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002026 *path = vim_strsave((char_u *)name);
2027 else
2028 *path = FullName_save((char_u *)name, FALSE);
2029 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002030 return TRUE;
2031 }
2032 return FALSE;
2033 }
2034
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002035 p = enc_to_utf16((char_u *)name, NULL);
2036 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002037 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002038
2039 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002040 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002041 if (wnewpath == NULL)
2042 return FALSE;
2043 wcscpy(wnewpath, L".;");
2044 wcscat(wnewpath, wcurpath);
2045 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2046 vim_free(wnewpath);
2047 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002048 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002049 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002050 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002051 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002052 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002053 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002054 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055}
2056
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002057#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2058 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002059/*
2060 * Bad parameter handler.
2061 *
2062 * Certain MS CRT functions will intentionally crash when passed invalid
2063 * parameters to highlight possible security holes. Setting this function as
2064 * the bad parameter handler will prevent the crash.
2065 *
2066 * In debug builds the parameters contain CRT information that might help track
2067 * down the source of a problem, but in non-debug builds the arguments are all
2068 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2069 * worth allowing these to make debugging of issues easier.
2070 */
2071 static void
2072bad_param_handler(const wchar_t *expression,
2073 const wchar_t *function,
2074 const wchar_t *file,
2075 unsigned int line,
2076 uintptr_t pReserved)
2077{
2078}
2079
2080# define SET_INVALID_PARAM_HANDLER \
2081 ((void)_set_invalid_parameter_handler(bad_param_handler))
2082#else
2083# define SET_INVALID_PARAM_HANDLER
2084#endif
2085
Bram Moolenaar4f974752019-02-17 17:44:42 +01002086#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
2088/*
2089 * GUI version of mch_init().
2090 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002091 static void
2092mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002094# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaar0f873732019-12-05 20:28:46 +01002098 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002099 SET_INVALID_PARAM_HANDLER;
2100
Bram Moolenaar0f873732019-12-05 20:28:46 +01002101 // Let critical errors result in a failure, not in a dialog box. Required
2102 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 SetErrorMode(SEM_FAILCRITICALERRORS);
2104
Bram Moolenaar0f873732019-12-05 20:28:46 +01002105 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar0f873732019-12-05 20:28:46 +01002107 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 Rows = 25;
2109 Columns = 80;
2110
Bram Moolenaar0f873732019-12-05 20:28:46 +01002111 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113 char_u vimrun_location[_MAX_PATH + 4];
2114
Bram Moolenaar0f873732019-12-05 20:28:46 +01002115 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 STRCPY(vimrun_location, exe_name);
2117 STRCPY(gettail(vimrun_location), "vimrun.exe");
2118 if (mch_getperm(vimrun_location) >= 0)
2119 {
2120 if (*skiptowhite(vimrun_location) != NUL)
2121 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002122 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 mch_memmove(vimrun_location + 1, vimrun_location,
2124 STRLEN(vimrun_location) + 1);
2125 *vimrun_location = '"';
2126 STRCPY(gettail(vimrun_location), "vimrun\" ");
2127 }
2128 else
2129 STRCPY(gettail(vimrun_location), "vimrun ");
2130
2131 vimrun_path = (char *)vim_strsave(vimrun_location);
2132 s_dont_use_vimrun = FALSE;
2133 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002134 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 s_dont_use_vimrun = FALSE;
2136
Bram Moolenaar0f873732019-12-05 20:28:46 +01002137 // Don't give the warning for a missing vimrun.exe right now, but only
2138 // when vimrun was supposed to be used. Don't bother people that do
2139 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (s_dont_use_vimrun)
2141 need_vimrun_warning = TRUE;
2142 }
2143
2144 /*
2145 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2146 * Otherwise the default "findstr /n" is used.
2147 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002148 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2150
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002151# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002152 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002153# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002154
2155 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156}
2157
2158
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002160
2161#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002163# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2164# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166/*
2167 * ClearConsoleBuffer()
2168 * Description:
2169 * Clears the entire contents of the console screen buffer, using the
2170 * specified attribute.
2171 * Returns:
2172 * TRUE on success
2173 */
2174 static BOOL
2175ClearConsoleBuffer(WORD wAttribute)
2176{
2177 CONSOLE_SCREEN_BUFFER_INFO csbi;
2178 COORD coord;
2179 DWORD NumCells, dummy;
2180
2181 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2182 return FALSE;
2183
2184 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2185 coord.X = 0;
2186 coord.Y = 0;
2187 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2188 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2191 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 return TRUE;
2195}
2196
2197/*
2198 * FitConsoleWindow()
2199 * Description:
2200 * Checks if the console window will fit within given buffer dimensions.
2201 * Also, if requested, will shrink the window to fit.
2202 * Returns:
2203 * TRUE on success
2204 */
2205 static BOOL
2206FitConsoleWindow(
2207 COORD dwBufferSize,
2208 BOOL WantAdjust)
2209{
2210 CONSOLE_SCREEN_BUFFER_INFO csbi;
2211 COORD dwWindowSize;
2212 BOOL NeedAdjust = FALSE;
2213
2214 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2215 {
2216 /*
2217 * A buffer resize will fail if the current console window does
2218 * not lie completely within that buffer. To avoid this, we might
2219 * have to move and possibly shrink the window.
2220 */
2221 if (csbi.srWindow.Right >= dwBufferSize.X)
2222 {
2223 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2224 if (dwWindowSize.X > dwBufferSize.X)
2225 dwWindowSize.X = dwBufferSize.X;
2226 csbi.srWindow.Right = dwBufferSize.X - 1;
2227 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2228 NeedAdjust = TRUE;
2229 }
2230 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2231 {
2232 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2233 if (dwWindowSize.Y > dwBufferSize.Y)
2234 dwWindowSize.Y = dwBufferSize.Y;
2235 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2236 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2237 NeedAdjust = TRUE;
2238 }
2239 if (NeedAdjust && WantAdjust)
2240 {
2241 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2242 return FALSE;
2243 }
2244 return TRUE;
2245 }
2246
2247 return FALSE;
2248}
2249
2250typedef struct ConsoleBufferStruct
2251{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002252 BOOL IsValid;
2253 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002254 PCHAR_INFO Buffer;
2255 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002256 PSMALL_RECT Regions;
2257 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258} ConsoleBuffer;
2259
2260/*
2261 * SaveConsoleBuffer()
2262 * Description:
2263 * Saves important information about the console buffer, including the
2264 * actual buffer contents. The saved information is suitable for later
2265 * restoration by RestoreConsoleBuffer().
2266 * Returns:
2267 * TRUE if all information was saved; FALSE otherwise
2268 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2269 */
2270 static BOOL
2271SaveConsoleBuffer(
2272 ConsoleBuffer *cb)
2273{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002274 DWORD NumCells;
2275 COORD BufferCoord;
2276 SMALL_RECT ReadRegion;
2277 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002278 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (cb == NULL)
2281 return FALSE;
2282
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002283 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 cb->IsValid = FALSE;
2286 return FALSE;
2287 }
2288 cb->IsValid = TRUE;
2289
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002290 /*
2291 * Allocate a buffer large enough to hold the entire console screen
2292 * buffer. If this ConsoleBuffer structure has already been initialized
2293 * with a buffer of the correct size, then just use that one.
2294 */
2295 if (!cb->IsValid || cb->Buffer == NULL ||
2296 cb->BufferSize.X != cb->Info.dwSize.X ||
2297 cb->BufferSize.Y != cb->Info.dwSize.Y)
2298 {
2299 cb->BufferSize.X = cb->Info.dwSize.X;
2300 cb->BufferSize.Y = cb->Info.dwSize.Y;
2301 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2302 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002303 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002304 if (cb->Buffer == NULL)
2305 return FALSE;
2306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002309 * We will now copy the console screen buffer into our buffer.
2310 * ReadConsoleOutput() seems to be limited as far as how much you
2311 * can read at a time. Empirically, this number seems to be about
2312 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2313 * in chunks until it is all copied. The chunks will all have the
2314 * same horizontal characteristics, so initialize them now. The
2315 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002317 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002318 ReadRegion.Left = 0;
2319 ReadRegion.Right = cb->Info.dwSize.X - 1;
2320 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002321
2322 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2323 if (cb->Regions == NULL || numregions != cb->NumRegions)
2324 {
2325 cb->NumRegions = numregions;
2326 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002327 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002328 if (cb->Regions == NULL)
2329 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002330 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002331 return FALSE;
2332 }
2333 }
2334
2335 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002337 /*
2338 * Read into position (0, Y) in our buffer.
2339 */
2340 BufferCoord.Y = Y;
2341 /*
2342 * Read the region whose top left corner is (0, Y) and whose bottom
2343 * right corner is (width - 1, Y + Y_incr - 1). This should define
2344 * a region of size width by Y_incr. Don't worry if this region is
2345 * too large for the remaining buffer; it will be cropped.
2346 */
2347 ReadRegion.Top = Y;
2348 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002349 if (!ReadConsoleOutputW(g_hConOut, // output handle
2350 cb->Buffer, // our buffer
2351 cb->BufferSize, // dimensions of our buffer
2352 BufferCoord, // offset in our buffer
2353 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002354 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002355 VIM_CLEAR(cb->Buffer);
2356 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002357 return FALSE;
2358 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002359 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361
2362 return TRUE;
2363}
2364
2365/*
2366 * RestoreConsoleBuffer()
2367 * Description:
2368 * Restores important information about the console buffer, including the
2369 * actual buffer contents, if desired. The information to restore is in
2370 * the same format used by SaveConsoleBuffer().
2371 * Returns:
2372 * TRUE on success
2373 */
2374 static BOOL
2375RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002376 ConsoleBuffer *cb,
2377 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002379 COORD BufferCoord;
2380 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002381 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 if (cb == NULL || !cb->IsValid)
2384 return FALSE;
2385
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002386 /*
2387 * Before restoring the buffer contents, clear the current buffer, and
2388 * restore the cursor position and window information. Doing this now
2389 * prevents old buffer contents from "flashing" onto the screen.
2390 */
2391 if (RestoreScreen)
2392 ClearConsoleBuffer(cb->Info.wAttributes);
2393
2394 FitConsoleWindow(cb->Info.dwSize, TRUE);
2395 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2396 return FALSE;
2397 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2398 return FALSE;
2399
2400 if (!RestoreScreen)
2401 {
2402 /*
2403 * No need to restore the screen buffer contents, so we're done.
2404 */
2405 return TRUE;
2406 }
2407
2408 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2409 return FALSE;
2410 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2411 return FALSE;
2412
2413 /*
2414 * Restore the screen buffer contents.
2415 */
2416 if (cb->Buffer != NULL)
2417 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002418 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002419 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002420 BufferCoord.X = cb->Regions[i].Left;
2421 BufferCoord.Y = cb->Regions[i].Top;
2422 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002423 if (!WriteConsoleOutputW(g_hConOut, // output handle
2424 cb->Buffer, // our buffer
2425 cb->BufferSize, // dimensions of our buffer
2426 BufferCoord, // offset in our buffer
2427 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002428 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002429 }
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 return TRUE;
2433}
2434
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002435# define FEAT_RESTORE_ORIG_SCREEN
2436# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002437static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439static ConsoleBuffer g_cbNonTermcap = { 0 };
2440static ConsoleBuffer g_cbTermcap = { 0 };
2441
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002442# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002444HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445static HICON g_hOrigIconSmall = NULL;
2446static HICON g_hOrigIcon = NULL;
2447static HICON g_hVimIcon = NULL;
2448static BOOL g_fCanChangeIcon = FALSE;
2449
Bram Moolenaar0f873732019-12-05 20:28:46 +01002450// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002451# ifndef ICON_SMALL
2452# define ICON_SMALL 0
2453# endif
2454# ifndef ICON_BIG
2455# define ICON_BIG 1
2456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457/*
2458 * GetConsoleIcon()
2459 * Description:
2460 * Attempts to retrieve the small icon and/or the big icon currently in
2461 * use by a given window.
2462 * Returns:
2463 * TRUE on success
2464 */
2465 static BOOL
2466GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002467 HWND hWnd,
2468 HICON *phIconSmall,
2469 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 if (hWnd == NULL)
2472 return FALSE;
2473
2474 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002475 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2476 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002478 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2479 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 return TRUE;
2481}
2482
2483/*
2484 * SetConsoleIcon()
2485 * Description:
2486 * Attempts to change the small icon and/or the big icon currently in
2487 * use by a given window.
2488 * Returns:
2489 * TRUE on success
2490 */
2491 static BOOL
2492SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002493 HWND hWnd,
2494 HICON hIconSmall,
2495 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (hWnd == NULL)
2498 return FALSE;
2499
2500 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002501 SendMessage(hWnd, WM_SETICON,
2502 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002504 SendMessage(hWnd, WM_SETICON,
2505 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 return TRUE;
2507}
2508
2509/*
2510 * SaveConsoleTitleAndIcon()
2511 * Description:
2512 * Saves the current console window title in g_szOrigTitle, for later
2513 * restoration. Also, attempts to obtain a handle to the console window,
2514 * and use it to save the small and big icons currently in use by the
2515 * console window. This is not always possible on some versions of Windows;
2516 * nor is it possible when running Vim remotely using Telnet (since the
2517 * console window the user sees is owned by a remote process).
2518 */
2519 static void
2520SaveConsoleTitleAndIcon(void)
2521{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002522 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2524 return;
2525
2526 /*
2527 * Obtain a handle to the console window using GetConsoleWindow() from
2528 * KERNEL32.DLL; we need to handle in order to change the window icon.
2529 * This function only exists on NT-based Windows, starting with Windows
2530 * 2000. On older operating systems, we can't change the window icon
2531 * anyway.
2532 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002533 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (g_hWnd == NULL)
2535 return;
2536
Bram Moolenaar0f873732019-12-05 20:28:46 +01002537 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2539 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2540 return;
2541
Bram Moolenaar0f873732019-12-05 20:28:46 +01002542 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002543 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002544 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (g_hVimIcon != NULL)
2546 g_fCanChangeIcon = TRUE;
2547}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550static int g_fWindInitCalled = FALSE;
2551static int g_fTermcapMode = FALSE;
2552static CONSOLE_CURSOR_INFO g_cci;
2553static DWORD g_cmodein = 0;
2554static DWORD g_cmodeout = 0;
2555
2556/*
2557 * non-GUI version of mch_init().
2558 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002559 static void
2560mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002562# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002563 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002564# endif
2565# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar0f873732019-12-05 20:28:46 +01002569 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002570 SET_INVALID_PARAM_HANDLER;
2571
Bram Moolenaar0f873732019-12-05 20:28:46 +01002572 // Let critical errors result in a failure, not in a dialog box. Required
2573 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 SetErrorMode(SEM_FAILCRITICALERRORS);
2575
Bram Moolenaar0f873732019-12-05 20:28:46 +01002576 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 out_flush();
2578
Bram Moolenaar0f873732019-12-05 20:28:46 +01002579 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (read_cmd_fd == 0)
2581 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2582 else
2583 create_conin();
2584 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2585
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002586# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002587 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002588 SaveConsoleBuffer(&g_cbOrig);
2589 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002590# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002591 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002592 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2593 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (cterm_normal_fg_color == 0)
2596 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2597 if (cterm_normal_bg_color == 0)
2598 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2599
Bram Moolenaarbdace832019-03-02 10:13:42 +01002600 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002601 g_color_index_fg = g_attrDefault & 0xf;
2602 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2603
Bram Moolenaar0f873732019-12-05 20:28:46 +01002604 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 update_tcap(g_attrCurrent);
2606
2607 GetConsoleCursorInfo(g_hConOut, &g_cci);
2608 GetConsoleMode(g_hConIn, &g_cmodein);
2609 GetConsoleMode(g_hConOut, &g_cmodeout);
2610
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002611# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 SaveConsoleTitleAndIcon();
2613 /*
2614 * Set both the small and big icons of the console window to Vim's icon.
2615 * Note that Vim presently only has one size of icon (32x32), but it
2616 * automatically gets scaled down to 16x16 when setting the small icon.
2617 */
2618 if (g_fCanChangeIcon)
2619 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 ui_get_shellsize();
2623
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002624# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 fdDump = fopen("dump", "wt");
2626
2627 if (fdDump)
2628 {
2629 time_t t;
2630
2631 time(&t);
2632 fputs(ctime(&t), fdDump);
2633 fflush(fdDump);
2634 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
2637 g_fWindInitCalled = TRUE;
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002641# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002642 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002643# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002644
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002645 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002646 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647}
2648
2649/*
2650 * non-GUI version of mch_exit().
2651 * Shut down and exit with status `r'
2652 * Careful: mch_exit() may be called before mch_init()!
2653 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002654 static void
2655mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002657 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002659 vtp_exit();
2660
Bram Moolenaar955f1982017-02-05 15:10:51 +01002661 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (g_fWindInitCalled)
2663 settmode(TMODE_COOK);
2664
Bram Moolenaar0f873732019-12-05 20:28:46 +01002665 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 if (g_fWindInitCalled)
2668 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002669# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002670 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 /*
2672 * Restore both the small and big icons of the console window to
2673 * what they were at startup. Don't do this when the window is
2674 * closed, Vim would hang here.
2675 */
2676 if (g_fCanChangeIcon && !g_fForceExit)
2677 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002680# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (fdDump)
2682 {
2683 time_t t;
2684
2685 time(&t);
2686 fputs(ctime(&t), fdDump);
2687 fclose(fdDump);
2688 }
2689 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 SetConsoleCursorInfo(g_hConOut, &g_cci);
2694 SetConsoleMode(g_hConIn, g_cmodein);
2695 SetConsoleMode(g_hConOut, g_cmodeout);
2696
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002697# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 exit(r);
2702}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002703#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002705 void
2706mch_init(void)
2707{
2708#ifdef VIMDLL
2709 if (gui.starting)
2710 mch_init_g();
2711 else
2712 mch_init_c();
2713#elif defined(FEAT_GUI_MSWIN)
2714 mch_init_g();
2715#else
2716 mch_init_c();
2717#endif
2718}
2719
2720 void
2721mch_exit(int r)
2722{
2723#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002724 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002725 mch_exit_g(r);
2726 else
2727 mch_exit_c(r);
2728#elif defined(FEAT_GUI_MSWIN)
2729 mch_exit_g(r);
2730#else
2731 mch_exit_c(r);
2732#endif
2733}
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735/*
2736 * Do we have an interactive window?
2737 */
2738 int
2739mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002740 int argc UNUSED,
2741 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 get_exe_name();
2744
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002745#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002746 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002748# ifdef VIMDLL
2749 if (gui.in_use)
2750 return OK;
2751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (isatty(1))
2753 return OK;
2754 return FAIL;
2755#endif
2756}
2757
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002758/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002759 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 * When "len" is > 0, also expand short to long filenames.
2761 */
2762 void
2763fname_case(
2764 char_u *name,
2765 int len)
2766{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002767 int flen;
2768 WCHAR *p;
2769 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002771 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002772 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return;
2774
2775 slash_adjust(name);
2776
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002777 p = enc_to_utf16(name, NULL);
2778 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002779 return;
2780
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002781 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002783 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002785 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002787 if (len > 0 || flen >= (int)STRLEN(q))
2788 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2789 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002792 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793}
2794
2795
2796/*
2797 * Insert user name in s[len].
2798 */
2799 int
2800mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002801 char_u *s,
2802 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002804 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002805 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002807 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002808 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002809 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002810
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002811 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002812 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002813 vim_strncpy(s, p, len - 1);
2814 vim_free(p);
2815 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002816 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 s[0] = NUL;
2819 return FAIL;
2820}
2821
2822
2823/*
2824 * Insert host name in s[len].
2825 */
2826 void
2827mch_get_host_name(
2828 char_u *s,
2829 int len)
2830{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002831 WCHAR wszHostName[256 + 1];
2832 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002834 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002835 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002836 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002837
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002838 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002839 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002840 vim_strncpy(s, p, len - 1);
2841 vim_free(p);
2842 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002843 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847
2848/*
2849 * return process ID
2850 */
2851 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002852mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 return (long)GetCurrentProcessId();
2855}
2856
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002857/*
2858 * return TRUE if process "pid" is still running
2859 */
2860 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002861mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002862{
2863 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2864 DWORD status = 0;
2865 int ret = FALSE;
2866
2867 if (hProcess == NULL)
2868 return FALSE; // might not have access
2869 if (GetExitCodeProcess(hProcess, &status) )
2870 ret = status == STILL_ACTIVE;
2871 CloseHandle(hProcess);
2872 return ret;
2873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875/*
2876 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2877 * Return OK for success, FAIL for failure.
2878 */
2879 int
2880mch_dirname(
2881 char_u *buf,
2882 int len)
2883{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002884 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 /*
2887 * Originally this was:
2888 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2889 * But the Win32s known bug list says that getcwd() doesn't work
2890 * so use the Win32 system call instead. <Negri>
2891 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002892 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002894 WCHAR wcbuf[_MAX_PATH + 1];
2895 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002897 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002899 p = utf16_to_enc(wcbuf, NULL);
2900 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002901 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002902 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002904 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002907 if (p == NULL)
2908 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002909
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002910 if (p != NULL)
2911 {
2912 vim_strncpy(buf, p, len - 1);
2913 vim_free(p);
2914 return OK;
2915 }
2916 }
2917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002921 * Get file permissions for "name".
2922 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 */
2924 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002925mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002927 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002928 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002930 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002931 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932}
2933
2934
2935/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002936 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002937 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002938 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002941mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002943 long n;
2944 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002945
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002946 p = enc_to_utf16(name, NULL);
2947 if (p == NULL)
2948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002950 n = _wchmod(p, perm);
2951 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002952 if (n == -1)
2953 return FAIL;
2954
2955 win32_set_archive(name);
2956
2957 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958}
2959
2960/*
2961 * Set hidden flag for "name".
2962 */
2963 void
2964mch_hide(char_u *name)
2965{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002966 int attrs = win32_getattrs(name);
2967 if (attrs == -1)
2968 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002970 attrs |= FILE_ATTRIBUTE_HIDDEN;
2971 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002975 * Return TRUE if file "name" exists and is hidden.
2976 */
2977 int
2978mch_ishidden(char_u *name)
2979{
2980 int f = win32_getattrs(name);
2981
2982 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002983 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01002984
2985 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
2986}
2987
2988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 * return TRUE if "name" is a directory
2990 * return FALSE if "name" is not a directory or upon error
2991 */
2992 int
2993mch_isdir(char_u *name)
2994{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002995 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
2997 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002998 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3001}
3002
3003/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003004 * return TRUE if "name" is a directory, NOT a symlink to a directory
3005 * return FALSE if "name" is not a directory
3006 * return FALSE for error
3007 */
3008 int
3009mch_isrealdir(char_u *name)
3010{
3011 return mch_isdir(name) && !mch_is_symbolic_link(name);
3012}
3013
3014/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003015 * Create directory "name".
3016 * Return 0 on success, -1 on error.
3017 */
3018 int
3019mch_mkdir(char_u *name)
3020{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003021 WCHAR *p;
3022 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003023
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003024 p = enc_to_utf16(name, NULL);
3025 if (p == NULL)
3026 return -1;
3027 retval = _wmkdir(p);
3028 vim_free(p);
3029 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003030}
3031
3032/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003033 * Delete directory "name".
3034 * Return 0 on success, -1 on error.
3035 */
3036 int
3037mch_rmdir(char_u *name)
3038{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003039 WCHAR *p;
3040 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003041
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003042 p = enc_to_utf16(name, NULL);
3043 if (p == NULL)
3044 return -1;
3045 retval = _wrmdir(p);
3046 vim_free(p);
3047 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003048}
3049
3050/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003051 * Return TRUE if file "fname" has more than one link.
3052 */
3053 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003054mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003055{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003056 BY_HANDLE_FILE_INFORMATION info;
3057
3058 return win32_fileinfo(fname, &info) == FILEINFO_OK
3059 && info.nNumberOfLinks > 1;
3060}
3061
3062/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003063 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003064 */
3065 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003066mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003067{
3068 HANDLE hFind;
3069 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003070 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003071 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003072 WIN32_FIND_DATAW findDataW;
3073
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003074 wn = enc_to_utf16(name, NULL);
3075 if (wn == NULL)
3076 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003077
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003078 hFind = FindFirstFileW(wn, &findDataW);
3079 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003080 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003081 {
3082 fileFlags = findDataW.dwFileAttributes;
3083 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003084 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003085 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003086
3087 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003088 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3089 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003090 res = TRUE;
3091
3092 return res;
3093}
3094
3095/*
3096 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3097 * link.
3098 */
3099 int
3100mch_is_linked(char_u *fname)
3101{
3102 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3103 return TRUE;
3104 return FALSE;
3105}
3106
3107/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003108 * Get the by-handle-file-information for "fname".
3109 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003110 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003111 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3112 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3113 */
3114 int
3115win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3116{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003117 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003118 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003119 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003120
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003121 wn = enc_to_utf16(fname, NULL);
3122 if (wn == NULL)
3123 return FILEINFO_ENC_FAIL;
3124
3125 hFile = CreateFileW(wn, // file name
3126 GENERIC_READ, // access mode
3127 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3128 NULL, // security descriptor
3129 OPEN_EXISTING, // creation disposition
3130 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3131 NULL); // handle to template file
3132 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003133
3134 if (hFile != INVALID_HANDLE_VALUE)
3135 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003136 if (GetFileInformationByHandle(hFile, info) != 0)
3137 res = FILEINFO_OK;
3138 else
3139 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003140 CloseHandle(hFile);
3141 }
3142
Bram Moolenaar03f48552006-02-28 23:52:23 +00003143 return res;
3144}
3145
3146/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003147 * get file attributes for `name'
3148 * -1 : error
3149 * else FILE_ATTRIBUTE_* defined in winnt.h
3150 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003151 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003152win32_getattrs(char_u *name)
3153{
3154 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003155 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003156
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003157 p = enc_to_utf16(name, NULL);
3158 if (p == NULL)
3159 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003160
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003161 attr = GetFileAttributesW(p);
3162 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003163
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003164 return attr;
3165}
3166
3167/*
3168 * set file attributes for `name' to `attrs'
3169 *
3170 * return -1 for failure, 0 otherwise
3171 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003172 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003173win32_setattrs(char_u *name, int attrs)
3174{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003175 int res;
3176 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003177
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003178 p = enc_to_utf16(name, NULL);
3179 if (p == NULL)
3180 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003181
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003182 res = SetFileAttributesW(p, attrs);
3183 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003184
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003185 return res ? 0 : -1;
3186}
3187
3188/*
3189 * Set archive flag for "name".
3190 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003191 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003192win32_set_archive(char_u *name)
3193{
3194 int attrs = win32_getattrs(name);
3195 if (attrs == -1)
3196 return -1;
3197
3198 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3199 return win32_setattrs(name, attrs);
3200}
3201
3202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * Return TRUE if file or directory "name" is writable (not readonly).
3204 * Strange semantics of Win32: a readonly directory is writable, but you can't
3205 * delete a file. Let's say this means it is writable.
3206 */
3207 int
3208mch_writable(char_u *name)
3209{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003210 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003212 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3213 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214}
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003217 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003218 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003219 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3220 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 */
3222 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003223mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
Bram Moolenaar86621892019-03-30 21:51:28 +01003225 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3226 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3227 // UTF-8.
3228 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003229 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003230 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003231
Bram Moolenaar86621892019-03-30 21:51:28 +01003232 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003233 return FALSE;
3234
Bram Moolenaar86621892019-03-30 21:51:28 +01003235 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003236 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003237 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003238 return TRUE;
3239
3240 /*
3241 * Loop over all extensions in $PATHEXT.
3242 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003243 p = mch_getenv("PATHEXT");
3244 if (p == NULL)
3245 p = (char_u *)".com;.exe;.bat;.cmd";
3246 saved = vim_strsave(p);
3247 if (saved == NULL)
3248 return FALSE;
3249 p = saved;
3250 while (*p)
3251 {
3252 char_u *tmp = vim_strchr(p, ';');
3253
3254 if (tmp != NULL)
3255 *tmp = NUL;
3256 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3257 && executable_exists((char *)name, path, use_path))
3258 {
3259 vim_free(saved);
3260 return TRUE;
3261 }
3262 if (tmp == NULL)
3263 break;
3264 p = tmp + 1;
3265 }
3266 vim_free(saved);
3267
Bram Moolenaar86621892019-03-30 21:51:28 +01003268 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003269 p = mch_getenv("PATHEXT");
3270 if (p == NULL)
3271 p = (char_u *)".com;.exe;.bat;.cmd";
3272 while (*p)
3273 {
3274 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3275 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003276 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003277 buf[len] = NUL;
3278 ++p;
3279 if (*p)
3280 ++p;
3281 }
3282 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003283 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003284 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003285 return TRUE;
3286 }
3287 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290/*
3291 * Check what "name" is:
3292 * NODE_NORMAL: file or directory (or doesn't exist)
3293 * NODE_WRITABLE: writable device, socket, fifo, etc.
3294 * NODE_OTHER: non-writable things
3295 */
3296 int
3297mch_nodetype(char_u *name)
3298{
3299 HANDLE hFile;
3300 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003301 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
Bram Moolenaar0f873732019-12-05 20:28:46 +01003303 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3304 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3305 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003306 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3307 return NODE_WRITABLE;
3308
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003309 wn = enc_to_utf16(name, NULL);
3310 if (wn == NULL)
3311 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003312
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003313 hFile = CreateFileW(wn, // file name
3314 GENERIC_WRITE, // access mode
3315 0, // share mode
3316 NULL, // security descriptor
3317 OPEN_EXISTING, // creation disposition
3318 0, // file attributes
3319 NULL); // handle to template file
3320 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (hFile == INVALID_HANDLE_VALUE)
3322 return NODE_NORMAL;
3323
3324 type = GetFileType(hFile);
3325 CloseHandle(hFile);
3326 if (type == FILE_TYPE_CHAR)
3327 return NODE_WRITABLE;
3328 if (type == FILE_TYPE_DISK)
3329 return NODE_NORMAL;
3330 return NODE_OTHER;
3331}
3332
3333#ifdef HAVE_ACL
3334struct my_acl
3335{
3336 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3337 PSID pSidOwner;
3338 PSID pSidGroup;
3339 PACL pDacl;
3340 PACL pSacl;
3341};
3342#endif
3343
3344/*
3345 * Return a pointer to the ACL of file "fname" in allocated memory.
3346 * Return NULL if the ACL is not available for whatever reason.
3347 */
3348 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003349mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351#ifndef HAVE_ACL
3352 return (vim_acl_T)NULL;
3353#else
3354 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003355 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003357 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003358 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003360 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003361
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003362 wn = enc_to_utf16(fname, NULL);
3363 if (wn == NULL)
3364 return NULL;
3365
3366 // Try to retrieve the entire security descriptor.
3367 err = GetNamedSecurityInfoW(
3368 wn, // Abstract filename
3369 SE_FILE_OBJECT, // File Object
3370 OWNER_SECURITY_INFORMATION |
3371 GROUP_SECURITY_INFORMATION |
3372 DACL_SECURITY_INFORMATION |
3373 SACL_SECURITY_INFORMATION,
3374 &p->pSidOwner, // Ownership information.
3375 &p->pSidGroup, // Group membership.
3376 &p->pDacl, // Discretionary information.
3377 &p->pSacl, // For auditing purposes.
3378 &p->pSecurityDescriptor);
3379 if (err == ERROR_ACCESS_DENIED ||
3380 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003381 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003382 // Retrieve only DACL.
3383 (void)GetNamedSecurityInfoW(
3384 wn,
3385 SE_FILE_OBJECT,
3386 DACL_SECURITY_INFORMATION,
3387 NULL,
3388 NULL,
3389 &p->pDacl,
3390 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003391 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003392 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003393 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003394 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003395 mch_free_acl((vim_acl_T)p);
3396 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003398 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400
3401 return (vim_acl_T)p;
3402#endif
3403}
3404
Bram Moolenaar27515922013-06-29 15:36:26 +02003405#ifdef HAVE_ACL
3406/*
3407 * Check if "acl" contains inherited ACE.
3408 */
3409 static BOOL
3410is_acl_inherited(PACL acl)
3411{
3412 DWORD i;
3413 ACL_SIZE_INFORMATION acl_info;
3414 PACCESS_ALLOWED_ACE ace;
3415
3416 acl_info.AceCount = 0;
3417 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3418 for (i = 0; i < acl_info.AceCount; i++)
3419 {
3420 GetAce(acl, i, (LPVOID *)&ace);
3421 if (ace->Header.AceFlags & INHERITED_ACE)
3422 return TRUE;
3423 }
3424 return FALSE;
3425}
3426#endif
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428/*
3429 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3430 * Errors are ignored.
3431 * This must only be called with "acl" equal to what mch_get_acl() returned.
3432 */
3433 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003434mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436#ifdef HAVE_ACL
3437 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003438 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003439 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003441 if (p == NULL)
3442 return;
3443
3444 wn = enc_to_utf16(fname, NULL);
3445 if (wn == NULL)
3446 return;
3447
3448 // Set security flags
3449 if (p->pSidOwner)
3450 sec_info |= OWNER_SECURITY_INFORMATION;
3451 if (p->pSidGroup)
3452 sec_info |= GROUP_SECURITY_INFORMATION;
3453 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003454 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003455 sec_info |= DACL_SECURITY_INFORMATION;
3456 // Do not inherit its parent's DACL.
3457 // If the DACL is inherited, Cygwin permissions would be changed.
3458 if (!is_acl_inherited(p->pDacl))
3459 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003460 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003461 if (p->pSacl)
3462 sec_info |= SACL_SECURITY_INFORMATION;
3463
3464 (void)SetNamedSecurityInfoW(
3465 wn, // Abstract filename
3466 SE_FILE_OBJECT, // File Object
3467 sec_info,
3468 p->pSidOwner, // Ownership information.
3469 p->pSidGroup, // Group membership.
3470 p->pDacl, // Discretionary information.
3471 p->pSacl // For auditing purposes.
3472 );
3473 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#endif
3475}
3476
3477 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003478mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480#ifdef HAVE_ACL
3481 struct my_acl *p = (struct my_acl *)acl;
3482
3483 if (p != NULL)
3484 {
3485 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3486 vim_free(p);
3487 }
3488#endif
3489}
3490
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003491#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493/*
3494 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3495 */
3496 static BOOL WINAPI
3497handler_routine(
3498 DWORD dwCtrlType)
3499{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003500 INPUT_RECORD ir;
3501 DWORD out;
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 switch (dwCtrlType)
3504 {
3505 case CTRL_C_EVENT:
3506 if (ctrl_c_interrupts)
3507 g_fCtrlCPressed = TRUE;
3508 return TRUE;
3509
3510 case CTRL_BREAK_EVENT:
3511 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003512 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003513 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003514 ir.EventType = KEY_EVENT;
3515 ir.Event.KeyEvent.bKeyDown = TRUE;
3516 ir.Event.KeyEvent.wRepeatCount = 1;
3517 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3518 ir.Event.KeyEvent.wVirtualScanCode = 0;
3519 ir.Event.KeyEvent.dwControlKeyState = 0;
3520 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3521 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return TRUE;
3523
Bram Moolenaar0f873732019-12-05 20:28:46 +01003524 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 case CTRL_CLOSE_EVENT:
3526 case CTRL_LOGOFF_EVENT:
3527 case CTRL_SHUTDOWN_EVENT:
3528 windgoto((int)Rows - 1, 0);
3529 g_fForceExit = TRUE;
3530
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003531 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 (dwCtrlType == CTRL_CLOSE_EVENT
3533 ? _("close")
3534 : dwCtrlType == CTRL_LOGOFF_EVENT
3535 ? _("logoff")
3536 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003537# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar0f873732019-12-05 20:28:46 +01003541 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar0f873732019-12-05 20:28:46 +01003543 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545 default:
3546 return FALSE;
3547 }
3548}
3549
3550
3551/*
3552 * set the tty in (raw) ? "raw" : "cooked" mode
3553 */
3554 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003555mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 DWORD cmodein;
3558 DWORD cmodeout;
3559 BOOL bEnableHandler;
3560
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003561# ifdef VIMDLL
3562 if (gui.in_use)
3563 return;
3564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 GetConsoleMode(g_hConIn, &cmodein);
3566 GetConsoleMode(g_hConOut, &cmodeout);
3567 if (tmode == TMODE_RAW)
3568 {
3569 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3570 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (g_fMouseActive)
3572 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003573 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003574# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003575 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3576 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003577 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003578# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003579 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003580# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003581 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 bEnableHandler = TRUE;
3583 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003584 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3587 ENABLE_ECHO_INPUT);
3588 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3589 bEnableHandler = FALSE;
3590 }
3591 SetConsoleMode(g_hConIn, cmodein);
3592 SetConsoleMode(g_hConOut, cmodeout);
3593 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3594
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003595# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (fdDump)
3597 {
3598 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3599 tmode == TMODE_RAW ? "raw" :
3600 tmode == TMODE_COOK ? "cooked" : "normal",
3601 cmodein, cmodeout);
3602 fflush(fdDump);
3603 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606
3607
3608/*
3609 * Get the size of the current window in `Rows' and `Columns'
3610 * Return OK when size could be determined, FAIL otherwise.
3611 */
3612 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003613mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 CONSOLE_SCREEN_BUFFER_INFO csbi;
3616
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003617# ifdef VIMDLL
3618 if (gui.in_use)
3619 return OK;
3620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3622 {
3623 /*
3624 * For some reason, we are trying to get the screen dimensions
3625 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3626 * variables are really intended to mean the size of Vim screen
3627 * while in termcap mode.
3628 */
3629 Rows = g_cbTermcap.Info.dwSize.Y;
3630 Columns = g_cbTermcap.Info.dwSize.X;
3631 }
3632 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3633 {
3634 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3635 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3636 }
3637 else
3638 {
3639 Rows = 25;
3640 Columns = 80;
3641 }
3642 return OK;
3643}
3644
3645/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003646 * Resize console buffer to 'COORD'
3647 */
3648 static void
3649ResizeConBuf(
3650 HANDLE hConsole,
3651 COORD coordScreen)
3652{
3653 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3654 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003655# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003656 if (fdDump)
3657 {
3658 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3659 GetLastError());
3660 fflush(fdDump);
3661 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003662# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003663 }
3664}
3665
3666/*
3667 * Resize console window size to 'srWindowRect'
3668 */
3669 static void
3670ResizeWindow(
3671 HANDLE hConsole,
3672 SMALL_RECT srWindowRect)
3673{
3674 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3675 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003676# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003677 if (fdDump)
3678 {
3679 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3680 GetLastError());
3681 fflush(fdDump);
3682 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003683# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003684 }
3685}
3686
3687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 * Set a console window to `xSize' * `ySize'
3689 */
3690 static void
3691ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003692 HANDLE hConsole,
3693 int xSize,
3694 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003696 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3697 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003699 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003700 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003702# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (fdDump)
3704 {
3705 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3706 fflush(fdDump);
3707 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
Bram Moolenaar0f873732019-12-05 20:28:46 +01003710 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 coordScreen = GetLargestConsoleWindowSize(hConsole);
3712
Bram Moolenaar0f873732019-12-05 20:28:46 +01003713 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3715 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3716 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3717
3718 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3719 {
3720 int sx, sy;
3721
3722 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3723 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3724 if (sy < ySize || sx < xSize)
3725 {
3726 /*
3727 * Increasing number of lines/columns, do buffer first.
3728 * Use the maximal size in x and y direction.
3729 */
3730 if (sy < ySize)
3731 coordScreen.Y = ySize;
3732 else
3733 coordScreen.Y = sy;
3734 if (sx < xSize)
3735 coordScreen.X = xSize;
3736 else
3737 coordScreen.X = sx;
3738 SetConsoleScreenBufferSize(hConsole, coordScreen);
3739 }
3740 }
3741
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003742 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 coordScreen.X = xSize;
3744 coordScreen.Y = ySize;
3745
Bram Moolenaar2551c032018-08-23 22:38:31 +02003746 // In the new console call API, only the first time in reverse order
3747 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003749 ResizeWindow(hConsole, srWindowRect);
3750 ResizeConBuf(hConsole, coordScreen);
3751 }
3752 else
3753 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003754 // Workaround for a Windows 10 bug
3755 cursor.X = srWindowRect.Left;
3756 cursor.Y = srWindowRect.Top;
3757 SetConsoleCursorPosition(hConsole, cursor);
3758
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003759 ResizeConBuf(hConsole, coordScreen);
3760 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003761 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763}
3764
3765
3766/*
3767 * Set the console window to `Rows' * `Columns'
3768 */
3769 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003770mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 COORD coordScreen;
3773
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003774# ifdef VIMDLL
3775 if (gui.in_use)
3776 return;
3777# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003778 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (suppress_winsize != 0)
3780 {
3781 suppress_winsize = 2;
3782 return;
3783 }
3784
3785 if (term_console)
3786 {
3787 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3788
Bram Moolenaar0f873732019-12-05 20:28:46 +01003789 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (Rows > coordScreen.Y)
3791 Rows = coordScreen.Y;
3792 if (Columns > coordScreen.X)
3793 Columns = coordScreen.X;
3794
3795 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3796 }
3797}
3798
3799/*
3800 * Rows and/or Columns has changed.
3801 */
3802 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003803mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003805# ifdef VIMDLL
3806 if (gui.in_use)
3807 return;
3808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3810}
3811
3812
3813/*
3814 * Called when started up, to set the winsize that was delayed.
3815 */
3816 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003817mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 if (suppress_winsize == 2)
3820 {
3821 suppress_winsize = 0;
3822 mch_set_shellsize();
3823 shell_resized();
3824 }
3825 suppress_winsize = 0;
3826}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003827#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003829 static BOOL
3830vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003831 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003832 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003833 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003834 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003835 PROCESS_INFORMATION *pi,
3836 LPVOID *env,
3837 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003838{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003839 BOOL ret = FALSE;
3840 WCHAR *wcmd, *wcwd = NULL;
3841
3842 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3843 if (wcmd == NULL)
3844 return FALSE;
3845 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003846 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003847 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3848 if (wcwd == NULL)
3849 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003850 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003851
3852 ret = CreateProcessW(
3853 NULL, // Executable name
3854 wcmd, // Command to execute
3855 NULL, // Process security attributes
3856 NULL, // Thread security attributes
3857 inherit_handles, // Inherit handles
3858 flags, // Creation flags
3859 env, // Environment
3860 wcwd, // Current directory
3861 (LPSTARTUPINFOW)si, // Startup information
3862 pi); // Process information
3863theend:
3864 vim_free(wcmd);
3865 vim_free(wcwd);
3866 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
3869
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003870 static HINSTANCE
3871vim_shell_execute(
3872 char *cmd,
3873 INT n_show_cmd)
3874{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003875 HINSTANCE ret;
3876 WCHAR *wcmd;
3877
3878 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3879 if (wcmd == NULL)
3880 return (HINSTANCE) 0;
3881
3882 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3883 vim_free(wcmd);
3884 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003885}
3886
3887
Bram Moolenaar4f974752019-02-17 17:44:42 +01003888#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890/*
3891 * Specialised version of system() for Win32 GUI mode.
3892 * This version proceeds as follows:
3893 * 1. Create a console window for use by the subprocess
3894 * 2. Run the subprocess (it gets the allocated console by default)
3895 * 3. Wait for the subprocess to terminate and get its exit code
3896 * 4. Prompt the user to press a key to close the console window
3897 */
3898 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003899mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 STARTUPINFO si;
3902 PROCESS_INFORMATION pi;
3903 DWORD ret = 0;
3904 HWND hwnd = GetFocus();
3905
3906 si.cb = sizeof(si);
3907 si.lpReserved = NULL;
3908 si.lpDesktop = NULL;
3909 si.lpTitle = NULL;
3910 si.dwFlags = STARTF_USESHOWWINDOW;
3911 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003912 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003913 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003915 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003916 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 else
3918 si.wShowWindow = SW_SHOWNORMAL;
3919 si.cbReserved2 = 0;
3920 si.lpReserved2 = NULL;
3921
Bram Moolenaar0f873732019-12-05 20:28:46 +01003922 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003923 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003924 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3925 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar0f873732019-12-05 20:28:46 +01003927 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003929# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 int delay = 1;
3931
Bram Moolenaar0f873732019-12-05 20:28:46 +01003932 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 for (;;)
3934 {
3935 MSG msg;
3936
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003937 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
3939 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003940 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003941 delay = 1;
3942 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3945 break;
3946
Bram Moolenaar0f873732019-12-05 20:28:46 +01003947 // We start waiting for a very short time and then increase it, so
3948 // that we respond quickly when the process is quick, and don't
3949 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (delay < 50)
3951 delay += 10;
3952 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003953# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaar0f873732019-12-05 20:28:46 +01003957 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 GetExitCodeProcess(pi.hProcess, &ret);
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
Bram Moolenaar0f873732019-12-05 20:28:46 +01003961 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 CloseHandle(pi.hThread);
3963 CloseHandle(pi.hProcess);
3964
Bram Moolenaar0f873732019-12-05 20:28:46 +01003965 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
3967
3968 return ret;
3969}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003970
3971/*
3972 * Thread launched by the gui to send the current buffer data to the
3973 * process. This way avoid to hang up vim totally if the children
3974 * process take a long time to process the lines.
3975 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02003976 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003977sub_process_writer(LPVOID param)
3978{
3979 HANDLE g_hChildStd_IN_Wr = param;
3980 linenr_T lnum = curbuf->b_op_start.lnum;
3981 DWORD len = 0;
3982 DWORD l;
3983 char_u *lp = ml_get(lnum);
3984 char_u *s;
3985 int written = 0;
3986
3987 for (;;)
3988 {
3989 l = (DWORD)STRLEN(lp + written);
3990 if (l == 0)
3991 len = 0;
3992 else if (lp[written] == NL)
3993 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003995 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
3996 }
3997 else
3998 {
3999 s = vim_strchr(lp + written, NL);
4000 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4001 s == NULL ? l : (DWORD)(s - (lp + written)),
4002 &len, NULL);
4003 }
4004 if (len == (int)l)
4005 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004006 // Finished a line, add a NL, unless this line should not have
4007 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004008 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004009 || (!curbuf->b_p_bin
4010 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004011 || (lnum != curbuf->b_no_eol_lnum
4012 && (lnum != curbuf->b_ml.ml_line_count
4013 || curbuf->b_p_eol)))
4014 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004015 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4016 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004017 }
4018
4019 ++lnum;
4020 if (lnum > curbuf->b_op_end.lnum)
4021 break;
4022
4023 lp = ml_get(lnum);
4024 written = 0;
4025 }
4026 else if (len > 0)
4027 written += len;
4028 }
4029
Bram Moolenaar0f873732019-12-05 20:28:46 +01004030 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004031 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004032 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004033}
4034
4035
Bram Moolenaar0f873732019-12-05 20:28:46 +01004036# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004037
4038/*
4039 * This function read from the children's stdout and write the
4040 * data on screen or in the buffer accordingly.
4041 */
4042 static void
4043dump_pipe(int options,
4044 HANDLE g_hChildStd_OUT_Rd,
4045 garray_T *ga,
4046 char_u buffer[],
4047 DWORD *buffer_off)
4048{
4049 DWORD availableBytes = 0;
4050 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004051 int ret;
4052 DWORD len;
4053 DWORD toRead;
4054 int repeatCount;
4055
Bram Moolenaar0f873732019-12-05 20:28:46 +01004056 // we query the pipe to see if there is any data to read
4057 // to avoid to perform a blocking read
4058 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4059 NULL, // optional buffer
4060 0, // buffer size
4061 NULL, // number of read bytes
4062 &availableBytes, // available bytes total
4063 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004064
4065 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004066 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004067 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004068 {
4069 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004070 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004071 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004072 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004073
Bram Moolenaar0f873732019-12-05 20:28:46 +01004074 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004075 if (len == 0)
4076 break;
4077
4078 availableBytes -= len;
4079
4080 if (options & SHELL_READ)
4081 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004082 // Do NUL -> NL translation, append NL separated
4083 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004084 for (i = 0; i < len; ++i)
4085 {
4086 if (buffer[i] == NL)
4087 append_ga_line(ga);
4088 else if (buffer[i] == NUL)
4089 ga_append(ga, NL);
4090 else
4091 ga_append(ga, buffer[i]);
4092 }
4093 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004094 else if (has_mbyte)
4095 {
4096 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004097 int c;
4098 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004099
4100 len += *buffer_off;
4101 buffer[len] = NUL;
4102
Bram Moolenaar0f873732019-12-05 20:28:46 +01004103 // Check if the last character in buffer[] is
4104 // incomplete, keep these bytes for the next
4105 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004106 for (p = buffer; p < buffer + len; p += l)
4107 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004108 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004109 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004110 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004111 else if (MB_BYTE2LEN(*p) != l)
4112 break;
4113 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004114 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004115 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004116 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004117 if (len >= 12)
4118 ++p;
4119 else
4120 {
4121 *buffer_off = len;
4122 return;
4123 }
4124 }
4125 c = *p;
4126 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004128 if (p < buffer + len)
4129 {
4130 *p = c;
4131 *buffer_off = (DWORD)((buffer + len) - p);
4132 mch_memmove(buffer, p, *buffer_off);
4133 return;
4134 }
4135 *buffer_off = 0;
4136 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004137 else
4138 {
4139 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004140 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004141 }
4142
4143 windgoto(msg_row, msg_col);
4144 cursor_on();
4145 out_flush();
4146 }
4147}
4148
4149/*
4150 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4151 * for communication and doesn't open any new window.
4152 */
4153 static int
4154mch_system_piped(char *cmd, int options)
4155{
4156 STARTUPINFO si;
4157 PROCESS_INFORMATION pi;
4158 DWORD ret = 0;
4159
4160 HANDLE g_hChildStd_IN_Rd = NULL;
4161 HANDLE g_hChildStd_IN_Wr = NULL;
4162 HANDLE g_hChildStd_OUT_Rd = NULL;
4163 HANDLE g_hChildStd_OUT_Wr = NULL;
4164
Bram Moolenaar0f873732019-12-05 20:28:46 +01004165 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004166 DWORD len;
4167
Bram Moolenaar0f873732019-12-05 20:28:46 +01004168 // buffer used to receive keys
4169 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4170 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004171
4172 DWORD i;
4173 int c;
4174 int noread_cnt = 0;
4175 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004176 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004177 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004178 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004179
4180 SECURITY_ATTRIBUTES saAttr;
4181
Bram Moolenaar0f873732019-12-05 20:28:46 +01004182 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004183 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4184 saAttr.bInheritHandle = TRUE;
4185 saAttr.lpSecurityDescriptor = NULL;
4186
4187 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004188 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004189 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004190 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004191 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004192 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004193 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004194 {
4195 CloseHandle(g_hChildStd_IN_Rd);
4196 CloseHandle(g_hChildStd_IN_Wr);
4197 CloseHandle(g_hChildStd_OUT_Rd);
4198 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004199 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004200 }
4201
4202 si.cb = sizeof(si);
4203 si.lpReserved = NULL;
4204 si.lpDesktop = NULL;
4205 si.lpTitle = NULL;
4206 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4207
Bram Moolenaar0f873732019-12-05 20:28:46 +01004208 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004209 si.hStdError = g_hChildStd_OUT_Wr;
4210 si.hStdOutput = g_hChildStd_OUT_Wr;
4211 si.hStdInput = g_hChildStd_IN_Rd;
4212 si.wShowWindow = SW_HIDE;
4213 si.cbReserved2 = 0;
4214 si.lpReserved2 = NULL;
4215
4216 if (options & SHELL_READ)
4217 ga_init2(&ga, 1, BUFLEN);
4218
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004219 if (cmd != NULL)
4220 {
4221 p = (char *)vim_strsave((char_u *)cmd);
4222 if (p != NULL)
4223 unescape_shellxquote((char_u *)p, p_sxe);
4224 else
4225 p = cmd;
4226 }
4227
Bram Moolenaar0f873732019-12-05 20:28:46 +01004228 // Now, run the command.
4229 // About "Inherit handles" being TRUE: this command can be litigious,
4230 // handle inheritance was deactivated for pending temp file, but, if we
4231 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004232 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4233 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004234
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004235 if (p != cmd)
4236 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004237
Bram Moolenaar0f873732019-12-05 20:28:46 +01004238 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004239 CloseHandle(g_hChildStd_IN_Rd);
4240 CloseHandle(g_hChildStd_OUT_Wr);
4241
4242 if (options & SHELL_WRITE)
4243 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004244 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004245 _beginthreadex(NULL, // security attributes
4246 0, // default stack size
4247 sub_process_writer, // function to be executed
4248 g_hChildStd_IN_Wr, // parameter
4249 0, // creation flag, start immediately
4250 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004251 CloseHandle(thread);
4252 g_hChildStd_IN_Wr = NULL;
4253 }
4254
Bram Moolenaar0f873732019-12-05 20:28:46 +01004255 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004256 for (;;)
4257 {
4258 MSG msg;
4259
Bram Moolenaar175d0702013-12-11 18:36:33 +01004260 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004261 {
4262 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004263 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004264 }
4265
Bram Moolenaar0f873732019-12-05 20:28:46 +01004266 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004267 if ((options & (SHELL_READ|SHELL_WRITE))
4268# ifdef FEAT_GUI
4269 || gui.in_use
4270# endif
4271 )
4272 {
4273 len = 0;
4274 if (!(options & SHELL_EXPAND)
4275 && ((options &
4276 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4277 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4278# ifdef FEAT_GUI
4279 || gui.in_use
4280# endif
4281 )
4282 && (ta_len > 0 || noread_cnt > 4))
4283 {
4284 if (ta_len == 0)
4285 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004286 // Get extra characters when we don't have any. Reset the
4287 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004288 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004289 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4290 }
4291 if (ta_len > 0 || len > 0)
4292 {
4293 /*
4294 * For pipes: Check for CTRL-C: send interrupt signal to
4295 * child. Check for CTRL-D: EOF, close pipe to child.
4296 */
4297 if (len == 1 && cmd != NULL)
4298 {
4299 if (ta_buf[ta_len] == Ctrl_C)
4300 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004301 // Learn what exit code is expected, for
4302 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004303 TerminateProcess(pi.hProcess, 9);
4304 }
4305 if (ta_buf[ta_len] == Ctrl_D)
4306 {
4307 CloseHandle(g_hChildStd_IN_Wr);
4308 g_hChildStd_IN_Wr = NULL;
4309 }
4310 }
4311
Bram Moolenaar0f873732019-12-05 20:28:46 +01004312 // replace K_BS by <BS> and K_DEL by <DEL>
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004313 for (i = ta_len; i < ta_len + len; ++i)
4314 {
4315 if (ta_buf[i] == CSI && len - i > 2)
4316 {
4317 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4318 if (c == K_DEL || c == K_KDEL || c == K_BS)
4319 {
4320 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4321 (size_t)(len - i - 2));
4322 if (c == K_DEL || c == K_KDEL)
4323 ta_buf[i] = DEL;
4324 else
4325 ta_buf[i] = Ctrl_H;
4326 len -= 2;
4327 }
4328 }
4329 else if (ta_buf[i] == '\r')
4330 ta_buf[i] = '\n';
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004331 if (has_mbyte)
4332 i += (*mb_ptr2len_len)(ta_buf + i,
4333 ta_len + len - i) - 1;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004334 }
4335
4336 /*
4337 * For pipes: echo the typed characters. For a pty this
4338 * does not seem to work.
4339 */
4340 for (i = ta_len; i < ta_len + len; ++i)
4341 {
4342 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4343 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004344 else if (has_mbyte)
4345 {
4346 int l = (*mb_ptr2len)(ta_buf + i);
4347
4348 msg_outtrans_len(ta_buf + i, l);
4349 i += l - 1;
4350 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004351 else
4352 msg_outtrans_len(ta_buf + i, 1);
4353 }
4354 windgoto(msg_row, msg_col);
4355 out_flush();
4356
4357 ta_len += len;
4358
4359 /*
4360 * Write the characters to the child, unless EOF has been
4361 * typed for pipes. Write one character at a time, to
4362 * avoid losing too much typeahead. When writing buffer
4363 * lines, drop the typed characters (only check for
4364 * CTRL-C).
4365 */
4366 if (options & SHELL_WRITE)
4367 ta_len = 0;
4368 else if (g_hChildStd_IN_Wr != NULL)
4369 {
4370 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4371 1, &len, NULL);
4372 // if we are typing in, we want to keep things reactive
4373 delay = 1;
4374 if (len > 0)
4375 {
4376 ta_len -= len;
4377 mch_memmove(ta_buf, ta_buf + len, ta_len);
4378 }
4379 }
4380 }
4381 }
4382 }
4383
4384 if (ta_len)
4385 ui_inchar_undo(ta_buf, ta_len);
4386
4387 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4388 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004389 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004390 break;
4391 }
4392
4393 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004394 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004395
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396 // We start waiting for a very short time and then increase it, so
4397 // that we respond quickly when the process is quick, and don't
4398 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004399 if (delay < 50)
4400 delay += 10;
4401 }
4402
Bram Moolenaar0f873732019-12-05 20:28:46 +01004403 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004404 CloseHandle(g_hChildStd_OUT_Rd);
4405 if (g_hChildStd_IN_Wr != NULL)
4406 CloseHandle(g_hChildStd_IN_Wr);
4407
4408 WaitForSingleObject(pi.hProcess, INFINITE);
4409
Bram Moolenaar0f873732019-12-05 20:28:46 +01004410 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004411 GetExitCodeProcess(pi.hProcess, &ret);
4412
4413 if (options & SHELL_READ)
4414 {
4415 if (ga.ga_len > 0)
4416 {
4417 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004418 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004419 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4420 }
4421 else
4422 curbuf->b_no_eol_lnum = 0;
4423 ga_clear(&ga);
4424 }
4425
Bram Moolenaar0f873732019-12-05 20:28:46 +01004426 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004427 CloseHandle(pi.hThread);
4428 CloseHandle(pi.hProcess);
4429
4430 return ret;
4431}
4432
4433 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004434mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004435{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004436 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004437 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004438 return mch_system_piped(cmd, options);
4439 else
4440 return mch_system_classic(cmd, options);
4441}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004444#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004445 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004446mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004447{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004448 int ret;
4449 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004450 char_u *buf;
4451 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004452
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004453 // If the command starts and ends with double quotes, enclose the command
4454 // in parentheses.
4455 len = STRLEN(cmd);
4456 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4457 {
4458 len += 3;
4459 buf = alloc(len);
4460 if (buf == NULL)
4461 return -1;
4462 vim_snprintf((char *)buf, len, "(%s)", cmd);
4463 wcmd = enc_to_utf16(buf, NULL);
4464 free(buf);
4465 }
4466 else
4467 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4468
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004469 if (wcmd == NULL)
4470 return -1;
4471
4472 ret = _wsystem(wcmd);
4473 vim_free(wcmd);
4474 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477#endif
4478
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004479 static int
4480mch_system(char *cmd, int options)
4481{
4482#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004483 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004484 return mch_system_g(cmd, options);
4485 else
4486 return mch_system_c(cmd, options);
4487#elif defined(FEAT_GUI_MSWIN)
4488 return mch_system_g(cmd, options);
4489#else
4490 return mch_system_c(cmd, options);
4491#endif
4492}
4493
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004494#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4495/*
4496 * Use a terminal window to run a shell command in.
4497 */
4498 static int
4499mch_call_shell_terminal(
4500 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004501 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004502{
4503 jobopt_T opt;
4504 char_u *newcmd = NULL;
4505 typval_T argvar[2];
4506 long_u cmdlen;
4507 int retval = -1;
4508 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004509 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004510 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004512
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004513 if (cmd == NULL)
4514 cmdlen = STRLEN(p_sh) + 1;
4515 else
4516 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004517 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004518 if (newcmd == NULL)
4519 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004520 if (cmd == NULL)
4521 {
4522 STRCPY(newcmd, p_sh);
4523 ch_log(NULL, "starting terminal to run a shell");
4524 }
4525 else
4526 {
4527 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4528 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4529 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004530
4531 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004532
4533 argvar[0].v_type = VAR_STRING;
4534 argvar[0].vval.v_string = newcmd;
4535 argvar[1].v_type = VAR_UNKNOWN;
4536 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004537 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004538 {
4539 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004540 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004541 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004542
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004543 job = term_getjob(buf->b_term);
4544 ++job->jv_refcount;
4545
Bram Moolenaar0f873732019-12-05 20:28:46 +01004546 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004547 aucmd_prepbuf(&aco, buf);
4548
4549 clear_oparg(&oa);
4550 while (term_use_loop())
4551 {
4552 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4553 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004554 // If terminal_loop() returns OK we got a key that is handled
4555 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004556 if (terminal_loop(TRUE) == OK)
4557 normal_cmd(&oa, TRUE);
4558 }
4559 else
4560 normal_cmd(&oa, TRUE);
4561 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004562 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004563 ch_log(NULL, "system command finished");
4564
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004565 job_unref(job);
4566
Bram Moolenaar0f873732019-12-05 20:28:46 +01004567 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004568 aucmd_restbuf(&aco);
4569
4570 wait_return(TRUE);
4571 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4572
4573 vim_free(newcmd);
4574 return retval;
4575}
4576#endif
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578/*
4579 * Either execute a command by calling the shell or start a new shell
4580 */
4581 int
4582mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004583 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004584 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 int x = 0;
4587 int tmode = cur_tmode;
4588#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004589 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004590
Bram Moolenaar0f873732019-12-05 20:28:46 +01004591 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004592 if (GetConsoleTitleW(szShellTitle,
4593 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004594 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004595 if (cmd == NULL)
4596 wcscat(szShellTitle, L" :sh");
4597 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004598 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004599 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004600
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004601 if (wn != NULL)
4602 {
4603 wcscat(szShellTitle, L" - !");
4604 if ((wcslen(szShellTitle) + wcslen(wn) <
4605 sizeof(szShellTitle)/sizeof(WCHAR)))
4606 wcscat(szShellTitle, wn);
4607 SetConsoleTitleW(szShellTitle);
4608 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004609 }
4610 }
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#endif
4613
4614 out_flush();
4615
4616#ifdef MCH_WRITE_DUMP
4617 if (fdDump)
4618 {
4619 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4620 fflush(fdDump);
4621 }
4622#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004623#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004624 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004625 if (
4626# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004627 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004628# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004629 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004630 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4631 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004632 char_u *cmdbase = cmd;
4633
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004634 if (cmdbase != NULL)
4635 // Skip a leading quote and (.
4636 while (*cmdbase == '"' || *cmdbase == '(')
4637 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004638
4639 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004640 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4641 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004642 {
4643 // Use a terminal window to run the command in.
4644 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004645# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004646 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004647# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004648 return x;
4649 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004650 }
4651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653 /*
4654 * Catch all deadly signals while running the external command, because a
4655 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4656 */
4657 signal(SIGINT, SIG_IGN);
4658#if defined(__GNUC__) && !defined(__MINGW32__)
4659 signal(SIGKILL, SIG_IGN);
4660#else
4661 signal(SIGBREAK, SIG_IGN);
4662#endif
4663 signal(SIGILL, SIG_IGN);
4664 signal(SIGFPE, SIG_IGN);
4665 signal(SIGSEGV, SIG_IGN);
4666 signal(SIGTERM, SIG_IGN);
4667 signal(SIGABRT, SIG_IGN);
4668
4669 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004670 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 if (cmd == NULL)
4673 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004674 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
4676 else
4677 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004678 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004679 char_u *newcmd = NULL;
4680 char_u *cmdbase = cmd;
4681 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004682
Bram Moolenaar0f873732019-12-05 20:28:46 +01004683 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004684 if (*cmdbase == '"' )
4685 ++cmdbase;
4686 if (*cmdbase == '(')
4687 ++cmdbase;
4688
Bram Moolenaar1c465442017-03-12 20:10:05 +01004689 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004690 {
4691 STARTUPINFO si;
4692 PROCESS_INFORMATION pi;
4693 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004694 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004695 char_u *p;
4696
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004697 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004698 si.cb = sizeof(si);
4699 si.lpReserved = NULL;
4700 si.lpDesktop = NULL;
4701 si.lpTitle = NULL;
4702 si.dwFlags = 0;
4703 si.cbReserved2 = 0;
4704 si.lpReserved2 = NULL;
4705
4706 cmdbase = skipwhite(cmdbase + 5);
4707 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004708 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004709 {
4710 cmdbase = skipwhite(cmdbase + 4);
4711 si.dwFlags = STARTF_USESHOWWINDOW;
4712 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004713 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004714 }
4715 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004716 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004717 {
4718 cmdbase = skipwhite(cmdbase + 2);
4719 flags = CREATE_NO_WINDOW;
4720 si.dwFlags = STARTF_USESTDHANDLES;
4721 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004722 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004723 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004724 NULL, // Security att.
4725 OPEN_EXISTING, // Open flags
4726 FILE_ATTRIBUTE_NORMAL, // File att.
4727 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004728 si.hStdOutput = si.hStdInput;
4729 si.hStdError = si.hStdInput;
4730 }
4731
Bram Moolenaar0f873732019-12-05 20:28:46 +01004732 // Remove a trailing ", ) and )" if they have a match
4733 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004734 if (cmdbase > cmd)
4735 {
4736 p = cmdbase + STRLEN(cmdbase);
4737 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4738 *--p = NUL;
4739 if (p > cmdbase && p[-1] == ')'
4740 && (*cmd =='(' || cmd[1] == '('))
4741 *--p = NUL;
4742 }
4743
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004744 newcmd = cmdbase;
4745 unescape_shellxquote(cmdbase, p_sxe);
4746
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004747 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004748 * If creating new console, arguments are passed to the
4749 * 'cmd.exe' as-is. If it's not, arguments are not treated
4750 * correctly for current 'cmd.exe'. So unescape characters in
4751 * shellxescape except '|' for avoiding to be treated as
4752 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004753 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004754 if (flags != CREATE_NEW_CONSOLE)
4755 {
4756 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004757 char_u *cmd_shell = mch_getenv("COMSPEC");
4758
4759 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004760 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004761
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004762 subcmd = vim_strsave_escaped_ext(cmdbase,
4763 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004764 if (subcmd != NULL)
4765 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004766 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004767 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004768 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004769 if (newcmd != NULL)
4770 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004771 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004772 else
4773 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004774 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004775 }
4776 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004777
4778 /*
4779 * Now, start the command as a process, so that it doesn't
4780 * inherit our handles which causes unpleasant dangling swap
4781 * files if we exit before the spawned process
4782 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004783 if (vim_create_process((char *)newcmd, FALSE, flags,
4784 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004785 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004786 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4787 > (HINSTANCE)32)
4788 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004789 else
4790 {
4791 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004792#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004793# ifdef VIMDLL
4794 if (gui.in_use)
4795# endif
4796 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004797#endif
4798 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004799
4800 if (newcmd != cmdbase)
4801 vim_free(newcmd);
4802
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004803 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004805 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004806 CloseHandle(si.hStdInput);
4807 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004808 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004809 CloseHandle(pi.hThread);
4810 CloseHandle(pi.hProcess);
4811 }
4812 else
4813 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004814 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004815#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004816 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004817 (!s_dont_use_vimrun && p_stmp ?
4818 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4819 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004821 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004822
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004823 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004824 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004826#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004827 if (
4828# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004829 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004830# endif
4831 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004833 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4834 "External commands will not pause after completion.\n"
4835 "See :help win32-vimrun for more information.");
4836 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004837 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4838 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004839
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004840 if (wmsg != NULL && wtitle != NULL)
4841 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4842 vim_free(wmsg);
4843 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 need_vimrun_warning = FALSE;
4845 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004846 if (
4847# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004848 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004849# endif
4850 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004851 // Use vimrun to execute the command. It opens a console
4852 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004853 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 vimrun_path,
4855 (msg_silent != 0 || (options & SHELL_DOOUT))
4856 ? "-s " : "",
4857 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004858 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004859# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004860 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004861# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004862 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004863 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004864 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4865 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004868 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004869 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004871 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
4875
4876 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004877 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar0f873732019-12-05 20:28:46 +01004879 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004881#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004882 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004883 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
4885 )
4886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004887 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 msg_putchar('\n');
4889 }
4890#ifdef FEAT_TITLE
4891 resettitle();
4892#endif
4893
4894 signal(SIGINT, SIG_DFL);
4895#if defined(__GNUC__) && !defined(__MINGW32__)
4896 signal(SIGKILL, SIG_DFL);
4897#else
4898 signal(SIGBREAK, SIG_DFL);
4899#endif
4900 signal(SIGILL, SIG_DFL);
4901 signal(SIGFPE, SIG_DFL);
4902 signal(SIGSEGV, SIG_DFL);
4903 signal(SIGTERM, SIG_DFL);
4904 signal(SIGABRT, SIG_DFL);
4905
4906 return x;
4907}
4908
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004909#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004910 static HANDLE
4911job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004912 char_u *fname,
4913 DWORD dwDesiredAccess,
4914 DWORD dwShareMode,
4915 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4916 DWORD dwCreationDisposition,
4917 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004918{
4919 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004920 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004921
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004922 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004923 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004924 return INVALID_HANDLE_VALUE;
4925
4926 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4927 lpSecurityAttributes, dwCreationDisposition,
4928 dwFlagsAndAttributes, NULL);
4929 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004930 return h;
4931}
4932
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004933/*
4934 * Turn the dictionary "env" into a NUL separated list that can be used as the
4935 * environment argument of vim_create_process().
4936 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004937 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004938win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004939{
4940 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004941 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004942 LPVOID base = GetEnvironmentStringsW();
4943
Bram Moolenaar0f873732019-12-05 20:28:46 +01004944 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004945 if (ga_grow(gap, 1) == FAIL)
4946 return;
4947
4948 if (base)
4949 {
4950 WCHAR *p = (WCHAR*) base;
4951
Bram Moolenaar0f873732019-12-05 20:28:46 +01004952 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004953 if (ga_grow(gap, 1) == FAIL)
4954 return;
4955
4956 while (*p != 0 || *(p + 1) != 0)
4957 {
4958 if (ga_grow(gap, 1) == OK)
4959 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
4960 p++;
4961 }
4962 FreeEnvironmentStrings(base);
4963 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4964 }
4965
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004966 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004967 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004968 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004969 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004970 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004971 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004972 typval_T *item = &dict_lookup(hi)->di_tv;
4973 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004975 --todo;
4976 if (wkey != NULL && wval != NULL)
4977 {
4978 size_t n;
4979 size_t lkey = wcslen(wkey);
4980 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004981
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004982 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4983 continue;
4984 for (n = 0; n < lkey; n++)
4985 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4986 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4987 for (n = 0; n < lval; n++)
4988 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4989 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4990 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004991 vim_free(wkey);
4992 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004993 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004994 }
4995 }
4996
Bram Moolenaar493359e2018-06-12 20:25:52 +02004997# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004998 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02004999# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005000 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005001 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005002# endif
5003# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005004 char_u *version = get_vim_var_str(VV_VERSION);
5005 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005006# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005007 // size of "VIM_SERVERNAME=" and value,
5008 // plus "VIM_TERMINAL=" and value,
5009 // plus two terminating NULs
5010 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005011# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005012 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005013# endif
5014# ifdef FEAT_TERMINAL
5015 + 13 + version_len + 2
5016# endif
5017 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005018
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005019 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005020 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005021# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005022 for (n = 0; n < 15; n++)
5023 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5024 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005025 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005026 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5027 (WCHAR)servername[n];
5028 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005029# endif
5030# ifdef FEAT_TERMINAL
5031 if (is_terminal)
5032 {
5033 for (n = 0; n < 13; n++)
5034 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5035 (WCHAR)"VIM_TERMINAL="[n];
5036 for (n = 0; n < version_len; n++)
5037 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5038 (WCHAR)version[n];
5039 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5040 }
5041# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005042 }
5043 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005044# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005045}
5046
Bram Moolenaarb091f302019-01-19 14:37:00 +01005047/*
5048 * Create a pair of pipes.
5049 * Return TRUE for success, FALSE for failure.
5050 */
5051 static BOOL
5052create_pipe_pair(HANDLE handles[2])
5053{
5054 static LONG s;
5055 char name[64];
5056 SECURITY_ATTRIBUTES sa;
5057
5058 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5059 GetCurrentProcessId(),
5060 InterlockedIncrement(&s));
5061
5062 // Create named pipe. Max size of named pipe is 65535.
5063 handles[1] = CreateNamedPipe(
5064 name,
5065 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5066 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005067 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005068
5069 if (handles[1] == INVALID_HANDLE_VALUE)
5070 return FALSE;
5071
5072 sa.nLength = sizeof(sa);
5073 sa.bInheritHandle = TRUE;
5074 sa.lpSecurityDescriptor = NULL;
5075
5076 handles[0] = CreateFile(name,
5077 FILE_GENERIC_READ,
5078 FILE_SHARE_READ, &sa,
5079 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5080
5081 if (handles[0] == INVALID_HANDLE_VALUE)
5082 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005083 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005084 return FALSE;
5085 }
5086
5087 return TRUE;
5088}
5089
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005090 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005091mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005092{
5093 STARTUPINFO si;
5094 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005095 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005096 SECURITY_ATTRIBUTES saAttr;
5097 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005098 HANDLE ifd[2];
5099 HANDLE ofd[2];
5100 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005101 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005102
5103 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5104 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5105 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5106 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5107 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5108 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5109 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5110
5111 if (use_out_for_err && use_null_for_out)
5112 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005113
5114 ifd[0] = INVALID_HANDLE_VALUE;
5115 ifd[1] = INVALID_HANDLE_VALUE;
5116 ofd[0] = INVALID_HANDLE_VALUE;
5117 ofd[1] = INVALID_HANDLE_VALUE;
5118 efd[0] = INVALID_HANDLE_VALUE;
5119 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005120 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005121
Bram Moolenaar14207f42016-10-27 21:13:10 +02005122 jo = CreateJobObject(NULL, NULL);
5123 if (jo == NULL)
5124 {
5125 job->jv_status = JOB_FAILED;
5126 goto failed;
5127 }
5128
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005129 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005130 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005131
Bram Moolenaar76467df2016-02-12 19:30:26 +01005132 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005133 ZeroMemory(&si, sizeof(si));
5134 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005135 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005136 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005137
Bram Moolenaard8070362016-02-15 21:56:54 +01005138 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5139 saAttr.bInheritHandle = TRUE;
5140 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005141
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005142 if (use_file_for_in)
5143 {
5144 char_u *fname = options->jo_io_name[PART_IN];
5145
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005146 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5147 FILE_SHARE_READ | FILE_SHARE_WRITE,
5148 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5149 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005151 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005152 goto failed;
5153 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005154 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005155 else if (!use_null_for_in
5156 && (!create_pipe_pair(ifd)
5157 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005158 goto failed;
5159
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005160 if (use_file_for_out)
5161 {
5162 char_u *fname = options->jo_io_name[PART_OUT];
5163
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005164 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5165 FILE_SHARE_READ | FILE_SHARE_WRITE,
5166 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5167 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005169 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005170 goto failed;
5171 }
5172 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005173 else if (!use_null_for_out &&
5174 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005175 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005176 goto failed;
5177
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005178 if (use_file_for_err)
5179 {
5180 char_u *fname = options->jo_io_name[PART_ERR];
5181
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005182 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5183 FILE_SHARE_READ | FILE_SHARE_WRITE,
5184 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5185 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005187 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005188 goto failed;
5189 }
5190 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005191 else if (!use_out_for_err && !use_null_for_err &&
5192 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005193 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005194 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005195
Bram Moolenaard8070362016-02-15 21:56:54 +01005196 si.dwFlags |= STARTF_USESTDHANDLES;
5197 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005198 si.hStdOutput = ofd[1];
5199 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5200
5201 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5202 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005203 if (options->jo_set & JO_CHANNEL)
5204 {
5205 channel = options->jo_channel;
5206 if (channel != NULL)
5207 ++channel->ch_refcount;
5208 }
5209 else
5210 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005211 if (channel == NULL)
5212 goto failed;
5213 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005214
5215 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005216 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005217 CREATE_DEFAULT_ERROR_MODE |
5218 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005219 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005220 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005221 &si, &pi,
5222 ga.ga_data,
5223 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005224 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005225 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005226 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005227 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005228 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005229
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005230 ga_clear(&ga);
5231
Bram Moolenaar14207f42016-10-27 21:13:10 +02005232 if (!AssignProcessToJobObject(jo, pi.hProcess))
5233 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005234 // if failing, switch the way to terminate
5235 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005236 CloseHandle(jo);
5237 jo = NULL;
5238 }
5239 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005240 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005241 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005242 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005243 job->jv_status = JOB_STARTED;
5244
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005245 CloseHandle(ifd[0]);
5246 CloseHandle(ofd[1]);
5247 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005248 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005249
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005250 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005251 if (channel != NULL)
5252 {
5253 channel_set_pipes(channel,
5254 use_file_for_in || use_null_for_in
5255 ? INVALID_FD : (sock_T)ifd[1],
5256 use_file_for_out || use_null_for_out
5257 ? INVALID_FD : (sock_T)ofd[0],
5258 use_out_for_err || use_file_for_err || use_null_for_err
5259 ? INVALID_FD : (sock_T)efd[0]);
5260 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005261 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005262 return;
5263
5264failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005265 CloseHandle(ifd[0]);
5266 CloseHandle(ofd[0]);
5267 CloseHandle(efd[0]);
5268 CloseHandle(ifd[1]);
5269 CloseHandle(ofd[1]);
5270 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005271 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005272 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005273}
5274
5275 char *
5276mch_job_status(job_T *job)
5277{
5278 DWORD dwExitCode = 0;
5279
Bram Moolenaar76467df2016-02-12 19:30:26 +01005280 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5281 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005282 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005283 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005284 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005285 {
5286 ch_log(job->jv_channel, "Job ended");
5287 job->jv_status = JOB_ENDED;
5288 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005289 return "dead";
5290 }
5291 return "run";
5292}
5293
Bram Moolenaar97792de2016-10-15 18:36:49 +02005294 job_T *
5295mch_detect_ended_job(job_T *job_list)
5296{
5297 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5298 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5299 job_T *job = job_list;
5300
5301 while (job != NULL)
5302 {
5303 DWORD n;
5304 DWORD result;
5305
5306 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5307 && job != NULL; job = job->jv_next)
5308 {
5309 if (job->jv_status == JOB_STARTED)
5310 {
5311 jobHandles[n] = job->jv_proc_info.hProcess;
5312 jobArray[n] = job;
5313 ++n;
5314 }
5315 }
5316 if (n == 0)
5317 continue;
5318 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5319 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5320 {
5321 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5322
5323 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5324 return wait_job;
5325 }
5326 }
5327 return NULL;
5328}
5329
Bram Moolenaarfb630902016-10-29 14:55:00 +02005330 static BOOL
5331terminate_all(HANDLE process, int code)
5332{
5333 PROCESSENTRY32 pe;
5334 HANDLE h = INVALID_HANDLE_VALUE;
5335 DWORD pid = GetProcessId(process);
5336
5337 if (pid != 0)
5338 {
5339 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5340 if (h != INVALID_HANDLE_VALUE)
5341 {
5342 pe.dwSize = sizeof(PROCESSENTRY32);
5343 if (!Process32First(h, &pe))
5344 goto theend;
5345
5346 do
5347 {
5348 if (pe.th32ParentProcessID == pid)
5349 {
5350 HANDLE ph = OpenProcess(
5351 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5352 if (ph != NULL)
5353 {
5354 terminate_all(ph, code);
5355 CloseHandle(ph);
5356 }
5357 }
5358 } while (Process32Next(h, &pe));
5359
5360 CloseHandle(h);
5361 }
5362 }
5363
5364theend:
5365 return TerminateProcess(process, code);
5366}
5367
5368/*
5369 * Send a (deadly) signal to "job".
5370 * Return FAIL if it didn't work.
5371 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005372 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005373mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005374{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005375 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005376
Bram Moolenaar923d9262016-02-25 20:56:01 +01005377 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005379 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005380 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005381 {
5382 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5383 job->jv_channel->ch_killing = TRUE;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005384 return TerminateJobObject(job->jv_job_object, 0) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005385 }
Bram Moolenaarfb630902016-10-29 14:55:00 +02005386 return terminate_all(job->jv_proc_info.hProcess, 0) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005387 }
5388
5389 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5390 return FAIL;
5391 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005392 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5393 job->jv_proc_info.dwProcessId)
5394 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005395 FreeConsole();
5396 return ret;
5397}
5398
5399/*
5400 * Clear the data related to "job".
5401 */
5402 void
5403mch_clear_job(job_T *job)
5404{
5405 if (job->jv_status != JOB_FAILED)
5406 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005407 if (job->jv_job_object != NULL)
5408 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005409 CloseHandle(job->jv_proc_info.hProcess);
5410 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005411}
5412#endif
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005415#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417/*
5418 * Start termcap mode
5419 */
5420 static void
5421termcap_mode_start(void)
5422{
5423 DWORD cmodein;
5424
5425 if (g_fTermcapMode)
5426 return;
5427
5428 SaveConsoleBuffer(&g_cbNonTermcap);
5429
5430 if (g_cbTermcap.IsValid)
5431 {
5432 /*
5433 * We've been in termcap mode before. Restore certain screen
5434 * characteristics, including the buffer size and the window
5435 * size. Since we will be redrawing the screen, we don't need
5436 * to restore the actual contents of the buffer.
5437 */
5438 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005439 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5441 Rows = g_cbTermcap.Info.dwSize.Y;
5442 Columns = g_cbTermcap.Info.dwSize.X;
5443 }
5444 else
5445 {
5446 /*
5447 * This is our first time entering termcap mode. Clear the console
5448 * screen buffer, and resize the buffer to match the current window
5449 * size. We will use this as the size of our editing environment.
5450 */
5451 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005452 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5454 }
5455
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005456# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 if (g_fMouseActive)
5462 cmodein |= ENABLE_MOUSE_INPUT;
5463 else
5464 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 cmodein |= ENABLE_WINDOW_INPUT;
5466 SetConsoleMode(g_hConIn, cmodein);
5467
5468 redraw_later_clear();
5469 g_fTermcapMode = TRUE;
5470}
5471
5472
5473/*
5474 * End termcap mode
5475 */
5476 static void
5477termcap_mode_end(void)
5478{
5479 DWORD cmodein;
5480 ConsoleBuffer *cb;
5481 COORD coord;
5482 DWORD dwDummy;
5483
5484 if (!g_fTermcapMode)
5485 return;
5486
5487 SaveConsoleBuffer(&g_cbTermcap);
5488
5489 GetConsoleMode(g_hConIn, &cmodein);
5490 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5491 SetConsoleMode(g_hConIn, cmodein);
5492
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005493# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005494 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005495# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005499 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 SetConsoleCursorInfo(g_hConOut, &g_cci);
5501
5502 if (p_rs || exiting)
5503 {
5504 /*
5505 * Clear anything that happens to be on the current line.
5506 */
5507 coord.X = 0;
5508 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5509 FillConsoleOutputCharacter(g_hConOut, ' ',
5510 cb->Info.dwSize.X, coord, &dwDummy);
5511 /*
5512 * The following is just for aesthetics. If we are exiting without
5513 * restoring the screen, then we want to have a prompt string
5514 * appear at the bottom line. However, the command interpreter
5515 * seems to always advance the cursor one line before displaying
5516 * the prompt string, which causes the screen to scroll. To
5517 * counter this, move the cursor up one line before exiting.
5518 */
5519 if (exiting && !p_rs)
5520 coord.Y--;
5521 /*
5522 * Position the cursor at the leftmost column of the desired row.
5523 */
5524 SetConsoleCursorPosition(g_hConOut, coord);
5525 }
5526
5527 g_fTermcapMode = FALSE;
5528}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005529#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005532#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 void
5534mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005535 char_u *s UNUSED,
5536 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005538 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541#else
5542
5543/*
5544 * clear `n' chars, starting from `coord'
5545 */
5546 static void
5547clear_chars(
5548 COORD coord,
5549 DWORD n)
5550{
5551 DWORD dwDummy;
5552
5553 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005554
5555 if (!USE_VTP)
5556 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5557 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005558 {
5559 set_console_color_rgb();
5560 gotoxy(coord.X + 1, coord.Y + 1);
5561 vtp_printf("\033[%dX", n);
5562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
5565
5566/*
5567 * Clear the screen
5568 */
5569 static void
5570clear_screen(void)
5571{
5572 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005573
5574 if (!USE_VTP)
5575 clear_chars(g_coord, Rows * Columns);
5576 else
5577 {
5578 set_console_color_rgb();
5579 gotoxy(1, 1);
5580 vtp_printf("\033[2J");
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584
5585/*
5586 * Clear to end of display
5587 */
5588 static void
5589clear_to_end_of_display(void)
5590{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005591 COORD save = g_coord;
5592
5593 if (!USE_VTP)
5594 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005596 else
5597 {
5598 set_console_color_rgb();
5599 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5600 vtp_printf("\033[0J");
5601
5602 gotoxy(save.X + 1, save.Y + 1);
5603 g_coord = save;
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605}
5606
5607
5608/*
5609 * Clear to end of line
5610 */
5611 static void
5612clear_to_end_of_line(void)
5613{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005614 COORD save = g_coord;
5615
5616 if (!USE_VTP)
5617 clear_chars(g_coord, Columns - g_coord.X);
5618 else
5619 {
5620 set_console_color_rgb();
5621 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5622 vtp_printf("\033[0K");
5623
5624 gotoxy(save.X + 1, save.Y + 1);
5625 g_coord = save;
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629
5630/*
5631 * Scroll the scroll region up by `cLines' lines
5632 */
5633 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005634scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 COORD oldcoord = g_coord;
5637
5638 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5639 delete_lines(cLines);
5640
5641 g_coord = oldcoord;
5642}
5643
5644
5645/*
5646 * Set the scroll region
5647 */
5648 static void
5649set_scroll_region(
5650 unsigned left,
5651 unsigned top,
5652 unsigned right,
5653 unsigned bottom)
5654{
5655 if (left >= right
5656 || top >= bottom
5657 || right > (unsigned) Columns - 1
5658 || bottom > (unsigned) Rows - 1)
5659 return;
5660
5661 g_srScrollRegion.Left = left;
5662 g_srScrollRegion.Top = top;
5663 g_srScrollRegion.Right = right;
5664 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005665}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005666
Bram Moolenaar6982f422019-02-16 16:48:01 +01005667 static void
5668set_scroll_region_tb(
5669 unsigned top,
5670 unsigned bottom)
5671{
5672 if (top >= bottom || bottom > (unsigned)Rows - 1)
5673 return;
5674
5675 g_srScrollRegion.Top = top;
5676 g_srScrollRegion.Bottom = bottom;
5677}
5678
5679 static void
5680set_scroll_region_lr(
5681 unsigned left,
5682 unsigned right)
5683{
5684 if (left >= right || right > (unsigned)Columns - 1)
5685 return;
5686
5687 g_srScrollRegion.Left = left;
5688 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689}
5690
5691
5692/*
5693 * Insert `cLines' lines at the current cursor position
5694 */
5695 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005696insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005698 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 COORD dest;
5700 CHAR_INFO fill;
5701
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005702 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5703
Bram Moolenaar6982f422019-02-16 16:48:01 +01005704 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 dest.Y = g_coord.Y + cLines;
5706
Bram Moolenaar6982f422019-02-16 16:48:01 +01005707 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 source.Top = g_coord.Y;
5709 source.Right = g_srScrollRegion.Right;
5710 source.Bottom = g_srScrollRegion.Bottom - cLines;
5711
Bram Moolenaar6982f422019-02-16 16:48:01 +01005712 clip.Left = g_srScrollRegion.Left;
5713 clip.Top = g_coord.Y;
5714 clip.Right = g_srScrollRegion.Right;
5715 clip.Bottom = g_srScrollRegion.Bottom;
5716
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005717 fill.Char.AsciiChar = ' ';
5718 if (!USE_VTP)
5719 fill.Attributes = g_attrCurrent;
5720 else
5721 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005723 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005724
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005725 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5726
Bram Moolenaar6982f422019-02-16 16:48:01 +01005727 // Here we have to deal with a win32 console flake: If the scroll
5728 // region looks like abc and we scroll c to a and fill with d we get
5729 // cbd... if we scroll block c one line at a time to a, we get cdd...
5730 // vim expects cdd consistently... So we have to deal with that
5731 // here... (this also occurs scrolling the same way in the other
5732 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 if (source.Bottom < dest.Y)
5735 {
5736 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005737 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar6982f422019-02-16 16:48:01 +01005739 coord.X = source.Left;
5740 for (i = clip.Top; i < dest.Y; ++i)
5741 {
5742 coord.Y = i;
5743 clear_chars(coord, source.Right - source.Left + 1);
5744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746}
5747
5748
5749/*
5750 * Delete `cLines' lines at the current cursor position
5751 */
5752 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005753delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005755 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 COORD dest;
5757 CHAR_INFO fill;
5758 int nb;
5759
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005760 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5761
Bram Moolenaar6982f422019-02-16 16:48:01 +01005762 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 dest.Y = g_coord.Y;
5764
Bram Moolenaar6982f422019-02-16 16:48:01 +01005765 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 source.Top = g_coord.Y + cLines;
5767 source.Right = g_srScrollRegion.Right;
5768 source.Bottom = g_srScrollRegion.Bottom;
5769
Bram Moolenaar6982f422019-02-16 16:48:01 +01005770 clip.Left = g_srScrollRegion.Left;
5771 clip.Top = g_coord.Y;
5772 clip.Right = g_srScrollRegion.Right;
5773 clip.Bottom = g_srScrollRegion.Bottom;
5774
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005775 fill.Char.AsciiChar = ' ';
5776 if (!USE_VTP)
5777 fill.Attributes = g_attrCurrent;
5778 else
5779 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005781 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005782
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005783 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5784
Bram Moolenaar6982f422019-02-16 16:48:01 +01005785 // Here we have to deal with a win32 console flake; See insert_lines()
5786 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 nb = dest.Y + (source.Bottom - source.Top) + 1;
5789
5790 if (nb < source.Top)
5791 {
5792 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005793 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar6982f422019-02-16 16:48:01 +01005795 coord.X = source.Left;
5796 for (i = nb; i < clip.Bottom; ++i)
5797 {
5798 coord.Y = i;
5799 clear_chars(coord, source.Right - source.Left + 1);
5800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802}
5803
5804
5805/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005806 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 */
5808 static void
5809gotoxy(
5810 unsigned x,
5811 unsigned y)
5812{
5813 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5814 return;
5815
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005816 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005817 {
5818 // external cursor coords are 1-based; internal are 0-based
5819 g_coord.X = x - 1;
5820 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005821 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005822 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005823 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005824 {
5825 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005826 // destruction. Insider build bug. Always enabled because it's cheap
5827 // and avoids mistakes with recognizing the build.
5828 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005829
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005830 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005831
5832 g_coord.X = x - 1;
5833 g_coord.Y = y - 1;
5834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835}
5836
5837
5838/*
5839 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005840 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
5842 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005843textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005845 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 SetConsoleTextAttribute(g_hConOut, wAttr);
5848}
5849
5850
5851 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005852textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005854 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005856 if (!USE_VTP)
5857 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5858 else
5859 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860}
5861
5862
5863 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005864textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005866 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005868 if (!USE_VTP)
5869 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5870 else
5871 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872}
5873
5874
5875/*
5876 * restore the default text attribute (whatever we started with)
5877 */
5878 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005879normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005881 if (!USE_VTP)
5882 textattr(g_attrDefault);
5883 else
5884 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886
5887
5888static WORD g_attrPreStandout = 0;
5889
5890/*
5891 * Make the text standout, by brightening it
5892 */
5893 static void
5894standout(void)
5895{
5896 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005897
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5899}
5900
5901
5902/*
5903 * Turn off standout mode
5904 */
5905 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005906standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005910
5911 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912}
5913
5914
5915/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005916 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005919mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 char_u *p;
5922 int n;
5923
5924 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5925 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005926 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005927# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005928 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005929# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005930 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
5932 p = T_ME + 2;
5933 n = getdigits(&p);
5934 if (*p == 'm' && n > 0)
5935 {
5936 cterm_normal_fg_color = (n & 0xf) + 1;
5937 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5938 }
5939 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005940# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005941 cterm_normal_fg_gui_color = INVALCOLOR;
5942 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}
5945
5946
5947/*
5948 * visual bell: flash the screen
5949 */
5950 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005951visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 COORD coordOrigin = {0, 0};
5954 WORD attrFlash = ~g_attrCurrent & 0xff;
5955
5956 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005957 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959 if (oldattrs == NULL)
5960 return;
5961 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5962 coordOrigin, &dwDummy);
5963 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5964 coordOrigin, &dwDummy);
5965
Bram Moolenaar0f873732019-12-05 20:28:46 +01005966 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005967 if (!USE_VTP)
5968 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 coordOrigin, &dwDummy);
5970 vim_free(oldattrs);
5971}
5972
5973
5974/*
5975 * Make the cursor visible or invisible
5976 */
5977 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005978cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979{
5980 s_cursor_visible = fVisible;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005981# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984}
5985
5986
5987/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005988 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005989 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005991 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02005993 char_u *pchBuf,
5994 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005996 COORD coord = g_coord;
5997 DWORD written;
5998 DWORD n, cchwritten, cells;
5999 static WCHAR *unicodebuf = NULL;
6000 static int unibuflen = 0;
6001 int length;
6002 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006004 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6005 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006006 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006007 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006008 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006009 unibuflen = length;
6010 }
6011 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6012 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006014 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006015
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006016 if (!USE_VTP)
6017 {
6018 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6019 coord, &written);
6020 // When writing fails or didn't write a single character, pretend one
6021 // character was written, otherwise we get stuck.
6022 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6023 coord, &cchwritten) == 0
6024 || cchwritten == 0 || cchwritten == (DWORD)-1)
6025 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006026 }
6027 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006028 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006029 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6030 NULL) == 0 || cchwritten == 0)
6031 cchwritten = 1;
6032 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006033
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006034 if (cchwritten == length)
6035 {
6036 written = cbToWrite;
6037 g_coord.X += (SHORT)cells;
6038 }
6039 else
6040 {
6041 char_u *p = pchBuf;
6042 for (n = 0; n < cchwritten; n++)
6043 MB_CPTR_ADV(p);
6044 written = p - pchBuf;
6045 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 while (g_coord.X > g_srScrollRegion.Right)
6049 {
6050 g_coord.X -= (SHORT) Columns;
6051 if (g_coord.Y < g_srScrollRegion.Bottom)
6052 g_coord.Y++;
6053 }
6054
6055 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6056
6057 return written;
6058}
6059
6060
6061/*
6062 * mch_write(): write the output buffer to the screen, translating ESC
6063 * sequences into calls to console output routines.
6064 */
6065 void
6066mch_write(
6067 char_u *s,
6068 int len)
6069{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006070# ifdef VIMDLL
6071 if (gui.in_use)
6072 return;
6073# endif
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 s[len] = NUL;
6076
6077 if (!term_console)
6078 {
6079 write(1, s, (unsigned)len);
6080 return;
6081 }
6082
Bram Moolenaar0f873732019-12-05 20:28:46 +01006083 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 while (len--)
6085 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006086 // optimization: use one single write_chars for runs of text,
6087 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006088 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090 if (p_wd)
6091 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006092 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (prefix != 0)
6094 prefix = 1;
6095 }
6096
6097 if (prefix != 0)
6098 {
6099 DWORD nWritten;
6100
6101 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006102# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (fdDump)
6104 {
6105 fputc('>', fdDump);
6106 fwrite(s, sizeof(char_u), nWritten, fdDump);
6107 fputs("<\n", fdDump);
6108 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 len -= (nWritten - 1);
6111 s += nWritten;
6112 }
6113 else if (s[0] == '\n')
6114 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006115 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (g_coord.Y == g_srScrollRegion.Bottom)
6117 {
6118 scroll(1);
6119 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6120 }
6121 else
6122 {
6123 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6124 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006125# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (fdDump)
6127 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 s++;
6130 }
6131 else if (s[0] == '\r')
6132 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006133 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006135# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (fdDump)
6137 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 s++;
6140 }
6141 else if (s[0] == '\b')
6142 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006143 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (g_coord.X > g_srScrollRegion.Left)
6145 g_coord.X--;
6146 else if (g_coord.Y > g_srScrollRegion.Top)
6147 {
6148 g_coord.X = g_srScrollRegion.Right;
6149 g_coord.Y--;
6150 }
6151 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006152# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (fdDump)
6154 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 s++;
6157 }
6158 else if (s[0] == '\a')
6159 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006160 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006162# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (fdDump)
6164 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 s++;
6167 }
6168 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6169 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006170# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006171 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006172# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006173 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006174 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176 switch (s[2])
6177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 case '0': case '1': case '2': case '3': case '4':
6179 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006180 p = s + 1;
6181 do
6182 {
6183 ++p;
6184 args[argc] = getdigits(&p);
6185 argc += (argc < 15) ? 1 : 0;
6186 if (p > s + len)
6187 break;
6188 } while (*p == ';');
6189
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (p > s + len)
6191 break;
6192
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006193 arg1 = args[0];
6194 arg2 = args[1];
6195 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006197 if (argc == 1 && args[0] == 0)
6198 normvideo();
6199 else if (argc == 1)
6200 {
6201 if (USE_VTP)
6202 textcolor((WORD) arg1);
6203 else
6204 textattr((WORD) arg1);
6205 }
6206 else if (USE_VTP)
6207 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006209 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006211 gotoxy(arg2, arg1);
6212 }
6213 else if (argc == 2 && *p == 'r')
6214 {
6215 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6216 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006217 else if (argc == 2 && *p == 'R')
6218 {
6219 set_scroll_region_tb(arg1, arg2);
6220 }
6221 else if (argc == 2 && *p == 'V')
6222 {
6223 set_scroll_region_lr(arg1, arg2);
6224 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006225 else if (argc == 1 && *p == 'A')
6226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 gotoxy(g_coord.X + 1,
6228 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6229 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006230 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 textbackground((WORD) arg1);
6233 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006234 else if (argc == 1 && *p == 'C')
6235 {
6236 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6237 g_coord.Y + 1);
6238 }
6239 else if (argc == 1 && *p == 'f')
6240 {
6241 textcolor((WORD) arg1);
6242 }
6243 else if (argc == 1 && *p == 'H')
6244 {
6245 gotoxy(1, arg1);
6246 }
6247 else if (argc == 1 && *p == 'L')
6248 {
6249 insert_lines(arg1);
6250 }
6251 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 delete_lines(arg1);
6254 }
6255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006256 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 s = p + 1;
6258 break;
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 gotoxy(g_coord.X + 1,
6262 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6263 goto got3;
6264
6265 case 'B':
6266 visual_bell();
6267 goto got3;
6268
6269 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6271 g_coord.Y + 1);
6272 goto got3;
6273
6274 case 'E':
6275 termcap_mode_end();
6276 goto got3;
6277
6278 case 'F':
6279 standout();
6280 goto got3;
6281
6282 case 'f':
6283 standend();
6284 goto got3;
6285
6286 case 'H':
6287 gotoxy(1, 1);
6288 goto got3;
6289
6290 case 'j':
6291 clear_to_end_of_display();
6292 goto got3;
6293
6294 case 'J':
6295 clear_screen();
6296 goto got3;
6297
6298 case 'K':
6299 clear_to_end_of_line();
6300 goto got3;
6301
6302 case 'L':
6303 insert_lines(1);
6304 goto got3;
6305
6306 case 'M':
6307 delete_lines(1);
6308 goto got3;
6309
6310 case 'S':
6311 termcap_mode_start();
6312 goto got3;
6313
6314 case 'V':
6315 cursor_visible(TRUE);
6316 goto got3;
6317
6318 case 'v':
6319 cursor_visible(FALSE);
6320 goto got3;
6321
6322 got3:
6323 s += 3;
6324 len -= 2;
6325 }
6326
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006327# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (fdDump)
6329 {
6330 fputs("ESC | ", fdDump);
6331 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6332 fputc('\n', fdDump);
6333 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336 else
6337 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006338 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 DWORD nWritten;
6340
6341 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006342# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (fdDump)
6344 {
6345 fputc('>', fdDump);
6346 fwrite(s, sizeof(char_u), nWritten, fdDump);
6347 fputs("<\n", fdDump);
6348 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351 len -= (nWritten - 1);
6352 s += nWritten;
6353 }
6354 }
6355
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006356# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 if (fdDump)
6358 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360}
6361
Bram Moolenaar0f873732019-12-05 20:28:46 +01006362#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
6364
6365/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006366 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 */
6368 void
6369mch_delay(
6370 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006371 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006373#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006374 Sleep((int)msec); // never wait for input
6375#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006376# ifdef VIMDLL
6377 if (gui.in_use)
6378 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006379 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006380 return;
6381 }
6382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006384# ifdef FEAT_MZSCHEME
6385 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6386 {
6387 int towait = p_mzq;
6388
Bram Moolenaar0f873732019-12-05 20:28:46 +01006389 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006390 while (msec > 0)
6391 {
6392 mzvim_check_threads();
6393 if (msec < towait)
6394 towait = msec;
6395 Sleep(towait);
6396 msec -= towait;
6397 }
6398 }
6399 else
6400# endif
6401 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006403 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404#endif
6405}
6406
6407
6408/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006409 * This version of remove is not scared by a readonly (backup) file.
6410 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 * Return 0 for success, -1 for failure.
6412 */
6413 int
6414mch_remove(char_u *name)
6415{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006416 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int n;
6418
Bram Moolenaar203258c2016-01-17 22:15:16 +01006419 /*
6420 * On Windows, deleting a directory's symbolic link is done by
6421 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6422 */
6423 if (mch_isdir(name) && mch_is_symbolic_link(name))
6424 return mch_rmdir(name);
6425
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006426 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6427
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006428 wn = enc_to_utf16(name, NULL);
6429 if (wn == NULL)
6430 return -1;
6431
6432 n = DeleteFileW(wn) ? 0 : -1;
6433 vim_free(wn);
6434 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435}
6436
6437
6438/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006439 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
6441 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006442mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006444#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6445# ifdef VIMDLL
6446 if (!gui.in_use)
6447# endif
6448 if (g_fCtrlCPressed || g_fCBrkPressed)
6449 {
6450 ctrl_break_was_pressed = g_fCBrkPressed;
6451 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6452 got_int = TRUE;
6453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454#endif
6455}
6456
Bram Moolenaar0f873732019-12-05 20:28:46 +01006457// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006458#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006459
6460/*
6461 * How much main memory in KiB that can be used by VIM.
6462 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006463 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006464mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006465{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006466 MEMORYSTATUSEX ms;
6467
Bram Moolenaar0f873732019-12-05 20:28:46 +01006468 // Need to use GlobalMemoryStatusEx() when there is more memory than
6469 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006470 ms.dwLength = sizeof(MEMORYSTATUSEX);
6471 GlobalMemoryStatusEx(&ms);
6472 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006473 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006474 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006475 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006476 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006477 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006478 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006479 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006480 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006481 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006482 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006483 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006487 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6489 * file whose short file name is "FOO.BAR" (its long file name will
6490 * be correct: "foo.bar~"). Because a file can be accessed by
6491 * either its SFN or its LFN, "foo.bar" has effectively been
6492 * renamed to "foo.bar", which is not at all what was wanted. This
6493 * seems to happen only when renaming files with three-character
6494 * extensions by appending a suffix that does not include ".".
6495 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6496 *
6497 * There is another problem, which isn't really a bug but isn't right either:
6498 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6499 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6500 * service pack 6. Doesn't seem to happen on Windows 98.
6501 *
6502 * Like rename(), returns 0 upon success, non-zero upon failure.
6503 * Should probably set errno appropriately when errors occur.
6504 */
6505 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006506mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006508 WCHAR *p;
6509 int i;
6510 WCHAR szTempFile[_MAX_PATH + 1];
6511 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006514 // No need to play tricks unless the file name contains a "~" as the
6515 // seventh character.
6516 p = wold;
6517 for (i = 0; wold[i] != NUL; ++i)
6518 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6519 && wold[i + 1] != 0)
6520 p = wold + i + 1;
6521 if ((int)(wold + i - p) < 8 || p[6] != '~')
6522 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006524 // Get base path of new file name. Undocumented feature: If pszNewFile is
6525 // a directory, no error is returned and pszFilePart will be NULL.
6526 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006528 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006530 // Get (and create) a unique temporary file name in directory of new file
6531 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 return -2;
6533
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 // blow the temp file away
6535 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 return -3;
6537
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006538 // rename old file to the temp file
6539 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 return -4;
6541
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006542 // now create an empty file called pszOldFile; this prevents the operating
6543 // system using pszOldFile as an alias (SFN) if we're renaming within the
6544 // same directory. For example, we're editing a file called
6545 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6546 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6547 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6548 // cause all sorts of problems later in buf_write(). So, we create an
6549 // empty file called filena~1.txt and the system will have to find some
6550 // other SFN for filena~1.txt~, such as filena~2.txt
6551 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6553 return -5;
6554 if (!CloseHandle(hf))
6555 return -6;
6556
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006557 // rename the temp file to the new file
6558 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006560 // Renaming failed. Rename the file back to its old name, so that it
6561 // looks like nothing happened.
6562 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 return -7;
6564 }
6565
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006566 // Seems to be left around on Novell filesystems
6567 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006569 // finally, remove the empty old file
6570 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 return -8;
6572
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006573 return 0;
6574}
6575
6576
6577/*
6578 * Converts the filenames to UTF-16, then call mch_wrename().
6579 * Like rename(), returns 0 upon success, non-zero upon failure.
6580 */
6581 int
6582mch_rename(
6583 const char *pszOldFile,
6584 const char *pszNewFile)
6585{
6586 WCHAR *wold = NULL;
6587 WCHAR *wnew = NULL;
6588 int retval = -1;
6589
6590 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6591 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6592 if (wold != NULL && wnew != NULL)
6593 retval = mch_wrename(wold, wnew);
6594 vim_free(wold);
6595 vim_free(wnew);
6596 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
6599/*
6600 * Get the default shell for the current hardware platform
6601 */
6602 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006603default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006605 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
6607
6608/*
6609 * mch_access() extends access() to do more detailed check on network drives.
6610 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6611 */
6612 int
6613mch_access(char *n, int p)
6614{
6615 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006616 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006617 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006619 wn = enc_to_utf16((char_u *)n, NULL);
6620 if (wn == NULL)
6621 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006623 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 if (p & R_OK)
6628 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006629 // Read check is performed by seeing if we can do a find file on
6630 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006631 int i;
6632 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006634 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6635 TempNameW[i] = wn[i];
6636 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6637 TempNameW[i++] = '\\';
6638 TempNameW[i++] = '*';
6639 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006641 hFile = FindFirstFileW(TempNameW, &d);
6642 if (hFile == INVALID_HANDLE_VALUE)
6643 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
6647
6648 if (p & W_OK)
6649 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006650 // Trying to create a temporary file in the directory should catch
6651 // directories on read-only network shares. However, in
6652 // directories whose ACL allows writes but denies deletes will end
6653 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006654 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6655 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006656 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006657 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659 }
6660 else
6661 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006662 // Don't consider a file read-only if another process has opened it.
6663 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6664
Bram Moolenaar0f873732019-12-05 20:28:46 +01006665 // Trying to open the file for the required access does ACL, read-only
6666 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006667 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6668 | ((p & R_OK) ? GENERIC_READ : 0);
6669
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006670 hFile = CreateFileW(wn, access_mode, share_mode,
6671 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (hFile == INVALID_HANDLE_VALUE)
6673 goto getout;
6674 CloseHandle(hFile);
6675 }
6676
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 return retval;
6681}
6682
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006684 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
6686 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006687mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 WCHAR *wn;
6690 int f;
6691
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006692 wn = enc_to_utf16((char_u *)name, NULL);
6693 if (wn == NULL)
6694 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006696 f = _wopen(wn, flags, mode);
6697 vim_free(wn);
6698 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
6700
6701/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006702 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006705mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 WCHAR *wn, *wm;
6708 FILE *f = NULL;
6709
Bram Moolenaara12a1612019-01-24 16:39:02 +01006710#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006711 // Work around an annoying assertion in the Microsoft debug CRT
6712 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006713 char newMode = mode[strlen(mode) - 1];
6714 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006715
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006716 _get_fmode(&oldMode);
6717 if (newMode == 't')
6718 _set_fmode(_O_TEXT);
6719 else if (newMode == 'b')
6720 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006721#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006722 wn = enc_to_utf16((char_u *)name, NULL);
6723 wm = enc_to_utf16((char_u *)mode, NULL);
6724 if (wn != NULL && wm != NULL)
6725 f = _wfopen(wn, wm);
6726 vim_free(wn);
6727 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006728
Bram Moolenaara12a1612019-01-24 16:39:02 +01006729#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006730 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006731#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006732 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735/*
6736 * SUB STREAM (aka info stream) handling:
6737 *
6738 * NTFS can have sub streams for each file. Normal contents of file is
6739 * stored in the main stream, and extra contents (author information and
6740 * title and so on) can be stored in sub stream. After Windows 2000, user
6741 * can access and store those informations in sub streams via explorer's
6742 * property menuitem in right click menu. Those informations in sub streams
6743 * were lost when copying only the main stream. So we have to copy sub
6744 * streams.
6745 *
6746 * Incomplete explanation:
6747 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6748 * More useful info and an example:
6749 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6750 */
6751
6752/*
6753 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6754 * and write to stream "substream" of file "to".
6755 * Errors are ignored.
6756 */
6757 static void
6758copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6759{
6760 HANDLE hTo;
6761 WCHAR *to_name;
6762
6763 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6764 wcscpy(to_name, to);
6765 wcscat(to_name, substream);
6766
6767 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6768 FILE_ATTRIBUTE_NORMAL, NULL);
6769 if (hTo != INVALID_HANDLE_VALUE)
6770 {
6771 long done;
6772 DWORD todo;
6773 DWORD readcnt, written;
6774 char buf[4096];
6775
Bram Moolenaar0f873732019-12-05 20:28:46 +01006776 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 for (done = 0; done < len; done += written)
6778 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006779 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006780 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6781 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6783 FALSE, FALSE, context)
6784 || readcnt != todo
6785 || !WriteFile(hTo, buf, todo, &written, NULL)
6786 || written != todo)
6787 break;
6788 }
6789 CloseHandle(hTo);
6790 }
6791
6792 free(to_name);
6793}
6794
6795/*
6796 * Copy info streams from file "from" to file "to".
6797 */
6798 static void
6799copy_infostreams(char_u *from, char_u *to)
6800{
6801 WCHAR *fromw;
6802 WCHAR *tow;
6803 HANDLE sh;
6804 WIN32_STREAM_ID sid;
6805 int headersize;
6806 WCHAR streamname[_MAX_PATH];
6807 DWORD readcount;
6808 void *context = NULL;
6809 DWORD lo, hi;
6810 int len;
6811
Bram Moolenaar0f873732019-12-05 20:28:46 +01006812 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006813 fromw = enc_to_utf16(from, NULL);
6814 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (fromw != NULL && tow != NULL)
6816 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006817 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6819 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6820 if (sh != INVALID_HANDLE_VALUE)
6821 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 // Use BackupRead() to find the info streams. Repeat until we
6823 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 for (;;)
6825 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006826 // Get the header to find the length of the stream name. If
6827 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006829 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6831 &readcount, FALSE, FALSE, &context)
6832 || readcount == 0)
6833 break;
6834
Bram Moolenaar0f873732019-12-05 20:28:46 +01006835 // We only deal with streams that have a name. The normal
6836 // file data appears to be without a name, even though docs
6837 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (sid.dwStreamNameSize > 0)
6839 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006840 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (!BackupRead(sh, (LPBYTE)streamname,
6842 sid.dwStreamNameSize,
6843 &readcount, FALSE, FALSE, &context))
6844 break;
6845
Bram Moolenaar0f873732019-12-05 20:28:46 +01006846 // Copy an info stream with a name ":anything:$DATA".
6847 // Skip "::$DATA", it has no stream name (examples suggest
6848 // it might be used for the normal file contents).
6849 // Note that BackupRead() counts bytes, but the name is in
6850 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 len = readcount / sizeof(WCHAR);
6852 streamname[len] = 0;
6853 if (len > 7 && wcsicmp(streamname + len - 6,
6854 L":$DATA") == 0)
6855 {
6856 streamname[len - 6] = 0;
6857 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006858 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 }
6860 }
6861
Bram Moolenaar0f873732019-12-05 20:28:46 +01006862 // Advance to the next stream. We might try seeking too far,
6863 // but BackupSeek() doesn't skip over stream borders, thus
6864 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006865 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 &lo, &hi, &context);
6867 }
6868
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6871
6872 CloseHandle(sh);
6873 }
6874 }
6875 vim_free(fromw);
6876 vim_free(tow);
6877}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879/*
6880 * Copy file attributes from file "from" to file "to".
6881 * For Windows NT and later we copy info streams.
6882 * Always returns zero, errors are ignored.
6883 */
6884 int
6885mch_copy_file_attribute(char_u *from, char_u *to)
6886{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006887 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006888 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 return 0;
6890}
6891
6892#if defined(MYRESETSTKOFLW) || defined(PROTO)
6893/*
6894 * Recreate a destroyed stack guard page in win32.
6895 * Written by Benjamin Peterson.
6896 */
6897
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006899# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901/*
6902 * This function does the same thing as _resetstkoflw(), which is only
6903 * available in DevStudio .net and later.
6904 * Returns 0 for failure, 1 for success.
6905 */
6906 int
6907myresetstkoflw(void)
6908{
6909 BYTE *pStackPtr;
6910 BYTE *pGuardPage;
6911 BYTE *pStackBase;
6912 BYTE *pLowestPossiblePage;
6913 MEMORY_BASIC_INFORMATION mbi;
6914 SYSTEM_INFO si;
6915 DWORD nPageSize;
6916 DWORD dummy;
6917
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 GetSystemInfo(&si);
6920 nPageSize = si.dwPageSize;
6921
Bram Moolenaar0f873732019-12-05 20:28:46 +01006922 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 pStackPtr = (BYTE*)_alloca(1);
6924
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6927 return 0;
6928 pStackBase = (BYTE*)mbi.AllocationBase;
6929
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006930 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006931 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006932 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 // We want the first committed page in the stack Start at the stack
6936 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 BYTE *pBlock = pStackBase;
6938
Bram Moolenaara466c992005-07-09 21:03:22 +00006939 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6942 return 0;
6943
6944 pBlock += mbi.RegionSize;
6945
6946 if (mbi.State & MEM_COMMIT)
6947 break;
6948 }
6949
Bram Moolenaar0f873732019-12-05 20:28:46 +01006950 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (mbi.Protect & PAGE_GUARD)
6952 return 1;
6953
Bram Moolenaar0f873732019-12-05 20:28:46 +01006954 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6956 pGuardPage = pLowestPossiblePage;
6957 else
6958 pGuardPage = (BYTE*)mbi.BaseAddress;
6959
Bram Moolenaar0f873732019-12-05 20:28:46 +01006960 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6962 return 0;
6963
Bram Moolenaar0f873732019-12-05 20:28:46 +01006964 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6966 &dummy))
6967 return 0;
6968 }
6969
6970 return 1;
6971}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006974
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006975/*
6976 * The command line arguments in UCS2
6977 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006978static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006979static LPWSTR *ArglistW = NULL;
6980static int global_argc = 0;
6981static char **global_argv;
6982
Bram Moolenaar0f873732019-12-05 20:28:46 +01006983static int used_file_argc = 0; // last argument in global_argv[] used
6984 // for the argument list.
6985static int *used_file_indexes = NULL; // indexes in global_argv[] for
6986 // command line arguments added to
6987 // the argument list
6988static int used_file_count = 0; // nr of entries in used_file_indexes
6989static int used_file_literal = FALSE; // take file names literally
6990static int used_file_full_path = FALSE; // file name was full path
6991static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006992static int used_alist_count = 0;
6993
6994
6995/*
6996 * Get the command line arguments. Unicode version.
6997 * Returns argc. Zero when something fails.
6998 */
6999 int
7000get_cmd_argsW(char ***argvp)
7001{
7002 char **argv = NULL;
7003 int argc = 0;
7004 int i;
7005
Bram Moolenaar14993322014-09-09 12:25:33 +02007006 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007007 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7008 if (ArglistW != NULL)
7009 {
7010 argv = malloc((nArgsW + 1) * sizeof(char *));
7011 if (argv != NULL)
7012 {
7013 argc = nArgsW;
7014 argv[argc] = NULL;
7015 for (i = 0; i < argc; ++i)
7016 {
7017 int len;
7018
Bram Moolenaar0f873732019-12-05 20:28:46 +01007019 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007020 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007021 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007022 (LPSTR *)&argv[i], &len, 0, 0);
7023 if (argv[i] == NULL)
7024 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007025 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007026 while (i > 0)
7027 free(argv[--i]);
7028 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007029 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007030 argc = 0;
7031 }
7032 }
7033 }
7034 }
7035
7036 global_argc = argc;
7037 global_argv = argv;
7038 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007039 {
7040 if (used_file_indexes != NULL)
7041 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007042 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007043 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007044
7045 if (argvp != NULL)
7046 *argvp = argv;
7047 return argc;
7048}
7049
7050 void
7051free_cmd_argsW(void)
7052{
7053 if (ArglistW != NULL)
7054 {
7055 GlobalFree(ArglistW);
7056 ArglistW = NULL;
7057 }
7058}
7059
7060/*
7061 * Remember "name" is an argument that was added to the argument list.
7062 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7063 * is called.
7064 */
7065 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007066used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007067{
7068 int i;
7069
7070 if (used_file_indexes == NULL)
7071 return;
7072 for (i = used_file_argc + 1; i < global_argc; ++i)
7073 if (STRCMP(global_argv[i], name) == 0)
7074 {
7075 used_file_argc = i;
7076 used_file_indexes[used_file_count++] = i;
7077 break;
7078 }
7079 used_file_literal = literal;
7080 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007081 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007082}
7083
7084/*
7085 * Remember the length of the argument list as it was. If it changes then we
7086 * leave it alone when 'encoding' is set.
7087 */
7088 void
7089set_alist_count(void)
7090{
7091 used_alist_count = GARGCOUNT;
7092}
7093
7094/*
7095 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7096 * has been changed while starting up. Use the UCS-2 command line arguments
7097 * and convert them to 'encoding'.
7098 */
7099 void
7100fix_arg_enc(void)
7101{
7102 int i;
7103 int idx;
7104 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007105 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007106
Bram Moolenaar0f873732019-12-05 20:28:46 +01007107 // Safety checks:
7108 // - if argument count differs between the wide and non-wide argument
7109 // list, something must be wrong.
7110 // - the file name arguments must have been located.
7111 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007112 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007113 || ArglistW == NULL
7114 || used_file_indexes == NULL
7115 || used_file_count == 0
7116 || used_alist_count != GARGCOUNT)
7117 return;
7118
Bram Moolenaar0f873732019-12-05 20:28:46 +01007119 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007120 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007121 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007122 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007123 for (i = 0; i < GARGCOUNT; ++i)
7124 fnum_list[i] = GARGLIST[i].ae_fnum;
7125
Bram Moolenaar0f873732019-12-05 20:28:46 +01007126 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007127 alist_clear(&global_alist);
7128 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007129 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007130
7131 for (i = 0; i < used_file_count; ++i)
7132 {
7133 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007134 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007135 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007136 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007137 int literal = used_file_literal;
7138
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007139#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007140 // When using diff mode may need to concatenate file name to
7141 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007142 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7143 && !mch_isdir(alist_name(&GARGLIST[0])))
7144 {
7145 char_u *r;
7146
7147 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7148 if (r != NULL)
7149 {
7150 vim_free(str);
7151 str = r;
7152 }
7153 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007154#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007155 // Re-use the old buffer by renaming it. When not using literal
7156 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007157 if (used_file_literal)
7158 buf_set_name(fnum_list[i], str);
7159
Bram Moolenaar0f873732019-12-05 20:28:46 +01007160 // Check backtick literal. backtick literal is already expanded in
7161 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007162 if (literal == FALSE)
7163 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007164 size_t len = STRLEN(str);
7165
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007166 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7167 literal = TRUE;
7168 }
7169 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007170 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007171 }
7172
7173 if (!used_file_literal)
7174 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007175 // Now expand wildcards in the arguments.
7176 // Temporarily add '(' and ')' to 'isfname'. These are valid
7177 // filename characters but are excluded from 'isfname' to make
7178 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7179 // Also, unset wildignore to not be influenced by this option.
7180 // The arguments specified in command-line should be kept even if
7181 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007182 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007183 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007184 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007185 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007186 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007187 }
7188
Bram Moolenaar0f873732019-12-05 20:28:46 +01007189 // If wildcard expansion failed, we are editing the first file of the
7190 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007191 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7192 {
7193 do_cmdline_cmd((char_u *)":rewind");
7194 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007195 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007196 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007197
7198 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007199}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007200
7201 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007202mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007203{
7204 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007205 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007206
Bram Moolenaar964b3742019-05-24 18:54:09 +02007207 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007208 if (envbuf == NULL)
7209 return -1;
7210
7211 sprintf((char *)envbuf, "%s=%s", var, value);
7212
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007213 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007214
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007215 vim_free(envbuf);
7216 if (p == NULL)
7217 return -1;
7218 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007219#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007220 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007221#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007222 // Unlike Un*x systems, we can free the string for _wputenv().
7223 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007224
7225 return 0;
7226}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007227
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007228/*
7229 * Support for 256 colors and 24-bit colors was added in Windows 10
7230 * version 1703 (Creators update).
7231 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007232#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7233
7234/*
7235 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007236 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007237 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007238#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007239
7240/*
7241 * ConPTY differences between versions, need different logic.
7242 * version 1903 (May 2019 update).
7243 */
7244#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7245
7246/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007247 * version 1909 (November 2019 update).
7248 */
7249#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7250
7251/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007252 * Confirm until this version. Also the logic changes.
7253 * insider preview.
7254 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007255#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007256
7257/*
7258 * Not stable now.
7259 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007260#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007261
7262 static void
7263vtp_flag_init(void)
7264{
7265 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007266#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007267 DWORD mode;
7268 HANDLE out;
7269
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007270# ifdef VIMDLL
7271 if (!gui.in_use)
7272# endif
7273 {
7274 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007275
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007276 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7277 GetConsoleMode(out, &mode);
7278 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7279 if (SetConsoleMode(out, mode) == 0)
7280 vtp_working = 0;
7281 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007282#endif
7283
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007284 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007285 conpty_working = 1;
7286 if (ver >= CONPTY_STABLE_BUILD)
7287 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007288
Bram Moolenaar57da6982019-09-13 22:30:11 +02007289 if (ver <= CONPTY_INSIDER_BUILD)
7290 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007291 if (ver <= CONPTY_1909_BUILD)
7292 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007293 if (ver <= CONPTY_1903_BUILD)
7294 conpty_type = 2;
7295 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7296 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007297}
7298
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007299#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007300
7301 static void
7302vtp_init(void)
7303{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007304 HMODULE hKerneldll;
7305 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007306# ifdef FEAT_TERMGUICOLORS
7307 COLORREF fg, bg;
7308# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007309
Bram Moolenaar0f873732019-12-05 20:28:46 +01007310 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007311 hKerneldll = GetModuleHandle("kernel32.dll");
7312 if (hKerneldll != NULL)
7313 {
7314 pGetConsoleScreenBufferInfoEx =
7315 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7316 hKerneldll, "GetConsoleScreenBufferInfoEx");
7317 pSetConsoleScreenBufferInfoEx =
7318 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7319 hKerneldll, "SetConsoleScreenBufferInfoEx");
7320 if (pGetConsoleScreenBufferInfoEx != NULL
7321 && pSetConsoleScreenBufferInfoEx != NULL)
7322 has_csbiex = TRUE;
7323 }
7324
7325 csbi.cbSize = sizeof(csbi);
7326 if (has_csbiex)
7327 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007328 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7329 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
7330
7331# ifdef FEAT_TERMGUICOLORS
7332 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7333 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7334 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7335 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7336 default_console_color_bg = bg;
7337 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007338# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007339
7340 set_console_color_rgb();
7341}
7342
7343 static void
7344vtp_exit(void)
7345{
7346 reset_console_color_rgb();
7347}
7348
7349 static int
7350vtp_printf(
7351 char *format,
7352 ...)
7353{
7354 char_u buf[100];
7355 va_list list;
7356 DWORD result;
7357
7358 va_start(list, format);
7359 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7360 va_end(list);
7361 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7362 return (int)result;
7363}
7364
7365 static void
7366vtp_sgr_bulk(
7367 int arg)
7368{
7369 int args[1];
7370
7371 args[0] = arg;
7372 vtp_sgr_bulks(1, args);
7373}
7374
7375 static void
7376vtp_sgr_bulks(
7377 int argc,
7378 int *args
7379)
7380{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007381 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007382 char_u buf[2 + (4 * 16) + 1];
7383 char_u *p;
7384 int i;
7385
7386 p = buf;
7387 *p++ = '\033';
7388 *p++ = '[';
7389
7390 for (i = 0; i < argc; ++i)
7391 {
7392 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7393 *p++ = ';';
7394 }
7395 p--;
7396 *p++ = 'm';
7397 *p = NUL;
7398 vtp_printf((char *)buf);
7399}
7400
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007401# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007402 static int
7403ctermtoxterm(
7404 int cterm)
7405{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007406 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007407
7408 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7409 return (((int)r << 16) | ((int)g << 8) | (int)b);
7410}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007411# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007412
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007413 static void
7414set_console_color_rgb(void)
7415{
7416# ifdef FEAT_TERMGUICOLORS
7417 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007418 guicolor_T fg, bg;
7419 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007420
7421 if (!USE_VTP)
7422 return;
7423
Bram Moolenaara050b942019-12-02 21:35:31 +01007424 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7425
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007426 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7427 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7428
7429 csbi.cbSize = sizeof(csbi);
7430 if (has_csbiex)
7431 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7432
7433 csbi.cbSize = sizeof(csbi);
7434 csbi.srWindow.Right += 1;
7435 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007436 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7437 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007438 if (has_csbiex)
7439 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7440# endif
7441}
7442
Bram Moolenaara050b942019-12-02 21:35:31 +01007443# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7444 void
7445get_default_console_color(
7446 int *cterm_fg,
7447 int *cterm_bg,
7448 guicolor_T *gui_fg,
7449 guicolor_T *gui_bg)
7450{
7451 int id;
7452 guicolor_T guifg = INVALCOLOR;
7453 guicolor_T guibg = INVALCOLOR;
7454 int ctermfg = 0;
7455 int ctermbg = 0;
7456
7457 id = syn_name2id((char_u *)"Normal");
7458 if (id > 0 && p_tgc)
7459 syn_id2colors(id, &guifg, &guibg);
7460 if (guifg == INVALCOLOR)
7461 {
7462 ctermfg = -1;
7463 if (id > 0)
7464 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7465 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7466 : default_console_color_fg;
7467 cterm_normal_fg_gui_color = guifg;
7468 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7469 }
7470 if (guibg == INVALCOLOR)
7471 {
7472 ctermbg = -1;
7473 if (id > 0)
7474 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7475 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7476 : default_console_color_bg;
7477 cterm_normal_bg_gui_color = guibg;
7478 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7479 }
7480
7481 *cterm_fg = ctermfg;
7482 *cterm_bg = ctermbg;
7483 *gui_fg = guifg;
7484 *gui_bg = guibg;
7485}
7486# endif
7487
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007488 static void
7489reset_console_color_rgb(void)
7490{
7491# ifdef FEAT_TERMGUICOLORS
7492 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7493
7494 csbi.cbSize = sizeof(csbi);
7495 if (has_csbiex)
7496 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7497
7498 csbi.cbSize = sizeof(csbi);
7499 csbi.srWindow.Right += 1;
7500 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007501 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7502 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007503 if (has_csbiex)
7504 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7505# endif
7506}
7507
7508 void
7509control_console_color_rgb(void)
7510{
7511 if (USE_VTP)
7512 set_console_color_rgb();
7513 else
7514 reset_console_color_rgb();
7515}
7516
7517 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007518use_vtp(void)
7519{
7520 return USE_VTP;
7521}
7522
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007523 int
7524is_term_win32(void)
7525{
7526 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7527}
7528
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007529 int
7530has_vtp_working(void)
7531{
7532 return vtp_working;
7533}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007534
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007535#endif
7536
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007537 int
7538has_conpty_working(void)
7539{
7540 return conpty_working;
7541}
7542
7543 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007544get_conpty_type(void)
7545{
7546 return conpty_type;
7547}
7548
7549 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007550is_conpty_stable(void)
7551{
7552 return conpty_stable;
7553}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007554
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007555#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007556 void
7557resize_console_buf(void)
7558{
7559 CONSOLE_SCREEN_BUFFER_INFO csbi;
7560 COORD coord;
7561 SMALL_RECT newsize;
7562
7563 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7564 {
7565 coord.X = SRWIDTH(csbi.srWindow);
7566 coord.Y = SRHEIGHT(csbi.srWindow);
7567 SetConsoleScreenBufferSize(g_hConOut, coord);
7568
7569 newsize.Left = 0;
7570 newsize.Top = 0;
7571 newsize.Right = coord.X - 1;
7572 newsize.Bottom = coord.Y - 1;
7573 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7574
7575 SetConsoleScreenBufferSize(g_hConOut, coord);
7576 }
7577}
7578#endif