blob: 6b32869756b538ae6df32cc40b3e09b722847e10 [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;
Bram Moolenaardf543822020-01-30 11:53:59 +0100203static guicolor_T store_console_bg_rgb;
204static guicolor_T store_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100205
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +0200206static int g_color_index_bg = 0;
207static int g_color_index_fg = 7;
208
209# ifdef FEAT_TERMGUICOLORS
210static int default_console_color_bg = 0x000000; // black
211static int default_console_color_fg = 0xc0c0c0; // white
212# endif
213
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100214# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +0200215# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100216# else
217# define USE_VTP 0
218# endif
219
220static void set_console_color_rgb(void);
221static void reset_console_color_rgb(void);
Bram Moolenaardf543822020-01-30 11:53:59 +0100222static void restore_console_color_rgb(void);
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100223#endif
224
Bram Moolenaar0f873732019-12-05 20:28:46 +0100225// This flag is newly created from Windows 10
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100226#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
227# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
228#endif
229
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200230#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100231static int suppress_winsize = 1; // don't fiddle with console
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
233
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200234static char_u *exe_path = NULL;
235
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100236static BOOL win8_or_later = FALSE;
237
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200238#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100239// Dynamic loading for portability
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100240typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
241{
242 ULONG cbSize;
243 COORD dwSize;
244 COORD dwCursorPosition;
245 WORD wAttributes;
246 SMALL_RECT srWindow;
247 COORD dwMaximumWindowSize;
248 WORD wPopupAttributes;
249 BOOL bFullscreenSupported;
250 COLORREF ColorTable[16];
251} DYN_CONSOLE_SCREEN_BUFFER_INFOEX, *PDYN_CONSOLE_SCREEN_BUFFER_INFOEX;
252typedef BOOL (WINAPI *PfnGetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
253static PfnGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx;
254typedef BOOL (WINAPI *PfnSetConsoleScreenBufferInfoEx)(HANDLE, PDYN_CONSOLE_SCREEN_BUFFER_INFOEX);
255static PfnSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx;
256static BOOL has_csbiex = FALSE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100257#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100258
259/*
260 * Get version number including build number
261 */
262typedef BOOL (WINAPI *PfnRtlGetVersion)(LPOSVERSIONINFOW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100263#define MAKE_VER(major, minor, build) \
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100264 (((major) << 24) | ((minor) << 16) | (build))
265
266 static DWORD
267get_build_number(void)
268{
269 OSVERSIONINFOW osver = {sizeof(OSVERSIONINFOW)};
270 HMODULE hNtdll;
271 PfnRtlGetVersion pRtlGetVersion;
272 DWORD ver = MAKE_VER(0, 0, 0);
273
274 hNtdll = GetModuleHandle("ntdll.dll");
275 if (hNtdll != NULL)
276 {
277 pRtlGetVersion =
278 (PfnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
279 pRtlGetVersion(&osver);
280 ver = MAKE_VER(min(osver.dwMajorVersion, 255),
281 min(osver.dwMinorVersion, 255),
282 min(osver.dwBuildNumber, 32767));
283 }
284 return ver;
285}
286
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200287#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100288/*
289 * Version of ReadConsoleInput() that works with IME.
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100290 * Works around problems on Windows 8.
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100291 */
292 static BOOL
293read_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100294 HANDLE hInput,
295 INPUT_RECORD *lpBuffer,
296 DWORD nLength,
297 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100298{
299 enum
300 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100301 IRSIZE = 10
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100302 };
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100303 static INPUT_RECORD s_irCache[IRSIZE];
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100304 static DWORD s_dwIndex = 0;
305 static DWORD s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100306 DWORD dwEvents;
Bram Moolenaardd415a62014-02-05 14:02:27 +0100307 int head;
308 int tail;
309 int i;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100310
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200311 if (nLength == -2)
312 return (s_dwMax > 0) ? TRUE : FALSE;
313
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100314 if (!win8_or_later)
315 {
316 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200317 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
318 return ReadConsoleInputW(hInput, lpBuffer, 1, &dwEvents);
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100319 }
320
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100321 if (s_dwMax == 0)
322 {
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100323 if (nLength == -1)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200324 return PeekConsoleInputW(hInput, lpBuffer, 1, lpEvents);
325 if (!ReadConsoleInputW(hInput, s_irCache, IRSIZE, &dwEvents))
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100326 return FALSE;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100327 s_dwIndex = 0;
328 s_dwMax = dwEvents;
329 if (dwEvents == 0)
330 {
331 *lpEvents = 0;
332 return TRUE;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100333 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100334
335 if (s_dwMax > 1)
336 {
337 head = 0;
338 tail = s_dwMax - 1;
339 while (head != tail)
340 {
341 if (s_irCache[head].EventType == WINDOW_BUFFER_SIZE_EVENT
342 && s_irCache[head + 1].EventType
343 == WINDOW_BUFFER_SIZE_EVENT)
344 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100345 // Remove duplicate event to avoid flicker.
Bram Moolenaardd415a62014-02-05 14:02:27 +0100346 for (i = head; i < tail; ++i)
347 s_irCache[i] = s_irCache[i + 1];
348 --tail;
349 continue;
350 }
351 head++;
352 }
353 s_dwMax = tail + 1;
354 }
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100355 }
Bram Moolenaardd415a62014-02-05 14:02:27 +0100356
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100357 *lpBuffer = s_irCache[s_dwIndex];
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200358 if (!(nLength == -1 || nLength == -2) && ++s_dwIndex >= s_dwMax)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100359 s_dwMax = 0;
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100360 *lpEvents = 1;
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100361 return TRUE;
362}
363
364/*
365 * Version of PeekConsoleInput() that works with IME.
366 */
367 static BOOL
368peek_console_input(
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100369 HANDLE hInput,
370 INPUT_RECORD *lpBuffer,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200371 DWORD nLength UNUSED,
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100372 LPDWORD lpEvents)
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100373{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100374 return read_console_input(hInput, lpBuffer, -1, lpEvents);
Bram Moolenaar3a69e112014-01-10 13:51:42 +0100375}
376
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100377# ifdef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200378 static DWORD
379msg_wait_for_multiple_objects(
380 DWORD nCount,
381 LPHANDLE pHandles,
382 BOOL fWaitAll,
383 DWORD dwMilliseconds,
384 DWORD dwWakeMask)
385{
386 if (read_console_input(NULL, NULL, -2, NULL))
387 return WAIT_OBJECT_0;
388 return MsgWaitForMultipleObjects(nCount, pHandles, fWaitAll,
389 dwMilliseconds, dwWakeMask);
390}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100391# endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200392
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100393# ifndef FEAT_CLIENTSERVER
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200394 static DWORD
395wait_for_single_object(
396 HANDLE hHandle,
397 DWORD dwMilliseconds)
398{
399 if (read_console_input(NULL, NULL, -2, NULL))
400 return WAIT_OBJECT_0;
401 return WaitForSingleObject(hHandle, dwMilliseconds);
402}
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100403# endif
404#endif
Bram Moolenaarbb86ebb2015-08-04 19:27:05 +0200405
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 static void
407get_exe_name(void)
408{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100409 // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned
410 // as the maximum length that works (plus a NUL byte).
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100411#define MAX_ENV_PATH_LEN 8192
412 char temp[MAX_ENV_PATH_LEN];
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200413 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415 if (exe_name == NULL)
416 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100417 // store the name of the executable, may be used for $VIM
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100418 GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 if (*temp != NUL)
420 exe_name = FullName_save((char_u *)temp, FALSE);
421 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000422
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200423 if (exe_path == NULL && exe_name != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000424 {
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200425 exe_path = vim_strnsave(exe_name,
426 (int)(gettail_sep(exe_name) - exe_name));
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200427 if (exe_path != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000428 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100429 // Append our starting directory to $PATH, so that when doing
430 // "!xxd" it's found in our starting directory. Needed because
431 // SearchPath() also looks there.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200432 p = mch_getenv("PATH");
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100433 if (p == NULL
434 || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200435 {
Bram Moolenaar27d9ece2010-11-10 15:37:05 +0100436 if (p == NULL || *p == NUL)
437 temp[0] = NUL;
438 else
439 {
440 STRCPY(temp, p);
441 STRCAT(temp, ";");
442 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200443 STRCAT(temp, exe_path);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100444 vim_setenv((char_u *)"PATH", (char_u *)temp);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200445 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000446 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448}
449
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200450/*
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100451 * Unescape characters in "p" that appear in "escaped".
452 */
453 static void
454unescape_shellxquote(char_u *p, char_u *escaped)
455{
Bram Moolenaar4336cdf2012-02-29 13:58:47 +0100456 int l = (int)STRLEN(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100457 int n;
458
459 while (*p != NUL)
460 {
461 if (*p == '^' && vim_strchr(escaped, p[1]) != NULL)
462 mch_memmove(p, p + 1, l--);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100463 n = (*mb_ptr2len)(p);
Bram Moolenaar6b707b42012-02-21 21:22:44 +0100464 p += n;
465 l -= n;
466 }
467}
468
469/*
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200470 * Load library "name".
471 */
472 HINSTANCE
473vimLoadLib(char *name)
474{
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200475 HINSTANCE dll = NULL;
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200476
Bram Moolenaar0f873732019-12-05 20:28:46 +0100477 // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call
478 // vimLoadLib() recursively, which causes a stack overflow.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200479 if (exe_path == NULL)
480 get_exe_name();
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200481 if (exe_path != NULL)
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200482 {
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200483 WCHAR old_dirw[MAXPATHL];
484
485 if (GetCurrentDirectoryW(MAXPATHL, old_dirw) != 0)
486 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100487 // Change directory to where the executable is, both to make
488 // sure we find a .dll there and to avoid looking for a .dll
489 // in the current directory.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100490 SetCurrentDirectory((LPCSTR)exe_path);
Bram Moolenaar17aa8cc2012-10-21 21:38:45 +0200491 dll = LoadLibrary(name);
492 SetCurrentDirectoryW(old_dirw);
493 return dll;
494 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200495 }
496 return dll;
497}
498
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200499#if defined(VIMDLL) || defined(PROTO)
500/*
501 * Check if the current executable file is for the GUI subsystem.
502 */
503 int
504mch_is_gui_executable(void)
505{
506 PBYTE pImage = (PBYTE)GetModuleHandle(NULL);
507 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pImage;
508 PIMAGE_NT_HEADERS pPE;
509
510 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
511 return FALSE;
512 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
513 if (pPE->Signature != IMAGE_NT_SIGNATURE)
514 return FALSE;
515 if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
516 return TRUE;
517 return FALSE;
518}
519#endif
520
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100521#if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
522/*
523 * Get related information about 'funcname' which is imported by 'hInst'.
524 * If 'info' is 0, return the function address.
525 * If 'info' is 1, return the module name which the function is imported from.
526 */
527 static void *
528get_imported_func_info(HINSTANCE hInst, const char *funcname, int info)
529{
530 PBYTE pImage = (PBYTE)hInst;
531 PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
532 PIMAGE_NT_HEADERS pPE;
533 PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100534 PIMAGE_THUNK_DATA pIAT; // Import Address Table
535 PIMAGE_THUNK_DATA pINT; // Import Name Table
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100536 PIMAGE_IMPORT_BY_NAME pImpName;
537
538 if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
539 return NULL;
540 pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
541 if (pPE->Signature != IMAGE_NT_SIGNATURE)
542 return NULL;
543 pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
544 + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
545 .VirtualAddress);
546 for (; pImpDesc->FirstThunk; ++pImpDesc)
547 {
548 if (!pImpDesc->OriginalFirstThunk)
549 continue;
550 pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
551 pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
552 for (; pIAT->u1.Function; ++pIAT, ++pINT)
553 {
554 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
555 continue;
556 pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage
557 + (UINT_PTR)(pINT->u1.AddressOfData));
558 if (strcmp((char *)pImpName->Name, funcname) == 0)
559 {
560 switch (info)
561 {
562 case 0:
563 return (void *)pIAT->u1.Function;
564 case 1:
565 return (void *)(pImage + pImpDesc->Name);
566 default:
567 return NULL;
568 }
569 }
570 }
571 }
572 return NULL;
573}
574
575/*
576 * Get the module handle which 'funcname' in 'hInst' is imported from.
577 */
578 HINSTANCE
579find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname)
580{
581 char *modulename;
582
583 modulename = (char *)get_imported_func_info(hInst, funcname, 1);
584 if (modulename != NULL)
585 return GetModuleHandleA(modulename);
586 return NULL;
587}
588
589/*
590 * Get the address of 'funcname' which is imported by 'hInst' DLL.
591 */
592 void *
593get_dll_import_func(HINSTANCE hInst, const char *funcname)
594{
595 return get_imported_func_info(hInst, funcname, 0);
596}
597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599#if defined(DYNAMIC_GETTEXT) || defined(PROTO)
600# ifndef GETTEXT_DLL
601# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar7554c542018-10-06 15:03:15 +0200602# define GETTEXT_DLL_ALT1 "libintl-8.dll"
603# define GETTEXT_DLL_ALT2 "intl.dll"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100605// Dummy functions
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000606static char *null_libintl_gettext(const char *);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200607static char *null_libintl_ngettext(const char *, const char *, unsigned long n);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000608static char *null_libintl_textdomain(const char *);
609static char *null_libintl_bindtextdomain(const char *, const char *);
610static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100611static int null_libintl_wputenv(const wchar_t *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200613static HINSTANCE hLibintlDLL = NULL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000614char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200615char *(*dyn_libintl_ngettext)(const char *, const char *, unsigned long n)
616 = null_libintl_ngettext;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000617char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
618char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000620char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
621 = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100622int (*dyn_libintl_wputenv)(const wchar_t *) = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100625dyn_libintl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 int i;
628 static struct
629 {
630 char *name;
631 FARPROC *ptr;
632 } libintl_entry[] =
633 {
634 {"gettext", (FARPROC*)&dyn_libintl_gettext},
Bram Moolenaaree695f72016-08-03 22:08:45 +0200635 {"ngettext", (FARPROC*)&dyn_libintl_ngettext},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {"textdomain", (FARPROC*)&dyn_libintl_textdomain},
637 {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
638 {NULL, NULL}
639 };
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100640 HINSTANCE hmsvcrt;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar7554c542018-10-06 15:03:15 +0200642 // No need to initialize twice.
643 if (hLibintlDLL != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 return 1;
Bram Moolenaar7554c542018-10-06 15:03:15 +0200645 // Load gettext library (libintl.dll and other names).
Bram Moolenaar923e43b2016-01-28 15:07:38 +0100646 hLibintlDLL = vimLoadLib(GETTEXT_DLL);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100647# ifdef GETTEXT_DLL_ALT1
Bram Moolenaar286eacd2016-01-16 18:05:50 +0100648 if (!hLibintlDLL)
Bram Moolenaar7554c542018-10-06 15:03:15 +0200649 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100650# endif
651# ifdef GETTEXT_DLL_ALT2
Bram Moolenaar7554c542018-10-06 15:03:15 +0200652 if (!hLibintlDLL)
653 hLibintlDLL = vimLoadLib(GETTEXT_DLL_ALT2);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100654# endif
Bram Moolenaar938ee832016-01-24 15:36:03 +0100655 if (!hLibintlDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200657 if (p_verbose > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200659 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100660 semsg(_(e_loadlib), GETTEXT_DLL);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200661 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200663 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665 for (i = 0; libintl_entry[i].name != NULL
666 && libintl_entry[i].ptr != NULL; ++i)
667 {
668 if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL,
669 libintl_entry[i].name)) == NULL)
670 {
671 dyn_libintl_end();
672 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000673 {
674 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_(e_loadfunc), libintl_entry[i].name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +0000676 verbose_leave();
677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return 0;
679 }
680 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000681
Bram Moolenaar0f873732019-12-05 20:28:46 +0100682 // The bind_textdomain_codeset() function is optional.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000683 dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000684 "bind_textdomain_codeset");
685 if (dyn_libintl_bind_textdomain_codeset == NULL)
686 dyn_libintl_bind_textdomain_codeset =
687 null_libintl_bind_textdomain_codeset;
688
Bram Moolenaar0f873732019-12-05 20:28:46 +0100689 // _wputenv() function for the libintl.dll is optional.
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100690 hmsvcrt = find_imported_module_by_funcname(hLibintlDLL, "getenv");
691 if (hmsvcrt != NULL)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100692 dyn_libintl_wputenv = (void *)GetProcAddress(hmsvcrt, "_wputenv");
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100693 if (dyn_libintl_wputenv == NULL || dyn_libintl_wputenv == _wputenv)
694 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar972c3b82017-01-12 21:44:49 +0100695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 return 1;
697}
698
699 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100700dyn_libintl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 if (hLibintlDLL)
703 FreeLibrary(hLibintlDLL);
704 hLibintlDLL = NULL;
705 dyn_libintl_gettext = null_libintl_gettext;
Bram Moolenaaree695f72016-08-03 22:08:45 +0200706 dyn_libintl_ngettext = null_libintl_ngettext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 dyn_libintl_textdomain = null_libintl_textdomain;
708 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000709 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100710 dyn_libintl_wputenv = null_libintl_wputenv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711}
712
713 static char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000714null_libintl_gettext(const char *msgid)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 return (char*)msgid;
717}
718
719 static char *
Bram Moolenaaree695f72016-08-03 22:08:45 +0200720null_libintl_ngettext(
721 const char *msgid,
722 const char *msgid_plural,
723 unsigned long n)
724{
Bram Moolenaarc90f2ae2016-08-04 22:00:15 +0200725 return (char *)(n == 1 ? msgid : msgid_plural);
Bram Moolenaaree695f72016-08-03 22:08:45 +0200726}
727
Bram Moolenaaree695f72016-08-03 22:08:45 +0200728 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100729null_libintl_bindtextdomain(
730 const char *domainname UNUSED,
731 const char *dirname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 return NULL;
734}
735
736 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100737null_libintl_bind_textdomain_codeset(
738 const char *domainname UNUSED,
739 const char *codeset UNUSED)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000740{
741 return NULL;
742}
743
744 static char *
Bram Moolenaar1266d672017-02-01 13:43:36 +0100745null_libintl_textdomain(const char *domainname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 return NULL;
748}
749
Bram Moolenaare0ab9792017-08-02 23:18:25 +0200750 static int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100751null_libintl_wputenv(const wchar_t *envstring UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +0100752{
753 return 0;
754}
755
Bram Moolenaar0f873732019-12-05 20:28:46 +0100756#endif // DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaar0f873732019-12-05 20:28:46 +0100758// This symbol is not defined in older versions of the SDK or Visual C++
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760#ifndef VER_PLATFORM_WIN32_WINDOWS
761# define VER_PLATFORM_WIN32_WINDOWS 1
762#endif
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764#ifdef HAVE_ACL
Bram Moolenaar82881492012-11-20 16:53:39 +0100765# ifndef PROTO
766# include <aclapi.h>
767# endif
Bram Moolenaar27515922013-06-29 15:36:26 +0200768# ifndef PROTECTED_DACL_SECURITY_INFORMATION
769# define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772
Bram Moolenaar27515922013-06-29 15:36:26 +0200773#ifdef HAVE_ACL
774/*
775 * Enables or disables the specified privilege.
776 */
777 static BOOL
778win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
779{
Bram Moolenaarb0d5c962014-01-12 13:24:51 +0100780 BOOL bResult;
781 LUID luid;
782 HANDLE hToken;
783 TOKEN_PRIVILEGES tokenPrivileges;
Bram Moolenaar27515922013-06-29 15:36:26 +0200784
785 if (!OpenProcessToken(GetCurrentProcess(),
786 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
787 return FALSE;
788
789 if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
790 {
791 CloseHandle(hToken);
792 return FALSE;
793 }
794
Bram Moolenaar45500912014-07-09 20:51:07 +0200795 tokenPrivileges.PrivilegeCount = 1;
Bram Moolenaar27515922013-06-29 15:36:26 +0200796 tokenPrivileges.Privileges[0].Luid = luid;
797 tokenPrivileges.Privileges[0].Attributes = bEnable ?
798 SE_PRIVILEGE_ENABLED : 0;
799
800 bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
801 sizeof(TOKEN_PRIVILEGES), NULL, NULL);
802
803 CloseHandle(hToken);
804
805 return bResult && GetLastError() == ERROR_SUCCESS;
806}
807#endif
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100810 * Set "win8_or_later" and fill in "windowsVersion".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 */
812 void
813PlatformId(void)
814{
815 static int done = FALSE;
816
817 if (!done)
818 {
819 OSVERSIONINFO ovi;
820
821 ovi.dwOSVersionInfoSize = sizeof(ovi);
822 GetVersionEx(&ovi);
823
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100824 vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
825 (int)ovi.dwMajorVersion, (int)ovi.dwMinorVersion);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaarf50eb782014-02-05 13:36:54 +0100827 if ((ovi.dwMajorVersion == 6 && ovi.dwMinorVersion >= 2)
828 || ovi.dwMajorVersion > 6)
829 win8_or_later = TRUE;
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef HAVE_ACL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100832 // Enable privilege for getting or setting SACLs.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200833 win32_enable_privilege(SE_SECURITY_NAME, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif
835 done = TRUE;
836 }
837}
838
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200839#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100841# define SHIFT (SHIFT_PRESSED)
842# define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
843# define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)
844# define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846
Bram Moolenaar0f873732019-12-05 20:28:46 +0100847// When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode.
848// We map function keys to their ANSI terminal equivalents, as produced
849// by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any
850// ANSI key with a value >= '\300' is nonstandard, but provided anyway
851// so that the user can have access to all SHIFT-, CTRL-, and ALT-
852// combinations of function/arrow/etc keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaard6f676d2005-06-01 21:51:55 +0000854static const struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 WORD wVirtKey;
857 BOOL fAnsiKey;
858 int chAlone;
859 int chShift;
860 int chCtrl;
861 int chAlt;
862} VirtKeyMap[] =
863{
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200864// Key ANSI alone shift ctrl alt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, },
866
867 { VK_F1, TRUE, ';', 'T', '^', 'h', },
868 { VK_F2, TRUE, '<', 'U', '_', 'i', },
869 { VK_F3, TRUE, '=', 'V', '`', 'j', },
870 { VK_F4, TRUE, '>', 'W', 'a', 'k', },
871 { VK_F5, TRUE, '?', 'X', 'b', 'l', },
872 { VK_F6, TRUE, '@', 'Y', 'c', 'm', },
873 { VK_F7, TRUE, 'A', 'Z', 'd', 'n', },
874 { VK_F8, TRUE, 'B', '[', 'e', 'o', },
875 { VK_F9, TRUE, 'C', '\\', 'f', 'p', },
876 { VK_F10, TRUE, 'D', ']', 'g', 'q', },
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200877 { VK_F11, TRUE, '\205', '\207', '\211', '\213', },
878 { VK_F12, TRUE, '\206', '\210', '\212', '\214', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200880 { VK_HOME, TRUE, 'G', '\302', 'w', '\303', },
881 { VK_UP, TRUE, 'H', '\304', '\305', '\306', },
882 { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, // PgUp
883 { VK_LEFT, TRUE, 'K', '\311', 's', '\312', },
884 { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', },
885 { VK_END, TRUE, 'O', '\315', 'u', '\316', },
886 { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', },
887 { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, // PgDn
888 { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', },
889 { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', },
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100890 { VK_BACK, TRUE, 'x', 'y', 'z', '{', }, // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200892 { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, // PrtScrn
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100894# if 0
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200895 // Most people don't have F13-F20, but what the hell...
896 { VK_F13, TRUE, '\332', '\333', '\334', '\335', },
897 { VK_F14, TRUE, '\336', '\337', '\340', '\341', },
898 { VK_F15, TRUE, '\342', '\343', '\344', '\345', },
899 { VK_F16, TRUE, '\346', '\347', '\350', '\351', },
900 { VK_F17, TRUE, '\352', '\353', '\354', '\355', },
901 { VK_F18, TRUE, '\356', '\357', '\360', '\361', },
902 { VK_F19, TRUE, '\362', '\363', '\364', '\365', },
903 { VK_F20, TRUE, '\366', '\367', '\370', '\371', },
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100904# endif
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200905 { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, // keyp '+'
906 { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, // keyp '-'
907 // { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, // keyp '/'
908 { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, // keyp '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +0200910 { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', },
911 { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', },
912 { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', },
913 { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', },
914 { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', },
915 { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', },
916 { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', },
917 { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', },
918 { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', },
919 // Sorry, out of number space! <negri>
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100920 { VK_NUMPAD9,TRUE, '\376', '\377', '|', '}', },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921};
922
923
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100924# if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__)
925# define UChar UnicodeChar
926# else
927# define UChar uChar.UnicodeChar
928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaar0f873732019-12-05 20:28:46 +0100930/*
931 * The return code indicates key code size.
932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934win32_kbd_patch_key(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000935 KEY_EVENT_RECORD *pker)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 UINT uMods = pker->dwControlKeyState;
938 static int s_iIsDead = 0;
939 static WORD awAnsiCode[2];
940 static BYTE abKeystate[256];
941
942
943 if (s_iIsDead == 2)
944 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200945 pker->UChar = (WCHAR) awAnsiCode[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 s_iIsDead = 0;
947 return 1;
948 }
949
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200950 if (pker->UChar != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return 1;
952
Bram Moolenaara80faa82020-04-12 19:37:17 +0200953 CLEAR_FIELD(abKeystate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
Bram Moolenaar0f873732019-12-05 20:28:46 +0100955 // Clear any pending dead keys
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200956 ToUnicode(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 if (uMods & SHIFT_PRESSED)
959 abKeystate[VK_SHIFT] = 0x80;
960 if (uMods & CAPSLOCK_ON)
961 abKeystate[VK_CAPITAL] = 1;
962
963 if ((uMods & ALT_GR) == ALT_GR)
964 {
965 abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] =
966 abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80;
967 }
968
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200969 s_iIsDead = ToUnicode(pker->wVirtualKeyCode, pker->wVirtualScanCode,
970 abKeystate, awAnsiCode, 2, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 if (s_iIsDead > 0)
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200973 pker->UChar = (WCHAR) awAnsiCode[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 return s_iIsDead;
976}
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978static BOOL g_fJustGotFocus = FALSE;
979
980/*
981 * Decode a KEY_EVENT into one or two keystrokes
982 */
983 static BOOL
984decode_key_event(
985 KEY_EVENT_RECORD *pker,
Bram Moolenaarac360bf2015-09-01 20:31:20 +0200986 WCHAR *pch,
987 WCHAR *pch2,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 int *pmodifiers,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200989 BOOL fDoPost UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 int i;
992 const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL);
993
994 *pch = *pch2 = NUL;
995 g_fJustGotFocus = FALSE;
996
Bram Moolenaar0f873732019-12-05 20:28:46 +0100997 // ignore key up events
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (!pker->bKeyDown)
999 return FALSE;
1000
Bram Moolenaar0f873732019-12-05 20:28:46 +01001001 // ignore some keystrokes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 switch (pker->wVirtualKeyCode)
1003 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001004 // modifiers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 case VK_SHIFT:
1006 case VK_CONTROL:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001007 case VK_MENU: // Alt key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 return FALSE;
1009
1010 default:
1011 break;
1012 }
1013
Bram Moolenaar0f873732019-12-05 20:28:46 +01001014 // special cases
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001015 if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->UChar == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001017 // Ctrl-6 is Ctrl-^
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (pker->wVirtualKeyCode == '6')
1019 {
1020 *pch = Ctrl_HAT;
1021 return TRUE;
1022 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001023 // Ctrl-2 is Ctrl-@
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 else if (pker->wVirtualKeyCode == '2')
1025 {
1026 *pch = NUL;
1027 return TRUE;
1028 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029 // Ctrl-- is Ctrl-_
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 else if (pker->wVirtualKeyCode == 0xBD)
1031 {
1032 *pch = Ctrl__;
1033 return TRUE;
1034 }
1035 }
1036
Bram Moolenaar0f873732019-12-05 20:28:46 +01001037 // Shift-TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED))
1039 {
1040 *pch = K_NUL;
1041 *pch2 = '\017';
1042 return TRUE;
1043 }
1044
1045 for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; )
1046 {
1047 if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode)
1048 {
1049 if (nModifs == 0)
1050 *pch = VirtKeyMap[i].chAlone;
1051 else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0)
1052 *pch = VirtKeyMap[i].chShift;
1053 else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0)
1054 *pch = VirtKeyMap[i].chCtrl;
1055 else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0)
1056 *pch = VirtKeyMap[i].chAlt;
1057
1058 if (*pch != 0)
1059 {
1060 if (VirtKeyMap[i].fAnsiKey)
1061 {
1062 *pch2 = *pch;
1063 *pch = K_NUL;
1064 }
1065
1066 return TRUE;
1067 }
1068 }
1069 }
1070
1071 i = win32_kbd_patch_key(pker);
1072
1073 if (i < 0)
1074 *pch = NUL;
1075 else
1076 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001077 *pch = (i > 0) ? pker->UChar : NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 if (pmodifiers != NULL)
1080 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001081 // Pass on the ALT key as a modifier, but only when not combined
1082 // with CTRL (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0)
1084 *pmodifiers |= MOD_MASK_ALT;
1085
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 // Pass on SHIFT only for special keys, because we don't know when
1087 // it's already included with the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if ((nModifs & SHIFT) != 0 && *pch <= 0x20)
1089 *pmodifiers |= MOD_MASK_SHIFT;
1090
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // Pass on CTRL only for non-special keys, because we don't know
1092 // when it's already included with the character. And not when
1093 // combined with ALT (which is ALTGR).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0
1095 && *pch >= 0x20 && *pch < 0x80)
1096 *pmodifiers |= MOD_MASK_CTRL;
1097 }
1098 }
1099
1100 return (*pch != NUL);
1101}
1102
Bram Moolenaar0f873732019-12-05 20:28:46 +01001103#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106/*
1107 * For the GUI the mouse handling is in gui_w32.c.
1108 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001109#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001111mch_setmouse(int on UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001114#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001115static int g_fMouseAvail = FALSE; // mouse present
1116static int g_fMouseActive = FALSE; // mouse enabled
1117static int g_nMouseClick = -1; // mouse status
1118static int g_xMouse; // mouse x coordinate
1119static int g_yMouse; // mouse y coordinate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121/*
1122 * Enable or disable mouse input
1123 */
1124 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001125mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 DWORD cmodein;
1128
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001129# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001130 if (gui.in_use)
1131 return;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (!g_fMouseAvail)
1134 return;
1135
1136 g_fMouseActive = on;
1137 GetConsoleMode(g_hConIn, &cmodein);
1138
1139 if (g_fMouseActive)
1140 cmodein |= ENABLE_MOUSE_INPUT;
1141 else
1142 cmodein &= ~ENABLE_MOUSE_INPUT;
1143
1144 SetConsoleMode(g_hConIn, cmodein);
1145}
1146
Bram Moolenaar157d8132018-03-06 17:09:20 +01001147
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001148# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001149/*
1150 * Called when 'balloonevalterm' changed.
1151 */
1152 void
1153mch_bevalterm_changed(void)
1154{
1155 mch_setmouse(g_fMouseActive);
1156}
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001157# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
1160 * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT,
1161 * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse
1162 * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG
1163 * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type,
1164 * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick,
1165 * and we return the mouse position in g_xMouse and g_yMouse.
1166 *
1167 * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more
1168 * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only
1169 * by MOUSE_LEFT, _MIDDLE, or _RIGHT.
1170 *
1171 * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE,
1172 * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, ....
1173 *
1174 * Windows will send us MOUSE_MOVED notifications whenever the mouse
1175 * moves, even if it stays within the same character cell. We ignore
1176 * all MOUSE_MOVED messages if the position hasn't really changed, and
1177 * we ignore all MOUSE_MOVED messages where no button is held down (i.e.,
1178 * we're only interested in MOUSE_DRAG).
1179 *
1180 * All of this is complicated by the code that fakes MOUSE_MIDDLE on
1181 * 2-button mouses by pressing the left & right buttons simultaneously.
1182 * In practice, it's almost impossible to click both at the same time,
1183 * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE
1184 * in such cases, if the user is clicking quickly.
1185 */
1186 static BOOL
1187decode_mouse_event(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001188 MOUSE_EVENT_RECORD *pmer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 static int s_nOldButton = -1;
1191 static int s_nOldMouseClick = -1;
1192 static int s_xOldMouse = -1;
1193 static int s_yOldMouse = -1;
1194 static linenr_T s_old_topline = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001195# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 static int s_old_topfill = 0;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 static int s_cClicks = 1;
1199 static BOOL s_fReleased = TRUE;
1200 static DWORD s_dwLastClickTime = 0;
1201 static BOOL s_fNextIsMiddle = FALSE;
1202
Bram Moolenaar0f873732019-12-05 20:28:46 +01001203 static DWORD cButtons = 0; // number of buttons supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED;
1206 const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED;
1207 const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED;
1208 const DWORD LEFT_RIGHT = LEFT | RIGHT;
1209
1210 int nButton;
1211
1212 if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons))
1213 cButtons = 2;
1214
1215 if (!g_fMouseAvail || !g_fMouseActive)
1216 {
1217 g_nMouseClick = -1;
1218 return FALSE;
1219 }
1220
Bram Moolenaar0f873732019-12-05 20:28:46 +01001221 // get a spurious MOUSE_EVENT immediately after receiving focus; ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (g_fJustGotFocus)
1223 {
1224 g_fJustGotFocus = FALSE;
1225 return FALSE;
1226 }
1227
Bram Moolenaar0f873732019-12-05 20:28:46 +01001228 // unprocessed mouse click?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (g_nMouseClick != -1)
1230 return TRUE;
1231
1232 nButton = -1;
1233 g_xMouse = pmer->dwMousePosition.X;
1234 g_yMouse = pmer->dwMousePosition.Y;
1235
1236 if (pmer->dwEventFlags == MOUSE_MOVED)
1237 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001238 // Ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these
1239 // events even when the mouse moves only within a char cell.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse)
1241 return FALSE;
1242 }
1243
Bram Moolenaar0f873732019-12-05 20:28:46 +01001244 // If no buttons are pressed...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0)
1246 {
Bram Moolenaar157d8132018-03-06 17:09:20 +01001247 nButton = MOUSE_RELEASE;
1248
Bram Moolenaar0f873732019-12-05 20:28:46 +01001249 // If the last thing returned was MOUSE_RELEASE, ignore this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (s_fReleased)
Bram Moolenaar157d8132018-03-06 17:09:20 +01001251 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001252# ifdef FEAT_BEVAL_TERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01001253 // do return mouse move events when we want them
Bram Moolenaar157d8132018-03-06 17:09:20 +01001254 if (p_bevalterm)
1255 nButton = MOUSE_DRAG;
1256 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001257# endif
Bram Moolenaar157d8132018-03-06 17:09:20 +01001258 return FALSE;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 s_fReleased = TRUE;
1262 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001263 else // one or more buttons pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001265 // on a 2-button mouse, hold down left and right buttons
1266 // simultaneously to get MIDDLE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 if (cButtons == 2 && s_nOldButton != MOUSE_DRAG)
1269 {
1270 DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT);
1271
Bram Moolenaar0f873732019-12-05 20:28:46 +01001272 // if either left or right button only is pressed, see if the
1273 // next mouse event has both of them pressed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 if (dwLR == LEFT || dwLR == RIGHT)
1275 {
1276 for (;;)
1277 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001278 // wait a short time for next input event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (WaitForSingleObject(g_hConIn, p_mouset / 3)
1280 != WAIT_OBJECT_0)
1281 break;
1282 else
1283 {
1284 DWORD cRecords = 0;
1285 INPUT_RECORD ir;
1286 MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
1287
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001288 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 if (cRecords == 0 || ir.EventType != MOUSE_EVENT
1291 || !(pmer2->dwButtonState & LEFT_RIGHT))
1292 break;
1293 else
1294 {
1295 if (pmer2->dwEventFlags != MOUSE_MOVED)
1296 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001297 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 return decode_mouse_event(pmer2);
1300 }
1301 else if (s_xOldMouse == pmer2->dwMousePosition.X &&
1302 s_yOldMouse == pmer2->dwMousePosition.Y)
1303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001304 // throw away spurious mouse move
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001305 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Bram Moolenaar0f873732019-12-05 20:28:46 +01001307 // are there any more mouse events in queue?
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001308 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310 if (cRecords==0 || ir.EventType != MOUSE_EVENT)
1311 break;
1312 }
1313 else
1314 break;
1315 }
1316 }
1317 }
1318 }
1319 }
1320
1321 if (s_fNextIsMiddle)
1322 {
1323 nButton = (pmer->dwEventFlags == MOUSE_MOVED)
1324 ? MOUSE_DRAG : MOUSE_MIDDLE;
1325 s_fNextIsMiddle = FALSE;
1326 }
1327 else if (cButtons == 2 &&
1328 ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT))
1329 {
1330 nButton = MOUSE_MIDDLE;
1331
1332 if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED)
1333 {
1334 s_fNextIsMiddle = TRUE;
1335 nButton = MOUSE_RELEASE;
1336 }
1337 }
1338 else if ((pmer->dwButtonState & LEFT) == LEFT)
1339 nButton = MOUSE_LEFT;
1340 else if ((pmer->dwButtonState & MIDDLE) == MIDDLE)
1341 nButton = MOUSE_MIDDLE;
1342 else if ((pmer->dwButtonState & RIGHT) == RIGHT)
1343 nButton = MOUSE_RIGHT;
1344
1345 if (! s_fReleased && ! s_fNextIsMiddle
1346 && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG)
1347 return FALSE;
1348
1349 s_fReleased = s_fNextIsMiddle;
1350 }
1351
1352 if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK)
1353 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001354 // button pressed or released, without mouse moving
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (nButton != -1 && nButton != MOUSE_RELEASE)
1356 {
1357 DWORD dwCurrentTime = GetTickCount();
1358
1359 if (s_xOldMouse != g_xMouse
1360 || s_yOldMouse != g_yMouse
1361 || s_nOldButton != nButton
1362 || s_old_topline != curwin->w_topline
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001363# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || s_old_topfill != curwin->w_topfill
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset)
1367 {
1368 s_cClicks = 1;
1369 }
1370 else if (++s_cClicks > 4)
1371 {
1372 s_cClicks = 1;
1373 }
1374
1375 s_dwLastClickTime = dwCurrentTime;
1376 }
1377 }
1378 else if (pmer->dwEventFlags == MOUSE_MOVED)
1379 {
1380 if (nButton != -1 && nButton != MOUSE_RELEASE)
1381 nButton = MOUSE_DRAG;
1382
1383 s_cClicks = 1;
1384 }
1385
1386 if (nButton == -1)
1387 return FALSE;
1388
1389 if (nButton != MOUSE_RELEASE)
1390 s_nOldButton = nButton;
1391
1392 g_nMouseClick = nButton;
1393
1394 if (pmer->dwControlKeyState & SHIFT_PRESSED)
1395 g_nMouseClick |= MOUSE_SHIFT;
1396 if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
1397 g_nMouseClick |= MOUSE_CTRL;
1398 if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
1399 g_nMouseClick |= MOUSE_ALT;
1400
1401 if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE)
1402 SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks);
1403
Bram Moolenaar0f873732019-12-05 20:28:46 +01001404 // only pass on interesting (i.e., different) mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (s_xOldMouse == g_xMouse
1406 && s_yOldMouse == g_yMouse
1407 && s_nOldMouseClick == g_nMouseClick)
1408 {
1409 g_nMouseClick = -1;
1410 return FALSE;
1411 }
1412
1413 s_xOldMouse = g_xMouse;
1414 s_yOldMouse = g_yMouse;
1415 s_old_topline = curwin->w_topline;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001416# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 s_old_topfill = curwin->w_topfill;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 s_nOldMouseClick = g_nMouseClick;
1420
1421 return TRUE;
1422}
1423
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001424#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426
1427#ifdef MCH_CURSOR_SHAPE
1428/*
1429 * Set the shape of the cursor.
1430 * 'thickness' can be from 1 (thin) to 99 (block)
1431 */
1432 static void
1433mch_set_cursor_shape(int thickness)
1434{
1435 CONSOLE_CURSOR_INFO ConsoleCursorInfo;
1436 ConsoleCursorInfo.dwSize = thickness;
1437 ConsoleCursorInfo.bVisible = s_cursor_visible;
1438
1439 SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo);
1440 if (s_cursor_visible)
1441 SetConsoleCursorPosition(g_hConOut, g_coord);
1442}
1443
1444 void
1445mch_update_cursor(void)
1446{
1447 int idx;
1448 int thickness;
1449
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001450# ifdef VIMDLL
1451 if (gui.in_use)
1452 return;
1453# endif
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
1456 * How the cursor is drawn depends on the current mode.
1457 */
1458 idx = get_shape_idx(FALSE);
1459
1460 if (shape_table[idx].shape == SHAPE_BLOCK)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001461 thickness = 99; // 100 doesn't work on W95
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 else
1463 thickness = shape_table[idx].percentage;
1464 mch_set_cursor_shape(thickness);
1465}
1466#endif
1467
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001468#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469/*
1470 * Handle FOCUS_EVENT.
1471 */
1472 static void
1473handle_focus_event(INPUT_RECORD ir)
1474{
1475 g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus;
1476 ui_focus_change((int)g_fJustGotFocus);
1477}
1478
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001479static void ResizeConBuf(HANDLE hConsole, COORD coordScreen);
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * Wait until console input from keyboard or mouse is available,
1483 * or the time is up.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001484 * When "ignore_input" is TRUE even wait when input is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 * Return TRUE if something is available FALSE if not.
1486 */
1487 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001488WaitForChar(long msec, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 DWORD dwNow = 0, dwEndTime = 0;
1491 INPUT_RECORD ir;
1492 DWORD cRecords;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001493 WCHAR ch, ch2;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001494# ifdef FEAT_TIMERS
Bram Moolenaar40b1b542016-04-20 20:18:23 +02001495 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498 if (msec > 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001499 // Wait until the specified time has elapsed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 dwEndTime = GetTickCount() + msec;
1501 else if (msec < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001502 // Wait forever.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 dwEndTime = INFINITE;
1504
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001505 // We need to loop until the end of the time period, because
1506 // we might get multiple unusable mouse events in that time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 for (;;)
1508 {
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001509 // Only process messages when waiting.
1510 if (msec != 0)
1511 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001512# ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001513 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001514# endif
1515# ifdef FEAT_MZSCHEME
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001516 mzvim_check_threads();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001517# endif
1518# ifdef FEAT_CLIENTSERVER
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001519 serverProcessPendingMessages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001520# endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02001521 }
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001522
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001523 if (g_nMouseClick != -1
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001524# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001525 || (!ignore_input && input_available())
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 )
1528 return TRUE;
1529
1530 if (msec > 0)
1531 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001532 // If the specified wait time has passed, return. Beware that
1533 // GetTickCount() may wrap around (overflow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 dwNow = GetTickCount();
Bram Moolenaarb7512b72013-08-10 12:45:09 +02001535 if ((int)(dwNow - dwEndTime) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 break;
1537 }
1538 if (msec != 0)
1539 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001540 DWORD dwWaitTime = dwEndTime - dwNow;
1541
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001542# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001543 // Check channel while waiting for input.
Bram Moolenaar9186a272016-02-23 19:34:01 +01001544 if (dwWaitTime > 100)
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001545 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01001546 dwWaitTime = 100;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 // If there is readahead then parse_queued_messages() timed out
1548 // and we should call it again soon.
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01001549 if (channel_any_readahead())
1550 dwWaitTime = 10;
1551 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001552# endif
1553# ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +01001554 if (p_beval && dwWaitTime > 100)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001555 // The 'balloonexpr' may indirectly invoke a callback while
1556 // waiting for a character, need to check often.
Bram Moolenaar59716a22017-03-01 20:32:44 +01001557 dwWaitTime = 100;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001558# endif
1559# ifdef FEAT_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001560 if (mzthreads_allowed() && p_mzq > 0
1561 && (msec < 0 || (long)dwWaitTime > p_mzq))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001562 dwWaitTime = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001563# endif
1564# ifdef FEAT_TIMERS
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001565 // When waiting very briefly don't trigger timers.
1566 if (dwWaitTime > 10)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001567 {
1568 long due_time;
1569
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001570 // Trigger timers and then get the time in msec until the next
1571 // one is due. Wait up to that time.
1572 due_time = check_due_timer();
1573 if (typebuf.tb_change_cnt != tb_change_cnt)
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001574 {
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001575 // timer may have used feedkeys().
1576 return FALSE;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001577 }
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001578 if (due_time > 0 && dwWaitTime > (DWORD)due_time)
1579 dwWaitTime = due_time;
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001580 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001581# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001582 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001583# ifdef FEAT_CLIENTSERVER
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001584 // Wait for either an event on the console input or a
1585 // message in the client-server window.
1586 msg_wait_for_multiple_objects(1, &g_hConIn, FALSE,
1587 dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001588# else
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001589 wait_for_single_object(g_hConIn, dwWaitTime)
1590 != WAIT_OBJECT_0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001591# endif
Bram Moolenaar1f20daa2019-01-19 21:12:24 +01001592 )
1593 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595
1596 cRecords = 0;
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001597 peek_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001599# ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (State & CMDLINE && msg_row == Rows - 1)
1601 {
1602 CONSOLE_SCREEN_BUFFER_INFO csbi;
1603
1604 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
1605 {
1606 if (csbi.dwCursorPosition.Y != msg_row)
1607 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001608 // The screen is now messed up, must redraw the
1609 // command line and later all the windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 redraw_all_later(CLEAR);
1611 cmdline_row -= (msg_row - csbi.dwCursorPosition.Y);
1612 redrawcmd();
1613 }
1614 }
1615 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 if (cRecords > 0)
1619 {
1620 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown)
1621 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001622# ifdef FEAT_MBYTE_IME
Bram Moolenaar0f873732019-12-05 20:28:46 +01001623 // Windows IME sends two '\n's with only one 'ENTER'. First:
1624 // wVirtualKeyCode == 13. second: wVirtualKeyCode == 0
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001625 if (ir.Event.KeyEvent.UChar == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 && ir.Event.KeyEvent.wVirtualKeyCode == 13)
1627 {
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001628 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 continue;
1630 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2,
1633 NULL, FALSE))
1634 return TRUE;
1635 }
1636
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001637 read_console_input(g_hConIn, &ir, 1, &cRecords);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 if (ir.EventType == FOCUS_EVENT)
1640 handle_focus_event(ir);
1641 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001642 {
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001643 COORD dwSize = ir.Event.WindowBufferSizeEvent.dwSize;
1644
Bram Moolenaar36698e32019-12-11 22:57:40 +01001645 // Only call shell_resized() when the size actually changed to
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001646 // avoid the screen is cleared.
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001647 if (dwSize.X != Columns || dwSize.Y != Rows)
1648 {
1649 CONSOLE_SCREEN_BUFFER_INFO csbi;
1650 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
Bram Moolenaar36698e32019-12-11 22:57:40 +01001651 dwSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001652 dwSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
Bram Moolenaar36698e32019-12-11 22:57:40 +01001653 if (dwSize.X != Columns || dwSize.Y != Rows)
1654 {
1655 ResizeConBuf(g_hConOut, dwSize);
1656 shell_resized();
1657 }
Bram Moolenaar78d21da2019-02-17 15:00:52 +01001658 }
Bram Moolenaarc33ecb22018-02-11 16:40:45 +01001659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 else if (ir.EventType == MOUSE_EVENT
1661 && decode_mouse_event(&ir.Event.MouseEvent))
1662 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else if (msec == 0)
1665 break;
1666 }
1667
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001668# ifdef FEAT_CLIENTSERVER
Bram Moolenaar0f873732019-12-05 20:28:46 +01001669 // Something might have been received while we were waiting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (input_available())
1671 return TRUE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001672# endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 return FALSE;
1675}
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677/*
1678 * return non-zero if a character is available
1679 */
1680 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001681mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001683# ifdef VIMDLL
1684 if (gui.in_use)
1685 return TRUE;
1686# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001687 return WaitForChar(0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001689
1690# if defined(FEAT_TERMINAL) || defined(PROTO)
1691/*
1692 * Check for any pending input or messages.
1693 */
1694 int
1695mch_check_messages(void)
1696{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001697# ifdef VIMDLL
1698 if (gui.in_use)
1699 return TRUE;
1700# endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001701 return WaitForChar(0L, TRUE);
1702}
1703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705/*
1706 * Create the console input. Used when reading stdin doesn't work.
1707 */
1708 static void
1709create_conin(void)
1710{
1711 g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE,
1712 FILE_SHARE_READ|FILE_SHARE_WRITE,
1713 (LPSECURITY_ATTRIBUTES) NULL,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001714 OPEN_EXISTING, 0, (HANDLE)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 did_create_conin = TRUE;
1716}
1717
1718/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01001719 * Get a keystroke or a mouse event, use a blocking wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001721 static WCHAR
1722tgetch(int *pmodifiers, WCHAR *pch2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001724 WCHAR ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726 for (;;)
1727 {
1728 INPUT_RECORD ir;
1729 DWORD cRecords = 0;
1730
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001731# ifdef FEAT_CLIENTSERVER
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001732 (void)WaitForChar(-1L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (input_available())
1734 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (g_nMouseClick != -1)
1736 return 0;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001737# endif
Bram Moolenaar3a69e112014-01-10 13:51:42 +01001738 if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
1740 if (did_create_conin)
1741 read_error_exit();
1742 create_conin();
1743 continue;
1744 }
1745
1746 if (ir.EventType == KEY_EVENT)
1747 {
1748 if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2,
1749 pmodifiers, TRUE))
1750 return ch;
1751 }
1752 else if (ir.EventType == FOCUS_EVENT)
1753 handle_focus_event(ir);
1754 else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT)
1755 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 else if (ir.EventType == MOUSE_EVENT)
1757 {
1758 if (decode_mouse_event(&ir.Event.MouseEvent))
1759 return 0;
1760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001763#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765
1766/*
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02001767 * mch_inchar(): low-level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Get one or more characters from the keyboard or the mouse.
1769 * If time == 0, do not wait for characters.
1770 * If time == n, wait a short time for characters.
1771 * If time == -1, wait forever for characters.
1772 * Returns the number of characters read into buf.
1773 */
1774 int
1775mch_inchar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001776 char_u *buf UNUSED,
1777 int maxlen UNUSED,
1778 long time UNUSED,
1779 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001781#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 int len;
1784 int c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001785# ifdef VIMDLL
1786// Extra space for maximum three CSIs. E.g. U+1B6DB -> 0xF0 0x9B 0x9B 0x9B.
1787# define TYPEAHEADSPACE 6
1788# else
1789# define TYPEAHEADSPACE 0
1790# endif
1791# define TYPEAHEADLEN (20 + TYPEAHEADSPACE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001792 static char_u typeahead[TYPEAHEADLEN]; // previously typed bytes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 static int typeaheadlen = 0;
1794
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001795# ifdef VIMDLL
1796 if (gui.in_use)
1797 return 0;
1798# endif
1799
Bram Moolenaar0f873732019-12-05 20:28:46 +01001800 // First use any typeahead that was kept because "buf" was too small.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (typeaheadlen > 0)
1802 goto theend;
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (time >= 0)
1805 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001806 if (!WaitForChar(time, FALSE)) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001809 else // time == -1, wait forever
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001811 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaar3918c952005-03-15 22:34:55 +00001813 /*
1814 * If there is no character available within 2 seconds (default)
1815 * write the autoscript file to disk. Or cause the CursorHold event
1816 * to be triggered.
1817 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001818 if (!WaitForChar(p_ut, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
Bram Moolenaard35f9712005-12-18 22:02:33 +00001820 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00001822 buf[0] = K_SPECIAL;
1823 buf[1] = KS_EXTRA;
1824 buf[2] = (int)KE_CURSORHOLD;
1825 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
Bram Moolenaar702517d2005-06-27 22:34:07 +00001827 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 }
1830
1831 /*
1832 * Try to read as many characters as there are, until the buffer is full.
1833 */
1834
Bram Moolenaar0f873732019-12-05 20:28:46 +01001835 // we will get at least one key. Get more if they are available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 g_fCBrkPressed = FALSE;
1837
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001838# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (fdDump)
1840 fputc('[', fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar0f873732019-12-05 20:28:46 +01001843 // Keep looping until there is something in the typeahead buffer and more
1844 // to get and still room in the buffer (up to two bytes for a char and
1845 // three bytes for a modifier).
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001846 while ((typeaheadlen == 0 || WaitForChar(0L, FALSE))
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001847 && typeaheadlen + 5 + TYPEAHEADSPACE <= TYPEAHEADLEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 if (typebuf_changed(tb_change_cnt))
1850 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001851 // "buf" may be invalid now if a client put something in the
1852 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 typeaheadlen = 0;
1854 break;
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (g_nMouseClick != -1)
1857 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001858# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (fdDump)
1860 fprintf(fdDump, "{%02x @ %d, %d}",
1861 g_nMouseClick, g_xMouse, g_yMouse);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 typeahead[typeaheadlen++] = ESC + 128;
1864 typeahead[typeaheadlen++] = 'M';
1865 typeahead[typeaheadlen++] = g_nMouseClick;
1866 typeahead[typeaheadlen++] = g_xMouse + '!';
1867 typeahead[typeaheadlen++] = g_yMouse + '!';
1868 g_nMouseClick = -1;
1869 }
1870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001872 WCHAR ch2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int modifiers = 0;
1874
1875 c = tgetch(&modifiers, &ch2);
1876
1877 if (typebuf_changed(tb_change_cnt))
1878 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001879 // "buf" may be invalid now if a client put something in the
1880 // typeahead buffer and "buf" is in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 typeaheadlen = 0;
1882 break;
1883 }
1884
1885 if (c == Ctrl_C && ctrl_c_interrupts)
1886 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001887# if defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 trash_input_buf();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 got_int = TRUE;
1891 }
1892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (g_nMouseClick == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 int n = 1;
1896
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001897 if (ch2 == NUL)
1898 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001899 int i, j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001900 char_u *p;
1901 WCHAR ch[2];
1902
1903 ch[0] = c;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001904 if (c >= 0xD800 && c <= 0xDBFF) // High surrogate
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001905 {
1906 ch[1] = tgetch(&modifiers, &ch2);
1907 n++;
1908 }
1909 p = utf16_to_enc(ch, &n);
1910 if (p != NULL)
1911 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001912 for (i = 0, j = 0; i < n; i++)
1913 {
1914 typeahead[typeaheadlen + j++] = p[i];
1915# ifdef VIMDLL
1916 if (p[i] == CSI)
1917 {
1918 typeahead[typeaheadlen + j++] = KS_EXTRA;
1919 typeahead[typeaheadlen + j++] = KE_CSI;
1920 }
1921# endif
1922 }
1923 n = j;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001924 vim_free(p);
1925 }
1926 }
1927 else
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001928 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +02001929 typeahead[typeaheadlen] = c;
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001930# ifdef VIMDLL
1931 if (c == CSI)
1932 {
1933 typeahead[typeaheadlen + 1] = KS_EXTRA;
1934 typeahead[typeaheadlen + 2] = KE_CSI;
1935 n = 3;
1936 }
1937# endif
1938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (ch2 != NUL)
1940 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001941 if (c == K_NUL)
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001942 {
Bram Moolenaar0cc7b2d2018-10-07 15:49:56 +02001943 switch (ch2)
1944 {
1945 case (WCHAR)'\324': // SHIFT+Insert
1946 case (WCHAR)'\325': // CTRL+Insert
1947 case (WCHAR)'\327': // SHIFT+Delete
1948 case (WCHAR)'\330': // CTRL+Delete
1949 typeahead[typeaheadlen + n] = (char_u)ch2;
1950 n++;
1951 break;
1952
1953 default:
1954 typeahead[typeaheadlen + n] = 3;
1955 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1956 n += 2;
1957 break;
1958 }
Bram Moolenaarfeeb4d02017-12-05 15:14:46 +01001959 }
1960 else
1961 {
1962 typeahead[typeaheadlen + n] = 3;
1963 typeahead[typeaheadlen + n + 1] = (char_u)ch2;
1964 n += 2;
1965 }
Bram Moolenaar45500912014-07-09 20:51:07 +02001966 }
1967
Bram Moolenaar0f873732019-12-05 20:28:46 +01001968 // Use the ALT key to set the 8th bit of the character
1969 // when it's one byte, the 8th bit isn't set yet and not
1970 // using a double-byte encoding (would become a lead
1971 // byte).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if ((modifiers & MOD_MASK_ALT)
1973 && n == 1
1974 && (typeahead[typeaheadlen] & 0x80) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 )
1977 {
Bram Moolenaar85a3e5c2007-11-20 16:22:16 +00001978 n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80,
1979 typeahead + typeaheadlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 modifiers &= ~MOD_MASK_ALT;
1981 }
1982
1983 if (modifiers != 0)
1984 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001985 // Prepend modifiers to the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 mch_memmove(typeahead + typeaheadlen + 3,
1987 typeahead + typeaheadlen, n);
1988 typeahead[typeaheadlen++] = K_SPECIAL;
1989 typeahead[typeaheadlen++] = (char_u)KS_MODIFIER;
1990 typeahead[typeaheadlen++] = modifiers;
1991 }
1992
1993 typeaheadlen += n;
1994
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001995# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (fdDump)
1997 fputc(c, fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001 }
2002
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002003# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (fdDump)
2005 {
2006 fputs("]\n", fdDump);
2007 fflush(fdDump);
2008 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002012 // Move typeahead to "buf", as much as fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 len = 0;
2014 while (len < maxlen && typeaheadlen > 0)
2015 {
2016 buf[len++] = typeahead[0];
2017 mch_memmove(typeahead, typeahead + 1, --typeaheadlen);
2018 }
2019 return len;
2020
Bram Moolenaar0f873732019-12-05 20:28:46 +01002021#else // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002023#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
Bram Moolenaar82881492012-11-20 16:53:39 +01002026#ifndef PROTO
2027# ifndef __MINGW32__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002028# include <shellapi.h> // required for FindExecutable()
Bram Moolenaar82881492012-11-20 16:53:39 +01002029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#endif
2031
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002032/*
Bram Moolenaar43663192017-03-05 14:29:12 +01002033 * If "use_path" is TRUE: Return TRUE if "name" is in $PATH.
2034 * If "use_path" is FALSE: Return TRUE if "name" exists.
2035 * When returning TRUE and "path" is not NULL save the path and set "*path" to
2036 * the allocated memory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002037 * TODO: Should somehow check if it's really executable.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 static int
Bram Moolenaar43663192017-03-05 14:29:12 +01002040executable_exists(char *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002042 WCHAR *p;
2043 WCHAR fnamew[_MAX_PATH];
2044 WCHAR *dumw;
2045 WCHAR *wcurpath, *wnewpath;
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002046 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
Bram Moolenaar43663192017-03-05 14:29:12 +01002048 if (!use_path)
2049 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002050 if (mch_getperm((char_u *)name) != -1 && !mch_isdir((char_u *)name))
Bram Moolenaar43663192017-03-05 14:29:12 +01002051 {
2052 if (path != NULL)
Bram Moolenaar066029e2017-03-05 15:19:32 +01002053 {
Bram Moolenaarf7e894d2017-03-05 19:49:13 +01002054 if (mch_isFullName((char_u *)name))
Bram Moolenaar066029e2017-03-05 15:19:32 +01002055 *path = vim_strsave((char_u *)name);
2056 else
2057 *path = FullName_save((char_u *)name, FALSE);
2058 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002059 return TRUE;
2060 }
2061 return FALSE;
2062 }
2063
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002064 p = enc_to_utf16((char_u *)name, NULL);
2065 if (p == NULL)
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002066 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002067
2068 wcurpath = _wgetenv(L"PATH");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002069 wnewpath = ALLOC_MULT(WCHAR, wcslen(wcurpath) + 3);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002070 if (wnewpath == NULL)
2071 return FALSE;
2072 wcscpy(wnewpath, L".;");
2073 wcscat(wnewpath, wcurpath);
2074 n = (long)SearchPathW(wnewpath, p, NULL, _MAX_PATH, fnamew, &dumw);
2075 vim_free(wnewpath);
2076 vim_free(p);
Bram Moolenaarc40bdee2014-08-29 17:45:32 +02002077 if (n == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002078 return FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002079 if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002080 return FALSE;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02002081 if (path != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002082 *path = utf16_to_enc(fnamew, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002083 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084}
2085
Bram Moolenaar1eed5322019-02-26 17:03:54 +01002086#if (defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x800) || \
2087 (defined(_MSC_VER) && _MSC_VER >= 1400)
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002088/*
2089 * Bad parameter handler.
2090 *
2091 * Certain MS CRT functions will intentionally crash when passed invalid
2092 * parameters to highlight possible security holes. Setting this function as
2093 * the bad parameter handler will prevent the crash.
2094 *
2095 * In debug builds the parameters contain CRT information that might help track
2096 * down the source of a problem, but in non-debug builds the arguments are all
2097 * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is
2098 * worth allowing these to make debugging of issues easier.
2099 */
2100 static void
2101bad_param_handler(const wchar_t *expression,
2102 const wchar_t *function,
2103 const wchar_t *file,
2104 unsigned int line,
2105 uintptr_t pReserved)
2106{
2107}
2108
2109# define SET_INVALID_PARAM_HANDLER \
2110 ((void)_set_invalid_parameter_handler(bad_param_handler))
2111#else
2112# define SET_INVALID_PARAM_HANDLER
2113#endif
2114
Bram Moolenaar4f974752019-02-17 17:44:42 +01002115#ifdef FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117/*
2118 * GUI version of mch_init().
2119 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002120 static void
2121mch_init_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002123# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
Bram Moolenaar0f873732019-12-05 20:28:46 +01002127 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002128 SET_INVALID_PARAM_HANDLER;
2129
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130 // Let critical errors result in a failure, not in a dialog box. Required
2131 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 SetErrorMode(SEM_FAILCRITICALERRORS);
2133
Bram Moolenaar0f873732019-12-05 20:28:46 +01002134 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar0f873732019-12-05 20:28:46 +01002136 // Specify window size. Is there a place to get the default from?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 Rows = 25;
2138 Columns = 80;
2139
Bram Moolenaar0f873732019-12-05 20:28:46 +01002140 // Look for 'vimrun'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 char_u vimrun_location[_MAX_PATH + 4];
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // First try in same directory as gvim.exe
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 STRCPY(vimrun_location, exe_name);
2146 STRCPY(gettail(vimrun_location), "vimrun.exe");
2147 if (mch_getperm(vimrun_location) >= 0)
2148 {
2149 if (*skiptowhite(vimrun_location) != NUL)
2150 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002151 // Enclose path with white space in double quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 mch_memmove(vimrun_location + 1, vimrun_location,
2153 STRLEN(vimrun_location) + 1);
2154 *vimrun_location = '"';
2155 STRCPY(gettail(vimrun_location), "vimrun\" ");
2156 }
2157 else
2158 STRCPY(gettail(vimrun_location), "vimrun ");
2159
2160 vimrun_path = (char *)vim_strsave(vimrun_location);
2161 s_dont_use_vimrun = FALSE;
2162 }
Bram Moolenaar43663192017-03-05 14:29:12 +01002163 else if (executable_exists("vimrun.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 s_dont_use_vimrun = FALSE;
2165
Bram Moolenaar0f873732019-12-05 20:28:46 +01002166 // Don't give the warning for a missing vimrun.exe right now, but only
2167 // when vimrun was supposed to be used. Don't bother people that do
2168 // not need vimrun.exe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (s_dont_use_vimrun)
2170 need_vimrun_warning = TRUE;
2171 }
2172
2173 /*
2174 * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'.
2175 * Otherwise the default "findstr /n" is used.
2176 */
Bram Moolenaar43663192017-03-05 14:29:12 +01002177 if (!executable_exists("findstr.exe", NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0);
2179
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002180# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002181 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002182# endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002183
2184 vtp_flag_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185}
2186
2187
Bram Moolenaar0f873732019-12-05 20:28:46 +01002188#endif // FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002189
2190#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002192# define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
2193# define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195/*
2196 * ClearConsoleBuffer()
2197 * Description:
2198 * Clears the entire contents of the console screen buffer, using the
2199 * specified attribute.
2200 * Returns:
2201 * TRUE on success
2202 */
2203 static BOOL
2204ClearConsoleBuffer(WORD wAttribute)
2205{
2206 CONSOLE_SCREEN_BUFFER_INFO csbi;
2207 COORD coord;
2208 DWORD NumCells, dummy;
2209
2210 if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2211 return FALSE;
2212
2213 NumCells = csbi.dwSize.X * csbi.dwSize.Y;
2214 coord.X = 0;
2215 coord.Y = 0;
2216 if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells,
2217 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells,
2220 coord, &dummy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 return TRUE;
2224}
2225
2226/*
2227 * FitConsoleWindow()
2228 * Description:
2229 * Checks if the console window will fit within given buffer dimensions.
2230 * Also, if requested, will shrink the window to fit.
2231 * Returns:
2232 * TRUE on success
2233 */
2234 static BOOL
2235FitConsoleWindow(
2236 COORD dwBufferSize,
2237 BOOL WantAdjust)
2238{
2239 CONSOLE_SCREEN_BUFFER_INFO csbi;
2240 COORD dwWindowSize;
2241 BOOL NeedAdjust = FALSE;
2242
2243 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
2244 {
2245 /*
2246 * A buffer resize will fail if the current console window does
2247 * not lie completely within that buffer. To avoid this, we might
2248 * have to move and possibly shrink the window.
2249 */
2250 if (csbi.srWindow.Right >= dwBufferSize.X)
2251 {
2252 dwWindowSize.X = SRWIDTH(csbi.srWindow);
2253 if (dwWindowSize.X > dwBufferSize.X)
2254 dwWindowSize.X = dwBufferSize.X;
2255 csbi.srWindow.Right = dwBufferSize.X - 1;
2256 csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X;
2257 NeedAdjust = TRUE;
2258 }
2259 if (csbi.srWindow.Bottom >= dwBufferSize.Y)
2260 {
2261 dwWindowSize.Y = SRHEIGHT(csbi.srWindow);
2262 if (dwWindowSize.Y > dwBufferSize.Y)
2263 dwWindowSize.Y = dwBufferSize.Y;
2264 csbi.srWindow.Bottom = dwBufferSize.Y - 1;
2265 csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y;
2266 NeedAdjust = TRUE;
2267 }
2268 if (NeedAdjust && WantAdjust)
2269 {
2270 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow))
2271 return FALSE;
2272 }
2273 return TRUE;
2274 }
2275
2276 return FALSE;
2277}
2278
2279typedef struct ConsoleBufferStruct
2280{
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002281 BOOL IsValid;
2282 CONSOLE_SCREEN_BUFFER_INFO Info;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002283 PCHAR_INFO Buffer;
2284 COORD BufferSize;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002285 PSMALL_RECT Regions;
2286 int NumRegions;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287} ConsoleBuffer;
2288
2289/*
2290 * SaveConsoleBuffer()
2291 * Description:
2292 * Saves important information about the console buffer, including the
2293 * actual buffer contents. The saved information is suitable for later
2294 * restoration by RestoreConsoleBuffer().
2295 * Returns:
2296 * TRUE if all information was saved; FALSE otherwise
2297 * If FALSE, still sets cb->IsValid if buffer characteristics were saved.
2298 */
2299 static BOOL
2300SaveConsoleBuffer(
2301 ConsoleBuffer *cb)
2302{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002303 DWORD NumCells;
2304 COORD BufferCoord;
2305 SMALL_RECT ReadRegion;
2306 WORD Y, Y_incr;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002307 int i, numregions;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (cb == NULL)
2310 return FALSE;
2311
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002312 if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 cb->IsValid = FALSE;
2315 return FALSE;
2316 }
2317 cb->IsValid = TRUE;
2318
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002319 /*
2320 * Allocate a buffer large enough to hold the entire console screen
2321 * buffer. If this ConsoleBuffer structure has already been initialized
2322 * with a buffer of the correct size, then just use that one.
2323 */
2324 if (!cb->IsValid || cb->Buffer == NULL ||
2325 cb->BufferSize.X != cb->Info.dwSize.X ||
2326 cb->BufferSize.Y != cb->Info.dwSize.Y)
2327 {
2328 cb->BufferSize.X = cb->Info.dwSize.X;
2329 cb->BufferSize.Y = cb->Info.dwSize.Y;
2330 NumCells = cb->BufferSize.X * cb->BufferSize.Y;
2331 vim_free(cb->Buffer);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002332 cb->Buffer = ALLOC_MULT(CHAR_INFO, NumCells);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002333 if (cb->Buffer == NULL)
2334 return FALSE;
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /*
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002338 * We will now copy the console screen buffer into our buffer.
2339 * ReadConsoleOutput() seems to be limited as far as how much you
2340 * can read at a time. Empirically, this number seems to be about
2341 * 12000 cells (rows * columns). Start at position (0, 0) and copy
2342 * in chunks until it is all copied. The chunks will all have the
2343 * same horizontal characteristics, so initialize them now. The
2344 * height of each chunk will be (12000 / width).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
Bram Moolenaar61594242015-09-01 20:23:37 +02002346 BufferCoord.X = 0;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002347 ReadRegion.Left = 0;
2348 ReadRegion.Right = cb->Info.dwSize.X - 1;
2349 Y_incr = 12000 / cb->Info.dwSize.X;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002350
2351 numregions = (cb->Info.dwSize.Y + Y_incr - 1) / Y_incr;
2352 if (cb->Regions == NULL || numregions != cb->NumRegions)
2353 {
2354 cb->NumRegions = numregions;
2355 vim_free(cb->Regions);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002356 cb->Regions = ALLOC_MULT(SMALL_RECT, cb->NumRegions);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002357 if (cb->Regions == NULL)
2358 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002359 VIM_CLEAR(cb->Buffer);
Bram Moolenaar444fda22017-08-11 20:37:00 +02002360 return FALSE;
2361 }
2362 }
2363
2364 for (i = 0, Y = 0; i < cb->NumRegions; i++, Y += Y_incr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002366 /*
2367 * Read into position (0, Y) in our buffer.
2368 */
2369 BufferCoord.Y = Y;
2370 /*
2371 * Read the region whose top left corner is (0, Y) and whose bottom
2372 * right corner is (width - 1, Y + Y_incr - 1). This should define
2373 * a region of size width by Y_incr. Don't worry if this region is
2374 * too large for the remaining buffer; it will be cropped.
2375 */
2376 ReadRegion.Top = Y;
2377 ReadRegion.Bottom = Y + Y_incr - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002378 if (!ReadConsoleOutputW(g_hConOut, // output handle
2379 cb->Buffer, // our buffer
2380 cb->BufferSize, // dimensions of our buffer
2381 BufferCoord, // offset in our buffer
2382 &ReadRegion)) // region to save
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002383 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002384 VIM_CLEAR(cb->Buffer);
2385 VIM_CLEAR(cb->Regions);
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002386 return FALSE;
2387 }
Bram Moolenaar444fda22017-08-11 20:37:00 +02002388 cb->Regions[i] = ReadRegion;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390
2391 return TRUE;
2392}
2393
2394/*
2395 * RestoreConsoleBuffer()
2396 * Description:
2397 * Restores important information about the console buffer, including the
2398 * actual buffer contents, if desired. The information to restore is in
2399 * the same format used by SaveConsoleBuffer().
2400 * Returns:
2401 * TRUE on success
2402 */
2403 static BOOL
2404RestoreConsoleBuffer(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002405 ConsoleBuffer *cb,
2406 BOOL RestoreScreen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002408 COORD BufferCoord;
2409 SMALL_RECT WriteRegion;
Bram Moolenaar444fda22017-08-11 20:37:00 +02002410 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 if (cb == NULL || !cb->IsValid)
2413 return FALSE;
2414
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002415 /*
2416 * Before restoring the buffer contents, clear the current buffer, and
2417 * restore the cursor position and window information. Doing this now
2418 * prevents old buffer contents from "flashing" onto the screen.
2419 */
2420 if (RestoreScreen)
2421 ClearConsoleBuffer(cb->Info.wAttributes);
2422
2423 FitConsoleWindow(cb->Info.dwSize, TRUE);
2424 if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize))
2425 return FALSE;
2426 if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes))
2427 return FALSE;
2428
2429 if (!RestoreScreen)
2430 {
2431 /*
2432 * No need to restore the screen buffer contents, so we're done.
2433 */
2434 return TRUE;
2435 }
2436
2437 if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition))
2438 return FALSE;
2439 if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow))
2440 return FALSE;
2441
2442 /*
2443 * Restore the screen buffer contents.
2444 */
2445 if (cb->Buffer != NULL)
2446 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002447 for (i = 0; i < cb->NumRegions; i++)
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002448 {
Bram Moolenaar444fda22017-08-11 20:37:00 +02002449 BufferCoord.X = cb->Regions[i].Left;
2450 BufferCoord.Y = cb->Regions[i].Top;
2451 WriteRegion = cb->Regions[i];
Bram Moolenaar0f873732019-12-05 20:28:46 +01002452 if (!WriteConsoleOutputW(g_hConOut, // output handle
2453 cb->Buffer, // our buffer
2454 cb->BufferSize, // dimensions of our buffer
2455 BufferCoord, // offset in our buffer
2456 &WriteRegion)) // region to restore
Bram Moolenaar444fda22017-08-11 20:37:00 +02002457 return FALSE;
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002458 }
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 return TRUE;
2462}
2463
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002464# define FEAT_RESTORE_ORIG_SCREEN
2465# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002466static ConsoleBuffer g_cbOrig = { 0 };
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468static ConsoleBuffer g_cbNonTermcap = { 0 };
2469static ConsoleBuffer g_cbTermcap = { 0 };
2470
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002471# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472char g_szOrigTitle[256] = { 0 };
Bram Moolenaar0f873732019-12-05 20:28:46 +01002473HWND g_hWnd = NULL; // also used in os_mswin.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474static HICON g_hOrigIconSmall = NULL;
2475static HICON g_hOrigIcon = NULL;
2476static HICON g_hVimIcon = NULL;
2477static BOOL g_fCanChangeIcon = FALSE;
2478
Bram Moolenaar0f873732019-12-05 20:28:46 +01002479// ICON* are not defined in VC++ 4.0
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002480# ifndef ICON_SMALL
2481# define ICON_SMALL 0
2482# endif
2483# ifndef ICON_BIG
2484# define ICON_BIG 1
2485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
2487 * GetConsoleIcon()
2488 * Description:
2489 * Attempts to retrieve the small icon and/or the big icon currently in
2490 * use by a given window.
2491 * Returns:
2492 * TRUE on success
2493 */
2494 static BOOL
2495GetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002496 HWND hWnd,
2497 HICON *phIconSmall,
2498 HICON *phIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (hWnd == NULL)
2501 return FALSE;
2502
2503 if (phIconSmall != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002504 *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON,
2505 (WPARAM)ICON_SMALL, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (phIcon != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002507 *phIcon = (HICON)SendMessage(hWnd, WM_GETICON,
2508 (WPARAM)ICON_BIG, (LPARAM)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return TRUE;
2510}
2511
2512/*
2513 * SetConsoleIcon()
2514 * Description:
2515 * Attempts to change the small icon and/or the big icon currently in
2516 * use by a given window.
2517 * Returns:
2518 * TRUE on success
2519 */
2520 static BOOL
2521SetConsoleIcon(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002522 HWND hWnd,
2523 HICON hIconSmall,
2524 HICON hIcon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (hWnd == NULL)
2527 return FALSE;
2528
2529 if (hIconSmall != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002530 SendMessage(hWnd, WM_SETICON,
2531 (WPARAM)ICON_SMALL, (LPARAM)hIconSmall);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (hIcon != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002533 SendMessage(hWnd, WM_SETICON,
2534 (WPARAM)ICON_BIG, (LPARAM) hIcon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return TRUE;
2536}
2537
2538/*
2539 * SaveConsoleTitleAndIcon()
2540 * Description:
2541 * Saves the current console window title in g_szOrigTitle, for later
2542 * restoration. Also, attempts to obtain a handle to the console window,
2543 * and use it to save the small and big icons currently in use by the
2544 * console window. This is not always possible on some versions of Windows;
2545 * nor is it possible when running Vim remotely using Telnet (since the
2546 * console window the user sees is owned by a remote process).
2547 */
2548 static void
2549SaveConsoleTitleAndIcon(void)
2550{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002551 // Save the original title.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle)))
2553 return;
2554
2555 /*
2556 * Obtain a handle to the console window using GetConsoleWindow() from
2557 * KERNEL32.DLL; we need to handle in order to change the window icon.
2558 * This function only exists on NT-based Windows, starting with Windows
2559 * 2000. On older operating systems, we can't change the window icon
2560 * anyway.
2561 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002562 g_hWnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (g_hWnd == NULL)
2564 return;
2565
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Save the original console window icon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon);
2568 if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL)
2569 return;
2570
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 // Extract the first icon contained in the Vim executable.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02002572 if (mch_icon_load((HANDLE *)&g_hVimIcon) == FAIL || g_hVimIcon == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002573 g_hVimIcon = ExtractIcon(NULL, (LPCSTR)exe_name, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (g_hVimIcon != NULL)
2575 g_fCanChangeIcon = TRUE;
2576}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579static int g_fWindInitCalled = FALSE;
2580static int g_fTermcapMode = FALSE;
2581static CONSOLE_CURSOR_INFO g_cci;
2582static DWORD g_cmodein = 0;
2583static DWORD g_cmodeout = 0;
2584
2585/*
2586 * non-GUI version of mch_init().
2587 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002588 static void
2589mch_init_c(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002591# ifndef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002592 CONSOLE_SCREEN_BUFFER_INFO csbi;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002593# endif
2594# ifndef __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 extern int _fmode;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar0f873732019-12-05 20:28:46 +01002598 // Silently handle invalid parameters to CRT functions
Bram Moolenaard32a99a2010-09-21 17:29:23 +02002599 SET_INVALID_PARAM_HANDLER;
2600
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 // Let critical errors result in a failure, not in a dialog box. Required
2602 // for the timestamp test to work on removed floppies.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 SetErrorMode(SEM_FAILCRITICALERRORS);
2604
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 _fmode = O_BINARY; // we do our own CR-LF translation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 out_flush();
2607
Bram Moolenaar0f873732019-12-05 20:28:46 +01002608 // Obtain handles for the standard Console I/O devices
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (read_cmd_fd == 0)
2610 g_hConIn = GetStdHandle(STD_INPUT_HANDLE);
2611 else
2612 create_conin();
2613 g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
2614
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002615# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Save the initial console buffer for later restoration
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002617 SaveConsoleBuffer(&g_cbOrig);
2618 g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002619# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002620 // Get current text attributes
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01002621 GetConsoleScreenBufferInfo(g_hConOut, &csbi);
2622 g_attrCurrent = g_attrDefault = csbi.wAttributes;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (cterm_normal_fg_color == 0)
2625 cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1;
2626 if (cterm_normal_bg_color == 0)
2627 cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1;
2628
Bram Moolenaarbdace832019-03-02 10:13:42 +01002629 // Fg and Bg color index number at startup
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02002630 g_color_index_fg = g_attrDefault & 0xf;
2631 g_color_index_bg = (g_attrDefault >> 4) & 0xf;
2632
Bram Moolenaar0f873732019-12-05 20:28:46 +01002633 // set termcap codes to current text attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 update_tcap(g_attrCurrent);
2635
2636 GetConsoleCursorInfo(g_hConOut, &g_cci);
2637 GetConsoleMode(g_hConIn, &g_cmodein);
2638 GetConsoleMode(g_hConOut, &g_cmodeout);
2639
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002640# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 SaveConsoleTitleAndIcon();
2642 /*
2643 * Set both the small and big icons of the console window to Vim's icon.
2644 * Note that Vim presently only has one size of icon (32x32), but it
2645 * automatically gets scaled down to 16x16 when setting the small icon.
2646 */
2647 if (g_fCanChangeIcon)
2648 SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 ui_get_shellsize();
2652
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002653# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 fdDump = fopen("dump", "wt");
2655
2656 if (fdDump)
2657 {
2658 time_t t;
2659
2660 time(&t);
2661 fputs(ctime(&t), fdDump);
2662 fflush(fdDump);
2663 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 g_fWindInitCalled = TRUE;
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002670# ifdef FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002671 win_clip_init();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002672# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002673
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02002674 vtp_flag_init();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002675 vtp_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676}
2677
2678/*
2679 * non-GUI version of mch_exit().
2680 * Shut down and exit with status `r'
2681 * Careful: mch_exit() may be called before mch_init()!
2682 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002683 static void
2684mch_exit_c(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar955f1982017-02-05 15:10:51 +01002686 exiting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002688 vtp_exit();
2689
Bram Moolenaar955f1982017-02-05 15:10:51 +01002690 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (g_fWindInitCalled)
2692 settmode(TMODE_COOK);
2693
Bram Moolenaar0f873732019-12-05 20:28:46 +01002694 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 if (g_fWindInitCalled)
2697 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002698# ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02002699 mch_restore_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 /*
2701 * Restore both the small and big icons of the console window to
2702 * what they were at startup. Don't do this when the window is
2703 * closed, Vim would hang here.
2704 */
2705 if (g_fCanChangeIcon && !g_fForceExit)
2706 SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002709# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (fdDump)
2711 {
2712 time_t t;
2713
2714 time(&t);
2715 fputs(ctime(&t), fdDump);
2716 fclose(fdDump);
2717 }
2718 fdDump = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721
2722 SetConsoleCursorInfo(g_hConOut, &g_cci);
2723 SetConsoleMode(g_hConIn, g_cmodein);
2724 SetConsoleMode(g_hConOut, g_cmodeout);
2725
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002726# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 exit(r);
2731}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002732#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002734 void
2735mch_init(void)
2736{
2737#ifdef VIMDLL
2738 if (gui.starting)
2739 mch_init_g();
2740 else
2741 mch_init_c();
2742#elif defined(FEAT_GUI_MSWIN)
2743 mch_init_g();
2744#else
2745 mch_init_c();
2746#endif
2747}
2748
2749 void
2750mch_exit(int r)
2751{
Bram Moolenaar173d8412020-04-19 14:02:26 +02002752#ifdef FEAT_NETBEANS_INTG
2753 netbeans_send_disconnect();
2754#endif
2755
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002756#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02002757 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002758 mch_exit_g(r);
2759 else
2760 mch_exit_c(r);
2761#elif defined(FEAT_GUI_MSWIN)
2762 mch_exit_g(r);
2763#else
2764 mch_exit_c(r);
2765#endif
2766}
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768/*
2769 * Do we have an interactive window?
2770 */
2771 int
2772mch_check_win(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002773 int argc UNUSED,
2774 char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 get_exe_name();
2777
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002778#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002779 return OK; // GUI always has a tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002781# ifdef VIMDLL
2782 if (gui.in_use)
2783 return OK;
2784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (isatty(1))
2786 return OK;
2787 return FAIL;
2788#endif
2789}
2790
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002791/*
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002792 * Set the case of the file name, if it already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 * When "len" is > 0, also expand short to long filenames.
2794 */
2795 void
2796fname_case(
2797 char_u *name,
2798 int len)
2799{
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002800 int flen;
2801 WCHAR *p;
2802 WCHAR buf[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00002804 flen = (int)STRLEN(name);
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002805 if (flen == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 return;
2807
2808 slash_adjust(name);
2809
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002810 p = enc_to_utf16(name, NULL);
2811 if (p == NULL)
Bram Moolenaar65f04f62013-08-30 17:29:16 +02002812 return;
2813
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002814 if (GetLongPathNameW(p, buf, _MAX_PATH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002816 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002818 if (q != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002820 if (len > 0 || flen >= (int)STRLEN(q))
2821 vim_strncpy(name, q, (len > 0) ? len - 1 : flen);
2822 vim_free(q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824 }
Bram Moolenaar8bb41b32019-03-30 17:28:16 +01002825 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828
2829/*
2830 * Insert user name in s[len].
2831 */
2832 int
2833mch_get_user_name(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002834 char_u *s,
2835 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002837 WCHAR wszUserName[256 + 1]; // UNLEN is 256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002838 DWORD wcch = sizeof(wszUserName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002840 if (GetUserNameW(wszUserName, &wcch))
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002841 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002842 char_u *p = utf16_to_enc(wszUserName, NULL);
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002843
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002844 if (p != NULL)
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002845 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002846 vim_strncpy(s, p, len - 1);
2847 vim_free(p);
2848 return OK;
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002849 }
Bram Moolenaarc8020ee2013-12-11 18:18:06 +01002850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 s[0] = NUL;
2852 return FAIL;
2853}
2854
2855
2856/*
2857 * Insert host name in s[len].
2858 */
2859 void
2860mch_get_host_name(
2861 char_u *s,
2862 int len)
2863{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002864 WCHAR wszHostName[256 + 1];
2865 DWORD wcch = sizeof(wszHostName) / sizeof(WCHAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002867 if (GetComputerNameW(wszHostName, &wcch))
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002868 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002869 char_u *p = utf16_to_enc(wszHostName, NULL);
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002870
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002871 if (p != NULL)
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002872 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002873 vim_strncpy(s, p, len - 1);
2874 vim_free(p);
2875 return;
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002876 }
Bram Moolenaar2cc87382013-12-11 18:21:45 +01002877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878}
2879
2880
2881/*
2882 * return process ID
2883 */
2884 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002885mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 return (long)GetCurrentProcessId();
2888}
2889
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002890/*
2891 * return TRUE if process "pid" is still running
2892 */
2893 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002894mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002895{
2896 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, (DWORD)pid);
2897 DWORD status = 0;
2898 int ret = FALSE;
2899
2900 if (hProcess == NULL)
2901 return FALSE; // might not have access
2902 if (GetExitCodeProcess(hProcess, &status) )
2903 ret = status == STILL_ACTIVE;
2904 CloseHandle(hProcess);
2905 return ret;
2906}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
2908/*
2909 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2910 * Return OK for success, FAIL for failure.
2911 */
2912 int
2913mch_dirname(
2914 char_u *buf,
2915 int len)
2916{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002917 WCHAR wbuf[_MAX_PATH + 1];
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /*
2920 * Originally this was:
2921 * return (getcwd(buf, len) != NULL ? OK : FAIL);
2922 * But the Win32s known bug list says that getcwd() doesn't work
2923 * so use the Win32 system call instead. <Negri>
2924 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002925 if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002927 WCHAR wcbuf[_MAX_PATH + 1];
2928 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002930 if (GetLongPathNameW(wbuf, wcbuf, _MAX_PATH) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002932 p = utf16_to_enc(wcbuf, NULL);
2933 if (STRLEN(p) >= (size_t)len)
Bram Moolenaarcea1f9e2018-08-19 14:38:42 +02002934 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002935 // long path name is too long, fall back to short one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002937 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002940 if (p == NULL)
2941 p = utf16_to_enc(wbuf, NULL);
Bram Moolenaar3b9fcfc2018-08-18 20:20:27 +02002942
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002943 if (p != NULL)
2944 {
2945 vim_strncpy(buf, p, len - 1);
2946 vim_free(p);
2947 return OK;
2948 }
2949 }
2950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951}
2952
2953/*
Bram Moolenaarffa22202013-11-21 12:34:11 +01002954 * Get file permissions for "name".
2955 * Return mode_t or -1 for error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 */
2957 long
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002958mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002960 stat_T st;
Bram Moolenaarffa22202013-11-21 12:34:11 +01002961 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002963 n = mch_stat((char *)name, &st);
Bram Moolenaar78cf3f02014-01-10 18:16:07 +01002964 return n == 0 ? (long)(unsigned short)st.st_mode : -1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967
2968/*
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002969 * Set file permission for "name" to "perm".
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002970 *
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002971 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 */
2973 int
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002974mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002976 long n;
2977 WCHAR *p;
Bram Moolenaare24a9c02013-07-24 13:49:22 +02002978
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002979 p = enc_to_utf16(name, NULL);
2980 if (p == NULL)
2981 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002983 n = _wchmod(p, perm);
2984 vim_free(p);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002985 if (n == -1)
2986 return FAIL;
2987
2988 win32_set_archive(name);
2989
2990 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992
2993/*
2994 * Set hidden flag for "name".
2995 */
2996 void
2997mch_hide(char_u *name)
2998{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02002999 int attrs = win32_getattrs(name);
3000 if (attrs == -1)
3001 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003003 attrs |= FILE_ATTRIBUTE_HIDDEN;
3004 win32_setattrs(name, attrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
3007/*
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003008 * Return TRUE if file "name" exists and is hidden.
3009 */
3010 int
3011mch_ishidden(char_u *name)
3012{
3013 int f = win32_getattrs(name);
3014
3015 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003016 return FALSE; // file does not exist at all
Bram Moolenaar8a52ba72015-11-02 14:45:56 +01003017
3018 return (f & FILE_ATTRIBUTE_HIDDEN) != 0;
3019}
3020
3021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 * return TRUE if "name" is a directory
3023 * return FALSE if "name" is not a directory or upon error
3024 */
3025 int
3026mch_isdir(char_u *name)
3027{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003028 int f = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 if (f == -1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003031 return FALSE; // file does not exist at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 return (f & FILE_ATTRIBUTE_DIRECTORY) != 0;
3034}
3035
3036/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003037 * return TRUE if "name" is a directory, NOT a symlink to a directory
3038 * return FALSE if "name" is not a directory
3039 * return FALSE for error
3040 */
3041 int
3042mch_isrealdir(char_u *name)
3043{
3044 return mch_isdir(name) && !mch_is_symbolic_link(name);
3045}
3046
3047/*
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003048 * Create directory "name".
3049 * Return 0 on success, -1 on error.
3050 */
3051 int
3052mch_mkdir(char_u *name)
3053{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003054 WCHAR *p;
3055 int retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003056
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003057 p = enc_to_utf16(name, NULL);
3058 if (p == NULL)
3059 return -1;
3060 retval = _wmkdir(p);
3061 vim_free(p);
3062 return retval;
Bram Moolenaar3c9c99c2011-05-05 18:31:59 +02003063}
3064
3065/*
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003066 * Delete directory "name".
3067 * Return 0 on success, -1 on error.
3068 */
3069 int
3070mch_rmdir(char_u *name)
3071{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003072 WCHAR *p;
3073 int retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003075 p = enc_to_utf16(name, NULL);
3076 if (p == NULL)
3077 return -1;
3078 retval = _wrmdir(p);
3079 vim_free(p);
3080 return retval;
Bram Moolenaar4cf76792016-01-16 22:02:57 +01003081}
3082
3083/*
Bram Moolenaar03f48552006-02-28 23:52:23 +00003084 * Return TRUE if file "fname" has more than one link.
3085 */
3086 int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003087mch_is_hard_link(char_u *fname)
Bram Moolenaar03f48552006-02-28 23:52:23 +00003088{
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003089 BY_HANDLE_FILE_INFORMATION info;
3090
3091 return win32_fileinfo(fname, &info) == FILEINFO_OK
3092 && info.nNumberOfLinks > 1;
3093}
3094
3095/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01003096 * Return TRUE if "name" is a symbolic link (or a junction).
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003097 */
3098 int
Bram Moolenaar203258c2016-01-17 22:15:16 +01003099mch_is_symbolic_link(char_u *name)
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003100{
3101 HANDLE hFind;
3102 int res = FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003103 DWORD fileFlags = 0, reparseTag = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003104 WCHAR *wn;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003105 WIN32_FIND_DATAW findDataW;
3106
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003107 wn = enc_to_utf16(name, NULL);
3108 if (wn == NULL)
3109 return FALSE;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003110
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003111 hFind = FindFirstFileW(wn, &findDataW);
3112 vim_free(wn);
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003113 if (hFind != INVALID_HANDLE_VALUE)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003114 {
3115 fileFlags = findDataW.dwFileAttributes;
3116 reparseTag = findDataW.dwReserved0;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003117 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003118 }
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003119
3120 if ((fileFlags & FILE_ATTRIBUTE_REPARSE_POINT)
Bram Moolenaar203258c2016-01-17 22:15:16 +01003121 && (reparseTag == IO_REPARSE_TAG_SYMLINK
3122 || reparseTag == IO_REPARSE_TAG_MOUNT_POINT))
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003123 res = TRUE;
3124
3125 return res;
3126}
3127
3128/*
3129 * Return TRUE if file "fname" has more than one link or if it is a symbolic
3130 * link.
3131 */
3132 int
3133mch_is_linked(char_u *fname)
3134{
3135 if (mch_is_hard_link(fname) || mch_is_symbolic_link(fname))
3136 return TRUE;
3137 return FALSE;
3138}
3139
3140/*
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003141 * Get the by-handle-file-information for "fname".
3142 * Returns FILEINFO_OK when OK.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003143 * Returns FILEINFO_ENC_FAIL when enc_to_utf16() failed.
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003144 * Returns FILEINFO_READ_FAIL when CreateFile() failed.
3145 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed.
3146 */
3147 int
3148win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info)
3149{
Bram Moolenaar03f48552006-02-28 23:52:23 +00003150 HANDLE hFile;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003151 int res = FILEINFO_READ_FAIL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003152 WCHAR *wn;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003153
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003154 wn = enc_to_utf16(fname, NULL);
3155 if (wn == NULL)
3156 return FILEINFO_ENC_FAIL;
3157
3158 hFile = CreateFileW(wn, // file name
3159 GENERIC_READ, // access mode
3160 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
3161 NULL, // security descriptor
3162 OPEN_EXISTING, // creation disposition
3163 FILE_FLAG_BACKUP_SEMANTICS, // file attributes
3164 NULL); // handle to template file
3165 vim_free(wn);
Bram Moolenaar03f48552006-02-28 23:52:23 +00003166
3167 if (hFile != INVALID_HANDLE_VALUE)
3168 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003169 if (GetFileInformationByHandle(hFile, info) != 0)
3170 res = FILEINFO_OK;
3171 else
3172 res = FILEINFO_INFO_FAIL;
Bram Moolenaar03f48552006-02-28 23:52:23 +00003173 CloseHandle(hFile);
3174 }
3175
Bram Moolenaar03f48552006-02-28 23:52:23 +00003176 return res;
3177}
3178
3179/*
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003180 * get file attributes for `name'
3181 * -1 : error
3182 * else FILE_ATTRIBUTE_* defined in winnt.h
3183 */
Bram Moolenaarffa22202013-11-21 12:34:11 +01003184 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003185win32_getattrs(char_u *name)
3186{
3187 int attr;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003188 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003189
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003190 p = enc_to_utf16(name, NULL);
3191 if (p == NULL)
3192 return INVALID_FILE_ATTRIBUTES;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003193
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003194 attr = GetFileAttributesW(p);
3195 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003196
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003197 return attr;
3198}
3199
3200/*
3201 * set file attributes for `name' to `attrs'
3202 *
3203 * return -1 for failure, 0 otherwise
3204 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003205 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003206win32_setattrs(char_u *name, int attrs)
3207{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003208 int res;
3209 WCHAR *p;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003210
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003211 p = enc_to_utf16(name, NULL);
3212 if (p == NULL)
3213 return -1;
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003214
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003215 res = SetFileAttributesW(p, attrs);
3216 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003217
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003218 return res ? 0 : -1;
3219}
3220
3221/*
3222 * Set archive flag for "name".
3223 */
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02003224 static int
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003225win32_set_archive(char_u *name)
3226{
3227 int attrs = win32_getattrs(name);
3228 if (attrs == -1)
3229 return -1;
3230
3231 attrs |= FILE_ATTRIBUTE_ARCHIVE;
3232 return win32_setattrs(name, attrs);
3233}
3234
3235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * Return TRUE if file or directory "name" is writable (not readonly).
3237 * Strange semantics of Win32: a readonly directory is writable, but you can't
3238 * delete a file. Let's say this means it is writable.
3239 */
3240 int
3241mch_writable(char_u *name)
3242{
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003243 int attrs = win32_getattrs(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar12b559e2013-06-12 22:41:37 +02003245 return (attrs != -1 && (!(attrs & FILE_ATTRIBUTE_READONLY)
3246 || (attrs & FILE_ATTRIBUTE_DIRECTORY)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249/*
Bram Moolenaar43663192017-03-05 14:29:12 +01003250 * Return TRUE if "name" can be executed, FALSE if not.
Bram Moolenaar77b77102015-03-21 22:18:41 +01003251 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar43663192017-03-05 14:29:12 +01003252 * When returning TRUE and "path" is not NULL save the path and set "*path" to
3253 * the allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 */
3255 int
Bram Moolenaar77b77102015-03-21 22:18:41 +01003256mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
Bram Moolenaar86621892019-03-30 21:51:28 +01003258 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
3259 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3260 // UTF-8.
3261 char_u buf[_MAX_PATH * 3];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003262 int len = (int)STRLEN(name);
Bram Moolenaar82956662018-10-06 15:18:45 +02003263 char_u *p, *saved;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003264
Bram Moolenaar86621892019-03-30 21:51:28 +01003265 if (len >= sizeof(buf)) // safety check
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003266 return FALSE;
3267
Bram Moolenaar86621892019-03-30 21:51:28 +01003268 // Try using the name directly when a Unix-shell like 'shell'.
Bram Moolenaar82956662018-10-06 15:18:45 +02003269 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar43663192017-03-05 14:29:12 +01003270 if (executable_exists((char *)name, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003271 return TRUE;
3272
3273 /*
3274 * Loop over all extensions in $PATHEXT.
3275 */
Bram Moolenaar82956662018-10-06 15:18:45 +02003276 p = mch_getenv("PATHEXT");
3277 if (p == NULL)
3278 p = (char_u *)".com;.exe;.bat;.cmd";
3279 saved = vim_strsave(p);
3280 if (saved == NULL)
3281 return FALSE;
3282 p = saved;
3283 while (*p)
3284 {
3285 char_u *tmp = vim_strchr(p, ';');
3286
3287 if (tmp != NULL)
3288 *tmp = NUL;
3289 if (_stricoll((char *)name + len - STRLEN(p), (char *)p) == 0
3290 && executable_exists((char *)name, path, use_path))
3291 {
3292 vim_free(saved);
3293 return TRUE;
3294 }
3295 if (tmp == NULL)
3296 break;
3297 p = tmp + 1;
3298 }
3299 vim_free(saved);
3300
Bram Moolenaar86621892019-03-30 21:51:28 +01003301 vim_strncpy(buf, name, sizeof(buf) - 1);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003302 p = mch_getenv("PATHEXT");
3303 if (p == NULL)
3304 p = (char_u *)".com;.exe;.bat;.cmd";
3305 while (*p)
3306 {
3307 if (p[0] == '.' && (p[1] == NUL || p[1] == ';'))
3308 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003309 // A single "." means no extension is added.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003310 buf[len] = NUL;
3311 ++p;
3312 if (*p)
3313 ++p;
3314 }
3315 else
Bram Moolenaar86621892019-03-30 21:51:28 +01003316 copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
Bram Moolenaar43663192017-03-05 14:29:12 +01003317 if (executable_exists((char *)buf, path, use_path))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003318 return TRUE;
3319 }
3320 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323/*
3324 * Check what "name" is:
3325 * NODE_NORMAL: file or directory (or doesn't exist)
3326 * NODE_WRITABLE: writable device, socket, fifo, etc.
3327 * NODE_OTHER: non-writable things
3328 */
3329 int
3330mch_nodetype(char_u *name)
3331{
3332 HANDLE hFile;
3333 int type;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003334 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaar0f873732019-12-05 20:28:46 +01003336 // We can't open a file with a name "\\.\con" or "\\.\prn" and trying to
3337 // read from it later will cause Vim to hang. Thus return NODE_WRITABLE
3338 // here.
Bram Moolenaar043545e2006-10-10 16:44:07 +00003339 if (STRNCMP(name, "\\\\.\\", 4) == 0)
3340 return NODE_WRITABLE;
3341
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003342 wn = enc_to_utf16(name, NULL);
3343 if (wn == NULL)
3344 return NODE_NORMAL;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003345
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003346 hFile = CreateFileW(wn, // file name
3347 GENERIC_WRITE, // access mode
3348 0, // share mode
3349 NULL, // security descriptor
3350 OPEN_EXISTING, // creation disposition
3351 0, // file attributes
3352 NULL); // handle to template file
3353 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (hFile == INVALID_HANDLE_VALUE)
3355 return NODE_NORMAL;
3356
3357 type = GetFileType(hFile);
3358 CloseHandle(hFile);
3359 if (type == FILE_TYPE_CHAR)
3360 return NODE_WRITABLE;
3361 if (type == FILE_TYPE_DISK)
3362 return NODE_NORMAL;
3363 return NODE_OTHER;
3364}
3365
3366#ifdef HAVE_ACL
3367struct my_acl
3368{
3369 PSECURITY_DESCRIPTOR pSecurityDescriptor;
3370 PSID pSidOwner;
3371 PSID pSidGroup;
3372 PACL pDacl;
3373 PACL pSacl;
3374};
3375#endif
3376
3377/*
3378 * Return a pointer to the ACL of file "fname" in allocated memory.
3379 * Return NULL if the ACL is not available for whatever reason.
3380 */
3381 vim_acl_T
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003382mch_get_acl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384#ifndef HAVE_ACL
3385 return (vim_acl_T)NULL;
3386#else
3387 struct my_acl *p = NULL;
Bram Moolenaar27515922013-06-29 15:36:26 +02003388 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003390 p = ALLOC_CLEAR_ONE(struct my_acl);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003391 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003393 WCHAR *wn;
Bram Moolenaar27515922013-06-29 15:36:26 +02003394
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003395 wn = enc_to_utf16(fname, NULL);
3396 if (wn == NULL)
3397 return NULL;
3398
3399 // Try to retrieve the entire security descriptor.
3400 err = GetNamedSecurityInfoW(
3401 wn, // Abstract filename
3402 SE_FILE_OBJECT, // File Object
3403 OWNER_SECURITY_INFORMATION |
3404 GROUP_SECURITY_INFORMATION |
3405 DACL_SECURITY_INFORMATION |
3406 SACL_SECURITY_INFORMATION,
3407 &p->pSidOwner, // Ownership information.
3408 &p->pSidGroup, // Group membership.
3409 &p->pDacl, // Discretionary information.
3410 &p->pSacl, // For auditing purposes.
3411 &p->pSecurityDescriptor);
3412 if (err == ERROR_ACCESS_DENIED ||
3413 err == ERROR_PRIVILEGE_NOT_HELD)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003414 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003415 // Retrieve only DACL.
3416 (void)GetNamedSecurityInfoW(
3417 wn,
3418 SE_FILE_OBJECT,
3419 DACL_SECURITY_INFORMATION,
3420 NULL,
3421 NULL,
3422 &p->pDacl,
3423 NULL,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003424 &p->pSecurityDescriptor);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003425 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003426 if (p->pSecurityDescriptor == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003427 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003428 mch_free_acl((vim_acl_T)p);
3429 p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003431 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433
3434 return (vim_acl_T)p;
3435#endif
3436}
3437
Bram Moolenaar27515922013-06-29 15:36:26 +02003438#ifdef HAVE_ACL
3439/*
3440 * Check if "acl" contains inherited ACE.
3441 */
3442 static BOOL
3443is_acl_inherited(PACL acl)
3444{
3445 DWORD i;
3446 ACL_SIZE_INFORMATION acl_info;
3447 PACCESS_ALLOWED_ACE ace;
3448
3449 acl_info.AceCount = 0;
3450 GetAclInformation(acl, &acl_info, sizeof(acl_info), AclSizeInformation);
3451 for (i = 0; i < acl_info.AceCount; i++)
3452 {
3453 GetAce(acl, i, (LPVOID *)&ace);
3454 if (ace->Header.AceFlags & INHERITED_ACE)
3455 return TRUE;
3456 }
3457 return FALSE;
3458}
3459#endif
3460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461/*
3462 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3463 * Errors are ignored.
3464 * This must only be called with "acl" equal to what mch_get_acl() returned.
3465 */
3466 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003467mch_set_acl(char_u *fname, vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
3469#ifdef HAVE_ACL
3470 struct my_acl *p = (struct my_acl *)acl;
Bram Moolenaar27515922013-06-29 15:36:26 +02003471 SECURITY_INFORMATION sec_info = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003472 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003474 if (p == NULL)
3475 return;
3476
3477 wn = enc_to_utf16(fname, NULL);
3478 if (wn == NULL)
3479 return;
3480
3481 // Set security flags
3482 if (p->pSidOwner)
3483 sec_info |= OWNER_SECURITY_INFORMATION;
3484 if (p->pSidGroup)
3485 sec_info |= GROUP_SECURITY_INFORMATION;
3486 if (p->pDacl)
Bram Moolenaar27515922013-06-29 15:36:26 +02003487 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003488 sec_info |= DACL_SECURITY_INFORMATION;
3489 // Do not inherit its parent's DACL.
3490 // If the DACL is inherited, Cygwin permissions would be changed.
3491 if (!is_acl_inherited(p->pDacl))
3492 sec_info |= PROTECTED_DACL_SECURITY_INFORMATION;
Bram Moolenaar27515922013-06-29 15:36:26 +02003493 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003494 if (p->pSacl)
3495 sec_info |= SACL_SECURITY_INFORMATION;
3496
3497 (void)SetNamedSecurityInfoW(
3498 wn, // Abstract filename
3499 SE_FILE_OBJECT, // File Object
3500 sec_info,
3501 p->pSidOwner, // Ownership information.
3502 p->pSidGroup, // Group membership.
3503 p->pDacl, // Discretionary information.
3504 p->pSacl // For auditing purposes.
3505 );
3506 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#endif
3508}
3509
3510 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003511mch_free_acl(vim_acl_T acl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513#ifdef HAVE_ACL
3514 struct my_acl *p = (struct my_acl *)acl;
3515
3516 if (p != NULL)
3517 {
3518 LocalFree(p->pSecurityDescriptor); // Free the memory just in case
3519 vim_free(p);
3520 }
3521#endif
3522}
3523
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003524#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526/*
3527 * handler for ctrl-break, ctrl-c interrupts, and fatal events.
3528 */
3529 static BOOL WINAPI
3530handler_routine(
3531 DWORD dwCtrlType)
3532{
Bram Moolenaar589b1102017-08-12 16:39:05 +02003533 INPUT_RECORD ir;
3534 DWORD out;
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 switch (dwCtrlType)
3537 {
3538 case CTRL_C_EVENT:
3539 if (ctrl_c_interrupts)
3540 g_fCtrlCPressed = TRUE;
3541 return TRUE;
3542
3543 case CTRL_BREAK_EVENT:
3544 g_fCBrkPressed = TRUE;
Bram Moolenaar589b1102017-08-12 16:39:05 +02003545 ctrl_break_was_pressed = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003546 // ReadConsoleInput is blocking, send a key event to continue.
Bram Moolenaar589b1102017-08-12 16:39:05 +02003547 ir.EventType = KEY_EVENT;
3548 ir.Event.KeyEvent.bKeyDown = TRUE;
3549 ir.Event.KeyEvent.wRepeatCount = 1;
3550 ir.Event.KeyEvent.wVirtualKeyCode = VK_CANCEL;
3551 ir.Event.KeyEvent.wVirtualScanCode = 0;
3552 ir.Event.KeyEvent.dwControlKeyState = 0;
3553 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
3554 WriteConsoleInput(g_hConIn, &ir, 1, &out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return TRUE;
3556
Bram Moolenaar0f873732019-12-05 20:28:46 +01003557 // fatal events: shut down gracefully
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 case CTRL_CLOSE_EVENT:
3559 case CTRL_LOGOFF_EVENT:
3560 case CTRL_SHUTDOWN_EVENT:
3561 windgoto((int)Rows - 1, 0);
3562 g_fForceExit = TRUE;
3563
Bram Moolenaar0fde2902008-03-16 13:54:13 +00003564 vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 (dwCtrlType == CTRL_CLOSE_EVENT
3566 ? _("close")
3567 : dwCtrlType == CTRL_LOGOFF_EVENT
3568 ? _("logoff")
3569 : _("shutdown")));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003570# ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 OutputDebugString(IObuff);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
Bram Moolenaar0f873732019-12-05 20:28:46 +01003574 preserve_exit(); // output IObuff, preserve files and exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar0f873732019-12-05 20:28:46 +01003576 return TRUE; // not reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 default:
3579 return FALSE;
3580 }
3581}
3582
3583
3584/*
3585 * set the tty in (raw) ? "raw" : "cooked" mode
3586 */
3587 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003588mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 DWORD cmodein;
3591 DWORD cmodeout;
3592 BOOL bEnableHandler;
3593
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003594# ifdef VIMDLL
3595 if (gui.in_use)
3596 return;
3597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 GetConsoleMode(g_hConIn, &cmodein);
3599 GetConsoleMode(g_hConOut, &cmodeout);
3600 if (tmode == TMODE_RAW)
3601 {
3602 cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3603 ENABLE_ECHO_INPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (g_fMouseActive)
3605 cmodein |= ENABLE_MOUSE_INPUT;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003606 cmodeout &= ~(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003607# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003608 // Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
3609 // VTP.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003610 ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003611# else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003612 ENABLE_PROCESSED_OUTPUT |
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003613# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003614 ENABLE_WRAP_AT_EOL_OUTPUT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 bEnableHandler = TRUE;
3616 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01003617 else // cooked
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT |
3620 ENABLE_ECHO_INPUT);
3621 cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
3622 bEnableHandler = FALSE;
3623 }
3624 SetConsoleMode(g_hConIn, cmodein);
3625 SetConsoleMode(g_hConOut, cmodeout);
3626 SetConsoleCtrlHandler(handler_routine, bEnableHandler);
3627
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003628# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (fdDump)
3630 {
3631 fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n",
3632 tmode == TMODE_RAW ? "raw" :
3633 tmode == TMODE_COOK ? "cooked" : "normal",
3634 cmodein, cmodeout);
3635 fflush(fdDump);
3636 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638}
3639
3640
3641/*
3642 * Get the size of the current window in `Rows' and `Columns'
3643 * Return OK when size could be determined, FAIL otherwise.
3644 */
3645 int
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003646mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 CONSOLE_SCREEN_BUFFER_INFO csbi;
3649
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003650# ifdef VIMDLL
3651 if (gui.in_use)
3652 return OK;
3653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (!g_fTermcapMode && g_cbTermcap.IsValid)
3655 {
3656 /*
3657 * For some reason, we are trying to get the screen dimensions
3658 * even though we are not in termcap mode. The 'Rows' and 'Columns'
3659 * variables are really intended to mean the size of Vim screen
3660 * while in termcap mode.
3661 */
3662 Rows = g_cbTermcap.Info.dwSize.Y;
3663 Columns = g_cbTermcap.Info.dwSize.X;
3664 }
3665 else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3666 {
3667 Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3668 Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3669 }
3670 else
3671 {
3672 Rows = 25;
3673 Columns = 80;
3674 }
3675 return OK;
3676}
3677
3678/*
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003679 * Resize console buffer to 'COORD'
3680 */
3681 static void
3682ResizeConBuf(
3683 HANDLE hConsole,
3684 COORD coordScreen)
3685{
3686 if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
3687 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003688# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003689 if (fdDump)
3690 {
3691 fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n",
3692 GetLastError());
3693 fflush(fdDump);
3694 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003695# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003696 }
3697}
3698
3699/*
3700 * Resize console window size to 'srWindowRect'
3701 */
3702 static void
3703ResizeWindow(
3704 HANDLE hConsole,
3705 SMALL_RECT srWindowRect)
3706{
3707 if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
3708 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003709# ifdef MCH_WRITE_DUMP
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003710 if (fdDump)
3711 {
3712 fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n",
3713 GetLastError());
3714 fflush(fdDump);
3715 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003716# endif
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003717 }
3718}
3719
3720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * Set a console window to `xSize' * `ySize'
3722 */
3723 static void
3724ResizeConBufAndWindow(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003725 HANDLE hConsole,
3726 int xSize,
3727 int ySize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003729 CONSOLE_SCREEN_BUFFER_INFO csbi; // hold current console buffer info
3730 SMALL_RECT srWindowRect; // hold the new console size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 COORD coordScreen;
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003732 COORD cursor;
Bram Moolenaar2551c032018-08-23 22:38:31 +02003733 static int resized = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003735# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (fdDump)
3737 {
3738 fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize);
3739 fflush(fdDump);
3740 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
Bram Moolenaar0f873732019-12-05 20:28:46 +01003743 // get the largest size we can size the console window to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 coordScreen = GetLargestConsoleWindowSize(hConsole);
3745
Bram Moolenaar0f873732019-12-05 20:28:46 +01003746 // define the new console window size and scroll position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
3748 srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1);
3749 srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1);
3750
3751 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
3752 {
3753 int sx, sy;
3754
3755 sx = csbi.srWindow.Right - csbi.srWindow.Left + 1;
3756 sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
3757 if (sy < ySize || sx < xSize)
3758 {
3759 /*
3760 * Increasing number of lines/columns, do buffer first.
3761 * Use the maximal size in x and y direction.
3762 */
3763 if (sy < ySize)
3764 coordScreen.Y = ySize;
3765 else
3766 coordScreen.Y = sy;
3767 if (sx < xSize)
3768 coordScreen.X = xSize;
3769 else
3770 coordScreen.X = sx;
3771 SetConsoleScreenBufferSize(hConsole, coordScreen);
3772 }
3773 }
3774
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003775 // define the new console buffer size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 coordScreen.X = xSize;
3777 coordScreen.Y = ySize;
3778
Bram Moolenaar2551c032018-08-23 22:38:31 +02003779 // In the new console call API, only the first time in reverse order
3780 if (!vtp_working || resized)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003782 ResizeWindow(hConsole, srWindowRect);
3783 ResizeConBuf(hConsole, coordScreen);
3784 }
3785 else
3786 {
Bram Moolenaarf49a6922019-07-15 20:37:05 +02003787 // Workaround for a Windows 10 bug
3788 cursor.X = srWindowRect.Left;
3789 cursor.Y = srWindowRect.Top;
3790 SetConsoleCursorPosition(hConsole, cursor);
3791
Bram Moolenaarb1cf1612018-08-07 20:47:16 +02003792 ResizeConBuf(hConsole, coordScreen);
3793 ResizeWindow(hConsole, srWindowRect);
Bram Moolenaar2551c032018-08-23 22:38:31 +02003794 resized = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796}
3797
3798
3799/*
3800 * Set the console window to `Rows' * `Columns'
3801 */
3802 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003803mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 COORD coordScreen;
3806
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003807# ifdef VIMDLL
3808 if (gui.in_use)
3809 return;
3810# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01003811 // Don't change window size while still starting up
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (suppress_winsize != 0)
3813 {
3814 suppress_winsize = 2;
3815 return;
3816 }
3817
3818 if (term_console)
3819 {
3820 coordScreen = GetLargestConsoleWindowSize(g_hConOut);
3821
Bram Moolenaar0f873732019-12-05 20:28:46 +01003822 // Clamp Rows and Columns to reasonable values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (Rows > coordScreen.Y)
3824 Rows = coordScreen.Y;
3825 if (Columns > coordScreen.X)
3826 Columns = coordScreen.X;
3827
3828 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
3829 }
3830}
3831
3832/*
3833 * Rows and/or Columns has changed.
3834 */
3835 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003836mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003838# ifdef VIMDLL
3839 if (gui.in_use)
3840 return;
3841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 set_scroll_region(0, 0, Columns - 1, Rows - 1);
3843}
3844
3845
3846/*
3847 * Called when started up, to set the winsize that was delayed.
3848 */
3849 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003850mch_set_winsize_now(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 if (suppress_winsize == 2)
3853 {
3854 suppress_winsize = 0;
3855 mch_set_shellsize();
3856 shell_resized();
3857 }
3858 suppress_winsize = 0;
3859}
Bram Moolenaar0f873732019-12-05 20:28:46 +01003860#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003862 static BOOL
3863vim_create_process(
Bram Moolenaar36c85b22013-12-12 20:25:44 +01003864 char *cmd,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003865 BOOL inherit_handles,
Bram Moolenaar3f1138e2014-01-05 13:29:26 +01003866 DWORD flags,
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003867 STARTUPINFO *si,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003868 PROCESS_INFORMATION *pi,
3869 LPVOID *env,
3870 char *cwd)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003871{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003872 BOOL ret = FALSE;
3873 WCHAR *wcmd, *wcwd = NULL;
3874
3875 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3876 if (wcmd == NULL)
3877 return FALSE;
3878 if (cwd != NULL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003879 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003880 wcwd = enc_to_utf16((char_u *)cwd, NULL);
3881 if (wcwd == NULL)
3882 goto theend;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003883 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003884
3885 ret = CreateProcessW(
3886 NULL, // Executable name
3887 wcmd, // Command to execute
3888 NULL, // Process security attributes
3889 NULL, // Thread security attributes
3890 inherit_handles, // Inherit handles
3891 flags, // Creation flags
3892 env, // Environment
3893 wcwd, // Current directory
3894 (LPSTARTUPINFOW)si, // Startup information
3895 pi); // Process information
3896theend:
3897 vim_free(wcmd);
3898 vim_free(wcwd);
3899 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003900}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003903 static HINSTANCE
3904vim_shell_execute(
3905 char *cmd,
3906 INT n_show_cmd)
3907{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003908 HINSTANCE ret;
3909 WCHAR *wcmd;
3910
3911 wcmd = enc_to_utf16((char_u *)cmd, NULL);
3912 if (wcmd == NULL)
3913 return (HINSTANCE) 0;
3914
3915 ret = ShellExecuteW(NULL, NULL, wcmd, NULL, NULL, n_show_cmd);
3916 vim_free(wcmd);
3917 return ret;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01003918}
3919
3920
Bram Moolenaar4f974752019-02-17 17:44:42 +01003921#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923/*
3924 * Specialised version of system() for Win32 GUI mode.
3925 * This version proceeds as follows:
3926 * 1. Create a console window for use by the subprocess
3927 * 2. Run the subprocess (it gets the allocated console by default)
3928 * 3. Wait for the subprocess to terminate and get its exit code
3929 * 4. Prompt the user to press a key to close the console window
3930 */
3931 static int
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02003932mch_system_classic(char *cmd, int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 STARTUPINFO si;
3935 PROCESS_INFORMATION pi;
3936 DWORD ret = 0;
3937 HWND hwnd = GetFocus();
3938
3939 si.cb = sizeof(si);
3940 si.lpReserved = NULL;
3941 si.lpDesktop = NULL;
3942 si.lpTitle = NULL;
3943 si.dwFlags = STARTF_USESHOWWINDOW;
3944 /*
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003945 * It's nicer to run a filter command in a minimized window.
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003946 * Don't activate the window to keep focus on Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003948 if (options & SHELL_DOOUT)
Bram Moolenaar96e5cee2010-11-24 12:35:21 +01003949 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 else
3951 si.wShowWindow = SW_SHOWNORMAL;
3952 si.cbReserved2 = 0;
3953 si.lpReserved2 = NULL;
3954
Bram Moolenaar0f873732019-12-05 20:28:46 +01003955 // Now, run the command
Bram Moolenaar910cffb2013-12-11 17:58:35 +01003956 vim_create_process(cmd, FALSE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02003957 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE,
3958 &si, &pi, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
Bram Moolenaar0f873732019-12-05 20:28:46 +01003960 // Wait for the command to terminate before continuing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003962# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int delay = 1;
3964
Bram Moolenaar0f873732019-12-05 20:28:46 +01003965 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 for (;;)
3967 {
3968 MSG msg;
3969
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003970 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02003973 pDispatchMessage(&msg);
Bram Moolenaare4195c52012-08-02 12:31:44 +02003974 delay = 1;
3975 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
3978 break;
3979
Bram Moolenaar0f873732019-12-05 20:28:46 +01003980 // We start waiting for a very short time and then increase it, so
3981 // that we respond quickly when the process is quick, and don't
3982 // consume too much overhead when it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (delay < 50)
3984 delay += 10;
3985 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003986# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 WaitForSingleObject(pi.hProcess, INFINITE);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
Bram Moolenaar0f873732019-12-05 20:28:46 +01003990 // Get the command exit code
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 GetExitCodeProcess(pi.hProcess, &ret);
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 CloseHandle(pi.hThread);
3996 CloseHandle(pi.hProcess);
3997
Bram Moolenaar0f873732019-12-05 20:28:46 +01003998 // Try to get input focus back. Doesn't always work though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 PostMessage(hwnd, WM_SETFOCUS, 0, 0);
4000
4001 return ret;
4002}
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004003
4004/*
4005 * Thread launched by the gui to send the current buffer data to the
4006 * process. This way avoid to hang up vim totally if the children
4007 * process take a long time to process the lines.
4008 */
Bram Moolenaar4c38d662016-08-03 20:54:57 +02004009 static unsigned int __stdcall
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004010sub_process_writer(LPVOID param)
4011{
4012 HANDLE g_hChildStd_IN_Wr = param;
4013 linenr_T lnum = curbuf->b_op_start.lnum;
4014 DWORD len = 0;
4015 DWORD l;
4016 char_u *lp = ml_get(lnum);
4017 char_u *s;
4018 int written = 0;
4019
4020 for (;;)
4021 {
4022 l = (DWORD)STRLEN(lp + written);
4023 if (l == 0)
4024 len = 0;
4025 else if (lp[written] == NL)
4026 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004027 // NL -> NUL translation
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004028 WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
4029 }
4030 else
4031 {
4032 s = vim_strchr(lp + written, NL);
4033 WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
4034 s == NULL ? l : (DWORD)(s - (lp + written)),
4035 &len, NULL);
4036 }
4037 if (len == (int)l)
4038 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004039 // Finished a line, add a NL, unless this line should not have
4040 // one.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004041 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004042 || (!curbuf->b_p_bin
4043 && curbuf->b_p_fixeol)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004044 || (lnum != curbuf->b_no_eol_lnum
4045 && (lnum != curbuf->b_ml.ml_line_count
4046 || curbuf->b_p_eol)))
4047 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004048 WriteFile(g_hChildStd_IN_Wr, "\n", 1,
4049 (LPDWORD)&vim_ignored, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004050 }
4051
4052 ++lnum;
4053 if (lnum > curbuf->b_op_end.lnum)
4054 break;
4055
4056 lp = ml_get(lnum);
4057 written = 0;
4058 }
4059 else if (len > 0)
4060 written += len;
4061 }
4062
Bram Moolenaar0f873732019-12-05 20:28:46 +01004063 // finished all the lines, close pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004064 CloseHandle(g_hChildStd_IN_Wr);
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004065 return 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004066}
4067
4068
Bram Moolenaar0f873732019-12-05 20:28:46 +01004069# define BUFLEN 100 // length for buffer, stolen from unix version
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004070
4071/*
4072 * This function read from the children's stdout and write the
4073 * data on screen or in the buffer accordingly.
4074 */
4075 static void
4076dump_pipe(int options,
4077 HANDLE g_hChildStd_OUT_Rd,
4078 garray_T *ga,
4079 char_u buffer[],
4080 DWORD *buffer_off)
4081{
4082 DWORD availableBytes = 0;
4083 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004084 int ret;
4085 DWORD len;
4086 DWORD toRead;
4087 int repeatCount;
4088
Bram Moolenaar0f873732019-12-05 20:28:46 +01004089 // we query the pipe to see if there is any data to read
4090 // to avoid to perform a blocking read
4091 ret = PeekNamedPipe(g_hChildStd_OUT_Rd, // pipe to query
4092 NULL, // optional buffer
4093 0, // buffer size
4094 NULL, // number of read bytes
4095 &availableBytes, // available bytes total
4096 NULL); // byteLeft
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004097
4098 repeatCount = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004099 // We got real data in the pipe, read it
Bram Moolenaarf6a2b082012-06-29 13:14:03 +02004100 while (ret != 0 && availableBytes > 0)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004101 {
4102 repeatCount++;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004103 toRead = (DWORD)(BUFLEN - *buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004104 toRead = availableBytes < toRead ? availableBytes : toRead;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004105 ReadFile(g_hChildStd_OUT_Rd, buffer + *buffer_off, toRead , &len, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004106
Bram Moolenaar0f873732019-12-05 20:28:46 +01004107 // If we haven't read anything, there is a problem
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004108 if (len == 0)
4109 break;
4110
4111 availableBytes -= len;
4112
4113 if (options & SHELL_READ)
4114 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004115 // Do NUL -> NL translation, append NL separated
4116 // lines to the current buffer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004117 for (i = 0; i < len; ++i)
4118 {
4119 if (buffer[i] == NL)
4120 append_ga_line(ga);
4121 else if (buffer[i] == NUL)
4122 ga_append(ga, NL);
4123 else
4124 ga_append(ga, buffer[i]);
4125 }
4126 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004127 else if (has_mbyte)
4128 {
4129 int l;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004130 int c;
4131 char_u *p;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004132
4133 len += *buffer_off;
4134 buffer[len] = NUL;
4135
Bram Moolenaar0f873732019-12-05 20:28:46 +01004136 // Check if the last character in buffer[] is
4137 // incomplete, keep these bytes for the next
4138 // round.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004139 for (p = buffer; p < buffer + len; p += l)
4140 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004141 l = MB_CPTR2LEN(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004142 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004143 l = 1; // NUL byte?
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004144 else if (MB_BYTE2LEN(*p) != l)
4145 break;
4146 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004147 if (p == buffer) // no complete character
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004148 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004149 // avoid getting stuck at an illegal byte
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004150 if (len >= 12)
4151 ++p;
4152 else
4153 {
4154 *buffer_off = len;
4155 return;
4156 }
4157 }
4158 c = *p;
4159 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004160 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004161 if (p < buffer + len)
4162 {
4163 *p = c;
4164 *buffer_off = (DWORD)((buffer + len) - p);
4165 mch_memmove(buffer, p, *buffer_off);
4166 return;
4167 }
4168 *buffer_off = 0;
4169 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004170 else
4171 {
4172 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004173 msg_puts((char *)buffer);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004174 }
4175
4176 windgoto(msg_row, msg_col);
4177 cursor_on();
4178 out_flush();
4179 }
4180}
4181
4182/*
4183 * Version of system to use for windows NT > 5.0 (Win2K), use pipe
4184 * for communication and doesn't open any new window.
4185 */
4186 static int
4187mch_system_piped(char *cmd, int options)
4188{
4189 STARTUPINFO si;
4190 PROCESS_INFORMATION pi;
4191 DWORD ret = 0;
4192
4193 HANDLE g_hChildStd_IN_Rd = NULL;
4194 HANDLE g_hChildStd_IN_Wr = NULL;
4195 HANDLE g_hChildStd_OUT_Rd = NULL;
4196 HANDLE g_hChildStd_OUT_Wr = NULL;
4197
Bram Moolenaar0f873732019-12-05 20:28:46 +01004198 char_u buffer[BUFLEN + 1]; // reading buffer + size
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004199 DWORD len;
4200
Bram Moolenaar0f873732019-12-05 20:28:46 +01004201 // buffer used to receive keys
4202 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4203 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004204
4205 DWORD i;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004206 int noread_cnt = 0;
4207 garray_T ga;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004208 int delay = 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004209 DWORD buffer_off = 0; // valid bytes in buffer[]
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004210 char *p = NULL;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004211
4212 SECURITY_ATTRIBUTES saAttr;
4213
Bram Moolenaar0f873732019-12-05 20:28:46 +01004214 // Set the bInheritHandle flag so pipe handles are inherited.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004215 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4216 saAttr.bInheritHandle = TRUE;
4217 saAttr.lpSecurityDescriptor = NULL;
4218
4219 if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004220 // Ensure the read handle to the pipe for STDOUT is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004221 || ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004222 // Create a pipe for the child process's STDIN.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004223 || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004224 // Ensure the write handle to the pipe for STDIN is not inherited.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004225 || ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004226 {
4227 CloseHandle(g_hChildStd_IN_Rd);
4228 CloseHandle(g_hChildStd_IN_Wr);
4229 CloseHandle(g_hChildStd_OUT_Rd);
4230 CloseHandle(g_hChildStd_OUT_Wr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004231 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004232 }
4233
4234 si.cb = sizeof(si);
4235 si.lpReserved = NULL;
4236 si.lpDesktop = NULL;
4237 si.lpTitle = NULL;
4238 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4239
Bram Moolenaar0f873732019-12-05 20:28:46 +01004240 // set-up our file redirection
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004241 si.hStdError = g_hChildStd_OUT_Wr;
4242 si.hStdOutput = g_hChildStd_OUT_Wr;
4243 si.hStdInput = g_hChildStd_IN_Rd;
4244 si.wShowWindow = SW_HIDE;
4245 si.cbReserved2 = 0;
4246 si.lpReserved2 = NULL;
4247
4248 if (options & SHELL_READ)
4249 ga_init2(&ga, 1, BUFLEN);
4250
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004251 if (cmd != NULL)
4252 {
4253 p = (char *)vim_strsave((char_u *)cmd);
4254 if (p != NULL)
4255 unescape_shellxquote((char_u *)p, p_sxe);
4256 else
4257 p = cmd;
4258 }
4259
Bram Moolenaar0f873732019-12-05 20:28:46 +01004260 // Now, run the command.
4261 // About "Inherit handles" being TRUE: this command can be litigious,
4262 // handle inheritance was deactivated for pending temp file, but, if we
4263 // deactivate it, the pipes don't work for some reason.
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004264 vim_create_process(p, TRUE, CREATE_DEFAULT_ERROR_MODE,
4265 &si, &pi, NULL, NULL);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004266
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004267 if (p != cmd)
4268 vim_free(p);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004269
Bram Moolenaar0f873732019-12-05 20:28:46 +01004270 // Close our unused side of the pipes
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004271 CloseHandle(g_hChildStd_IN_Rd);
4272 CloseHandle(g_hChildStd_OUT_Wr);
4273
4274 if (options & SHELL_WRITE)
4275 {
Bram Moolenaar86f2cd52016-08-02 21:55:17 +02004276 HANDLE thread = (HANDLE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004277 _beginthreadex(NULL, // security attributes
4278 0, // default stack size
4279 sub_process_writer, // function to be executed
4280 g_hChildStd_IN_Wr, // parameter
4281 0, // creation flag, start immediately
4282 NULL); // we don't care about thread id
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004283 CloseHandle(thread);
4284 g_hChildStd_IN_Wr = NULL;
4285 }
4286
Bram Moolenaar0f873732019-12-05 20:28:46 +01004287 // Keep updating the window while waiting for the shell to finish.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004288 for (;;)
4289 {
4290 MSG msg;
4291
Bram Moolenaar175d0702013-12-11 18:36:33 +01004292 if (pPeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004293 {
4294 TranslateMessage(&msg);
Bram Moolenaar175d0702013-12-11 18:36:33 +01004295 pDispatchMessage(&msg);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004296 }
4297
Bram Moolenaar0f873732019-12-05 20:28:46 +01004298 // write pipe information in the window
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004299 if ((options & (SHELL_READ|SHELL_WRITE))
4300# ifdef FEAT_GUI
4301 || gui.in_use
4302# endif
4303 )
4304 {
4305 len = 0;
4306 if (!(options & SHELL_EXPAND)
4307 && ((options &
4308 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4309 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
4310# ifdef FEAT_GUI
4311 || gui.in_use
4312# endif
4313 )
4314 && (ta_len > 0 || noread_cnt > 4))
4315 {
4316 if (ta_len == 0)
4317 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004318 // Get extra characters when we don't have any. Reset the
4319 // counter and timer.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004320 noread_cnt = 0;
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004321 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4322 }
4323 if (ta_len > 0 || len > 0)
4324 {
4325 /*
4326 * For pipes: Check for CTRL-C: send interrupt signal to
4327 * child. Check for CTRL-D: EOF, close pipe to child.
4328 */
4329 if (len == 1 && cmd != NULL)
4330 {
4331 if (ta_buf[ta_len] == Ctrl_C)
4332 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004333 // Learn what exit code is expected, for
4334 // now put 9 as SIGKILL
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004335 TerminateProcess(pi.hProcess, 9);
4336 }
4337 if (ta_buf[ta_len] == Ctrl_D)
4338 {
4339 CloseHandle(g_hChildStd_IN_Wr);
4340 g_hChildStd_IN_Wr = NULL;
4341 }
4342 }
4343
Bram Moolenaarf4140482020-02-15 23:06:45 +01004344 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004345
4346 /*
4347 * For pipes: echo the typed characters. For a pty this
4348 * does not seem to work.
4349 */
4350 for (i = ta_len; i < ta_len + len; ++i)
4351 {
4352 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4353 msg_putchar(ta_buf[i]);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004354 else if (has_mbyte)
4355 {
4356 int l = (*mb_ptr2len)(ta_buf + i);
4357
4358 msg_outtrans_len(ta_buf + i, l);
4359 i += l - 1;
4360 }
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004361 else
4362 msg_outtrans_len(ta_buf + i, 1);
4363 }
4364 windgoto(msg_row, msg_col);
4365 out_flush();
4366
4367 ta_len += len;
4368
4369 /*
4370 * Write the characters to the child, unless EOF has been
4371 * typed for pipes. Write one character at a time, to
4372 * avoid losing too much typeahead. When writing buffer
4373 * lines, drop the typed characters (only check for
4374 * CTRL-C).
4375 */
4376 if (options & SHELL_WRITE)
4377 ta_len = 0;
4378 else if (g_hChildStd_IN_Wr != NULL)
4379 {
4380 WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
4381 1, &len, NULL);
4382 // if we are typing in, we want to keep things reactive
4383 delay = 1;
4384 if (len > 0)
4385 {
4386 ta_len -= len;
4387 mch_memmove(ta_buf, ta_buf + len, ta_len);
4388 }
4389 }
4390 }
4391 }
4392 }
4393
4394 if (ta_len)
4395 ui_inchar_undo(ta_buf, ta_len);
4396
4397 if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
4398 {
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004399 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004400 break;
4401 }
4402
4403 ++noread_cnt;
Bram Moolenaar2eba1822011-08-27 15:10:04 +02004404 dump_pipe(options, g_hChildStd_OUT_Rd, &ga, buffer, &buffer_off);
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004405
Bram Moolenaar0f873732019-12-05 20:28:46 +01004406 // We start waiting for a very short time and then increase it, so
4407 // that we respond quickly when the process is quick, and don't
4408 // consume too much overhead when it's slow.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004409 if (delay < 50)
4410 delay += 10;
4411 }
4412
Bram Moolenaar0f873732019-12-05 20:28:46 +01004413 // Close the pipe
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004414 CloseHandle(g_hChildStd_OUT_Rd);
4415 if (g_hChildStd_IN_Wr != NULL)
4416 CloseHandle(g_hChildStd_IN_Wr);
4417
4418 WaitForSingleObject(pi.hProcess, INFINITE);
4419
Bram Moolenaar0f873732019-12-05 20:28:46 +01004420 // Get the command exit code
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004421 GetExitCodeProcess(pi.hProcess, &ret);
4422
4423 if (options & SHELL_READ)
4424 {
4425 if (ga.ga_len > 0)
4426 {
4427 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004428 // remember that the NL was missing
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004429 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
4430 }
4431 else
4432 curbuf->b_no_eol_lnum = 0;
4433 ga_clear(&ga);
4434 }
4435
Bram Moolenaar0f873732019-12-05 20:28:46 +01004436 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004437 CloseHandle(pi.hThread);
4438 CloseHandle(pi.hProcess);
4439
4440 return ret;
4441}
4442
4443 static int
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004444mch_system_g(char *cmd, int options)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004445{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004446 // if we can pipe and the shelltemp option is off
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004447 if (!p_stmp)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02004448 return mch_system_piped(cmd, options);
4449 else
4450 return mch_system_classic(cmd, options);
4451}
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004454#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004455 static int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02004456mch_system_c(char *cmd, int options UNUSED)
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004457{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004458 int ret;
4459 WCHAR *wcmd;
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004460 char_u *buf;
4461 size_t len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004462
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004463 // If the command starts and ends with double quotes, enclose the command
4464 // in parentheses.
4465 len = STRLEN(cmd);
4466 if (len >= 2 && cmd[0] == '"' && cmd[len - 1] == '"')
4467 {
4468 len += 3;
4469 buf = alloc(len);
4470 if (buf == NULL)
4471 return -1;
4472 vim_snprintf((char *)buf, len, "(%s)", cmd);
4473 wcmd = enc_to_utf16(buf, NULL);
4474 free(buf);
4475 }
4476 else
4477 wcmd = enc_to_utf16((char_u *)cmd, NULL);
4478
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004479 if (wcmd == NULL)
4480 return -1;
4481
4482 ret = _wsystem(wcmd);
4483 vim_free(wcmd);
4484 return ret;
Bram Moolenaar910cffb2013-12-11 17:58:35 +01004485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487#endif
4488
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004489 static int
4490mch_system(char *cmd, int options)
4491{
4492#ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004493 if (gui.in_use || gui.starting)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004494 return mch_system_g(cmd, options);
4495 else
4496 return mch_system_c(cmd, options);
4497#elif defined(FEAT_GUI_MSWIN)
4498 return mch_system_g(cmd, options);
4499#else
4500 return mch_system_c(cmd, options);
4501#endif
4502}
4503
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004504#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4505/*
4506 * Use a terminal window to run a shell command in.
4507 */
4508 static int
4509mch_call_shell_terminal(
4510 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004511 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004512{
4513 jobopt_T opt;
4514 char_u *newcmd = NULL;
4515 typval_T argvar[2];
4516 long_u cmdlen;
4517 int retval = -1;
4518 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004519 job_T *job;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004520 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004521 oparg_T oa; // operator arguments
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004522
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004523 if (cmd == NULL)
4524 cmdlen = STRLEN(p_sh) + 1;
4525 else
4526 cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004527 newcmd = alloc(cmdlen);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004528 if (newcmd == NULL)
4529 return 255;
Bram Moolenaar42f652f2018-03-19 21:44:37 +01004530 if (cmd == NULL)
4531 {
4532 STRCPY(newcmd, p_sh);
4533 ch_log(NULL, "starting terminal to run a shell");
4534 }
4535 else
4536 {
4537 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
4538 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4539 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004540
4541 init_job_options(&opt);
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004542
4543 argvar[0].v_type = VAR_STRING;
4544 argvar[0].vval.v_string = newcmd;
4545 argvar[1].v_type = VAR_UNKNOWN;
4546 buf = term_start(argvar, NULL, &opt, TERM_START_SYSTEM);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004547 if (buf == NULL)
Bram Moolenaar9029b912019-03-21 19:58:00 +01004548 {
4549 vim_free(newcmd);
Bram Moolenaar81c3c89a2018-03-20 11:41:44 +01004550 return 255;
Bram Moolenaar9029b912019-03-21 19:58:00 +01004551 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004552
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004553 job = term_getjob(buf->b_term);
4554 ++job->jv_refcount;
4555
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556 // Find a window to make "buf" curbuf.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004557 aucmd_prepbuf(&aco, buf);
4558
4559 clear_oparg(&oa);
4560 while (term_use_loop())
4561 {
4562 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4563 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004564 // If terminal_loop() returns OK we got a key that is handled
4565 // in Normal model. We don't do redrawing anyway.
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004566 if (terminal_loop(TRUE) == OK)
4567 normal_cmd(&oa, TRUE);
4568 }
4569 else
4570 normal_cmd(&oa, TRUE);
4571 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004572 retval = job->jv_exitval;
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004573 ch_log(NULL, "system command finished");
4574
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004575 job_unref(job);
4576
Bram Moolenaar0f873732019-12-05 20:28:46 +01004577 // restore curwin/curbuf and a few other things
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004578 aucmd_restbuf(&aco);
4579
4580 wait_return(TRUE);
4581 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4582
4583 vim_free(newcmd);
4584 return retval;
4585}
4586#endif
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588/*
4589 * Either execute a command by calling the shell or start a new shell
4590 */
4591 int
4592mch_call_shell(
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004593 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004594 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 int x = 0;
4597 int tmode = cur_tmode;
4598#ifdef FEAT_TITLE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004599 WCHAR szShellTitle[512];
Bram Moolenaar799d6ab2014-10-16 16:16:37 +02004600
Bram Moolenaar0f873732019-12-05 20:28:46 +01004601 // Change the title to reflect that we are in a subshell.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004602 if (GetConsoleTitleW(szShellTitle,
4603 sizeof(szShellTitle)/sizeof(WCHAR) - 4) > 0)
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004604 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004605 if (cmd == NULL)
4606 wcscat(szShellTitle, L" :sh");
4607 else
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004608 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004609 WCHAR *wn = enc_to_utf16((char_u *)cmd, NULL);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004610
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004611 if (wn != NULL)
4612 {
4613 wcscat(szShellTitle, L" - !");
4614 if ((wcslen(szShellTitle) + wcslen(wn) <
4615 sizeof(szShellTitle)/sizeof(WCHAR)))
4616 wcscat(szShellTitle, wn);
4617 SetConsoleTitleW(szShellTitle);
4618 vim_free(wn);
Bram Moolenaar1df52d72014-10-15 22:50:10 +02004619 }
4620 }
4621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
4623
4624 out_flush();
4625
4626#ifdef MCH_WRITE_DUMP
4627 if (fdDump)
4628 {
4629 fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options);
4630 fflush(fdDump);
4631 }
4632#endif
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004633#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004634 // TODO: make the terminal window work with input or output redirected.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004635 if (
4636# ifdef VIMDLL
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004637 gui.in_use &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004638# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004639 vim_strchr(p_go, GO_TERMINAL) != NULL
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004640 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
4641 {
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004642 char_u *cmdbase = cmd;
4643
Bram Moolenaar4d5c1262019-09-20 17:20:02 +02004644 if (cmdbase != NULL)
4645 // Skip a leading quote and (.
4646 while (*cmdbase == '"' || *cmdbase == '(')
4647 ++cmdbase;
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004648
4649 // Check the command does not begin with "start "
Bram Moolenaar36e7a822019-11-13 21:49:24 +01004650 if (cmdbase == NULL || STRNICMP(cmdbase, "start", 5) != 0
4651 || !VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004652 {
4653 // Use a terminal window to run the command in.
4654 x = mch_call_shell_terminal(cmd, options);
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004655# ifdef FEAT_TITLE
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004656 resettitle();
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004657# endif
Bram Moolenaar7c348bb2019-06-08 12:05:22 +02004658 return x;
4659 }
Bram Moolenaarf05fa372018-03-18 19:29:34 +01004660 }
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663 /*
4664 * Catch all deadly signals while running the external command, because a
4665 * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us.
4666 */
4667 signal(SIGINT, SIG_IGN);
4668#if defined(__GNUC__) && !defined(__MINGW32__)
4669 signal(SIGKILL, SIG_IGN);
4670#else
4671 signal(SIGBREAK, SIG_IGN);
4672#endif
4673 signal(SIGILL, SIG_IGN);
4674 signal(SIGFPE, SIG_IGN);
4675 signal(SIGSEGV, SIG_IGN);
4676 signal(SIGTERM, SIG_IGN);
4677 signal(SIGABRT, SIG_IGN);
4678
4679 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004680 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
4682 if (cmd == NULL)
4683 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004684 x = mch_system((char *)p_sh, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 else
4687 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004688 // we use "command" or "cmd" to start the shell; slow but easy
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004689 char_u *newcmd = NULL;
4690 char_u *cmdbase = cmd;
4691 long_u cmdlen;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004692
Bram Moolenaar0f873732019-12-05 20:28:46 +01004693 // Skip a leading ", ( and "(.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004694 if (*cmdbase == '"' )
4695 ++cmdbase;
4696 if (*cmdbase == '(')
4697 ++cmdbase;
4698
Bram Moolenaar1c465442017-03-12 20:10:05 +01004699 if ((STRNICMP(cmdbase, "start", 5) == 0) && VIM_ISWHITE(cmdbase[5]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004700 {
4701 STARTUPINFO si;
4702 PROCESS_INFORMATION pi;
4703 DWORD flags = CREATE_NEW_CONSOLE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004704 INT n_show_cmd = SW_SHOWNORMAL;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004705 char_u *p;
4706
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004707 ZeroMemory(&si, sizeof(si));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004708 si.cb = sizeof(si);
4709 si.lpReserved = NULL;
4710 si.lpDesktop = NULL;
4711 si.lpTitle = NULL;
4712 si.dwFlags = 0;
4713 si.cbReserved2 = 0;
4714 si.lpReserved2 = NULL;
4715
4716 cmdbase = skipwhite(cmdbase + 5);
4717 if ((STRNICMP(cmdbase, "/min", 4) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004718 && VIM_ISWHITE(cmdbase[4]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004719 {
4720 cmdbase = skipwhite(cmdbase + 4);
4721 si.dwFlags = STARTF_USESHOWWINDOW;
4722 si.wShowWindow = SW_SHOWMINNOACTIVE;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004723 n_show_cmd = SW_SHOWMINNOACTIVE;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004724 }
4725 else if ((STRNICMP(cmdbase, "/b", 2) == 0)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004726 && VIM_ISWHITE(cmdbase[2]))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004727 {
4728 cmdbase = skipwhite(cmdbase + 2);
4729 flags = CREATE_NO_WINDOW;
4730 si.dwFlags = STARTF_USESTDHANDLES;
4731 si.hStdInput = CreateFile("\\\\.\\NUL", // File name
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004732 GENERIC_READ, // Access flags
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004733 0, // Share flags
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004734 NULL, // Security att.
4735 OPEN_EXISTING, // Open flags
4736 FILE_ATTRIBUTE_NORMAL, // File att.
4737 NULL); // Temp file
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004738 si.hStdOutput = si.hStdInput;
4739 si.hStdError = si.hStdInput;
4740 }
4741
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 // Remove a trailing ", ) and )" if they have a match
4743 // at the start of the command.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004744 if (cmdbase > cmd)
4745 {
4746 p = cmdbase + STRLEN(cmdbase);
4747 if (p > cmdbase && p[-1] == '"' && *cmd == '"')
4748 *--p = NUL;
4749 if (p > cmdbase && p[-1] == ')'
4750 && (*cmd =='(' || cmd[1] == '('))
4751 *--p = NUL;
4752 }
4753
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004754 newcmd = cmdbase;
4755 unescape_shellxquote(cmdbase, p_sxe);
4756
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004757 /*
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004758 * If creating new console, arguments are passed to the
4759 * 'cmd.exe' as-is. If it's not, arguments are not treated
4760 * correctly for current 'cmd.exe'. So unescape characters in
4761 * shellxescape except '|' for avoiding to be treated as
4762 * argument to them. Pass the arguments to sub-shell.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004763 */
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004764 if (flags != CREATE_NEW_CONSOLE)
4765 {
4766 char_u *subcmd;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004767 char_u *cmd_shell = mch_getenv("COMSPEC");
4768
4769 if (cmd_shell == NULL || *cmd_shell == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004770 cmd_shell = (char_u *)default_shell();
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004771
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004772 subcmd = vim_strsave_escaped_ext(cmdbase,
4773 (char_u *)"|", '^', FALSE);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004774 if (subcmd != NULL)
4775 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 // make "cmd.exe /c arguments"
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004777 cmdlen = STRLEN(cmd_shell) + STRLEN(subcmd) + 5;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004778 newcmd = alloc(cmdlen);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004779 if (newcmd != NULL)
4780 vim_snprintf((char *)newcmd, cmdlen, "%s /c %s",
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004781 cmd_shell, subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004782 else
4783 newcmd = cmdbase;
Bram Moolenaaree7d1002012-02-22 15:34:08 +01004784 vim_free(subcmd);
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004785 }
4786 }
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004787
4788 /*
4789 * Now, start the command as a process, so that it doesn't
4790 * inherit our handles which causes unpleasant dangling swap
4791 * files if we exit before the spawned process
4792 */
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004793 if (vim_create_process((char *)newcmd, FALSE, flags,
4794 &si, &pi, NULL, NULL))
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004795 x = 0;
Bram Moolenaarb2964f22017-03-21 19:29:26 +01004796 else if (vim_shell_execute((char *)newcmd, n_show_cmd)
4797 > (HINSTANCE)32)
4798 x = 0;
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004799 else
4800 {
4801 x = -1;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004802#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004803# ifdef VIMDLL
4804 if (gui.in_use)
4805# endif
4806 emsg(_("E371: Command not found"));
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004807#endif
4808 }
Bram Moolenaarfb7df7b2012-02-22 13:07:05 +01004809
4810 if (newcmd != cmdbase)
4811 vim_free(newcmd);
4812
Bram Moolenaarfcc3f462014-01-24 19:55:37 +01004813 if (si.dwFlags == STARTF_USESTDHANDLES && si.hStdInput != NULL)
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815 // Close the handle to \\.\NUL created above.
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004816 CloseHandle(si.hStdInput);
4817 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004818 // Close the handles to the subprocess, so that it goes away
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004819 CloseHandle(pi.hThread);
4820 CloseHandle(pi.hProcess);
4821 }
4822 else
4823 {
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004824 cmdlen =
Bram Moolenaar4f974752019-02-17 17:44:42 +01004825#ifdef FEAT_GUI_MSWIN
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004826 ((gui.in_use || gui.starting) ?
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004827 (!s_dont_use_vimrun && p_stmp ?
4828 STRLEN(vimrun_path) : STRLEN(p_sh) + STRLEN(p_shcf))
4829 : 0) +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004831 STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004832
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004833 newcmd = alloc(cmdlen);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004834 if (newcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004836#if defined(FEAT_GUI_MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004837 if (
4838# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004839 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004840# endif
4841 need_vimrun_warning)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar63e43442016-11-19 17:28:44 +01004843 char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
4844 "External commands will not pause after completion.\n"
4845 "See :help win32-vimrun for more information.");
4846 char *title = _("Vim Warning");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004847 WCHAR *wmsg = enc_to_utf16((char_u *)msg, NULL);
4848 WCHAR *wtitle = enc_to_utf16((char_u *)title, NULL);
Bram Moolenaar63e43442016-11-19 17:28:44 +01004849
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004850 if (wmsg != NULL && wtitle != NULL)
4851 MessageBoxW(NULL, wmsg, wtitle, MB_ICONWARNING);
4852 vim_free(wmsg);
4853 vim_free(wtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 need_vimrun_warning = FALSE;
4855 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004856 if (
4857# ifdef VIMDLL
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004858 (gui.in_use || gui.starting) &&
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004859# endif
4860 !s_dont_use_vimrun && p_stmp)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004861 // Use vimrun to execute the command. It opens a console
4862 // window, which can be closed without killing Vim.
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004863 vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 vimrun_path,
4865 (msg_silent != 0 || (options & SHELL_DOOUT))
4866 ? "-s " : "",
4867 p_sh, p_shcf, cmd);
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004868 else if (
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004869# ifdef VIMDLL
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004870 (gui.in_use || gui.starting) &&
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004871# endif
Bram Moolenaar0e6bfb92019-07-31 20:53:56 +02004872 s_dont_use_vimrun && STRCMP(p_shcf, "/c") == 0)
Bram Moolenaarfcc4d922019-05-24 13:32:36 +02004873 // workaround for the case that "vimrun" does not exist
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004874 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s %s %s",
4875 p_sh, p_shcf, p_sh, p_shcf, cmd);
Bram Moolenaar98ffe4c2019-05-07 23:01:39 +02004876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#endif
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004878 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s",
Bram Moolenaar0fde2902008-03-16 13:54:13 +00004879 p_sh, p_shcf, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 x = mch_system((char *)newcmd, options);
Bram Moolenaar6b707b42012-02-21 21:22:44 +01004881 vim_free(newcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 }
4885
4886 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004887 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
Bram Moolenaar0f873732019-12-05 20:28:46 +01004889 // Print the return value, unless "vimrun" was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
Bram Moolenaar4f974752019-02-17 17:44:42 +01004891#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar294d9bf2019-05-23 20:12:46 +02004892 && ((gui.in_use || gui.starting) ?
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004893 ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895 )
4896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004897 smsg(_("shell returned %d"), x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 msg_putchar('\n');
4899 }
4900#ifdef FEAT_TITLE
4901 resettitle();
4902#endif
4903
4904 signal(SIGINT, SIG_DFL);
4905#if defined(__GNUC__) && !defined(__MINGW32__)
4906 signal(SIGKILL, SIG_DFL);
4907#else
4908 signal(SIGBREAK, SIG_DFL);
4909#endif
4910 signal(SIGILL, SIG_DFL);
4911 signal(SIGFPE, SIG_DFL);
4912 signal(SIGSEGV, SIG_DFL);
4913 signal(SIGTERM, SIG_DFL);
4914 signal(SIGABRT, SIG_DFL);
4915
4916 return x;
4917}
4918
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004919#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004920 static HANDLE
4921job_io_file_open(
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004922 char_u *fname,
4923 DWORD dwDesiredAccess,
4924 DWORD dwShareMode,
4925 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
4926 DWORD dwCreationDisposition,
4927 DWORD dwFlagsAndAttributes)
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004928{
4929 HANDLE h;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004930 WCHAR *wn;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004931
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004932 wn = enc_to_utf16(fname, NULL);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004933 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004934 return INVALID_HANDLE_VALUE;
4935
4936 h = CreateFileW(wn, dwDesiredAccess, dwShareMode,
4937 lpSecurityAttributes, dwCreationDisposition,
4938 dwFlagsAndAttributes, NULL);
4939 vim_free(wn);
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01004940 return h;
4941}
4942
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004943/*
4944 * Turn the dictionary "env" into a NUL separated list that can be used as the
4945 * environment argument of vim_create_process().
4946 */
Bram Moolenaarba6febd2017-10-30 21:56:23 +01004947 void
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004948win32_build_env(dict_T *env, garray_T *gap, int is_terminal)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004949{
4950 hashitem_T *hi;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004951 long_u todo = env != NULL ? env->dv_hashtab.ht_used : 0;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004952 LPVOID base = GetEnvironmentStringsW();
4953
Bram Moolenaar0f873732019-12-05 20:28:46 +01004954 // for last \0
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004955 if (ga_grow(gap, 1) == FAIL)
4956 return;
4957
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004958 if (env != NULL)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004959 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004960 for (hi = env->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004961 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004962 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004963 {
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004964 typval_T *item = &dict_lookup(hi)->di_tv;
4965 WCHAR *wkey = enc_to_utf16((char_u *)hi->hi_key, NULL);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004966 WCHAR *wval = enc_to_utf16(tv_get_string(item), NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004967 --todo;
4968 if (wkey != NULL && wval != NULL)
4969 {
4970 size_t n;
4971 size_t lkey = wcslen(wkey);
4972 size_t lval = wcslen(wval);
Bram Moolenaar60104f12017-08-14 23:25:04 +02004973
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01004974 if (ga_grow(gap, (int)(lkey + lval + 2)) != OK)
4975 continue;
4976 for (n = 0; n < lkey; n++)
4977 *((WCHAR*)gap->ga_data + gap->ga_len++) = wkey[n];
4978 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'=';
4979 for (n = 0; n < lval; n++)
4980 *((WCHAR*)gap->ga_data + gap->ga_len++) = wval[n];
4981 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
4982 }
Bram Moolenaarbdace832019-03-02 10:13:42 +01004983 vim_free(wkey);
4984 vim_free(wval);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004985 }
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004986 }
4987 }
4988
Bram Moolenaar355757a2020-02-10 22:06:32 +01004989 if (base)
4990 {
4991 WCHAR *p = (WCHAR*) base;
4992
4993 // for last \0
4994 if (ga_grow(gap, 1) == FAIL)
4995 return;
4996
4997 while (*p != 0 || *(p + 1) != 0)
4998 {
4999 if (ga_grow(gap, 1) == OK)
5000 *((WCHAR*)gap->ga_data + gap->ga_len++) = *p;
5001 p++;
5002 }
5003 FreeEnvironmentStrings(base);
5004 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5005 }
5006
Bram Moolenaar493359e2018-06-12 20:25:52 +02005007# if defined(FEAT_CLIENTSERVER) || defined(FEAT_TERMINAL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005008 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005009# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005010 char_u *servername = get_vim_var_str(VV_SEND_SERVER);
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005011 size_t servername_len = STRLEN(servername);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005012# endif
5013# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005014 char_u *version = get_vim_var_str(VV_VERSION);
5015 size_t version_len = STRLEN(version);
Bram Moolenaar493359e2018-06-12 20:25:52 +02005016# endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005017 // size of "VIM_SERVERNAME=" and value,
5018 // plus "VIM_TERMINAL=" and value,
5019 // plus two terminating NULs
5020 size_t n = 0
Bram Moolenaar493359e2018-06-12 20:25:52 +02005021# ifdef FEAT_CLIENTSERVER
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005022 + 15 + servername_len
Bram Moolenaar493359e2018-06-12 20:25:52 +02005023# endif
5024# ifdef FEAT_TERMINAL
5025 + 13 + version_len + 2
5026# endif
5027 ;
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005028
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005029 if (ga_grow(gap, (int)n) == OK)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005030 {
Bram Moolenaar493359e2018-06-12 20:25:52 +02005031# ifdef FEAT_CLIENTSERVER
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005032 for (n = 0; n < 15; n++)
5033 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5034 (WCHAR)"VIM_SERVERNAME="[n];
Bram Moolenaard7a137f2018-06-12 18:05:24 +02005035 for (n = 0; n < servername_len; n++)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005036 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5037 (WCHAR)servername[n];
5038 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
Bram Moolenaar493359e2018-06-12 20:25:52 +02005039# endif
5040# ifdef FEAT_TERMINAL
5041 if (is_terminal)
5042 {
5043 for (n = 0; n < 13; n++)
5044 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5045 (WCHAR)"VIM_TERMINAL="[n];
5046 for (n = 0; n < version_len; n++)
5047 *((WCHAR*)gap->ga_data + gap->ga_len++) =
5048 (WCHAR)version[n];
5049 *((WCHAR*)gap->ga_data + gap->ga_len++) = L'\0';
5050 }
5051# endif
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005052 }
5053 }
Bram Moolenaar79c6b512018-06-12 21:11:12 +02005054# endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005055}
5056
Bram Moolenaarb091f302019-01-19 14:37:00 +01005057/*
5058 * Create a pair of pipes.
5059 * Return TRUE for success, FALSE for failure.
5060 */
5061 static BOOL
5062create_pipe_pair(HANDLE handles[2])
5063{
5064 static LONG s;
5065 char name[64];
5066 SECURITY_ATTRIBUTES sa;
5067
5068 sprintf(name, "\\\\?\\pipe\\vim-%08lx-%08lx",
5069 GetCurrentProcessId(),
5070 InterlockedIncrement(&s));
5071
5072 // Create named pipe. Max size of named pipe is 65535.
5073 handles[1] = CreateNamedPipe(
5074 name,
5075 PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
5076 PIPE_TYPE_BYTE | PIPE_NOWAIT,
Bram Moolenaar24058382019-01-24 23:11:49 +01005077 1, MAX_NAMED_PIPE_SIZE, 0, 0, NULL);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005078
5079 if (handles[1] == INVALID_HANDLE_VALUE)
5080 return FALSE;
5081
5082 sa.nLength = sizeof(sa);
5083 sa.bInheritHandle = TRUE;
5084 sa.lpSecurityDescriptor = NULL;
5085
5086 handles[0] = CreateFile(name,
5087 FILE_GENERIC_READ,
5088 FILE_SHARE_READ, &sa,
5089 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5090
5091 if (handles[0] == INVALID_HANDLE_VALUE)
5092 {
Bram Moolenaar6982f422019-02-16 16:48:01 +01005093 CloseHandle(handles[1]);
Bram Moolenaarb091f302019-01-19 14:37:00 +01005094 return FALSE;
5095 }
5096
5097 return TRUE;
5098}
5099
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005100 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005101mch_job_start(char *cmd, job_T *job, jobopt_T *options)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005102{
5103 STARTUPINFO si;
5104 PROCESS_INFORMATION pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005105 HANDLE jo;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005106 SECURITY_ATTRIBUTES saAttr;
5107 channel_T *channel = NULL;
Bram Moolenaard8070362016-02-15 21:56:54 +01005108 HANDLE ifd[2];
5109 HANDLE ofd[2];
5110 HANDLE efd[2];
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005111 garray_T ga;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005112
5113 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5114 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5115 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
5116 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
5117 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5118 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
5119 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
5120
5121 if (use_out_for_err && use_null_for_out)
5122 use_null_for_err = TRUE;
Bram Moolenaard8070362016-02-15 21:56:54 +01005123
5124 ifd[0] = INVALID_HANDLE_VALUE;
5125 ifd[1] = INVALID_HANDLE_VALUE;
5126 ofd[0] = INVALID_HANDLE_VALUE;
5127 ofd[1] = INVALID_HANDLE_VALUE;
5128 efd[0] = INVALID_HANDLE_VALUE;
5129 efd[1] = INVALID_HANDLE_VALUE;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005130 ga_init2(&ga, (int)sizeof(wchar_t), 500);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005131
Bram Moolenaar14207f42016-10-27 21:13:10 +02005132 jo = CreateJobObject(NULL, NULL);
5133 if (jo == NULL)
5134 {
5135 job->jv_status = JOB_FAILED;
5136 goto failed;
5137 }
5138
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005139 if (options->jo_env != NULL)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005140 win32_build_env(options->jo_env, &ga, FALSE);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005141
Bram Moolenaar76467df2016-02-12 19:30:26 +01005142 ZeroMemory(&pi, sizeof(pi));
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005143 ZeroMemory(&si, sizeof(si));
5144 si.cb = sizeof(si);
Bram Moolenaard8070362016-02-15 21:56:54 +01005145 si.dwFlags |= STARTF_USESHOWWINDOW;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005146 si.wShowWindow = SW_HIDE;
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005147
Bram Moolenaard8070362016-02-15 21:56:54 +01005148 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5149 saAttr.bInheritHandle = TRUE;
5150 saAttr.lpSecurityDescriptor = NULL;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005151
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005152 if (use_file_for_in)
5153 {
5154 char_u *fname = options->jo_io_name[PART_IN];
5155
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005156 ifd[0] = job_io_file_open(fname, GENERIC_READ,
5157 FILE_SHARE_READ | FILE_SHARE_WRITE,
5158 &saAttr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
5159 if (ifd[0] == INVALID_HANDLE_VALUE)
Bram Moolenaar94d01912016-03-08 13:48:51 +01005160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005161 semsg(_(e_notopen), fname);
Bram Moolenaar94d01912016-03-08 13:48:51 +01005162 goto failed;
5163 }
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005164 }
Bram Moolenaarb091f302019-01-19 14:37:00 +01005165 else if (!use_null_for_in
5166 && (!create_pipe_pair(ifd)
5167 || !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005168 goto failed;
5169
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005170 if (use_file_for_out)
5171 {
5172 char_u *fname = options->jo_io_name[PART_OUT];
5173
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005174 ofd[1] = job_io_file_open(fname, GENERIC_WRITE,
5175 FILE_SHARE_READ | FILE_SHARE_WRITE,
5176 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5177 if (ofd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005179 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005180 goto failed;
5181 }
5182 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005183 else if (!use_null_for_out &&
5184 (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005185 || !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005186 goto failed;
5187
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005188 if (use_file_for_err)
5189 {
5190 char_u *fname = options->jo_io_name[PART_ERR];
5191
Bram Moolenaar7bffaa92016-03-10 21:46:03 +01005192 efd[1] = job_io_file_open(fname, GENERIC_WRITE,
5193 FILE_SHARE_READ | FILE_SHARE_WRITE,
5194 &saAttr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
5195 if (efd[1] == INVALID_HANDLE_VALUE)
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005197 semsg(_(e_notopen), fname);
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005198 goto failed;
5199 }
5200 }
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005201 else if (!use_out_for_err && !use_null_for_err &&
5202 (!CreatePipe(&efd[0], &efd[1], &saAttr, 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005203 || !SetHandleInformation(efd[0], HANDLE_FLAG_INHERIT, 0)))
Bram Moolenaard8070362016-02-15 21:56:54 +01005204 goto failed;
Bram Moolenaar13d6fb12016-03-08 18:40:52 +01005205
Bram Moolenaard8070362016-02-15 21:56:54 +01005206 si.dwFlags |= STARTF_USESTDHANDLES;
5207 si.hStdInput = ifd[0];
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005208 si.hStdOutput = ofd[1];
5209 si.hStdError = use_out_for_err ? ofd[1] : efd[1];
5210
5211 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5212 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005213 if (options->jo_set & JO_CHANNEL)
5214 {
5215 channel = options->jo_channel;
5216 if (channel != NULL)
5217 ++channel->ch_refcount;
5218 }
5219 else
5220 channel = add_channel();
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005221 if (channel == NULL)
5222 goto failed;
5223 }
Bram Moolenaard8070362016-02-15 21:56:54 +01005224
5225 if (!vim_create_process(cmd, TRUE,
Bram Moolenaar14207f42016-10-27 21:13:10 +02005226 CREATE_SUSPENDED |
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005227 CREATE_DEFAULT_ERROR_MODE |
5228 CREATE_NEW_PROCESS_GROUP |
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005229 CREATE_UNICODE_ENVIRONMENT |
Bram Moolenaar76467df2016-02-12 19:30:26 +01005230 CREATE_NEW_CONSOLE,
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005231 &si, &pi,
5232 ga.ga_data,
5233 (char *)options->jo_cwd))
Bram Moolenaar76467df2016-02-12 19:30:26 +01005234 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005235 CloseHandle(jo);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005236 job->jv_status = JOB_FAILED;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005237 goto failed;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005238 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005239
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005240 ga_clear(&ga);
5241
Bram Moolenaar14207f42016-10-27 21:13:10 +02005242 if (!AssignProcessToJobObject(jo, pi.hProcess))
5243 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005244 // if failing, switch the way to terminate
5245 // process with TerminateProcess.
Bram Moolenaar14207f42016-10-27 21:13:10 +02005246 CloseHandle(jo);
5247 jo = NULL;
5248 }
5249 ResumeThread(pi.hThread);
Bram Moolenaar75578a32016-03-10 16:33:31 +01005250 CloseHandle(pi.hThread);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005251 job->jv_proc_info = pi;
Bram Moolenaar14207f42016-10-27 21:13:10 +02005252 job->jv_job_object = jo;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005253 job->jv_status = JOB_STARTED;
5254
Bram Moolenaar641ad6c2016-09-01 18:32:11 +02005255 CloseHandle(ifd[0]);
5256 CloseHandle(ofd[1]);
5257 if (!use_out_for_err && !use_null_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005258 CloseHandle(efd[1]);
Bram Moolenaard8070362016-02-15 21:56:54 +01005259
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005260 job->jv_channel = channel;
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005261 if (channel != NULL)
5262 {
5263 channel_set_pipes(channel,
5264 use_file_for_in || use_null_for_in
5265 ? INVALID_FD : (sock_T)ifd[1],
5266 use_file_for_out || use_null_for_out
5267 ? INVALID_FD : (sock_T)ofd[0],
5268 use_out_for_err || use_file_for_err || use_null_for_err
5269 ? INVALID_FD : (sock_T)efd[0]);
5270 channel_set_job(channel, job, options);
Bram Moolenaard5d3d302016-03-09 20:54:51 +01005271 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01005272 return;
5273
5274failed:
Bram Moolenaard8070362016-02-15 21:56:54 +01005275 CloseHandle(ifd[0]);
5276 CloseHandle(ofd[0]);
5277 CloseHandle(efd[0]);
5278 CloseHandle(ifd[1]);
5279 CloseHandle(ofd[1]);
5280 CloseHandle(efd[1]);
Bram Moolenaarde279892016-03-11 22:19:44 +01005281 channel_unref(channel);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005282 ga_clear(&ga);
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005283}
5284
5285 char *
5286mch_job_status(job_T *job)
5287{
5288 DWORD dwExitCode = 0;
5289
Bram Moolenaar76467df2016-02-12 19:30:26 +01005290 if (!GetExitCodeProcess(job->jv_proc_info.hProcess, &dwExitCode)
5291 || dwExitCode != STILL_ACTIVE)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005292 {
Bram Moolenaareab089d2016-02-21 19:32:02 +01005293 job->jv_exitval = (int)dwExitCode;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005294 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005295 {
5296 ch_log(job->jv_channel, "Job ended");
5297 job->jv_status = JOB_ENDED;
5298 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005299 return "dead";
5300 }
5301 return "run";
5302}
5303
Bram Moolenaar97792de2016-10-15 18:36:49 +02005304 job_T *
5305mch_detect_ended_job(job_T *job_list)
5306{
5307 HANDLE jobHandles[MAXIMUM_WAIT_OBJECTS];
5308 job_T *jobArray[MAXIMUM_WAIT_OBJECTS];
5309 job_T *job = job_list;
5310
5311 while (job != NULL)
5312 {
5313 DWORD n;
5314 DWORD result;
5315
5316 for (n = 0; n < MAXIMUM_WAIT_OBJECTS
5317 && job != NULL; job = job->jv_next)
5318 {
5319 if (job->jv_status == JOB_STARTED)
5320 {
5321 jobHandles[n] = job->jv_proc_info.hProcess;
5322 jobArray[n] = job;
5323 ++n;
5324 }
5325 }
5326 if (n == 0)
5327 continue;
5328 result = WaitForMultipleObjects(n, jobHandles, FALSE, 0);
5329 if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n)
5330 {
5331 job_T *wait_job = jobArray[result - WAIT_OBJECT_0];
5332
5333 if (STRCMP(mch_job_status(wait_job), "dead") == 0)
5334 return wait_job;
5335 }
5336 }
5337 return NULL;
5338}
5339
Bram Moolenaarfb630902016-10-29 14:55:00 +02005340 static BOOL
5341terminate_all(HANDLE process, int code)
5342{
5343 PROCESSENTRY32 pe;
5344 HANDLE h = INVALID_HANDLE_VALUE;
5345 DWORD pid = GetProcessId(process);
5346
5347 if (pid != 0)
5348 {
5349 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5350 if (h != INVALID_HANDLE_VALUE)
5351 {
5352 pe.dwSize = sizeof(PROCESSENTRY32);
5353 if (!Process32First(h, &pe))
5354 goto theend;
5355
5356 do
5357 {
5358 if (pe.th32ParentProcessID == pid)
5359 {
5360 HANDLE ph = OpenProcess(
5361 PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
5362 if (ph != NULL)
5363 {
5364 terminate_all(ph, code);
5365 CloseHandle(ph);
5366 }
5367 }
5368 } while (Process32Next(h, &pe));
5369
5370 CloseHandle(h);
5371 }
5372 }
5373
5374theend:
5375 return TerminateProcess(process, code);
5376}
5377
5378/*
5379 * Send a (deadly) signal to "job".
5380 * Return FAIL if it didn't work.
5381 */
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005382 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005383mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005384{
Bram Moolenaar923d9262016-02-25 20:56:01 +01005385 int ret;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005386
Bram Moolenaar923d9262016-02-25 20:56:01 +01005387 if (STRCMP(how, "term") == 0 || STRCMP(how, "kill") == 0 || *how == NUL)
Bram Moolenaar76467df2016-02-12 19:30:26 +01005388 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005389 // deadly signal
Bram Moolenaar14207f42016-10-27 21:13:10 +02005390 if (job->jv_job_object != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005391 {
5392 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5393 job->jv_channel->ch_killing = TRUE;
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005394 return TerminateJobObject(job->jv_job_object, -1) ? OK : FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005395 }
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01005396 return terminate_all(job->jv_proc_info.hProcess, -1) ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005397 }
5398
5399 if (!AttachConsole(job->jv_proc_info.dwProcessId))
5400 return FAIL;
5401 ret = GenerateConsoleCtrlEvent(
Bram Moolenaar923d9262016-02-25 20:56:01 +01005402 STRCMP(how, "int") == 0 ? CTRL_C_EVENT : CTRL_BREAK_EVENT,
5403 job->jv_proc_info.dwProcessId)
5404 ? OK : FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005405 FreeConsole();
5406 return ret;
5407}
5408
5409/*
5410 * Clear the data related to "job".
5411 */
5412 void
5413mch_clear_job(job_T *job)
5414{
5415 if (job->jv_status != JOB_FAILED)
5416 {
Bram Moolenaar14207f42016-10-27 21:13:10 +02005417 if (job->jv_job_object != NULL)
5418 CloseHandle(job->jv_job_object);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005419 CloseHandle(job->jv_proc_info.hProcess);
5420 }
Bram Moolenaar942d6b22016-02-07 19:57:16 +01005421}
5422#endif
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005425#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427/*
5428 * Start termcap mode
5429 */
5430 static void
5431termcap_mode_start(void)
5432{
5433 DWORD cmodein;
5434
5435 if (g_fTermcapMode)
5436 return;
5437
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005438 if (!p_rs && USE_VTP)
5439 vtp_printf("\033[?1049h");
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 SaveConsoleBuffer(&g_cbNonTermcap);
5442
5443 if (g_cbTermcap.IsValid)
5444 {
5445 /*
5446 * We've been in termcap mode before. Restore certain screen
5447 * characteristics, including the buffer size and the window
5448 * size. Since we will be redrawing the screen, we don't need
5449 * to restore the actual contents of the buffer.
5450 */
5451 RestoreConsoleBuffer(&g_cbTermcap, FALSE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005452 reset_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow);
5454 Rows = g_cbTermcap.Info.dwSize.Y;
5455 Columns = g_cbTermcap.Info.dwSize.X;
5456 }
5457 else
5458 {
5459 /*
5460 * This is our first time entering termcap mode. Clear the console
5461 * screen buffer, and resize the buffer to match the current window
5462 * size. We will use this as the size of our editing environment.
5463 */
5464 ClearConsoleBuffer(g_attrCurrent);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005465 set_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 ResizeConBufAndWindow(g_hConOut, Columns, Rows);
5467 }
5468
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005469# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 resettitle();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 GetConsoleMode(g_hConIn, &cmodein);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (g_fMouseActive)
5475 cmodein |= ENABLE_MOUSE_INPUT;
5476 else
5477 cmodein &= ~ENABLE_MOUSE_INPUT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 cmodein |= ENABLE_WINDOW_INPUT;
5479 SetConsoleMode(g_hConIn, cmodein);
5480
5481 redraw_later_clear();
5482 g_fTermcapMode = TRUE;
5483}
5484
5485
5486/*
5487 * End termcap mode
5488 */
5489 static void
5490termcap_mode_end(void)
5491{
5492 DWORD cmodein;
5493 ConsoleBuffer *cb;
5494 COORD coord;
5495 DWORD dwDummy;
5496
5497 if (!g_fTermcapMode)
5498 return;
5499
5500 SaveConsoleBuffer(&g_cbTermcap);
5501
5502 GetConsoleMode(g_hConIn, &cmodein);
5503 cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
5504 SetConsoleMode(g_hConIn, cmodein);
5505
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005506# ifdef FEAT_RESTORE_ORIG_SCREEN
Bram Moolenaar4c0aac52015-10-30 16:46:55 +01005507 cb = exiting ? &g_cbOrig : &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005508# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 cb = &g_cbNonTermcap;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005510# endif
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005511 RestoreConsoleBuffer(cb, p_rs);
Bram Moolenaardf543822020-01-30 11:53:59 +01005512 restore_console_color_rgb();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 SetConsoleCursorInfo(g_hConOut, &g_cci);
5514
5515 if (p_rs || exiting)
5516 {
5517 /*
5518 * Clear anything that happens to be on the current line.
5519 */
5520 coord.X = 0;
5521 coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1));
5522 FillConsoleOutputCharacter(g_hConOut, ' ',
5523 cb->Info.dwSize.X, coord, &dwDummy);
5524 /*
5525 * The following is just for aesthetics. If we are exiting without
5526 * restoring the screen, then we want to have a prompt string
5527 * appear at the bottom line. However, the command interpreter
5528 * seems to always advance the cursor one line before displaying
5529 * the prompt string, which causes the screen to scroll. To
5530 * counter this, move the cursor up one line before exiting.
5531 */
5532 if (exiting && !p_rs)
5533 coord.Y--;
5534 /*
5535 * Position the cursor at the leftmost column of the desired row.
5536 */
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005537 SetConsoleCursorPosition(g_hConOut, coord);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539
Bram Moolenaar81ccbf12020-04-15 21:05:30 +02005540 if (!p_rs && USE_VTP)
Bram Moolenaar0afdcf82020-04-01 18:29:10 +02005541 vtp_printf("\033[?1049l");
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 g_fTermcapMode = FALSE;
5544}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005545#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005548#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 void
5550mch_write(
Bram Moolenaar1266d672017-02-01 13:43:36 +01005551 char_u *s UNUSED,
5552 int len UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005554 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555}
5556
5557#else
5558
5559/*
5560 * clear `n' chars, starting from `coord'
5561 */
5562 static void
5563clear_chars(
5564 COORD coord,
5565 DWORD n)
5566{
5567 DWORD dwDummy;
5568
5569 FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005570
5571 if (!USE_VTP)
5572 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
5573 else
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005574 {
5575 set_console_color_rgb();
5576 gotoxy(coord.X + 1, coord.Y + 1);
5577 vtp_printf("\033[%dX", n);
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581
5582/*
5583 * Clear the screen
5584 */
5585 static void
5586clear_screen(void)
5587{
5588 g_coord.X = g_coord.Y = 0;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005589
5590 if (!USE_VTP)
5591 clear_chars(g_coord, Rows * Columns);
5592 else
5593 {
5594 set_console_color_rgb();
5595 gotoxy(1, 1);
5596 vtp_printf("\033[2J");
5597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
5600
5601/*
5602 * Clear to end of display
5603 */
5604 static void
5605clear_to_end_of_display(void)
5606{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005607 COORD save = g_coord;
5608
5609 if (!USE_VTP)
5610 clear_chars(g_coord, (Rows - g_coord.Y - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 * Columns + (Columns - g_coord.X));
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005612 else
5613 {
5614 set_console_color_rgb();
5615 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5616 vtp_printf("\033[0J");
5617
5618 gotoxy(save.X + 1, save.Y + 1);
5619 g_coord = save;
5620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623
5624/*
5625 * Clear to end of line
5626 */
5627 static void
5628clear_to_end_of_line(void)
5629{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005630 COORD save = g_coord;
5631
5632 if (!USE_VTP)
5633 clear_chars(g_coord, Columns - g_coord.X);
5634 else
5635 {
5636 set_console_color_rgb();
5637 gotoxy(g_coord.X + 1, g_coord.Y + 1);
5638 vtp_printf("\033[0K");
5639
5640 gotoxy(save.X + 1, save.Y + 1);
5641 g_coord = save;
5642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645
5646/*
5647 * Scroll the scroll region up by `cLines' lines
5648 */
5649 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005650scroll(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 COORD oldcoord = g_coord;
5653
5654 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5655 delete_lines(cLines);
5656
5657 g_coord = oldcoord;
5658}
5659
5660
5661/*
5662 * Set the scroll region
5663 */
5664 static void
5665set_scroll_region(
5666 unsigned left,
5667 unsigned top,
5668 unsigned right,
5669 unsigned bottom)
5670{
5671 if (left >= right
5672 || top >= bottom
5673 || right > (unsigned) Columns - 1
5674 || bottom > (unsigned) Rows - 1)
5675 return;
5676
5677 g_srScrollRegion.Left = left;
5678 g_srScrollRegion.Top = top;
5679 g_srScrollRegion.Right = right;
5680 g_srScrollRegion.Bottom = bottom;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005681}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005682
Bram Moolenaar6982f422019-02-16 16:48:01 +01005683 static void
5684set_scroll_region_tb(
5685 unsigned top,
5686 unsigned bottom)
5687{
5688 if (top >= bottom || bottom > (unsigned)Rows - 1)
5689 return;
5690
5691 g_srScrollRegion.Top = top;
5692 g_srScrollRegion.Bottom = bottom;
5693}
5694
5695 static void
5696set_scroll_region_lr(
5697 unsigned left,
5698 unsigned right)
5699{
5700 if (left >= right || right > (unsigned)Columns - 1)
5701 return;
5702
5703 g_srScrollRegion.Left = left;
5704 g_srScrollRegion.Right = right;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705}
5706
5707
5708/*
5709 * Insert `cLines' lines at the current cursor position
5710 */
5711 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005712insert_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005714 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 COORD dest;
5716 CHAR_INFO fill;
5717
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005718 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5719
Bram Moolenaar6982f422019-02-16 16:48:01 +01005720 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 dest.Y = g_coord.Y + cLines;
5722
Bram Moolenaar6982f422019-02-16 16:48:01 +01005723 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 source.Top = g_coord.Y;
5725 source.Right = g_srScrollRegion.Right;
5726 source.Bottom = g_srScrollRegion.Bottom - cLines;
5727
Bram Moolenaar6982f422019-02-16 16:48:01 +01005728 clip.Left = g_srScrollRegion.Left;
5729 clip.Top = g_coord.Y;
5730 clip.Right = g_srScrollRegion.Right;
5731 clip.Bottom = g_srScrollRegion.Bottom;
5732
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005733 fill.Char.AsciiChar = ' ';
5734 if (!USE_VTP)
5735 fill.Attributes = g_attrCurrent;
5736 else
5737 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005739 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005740
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005741 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5742
Bram Moolenaar6982f422019-02-16 16:48:01 +01005743 // Here we have to deal with a win32 console flake: If the scroll
5744 // region looks like abc and we scroll c to a and fill with d we get
5745 // cbd... if we scroll block c one line at a time to a, we get cdd...
5746 // vim expects cdd consistently... So we have to deal with that
5747 // here... (this also occurs scrolling the same way in the other
5748 // direction).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 if (source.Bottom < dest.Y)
5751 {
5752 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar6982f422019-02-16 16:48:01 +01005755 coord.X = source.Left;
5756 for (i = clip.Top; i < dest.Y; ++i)
5757 {
5758 coord.Y = i;
5759 clear_chars(coord, source.Right - source.Left + 1);
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762}
5763
5764
5765/*
5766 * Delete `cLines' lines at the current cursor position
5767 */
5768 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005769delete_lines(unsigned cLines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaar6982f422019-02-16 16:48:01 +01005771 SMALL_RECT source, clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 COORD dest;
5773 CHAR_INFO fill;
5774 int nb;
5775
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005776 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1);
5777
Bram Moolenaar6982f422019-02-16 16:48:01 +01005778 dest.X = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 dest.Y = g_coord.Y;
5780
Bram Moolenaar6982f422019-02-16 16:48:01 +01005781 source.Left = g_srScrollRegion.Left;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 source.Top = g_coord.Y + cLines;
5783 source.Right = g_srScrollRegion.Right;
5784 source.Bottom = g_srScrollRegion.Bottom;
5785
Bram Moolenaar6982f422019-02-16 16:48:01 +01005786 clip.Left = g_srScrollRegion.Left;
5787 clip.Top = g_coord.Y;
5788 clip.Right = g_srScrollRegion.Right;
5789 clip.Bottom = g_srScrollRegion.Bottom;
5790
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005791 fill.Char.AsciiChar = ' ';
5792 if (!USE_VTP)
5793 fill.Attributes = g_attrCurrent;
5794 else
5795 fill.Attributes = g_attrDefault;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005797 set_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005798
Bram Moolenaar3b5fef62019-03-17 14:54:53 +01005799 ScrollConsoleScreenBuffer(g_hConOut, &source, &clip, dest, &fill);
5800
Bram Moolenaar6982f422019-02-16 16:48:01 +01005801 // Here we have to deal with a win32 console flake; See insert_lines()
5802 // above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804 nb = dest.Y + (source.Bottom - source.Top) + 1;
5805
5806 if (nb < source.Top)
5807 {
5808 COORD coord;
Bram Moolenaar6982f422019-02-16 16:48:01 +01005809 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
Bram Moolenaar6982f422019-02-16 16:48:01 +01005811 coord.X = source.Left;
5812 for (i = nb; i < clip.Bottom; ++i)
5813 {
5814 coord.Y = i;
5815 clear_chars(coord, source.Right - source.Left + 1);
5816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818}
5819
5820
5821/*
Bram Moolenaar2313b612019-09-27 14:14:32 +02005822 * Set the cursor position to (x,y) (1-based).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 static void
5825gotoxy(
5826 unsigned x,
5827 unsigned y)
5828{
5829 if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows)
5830 return;
5831
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005832 if (!USE_VTP)
Bram Moolenaar2313b612019-09-27 14:14:32 +02005833 {
Bram Moolenaar82e743c2020-03-26 15:39:53 +01005834 // There are reports of double-width characters not displayed
5835 // correctly. This workaround should fix it, similar to how it's done
5836 // for VTP.
5837 g_coord.X = 0;
5838 SetConsoleCursorPosition(g_hConOut, g_coord);
5839
Bram Moolenaar2313b612019-09-27 14:14:32 +02005840 // external cursor coords are 1-based; internal are 0-based
5841 g_coord.X = x - 1;
5842 g_coord.Y = y - 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005843 SetConsoleCursorPosition(g_hConOut, g_coord);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005844 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005845 else
Bram Moolenaar2313b612019-09-27 14:14:32 +02005846 {
5847 // Move the cursor to the left edge of the screen to prevent screen
Bram Moolenaara1299742019-10-10 16:36:00 +02005848 // destruction. Insider build bug. Always enabled because it's cheap
5849 // and avoids mistakes with recognizing the build.
5850 vtp_printf("\033[%d;%dH", g_coord.Y + 1, 1);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005851
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005852 vtp_printf("\033[%d;%dH", y, x);
Bram Moolenaar2313b612019-09-27 14:14:32 +02005853
5854 g_coord.X = x - 1;
5855 g_coord.Y = y - 1;
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857}
5858
5859
5860/*
5861 * Set the current text attribute = (foreground | background)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005862 * See ../runtime/doc/os_win32.txt for the numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
5864 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005865textattr(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005867 g_attrCurrent = wAttr & 0xff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 SetConsoleTextAttribute(g_hConOut, wAttr);
5870}
5871
5872
5873 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005874textcolor(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005876 g_attrCurrent = (g_attrCurrent & 0xf0) + (wAttr & 0x0f);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005878 if (!USE_VTP)
5879 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5880 else
5881 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882}
5883
5884
5885 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005886textbackground(WORD wAttr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
Bram Moolenaar6383b922015-03-24 17:12:19 +01005888 g_attrCurrent = (g_attrCurrent & 0x0f) + ((wAttr & 0x0f) << 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005890 if (!USE_VTP)
5891 SetConsoleTextAttribute(g_hConOut, g_attrCurrent);
5892 else
5893 vtp_sgr_bulk(wAttr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
5896
5897/*
5898 * restore the default text attribute (whatever we started with)
5899 */
5900 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005901normvideo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005903 if (!USE_VTP)
5904 textattr(g_attrDefault);
5905 else
5906 vtp_sgr_bulk(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907}
5908
5909
5910static WORD g_attrPreStandout = 0;
5911
5912/*
5913 * Make the text standout, by brightening it
5914 */
5915 static void
5916standout(void)
5917{
5918 g_attrPreStandout = g_attrCurrent;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY));
5921}
5922
5923
5924/*
5925 * Turn off standout mode
5926 */
5927 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005928standend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 if (g_attrPreStandout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 textattr(g_attrPreStandout);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005932
5933 g_attrPreStandout = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934}
5935
5936
5937/*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00005938 * Set normal fg/bg color, based on T_ME. Called when t_me has been set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 */
5940 void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005941mch_set_normal_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 char_u *p;
5944 int n;
5945
5946 cterm_normal_fg_color = (g_attrDefault & 0xf) + 1;
5947 cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005948 if (
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005949# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005950 !p_tgc &&
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005951# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005952 T_ME[0] == ESC && T_ME[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 p = T_ME + 2;
5955 n = getdigits(&p);
5956 if (*p == 'm' && n > 0)
5957 {
5958 cterm_normal_fg_color = (n & 0xf) + 1;
5959 cterm_normal_bg_color = ((n >> 4) & 0xf) + 1;
5960 }
5961 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005962# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005963 cterm_normal_fg_gui_color = INVALCOLOR;
5964 cterm_normal_bg_gui_color = INVALCOLOR;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966}
5967
5968
5969/*
5970 * visual bell: flash the screen
5971 */
5972 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005973visual_bell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 COORD coordOrigin = {0, 0};
5976 WORD attrFlash = ~g_attrCurrent & 0xff;
5977
5978 DWORD dwDummy;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005979 LPWORD oldattrs = ALLOC_MULT(WORD, Rows * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 if (oldattrs == NULL)
5982 return;
5983 ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
5984 coordOrigin, &dwDummy);
5985 FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns,
5986 coordOrigin, &dwDummy);
5987
Bram Moolenaar0f873732019-12-05 20:28:46 +01005988 Sleep(15); // wait for 15 msec
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005989 if (!USE_VTP)
5990 WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 coordOrigin, &dwDummy);
5992 vim_free(oldattrs);
5993}
5994
5995
5996/*
5997 * Make the cursor visible or invisible
5998 */
5999 static void
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006000cursor_visible(BOOL fVisible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 s_cursor_visible = fVisible;
Bram Moolenaar2695de62020-04-17 21:13:01 +02006003
6004 if (USE_VTP)
6005 vtp_printf("\033[?25%c", fVisible ? 'h' : 'l');
6006
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006007# ifdef MCH_CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 mch_update_cursor();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010}
6011
6012
6013/*
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006014 * Write "cbToWrite" bytes in `pchBuf' to the screen.
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006015 * Returns the number of bytes actually written (at least one).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 */
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006017 static DWORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018write_chars(
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006019 char_u *pchBuf,
6020 DWORD cbToWrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006022 COORD coord = g_coord;
6023 DWORD written;
6024 DWORD n, cchwritten, cells;
6025 static WCHAR *unicodebuf = NULL;
6026 static int unibuflen = 0;
6027 int length;
6028 int cp = enc_utf8 ? CP_UTF8 : enc_codepage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006030 length = MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite, 0, 0);
6031 if (unicodebuf == NULL || length > unibuflen)
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006032 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006033 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006034 unicodebuf = LALLOC_MULT(WCHAR, length);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006035 unibuflen = length;
6036 }
6037 MultiByteToWideChar(cp, 0, (LPCSTR)pchBuf, cbToWrite,
6038 unicodebuf, unibuflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006040 cells = mb_string2cells(pchBuf, cbToWrite);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006041
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006042 if (!USE_VTP)
6043 {
6044 FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cells,
6045 coord, &written);
6046 // When writing fails or didn't write a single character, pretend one
6047 // character was written, otherwise we get stuck.
6048 if (WriteConsoleOutputCharacterW(g_hConOut, unicodebuf, length,
6049 coord, &cchwritten) == 0
6050 || cchwritten == 0 || cchwritten == (DWORD)-1)
6051 cchwritten = 1;
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006052 }
6053 else
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006054 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006055 if (WriteConsoleW(g_hConOut, unicodebuf, length, &cchwritten,
6056 NULL) == 0 || cchwritten == 0)
6057 cchwritten = 1;
6058 }
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006059
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006060 if (cchwritten == length)
6061 {
6062 written = cbToWrite;
6063 g_coord.X += (SHORT)cells;
6064 }
6065 else
6066 {
6067 char_u *p = pchBuf;
6068 for (n = 0; n < cchwritten; n++)
6069 MB_CPTR_ADV(p);
6070 written = p - pchBuf;
6071 g_coord.X += (SHORT)mb_string2cells(pchBuf, written);
Bram Moolenaarac360bf2015-09-01 20:31:20 +02006072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074 while (g_coord.X > g_srScrollRegion.Right)
6075 {
6076 g_coord.X -= (SHORT) Columns;
6077 if (g_coord.Y < g_srScrollRegion.Bottom)
6078 g_coord.Y++;
6079 }
6080
6081 gotoxy(g_coord.X + 1, g_coord.Y + 1);
6082
6083 return written;
6084}
6085
6086
6087/*
6088 * mch_write(): write the output buffer to the screen, translating ESC
6089 * sequences into calls to console output routines.
6090 */
6091 void
6092mch_write(
6093 char_u *s,
6094 int len)
6095{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006096# ifdef VIMDLL
6097 if (gui.in_use)
6098 return;
6099# endif
6100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 s[len] = NUL;
6102
6103 if (!term_console)
6104 {
6105 write(1, s, (unsigned)len);
6106 return;
6107 }
6108
Bram Moolenaar0f873732019-12-05 20:28:46 +01006109 // translate ESC | sequences into faked bios calls
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 while (len--)
6111 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006112 // optimization: use one single write_chars for runs of text,
6113 // rather than once per character It ain't curses, but it helps.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006114 DWORD prefix = (DWORD)strcspn((char *)s, "\n\r\b\a\033");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116 if (p_wd)
6117 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006118 WaitForChar(p_wd, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (prefix != 0)
6120 prefix = 1;
6121 }
6122
6123 if (prefix != 0)
6124 {
6125 DWORD nWritten;
6126
6127 nWritten = write_chars(s, prefix);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006128# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (fdDump)
6130 {
6131 fputc('>', fdDump);
6132 fwrite(s, sizeof(char_u), nWritten, fdDump);
6133 fputs("<\n", fdDump);
6134 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 len -= (nWritten - 1);
6137 s += nWritten;
6138 }
6139 else if (s[0] == '\n')
6140 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006141 // \n, newline: go to the beginning of the next line or scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (g_coord.Y == g_srScrollRegion.Bottom)
6143 {
6144 scroll(1);
6145 gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1);
6146 }
6147 else
6148 {
6149 gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2);
6150 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006151# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (fdDump)
6153 fputs("\\n\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 s++;
6156 }
6157 else if (s[0] == '\r')
6158 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006159 // \r, carriage return: go to beginning of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006161# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (fdDump)
6163 fputs("\\r\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 s++;
6166 }
6167 else if (s[0] == '\b')
6168 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006169 // \b, backspace: move cursor one position left
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (g_coord.X > g_srScrollRegion.Left)
6171 g_coord.X--;
6172 else if (g_coord.Y > g_srScrollRegion.Top)
6173 {
6174 g_coord.X = g_srScrollRegion.Right;
6175 g_coord.Y--;
6176 }
6177 gotoxy(g_coord.X + 1, g_coord.Y + 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006178# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (fdDump)
6180 fputs("\\b\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 s++;
6183 }
6184 else if (s[0] == '\a')
6185 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006186 // \a, bell
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 MessageBeep(0xFFFFFFFF);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006188# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (fdDump)
6190 fputs("\\a\n", fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 s++;
6193 }
6194 else if (s[0] == ESC && len >= 3-1 && s[1] == '|')
6195 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006196# ifdef MCH_WRITE_DUMP
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006197 char_u *old_s = s;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006198# endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006199 char_u *p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006200 int arg1 = 0, arg2 = 0, argc = 0, args[16];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 switch (s[2])
6203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case '0': case '1': case '2': case '3': case '4':
6205 case '5': case '6': case '7': case '8': case '9':
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006206 p = s + 1;
6207 do
6208 {
6209 ++p;
6210 args[argc] = getdigits(&p);
6211 argc += (argc < 15) ? 1 : 0;
6212 if (p > s + len)
6213 break;
6214 } while (*p == ';');
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (p > s + len)
6217 break;
6218
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006219 arg1 = args[0];
6220 arg2 = args[1];
6221 if (*p == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006223 if (argc == 1 && args[0] == 0)
6224 normvideo();
6225 else if (argc == 1)
6226 {
6227 if (USE_VTP)
6228 textcolor((WORD) arg1);
6229 else
6230 textattr((WORD) arg1);
6231 }
6232 else if (USE_VTP)
6233 vtp_sgr_bulks(argc, args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006235 else if (argc == 2 && *p == 'H')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006237 gotoxy(arg2, arg1);
6238 }
6239 else if (argc == 2 && *p == 'r')
6240 {
6241 set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1);
6242 }
Bram Moolenaar6982f422019-02-16 16:48:01 +01006243 else if (argc == 2 && *p == 'R')
6244 {
6245 set_scroll_region_tb(arg1, arg2);
6246 }
6247 else if (argc == 2 && *p == 'V')
6248 {
6249 set_scroll_region_lr(arg1, arg2);
6250 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006251 else if (argc == 1 && *p == 'A')
6252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 gotoxy(g_coord.X + 1,
6254 max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1);
6255 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006256 else if (argc == 1 && *p == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258 textbackground((WORD) arg1);
6259 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006260 else if (argc == 1 && *p == 'C')
6261 {
6262 gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1,
6263 g_coord.Y + 1);
6264 }
6265 else if (argc == 1 && *p == 'f')
6266 {
6267 textcolor((WORD) arg1);
6268 }
6269 else if (argc == 1 && *p == 'H')
6270 {
6271 gotoxy(1, arg1);
6272 }
6273 else if (argc == 1 && *p == 'L')
6274 {
6275 insert_lines(arg1);
6276 }
6277 else if (argc == 1 && *p == 'M')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
6279 delete_lines(arg1);
6280 }
6281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006282 len -= (int)(p - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 s = p + 1;
6284 break;
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 case 'A':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 gotoxy(g_coord.X + 1,
6288 max(g_srScrollRegion.Top, g_coord.Y - 1) + 1);
6289 goto got3;
6290
6291 case 'B':
6292 visual_bell();
6293 goto got3;
6294
6295 case 'C':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1,
6297 g_coord.Y + 1);
6298 goto got3;
6299
6300 case 'E':
6301 termcap_mode_end();
6302 goto got3;
6303
6304 case 'F':
6305 standout();
6306 goto got3;
6307
6308 case 'f':
6309 standend();
6310 goto got3;
6311
6312 case 'H':
6313 gotoxy(1, 1);
6314 goto got3;
6315
6316 case 'j':
6317 clear_to_end_of_display();
6318 goto got3;
6319
6320 case 'J':
6321 clear_screen();
6322 goto got3;
6323
6324 case 'K':
6325 clear_to_end_of_line();
6326 goto got3;
6327
6328 case 'L':
6329 insert_lines(1);
6330 goto got3;
6331
6332 case 'M':
6333 delete_lines(1);
6334 goto got3;
6335
6336 case 'S':
6337 termcap_mode_start();
6338 goto got3;
6339
6340 case 'V':
6341 cursor_visible(TRUE);
6342 goto got3;
6343
6344 case 'v':
6345 cursor_visible(FALSE);
6346 goto got3;
6347
6348 got3:
6349 s += 3;
6350 len -= 2;
6351 }
6352
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006353# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (fdDump)
6355 {
6356 fputs("ESC | ", fdDump);
6357 fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump);
6358 fputc('\n', fdDump);
6359 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362 else
6363 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006364 // Write a single character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 DWORD nWritten;
6366
6367 nWritten = write_chars(s, 1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006368# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (fdDump)
6370 {
6371 fputc('>', fdDump);
6372 fwrite(s, sizeof(char_u), nWritten, fdDump);
6373 fputs("<\n", fdDump);
6374 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377 len -= (nWritten - 1);
6378 s += nWritten;
6379 }
6380 }
6381
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006382# ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (fdDump)
6384 fflush(fdDump);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386}
6387
Bram Moolenaar0f873732019-12-05 20:28:46 +01006388#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390
6391/*
Bram Moolenaar0e0b3dd2016-03-17 17:58:56 +01006392 * Delay for "msec" milliseconds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 */
6394 void
6395mch_delay(
6396 long msec,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006397 int ignoreinput UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006399#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006400 Sleep((int)msec); // never wait for input
6401#else // Console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006402# ifdef VIMDLL
6403 if (gui.in_use)
6404 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006405 Sleep((int)msec); // never wait for input
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006406 return;
6407 }
6408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (ignoreinput)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006410# ifdef FEAT_MZSCHEME
6411 if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
6412 {
6413 int towait = p_mzq;
6414
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 // if msec is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006416 while (msec > 0)
6417 {
6418 mzvim_check_threads();
6419 if (msec < towait)
6420 towait = msec;
6421 Sleep(towait);
6422 msec -= towait;
6423 }
6424 }
6425 else
6426# endif
6427 Sleep((int)msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006429 WaitForChar(msec, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#endif
6431}
6432
6433
6434/*
Bram Moolenaar203258c2016-01-17 22:15:16 +01006435 * This version of remove is not scared by a readonly (backup) file.
6436 * This can also remove a symbolic link like Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 * Return 0 for success, -1 for failure.
6438 */
6439 int
6440mch_remove(char_u *name)
6441{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006442 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 int n;
6444
Bram Moolenaar203258c2016-01-17 22:15:16 +01006445 /*
6446 * On Windows, deleting a directory's symbolic link is done by
6447 * RemoveDirectory(): mch_rmdir. It seems unnatural, but it is fact.
6448 */
6449 if (mch_isdir(name) && mch_is_symbolic_link(name))
6450 return mch_rmdir(name);
6451
Bram Moolenaar12b559e2013-06-12 22:41:37 +02006452 win32_setattrs(name, FILE_ATTRIBUTE_NORMAL);
6453
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006454 wn = enc_to_utf16(name, NULL);
6455 if (wn == NULL)
6456 return -1;
6457
6458 n = DeleteFileW(wn) ? 0 : -1;
6459 vim_free(wn);
6460 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
6463
6464/*
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006465 * Check for an "interrupt signal": CTRL-break or CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 */
6467 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006468mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006470#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
6471# ifdef VIMDLL
6472 if (!gui.in_use)
6473# endif
6474 if (g_fCtrlCPressed || g_fCBrkPressed)
6475 {
6476 ctrl_break_was_pressed = g_fCBrkPressed;
6477 g_fCtrlCPressed = g_fCBrkPressed = FALSE;
6478 got_int = TRUE;
6479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#endif
6481}
6482
Bram Moolenaar0f873732019-12-05 20:28:46 +01006483// physical RAM to leave for the OS
Bram Moolenaaree273972016-01-02 21:11:51 +01006484#define WINNT_RESERVE_BYTES (256*1024*1024)
Bram Moolenaaree273972016-01-02 21:11:51 +01006485
6486/*
6487 * How much main memory in KiB that can be used by VIM.
6488 */
Bram Moolenaaree273972016-01-02 21:11:51 +01006489 long_u
Bram Moolenaar1266d672017-02-01 13:43:36 +01006490mch_total_mem(int special UNUSED)
Bram Moolenaaree273972016-01-02 21:11:51 +01006491{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006492 MEMORYSTATUSEX ms;
6493
Bram Moolenaar0f873732019-12-05 20:28:46 +01006494 // Need to use GlobalMemoryStatusEx() when there is more memory than
6495 // what fits in 32 bits.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006496 ms.dwLength = sizeof(MEMORYSTATUSEX);
6497 GlobalMemoryStatusEx(&ms);
6498 if (ms.ullAvailVirtual < ms.ullTotalPhys)
Bram Moolenaaree273972016-01-02 21:11:51 +01006499 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006500 // Process address space fits in physical RAM, use all of it.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006501 return (long_u)(ms.ullAvailVirtual / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006502 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006503 if (ms.ullTotalPhys <= WINNT_RESERVE_BYTES)
Bram Moolenaaree273972016-01-02 21:11:51 +01006504 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006505 // Catch old NT box or perverse hardware setup.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006506 return (long_u)((ms.ullTotalPhys / 2) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006507 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006508 // Use physical RAM less reserve for OS + data.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006509 return (long_u)((ms.ullTotalPhys - WINNT_RESERVE_BYTES) / 1024);
Bram Moolenaaree273972016-01-02 21:11:51 +01006510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006513 * mch_wrename() works around a bug in rename (aka MoveFile) in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 * Windows 95: rename("foo.bar", "foo.bar~") will generate a
6515 * file whose short file name is "FOO.BAR" (its long file name will
6516 * be correct: "foo.bar~"). Because a file can be accessed by
6517 * either its SFN or its LFN, "foo.bar" has effectively been
6518 * renamed to "foo.bar", which is not at all what was wanted. This
6519 * seems to happen only when renaming files with three-character
6520 * extensions by appending a suffix that does not include ".".
6521 * Windows NT gets it right, however, with an SFN of "FOO~1.BAR".
6522 *
6523 * There is another problem, which isn't really a bug but isn't right either:
6524 * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be
6525 * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with
6526 * service pack 6. Doesn't seem to happen on Windows 98.
6527 *
6528 * Like rename(), returns 0 upon success, non-zero upon failure.
6529 * Should probably set errno appropriately when errors occur.
6530 */
6531 int
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006532mch_wrename(WCHAR *wold, WCHAR *wnew)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 WCHAR *p;
6535 int i;
6536 WCHAR szTempFile[_MAX_PATH + 1];
6537 WCHAR szNewPath[_MAX_PATH + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 HANDLE hf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 // No need to play tricks unless the file name contains a "~" as the
6541 // seventh character.
6542 p = wold;
6543 for (i = 0; wold[i] != NUL; ++i)
6544 if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':')
6545 && wold[i + 1] != 0)
6546 p = wold + i + 1;
6547 if ((int)(wold + i - p) < 8 || p[6] != '~')
6548 return (MoveFileW(wold, wnew) == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006550 // Get base path of new file name. Undocumented feature: If pszNewFile is
6551 // a directory, no error is returned and pszFilePart will be NULL.
6552 if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 return -1;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006554 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006556 // Get (and create) a unique temporary file name in directory of new file
6557 if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 return -2;
6559
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006560 // blow the temp file away
6561 if (!DeleteFileW(szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 return -3;
6563
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006564 // rename old file to the temp file
6565 if (!MoveFileW(wold, szTempFile))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return -4;
6567
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006568 // now create an empty file called pszOldFile; this prevents the operating
6569 // system using pszOldFile as an alias (SFN) if we're renaming within the
6570 // same directory. For example, we're editing a file called
6571 // filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt
6572 // to filena~1.txt~ (i.e., we're making a backup while writing it), the
6573 // SFN for filena~1.txt~ will be filena~1.txt, by default, which will
6574 // cause all sorts of problems later in buf_write(). So, we create an
6575 // empty file called filena~1.txt and the system will have to find some
6576 // other SFN for filena~1.txt~, such as filena~2.txt
6577 if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
6579 return -5;
6580 if (!CloseHandle(hf))
6581 return -6;
6582
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006583 // rename the temp file to the new file
6584 if (!MoveFileW(szTempFile, wnew))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006586 // Renaming failed. Rename the file back to its old name, so that it
6587 // looks like nothing happened.
6588 (void)MoveFileW(szTempFile, wold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 return -7;
6590 }
6591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006592 // Seems to be left around on Novell filesystems
6593 DeleteFileW(szTempFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006595 // finally, remove the empty old file
6596 if (!DeleteFileW(wold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 return -8;
6598
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006599 return 0;
6600}
6601
6602
6603/*
6604 * Converts the filenames to UTF-16, then call mch_wrename().
6605 * Like rename(), returns 0 upon success, non-zero upon failure.
6606 */
6607 int
6608mch_rename(
6609 const char *pszOldFile,
6610 const char *pszNewFile)
6611{
6612 WCHAR *wold = NULL;
6613 WCHAR *wnew = NULL;
6614 int retval = -1;
6615
6616 wold = enc_to_utf16((char_u *)pszOldFile, NULL);
6617 wnew = enc_to_utf16((char_u *)pszNewFile, NULL);
6618 if (wold != NULL && wnew != NULL)
6619 retval = mch_wrename(wold, wnew);
6620 vim_free(wold);
6621 vim_free(wnew);
6622 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625/*
6626 * Get the default shell for the current hardware platform
6627 */
6628 char *
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006629default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006631 return "cmd.exe";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633
6634/*
6635 * mch_access() extends access() to do more detailed check on network drives.
6636 * Returns 0 if file "n" has access rights according to "p", -1 otherwise.
6637 */
6638 int
6639mch_access(char *n, int p)
6640{
6641 HANDLE hFile;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006642 int retval = -1; // default: fail
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006643 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006645 wn = enc_to_utf16((char_u *)n, NULL);
6646 if (wn == NULL)
6647 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006649 if (mch_isdir((char_u *)n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 WCHAR TempNameW[_MAX_PATH + 16] = L"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (p & R_OK)
6654 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006655 // Read check is performed by seeing if we can do a find file on
6656 // the directory for any file.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006657 int i;
6658 WIN32_FIND_DATAW d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006660 for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i)
6661 TempNameW[i] = wn[i];
6662 if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/')
6663 TempNameW[i++] = '\\';
6664 TempNameW[i++] = '*';
6665 TempNameW[i++] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006667 hFile = FindFirstFileW(TempNameW, &d);
6668 if (hFile == INVALID_HANDLE_VALUE)
6669 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 (void)FindClose(hFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673
6674 if (p & W_OK)
6675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006676 // Trying to create a temporary file in the directory should catch
6677 // directories on read-only network shares. However, in
6678 // directories whose ACL allows writes but denies deletes will end
6679 // up keeping the temporary file :-(.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006680 if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW))
6681 goto getout;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006682 else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006683 DeleteFileW(TempNameW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 }
6685 }
6686 else
6687 {
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006688 // Don't consider a file read-only if another process has opened it.
6689 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
6690
Bram Moolenaar0f873732019-12-05 20:28:46 +01006691 // Trying to open the file for the required access does ACL, read-only
6692 // network share, and file attribute checks.
Bram Moolenaar5aa98962018-05-06 17:09:38 +02006693 DWORD access_mode = ((p & W_OK) ? GENERIC_WRITE : 0)
6694 | ((p & R_OK) ? GENERIC_READ : 0);
6695
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006696 hFile = CreateFileW(wn, access_mode, share_mode,
6697 NULL, OPEN_EXISTING, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (hFile == INVALID_HANDLE_VALUE)
6699 goto getout;
6700 CloseHandle(hFile);
6701 }
6702
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 retval = 0; // success
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704getout:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 return retval;
6707}
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006710 * Version of open() that may use UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 */
6712 int
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006713mch_open(const char *name, int flags, int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 WCHAR *wn;
6716 int f;
6717
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006718 wn = enc_to_utf16((char_u *)name, NULL);
6719 if (wn == NULL)
6720 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006722 f = _wopen(wn, flags, mode);
6723 vim_free(wn);
6724 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725}
6726
6727/*
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006728 * Version of fopen() that uses UTF-16 file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 */
6730 FILE *
Bram Moolenaarb6843a02017-08-02 22:07:12 +02006731mch_fopen(const char *name, const char *mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 WCHAR *wn, *wm;
6734 FILE *f = NULL;
6735
Bram Moolenaara12a1612019-01-24 16:39:02 +01006736#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0f873732019-12-05 20:28:46 +01006737 // Work around an annoying assertion in the Microsoft debug CRT
6738 // when mode's text/binary setting doesn't match _get_fmode().
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006739 char newMode = mode[strlen(mode) - 1];
6740 int oldMode = 0;
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006741
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006742 _get_fmode(&oldMode);
6743 if (newMode == 't')
6744 _set_fmode(_O_TEXT);
6745 else if (newMode == 'b')
6746 _set_fmode(_O_BINARY);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006747#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006748 wn = enc_to_utf16((char_u *)name, NULL);
6749 wm = enc_to_utf16((char_u *)mode, NULL);
6750 if (wn != NULL && wm != NULL)
6751 f = _wfopen(wn, wm);
6752 vim_free(wn);
6753 vim_free(wm);
Bram Moolenaar0fde2902008-03-16 13:54:13 +00006754
Bram Moolenaara12a1612019-01-24 16:39:02 +01006755#if defined(DEBUG) && _MSC_VER >= 1400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006756 _set_fmode(oldMode);
Bram Moolenaara12a1612019-01-24 16:39:02 +01006757#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006758 return f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761/*
6762 * SUB STREAM (aka info stream) handling:
6763 *
6764 * NTFS can have sub streams for each file. Normal contents of file is
6765 * stored in the main stream, and extra contents (author information and
6766 * title and so on) can be stored in sub stream. After Windows 2000, user
6767 * can access and store those informations in sub streams via explorer's
6768 * property menuitem in right click menu. Those informations in sub streams
6769 * were lost when copying only the main stream. So we have to copy sub
6770 * streams.
6771 *
6772 * Incomplete explanation:
6773 * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp
6774 * More useful info and an example:
6775 * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams
6776 */
6777
6778/*
6779 * Copy info stream data "substream". Read from the file with BackupRead(sh)
6780 * and write to stream "substream" of file "to".
6781 * Errors are ignored.
6782 */
6783 static void
6784copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len)
6785{
6786 HANDLE hTo;
6787 WCHAR *to_name;
6788
6789 to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR));
6790 wcscpy(to_name, to);
6791 wcscat(to_name, substream);
6792
6793 hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
6794 FILE_ATTRIBUTE_NORMAL, NULL);
6795 if (hTo != INVALID_HANDLE_VALUE)
6796 {
6797 long done;
6798 DWORD todo;
6799 DWORD readcnt, written;
6800 char buf[4096];
6801
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 // Copy block of bytes at a time. Abort when something goes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 for (done = 0; done < len; done += written)
6804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 // (size_t) cast for Borland C 5.5
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006806 todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf)
6807 : (size_t)(len - done));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt,
6809 FALSE, FALSE, context)
6810 || readcnt != todo
6811 || !WriteFile(hTo, buf, todo, &written, NULL)
6812 || written != todo)
6813 break;
6814 }
6815 CloseHandle(hTo);
6816 }
6817
6818 free(to_name);
6819}
6820
6821/*
6822 * Copy info streams from file "from" to file "to".
6823 */
6824 static void
6825copy_infostreams(char_u *from, char_u *to)
6826{
6827 WCHAR *fromw;
6828 WCHAR *tow;
6829 HANDLE sh;
6830 WIN32_STREAM_ID sid;
6831 int headersize;
6832 WCHAR streamname[_MAX_PATH];
6833 DWORD readcount;
6834 void *context = NULL;
6835 DWORD lo, hi;
6836 int len;
6837
Bram Moolenaar0f873732019-12-05 20:28:46 +01006838 // Convert the file names to wide characters.
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006839 fromw = enc_to_utf16(from, NULL);
6840 tow = enc_to_utf16(to, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (fromw != NULL && tow != NULL)
6842 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 // Open the file for reading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL,
6845 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
6846 if (sh != INVALID_HANDLE_VALUE)
6847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006848 // Use BackupRead() to find the info streams. Repeat until we
6849 // have done them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 for (;;)
6851 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006852 // Get the header to find the length of the stream name. If
6853 // the "readcount" is zero we have done all info streams.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 ZeroMemory(&sid, sizeof(WIN32_STREAM_ID));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006855 headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (!BackupRead(sh, (LPBYTE)&sid, headersize,
6857 &readcount, FALSE, FALSE, &context)
6858 || readcount == 0)
6859 break;
6860
Bram Moolenaar0f873732019-12-05 20:28:46 +01006861 // We only deal with streams that have a name. The normal
6862 // file data appears to be without a name, even though docs
6863 // suggest it is called "::$DATA".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (sid.dwStreamNameSize > 0)
6865 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006866 // Read the stream name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (!BackupRead(sh, (LPBYTE)streamname,
6868 sid.dwStreamNameSize,
6869 &readcount, FALSE, FALSE, &context))
6870 break;
6871
Bram Moolenaar0f873732019-12-05 20:28:46 +01006872 // Copy an info stream with a name ":anything:$DATA".
6873 // Skip "::$DATA", it has no stream name (examples suggest
6874 // it might be used for the normal file contents).
6875 // Note that BackupRead() counts bytes, but the name is in
6876 // wide characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 len = readcount / sizeof(WCHAR);
6878 streamname[len] = 0;
6879 if (len > 7 && wcsicmp(streamname + len - 6,
6880 L":$DATA") == 0)
6881 {
6882 streamname[len - 6] = 0;
6883 copy_substream(sh, &context, tow, streamname,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006884 (long)sid.Size.u.LowPart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 }
6887
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 // Advance to the next stream. We might try seeking too far,
6889 // but BackupSeek() doesn't skip over stream borders, thus
6890 // that's OK.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006891 (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 &lo, &hi, &context);
6893 }
6894
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 // Clear the context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context);
6897
6898 CloseHandle(sh);
6899 }
6900 }
6901 vim_free(fromw);
6902 vim_free(tow);
6903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905/*
6906 * Copy file attributes from file "from" to file "to".
6907 * For Windows NT and later we copy info streams.
6908 * Always returns zero, errors are ignored.
6909 */
6910 int
6911mch_copy_file_attribute(char_u *from, char_u *to)
6912{
Bram Moolenaar0f873732019-12-05 20:28:46 +01006913 // File streams only work on Windows NT and later.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006914 copy_infostreams(from, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 return 0;
6916}
6917
6918#if defined(MYRESETSTKOFLW) || defined(PROTO)
6919/*
6920 * Recreate a destroyed stack guard page in win32.
6921 * Written by Benjamin Peterson.
6922 */
6923
Bram Moolenaar0f873732019-12-05 20:28:46 +01006924// These magic numbers are from the MS header files
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006925# define MIN_STACK_WINNT 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927/*
6928 * This function does the same thing as _resetstkoflw(), which is only
6929 * available in DevStudio .net and later.
6930 * Returns 0 for failure, 1 for success.
6931 */
6932 int
6933myresetstkoflw(void)
6934{
6935 BYTE *pStackPtr;
6936 BYTE *pGuardPage;
6937 BYTE *pStackBase;
6938 BYTE *pLowestPossiblePage;
6939 MEMORY_BASIC_INFORMATION mbi;
6940 SYSTEM_INFO si;
6941 DWORD nPageSize;
6942 DWORD dummy;
6943
Bram Moolenaar0f873732019-12-05 20:28:46 +01006944 // We need to know the system page size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 GetSystemInfo(&si);
6946 nPageSize = si.dwPageSize;
6947
Bram Moolenaar0f873732019-12-05 20:28:46 +01006948 // ...and the current stack pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 pStackPtr = (BYTE*)_alloca(1);
6950
Bram Moolenaar0f873732019-12-05 20:28:46 +01006951 // ...and the base of the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0)
6953 return 0;
6954 pStackBase = (BYTE*)mbi.AllocationBase;
6955
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006956 // ...and the page that's min_stack_req pages away from stack base; this is
Bram Moolenaar0f873732019-12-05 20:28:46 +01006957 // the lowest page we could use.
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006958 pLowestPossiblePage = pStackBase + MIN_STACK_WINNT * nPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006961 // We want the first committed page in the stack Start at the stack
6962 // base and move forward through memory until we find a committed block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 BYTE *pBlock = pStackBase;
6964
Bram Moolenaara466c992005-07-09 21:03:22 +00006965 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
6967 if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0)
6968 return 0;
6969
6970 pBlock += mbi.RegionSize;
6971
6972 if (mbi.State & MEM_COMMIT)
6973 break;
6974 }
6975
Bram Moolenaar0f873732019-12-05 20:28:46 +01006976 // mbi now describes the first committed block in the stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (mbi.Protect & PAGE_GUARD)
6978 return 1;
6979
Bram Moolenaar0f873732019-12-05 20:28:46 +01006980 // decide where the guard page should start
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage)
6982 pGuardPage = pLowestPossiblePage;
6983 else
6984 pGuardPage = (BYTE*)mbi.BaseAddress;
6985
Bram Moolenaar0f873732019-12-05 20:28:46 +01006986 // allocate the guard page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE))
6988 return 0;
6989
Bram Moolenaar0f873732019-12-05 20:28:46 +01006990 // apply the guard attribute to the page
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD,
6992 &dummy))
6993 return 0;
6994 }
6995
6996 return 1;
6997}
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007000
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007001/*
7002 * The command line arguments in UCS2
7003 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007004static int nArgsW = 0;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007005static LPWSTR *ArglistW = NULL;
7006static int global_argc = 0;
7007static char **global_argv;
7008
Bram Moolenaar0f873732019-12-05 20:28:46 +01007009static int used_file_argc = 0; // last argument in global_argv[] used
7010 // for the argument list.
7011static int *used_file_indexes = NULL; // indexes in global_argv[] for
7012 // command line arguments added to
7013 // the argument list
7014static int used_file_count = 0; // nr of entries in used_file_indexes
7015static int used_file_literal = FALSE; // take file names literally
7016static int used_file_full_path = FALSE; // file name was full path
7017static int used_file_diff_mode = FALSE; // file name was with diff mode
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007018static int used_alist_count = 0;
7019
7020
7021/*
7022 * Get the command line arguments. Unicode version.
7023 * Returns argc. Zero when something fails.
7024 */
7025 int
7026get_cmd_argsW(char ***argvp)
7027{
7028 char **argv = NULL;
7029 int argc = 0;
7030 int i;
7031
Bram Moolenaar14993322014-09-09 12:25:33 +02007032 free_cmd_argsW();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007033 ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW);
7034 if (ArglistW != NULL)
7035 {
7036 argv = malloc((nArgsW + 1) * sizeof(char *));
7037 if (argv != NULL)
7038 {
7039 argc = nArgsW;
7040 argv[argc] = NULL;
7041 for (i = 0; i < argc; ++i)
7042 {
7043 int len;
7044
Bram Moolenaar0f873732019-12-05 20:28:46 +01007045 // Convert each Unicode argument to the current codepage.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007046 WideCharToMultiByte_alloc(GetACP(), 0,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 ArglistW[i], (int)wcslen(ArglistW[i]) + 1,
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007048 (LPSTR *)&argv[i], &len, 0, 0);
7049 if (argv[i] == NULL)
7050 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 // Out of memory, clear everything.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007052 while (i > 0)
7053 free(argv[--i]);
7054 free(argv);
Bram Moolenaar73c61632013-12-07 14:48:10 +01007055 argv = NULL;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007056 argc = 0;
7057 }
7058 }
7059 }
7060 }
7061
7062 global_argc = argc;
7063 global_argv = argv;
7064 if (argc > 0)
Bram Moolenaar14993322014-09-09 12:25:33 +02007065 {
7066 if (used_file_indexes != NULL)
7067 free(used_file_indexes);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007068 used_file_indexes = malloc(argc * sizeof(int));
Bram Moolenaar14993322014-09-09 12:25:33 +02007069 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007070
7071 if (argvp != NULL)
7072 *argvp = argv;
7073 return argc;
7074}
7075
7076 void
7077free_cmd_argsW(void)
7078{
7079 if (ArglistW != NULL)
7080 {
7081 GlobalFree(ArglistW);
7082 ArglistW = NULL;
7083 }
7084}
7085
7086/*
7087 * Remember "name" is an argument that was added to the argument list.
7088 * This avoids that we have to re-parse the argument list when fix_arg_enc()
7089 * is called.
7090 */
7091 void
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007092used_file_arg(char *name, int literal, int full_path, int diff_mode)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007093{
7094 int i;
7095
7096 if (used_file_indexes == NULL)
7097 return;
7098 for (i = used_file_argc + 1; i < global_argc; ++i)
7099 if (STRCMP(global_argv[i], name) == 0)
7100 {
7101 used_file_argc = i;
7102 used_file_indexes[used_file_count++] = i;
7103 break;
7104 }
7105 used_file_literal = literal;
7106 used_file_full_path = full_path;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007107 used_file_diff_mode = diff_mode;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007108}
7109
7110/*
7111 * Remember the length of the argument list as it was. If it changes then we
7112 * leave it alone when 'encoding' is set.
7113 */
7114 void
7115set_alist_count(void)
7116{
7117 used_alist_count = GARGCOUNT;
7118}
7119
7120/*
7121 * Fix the encoding of the command line arguments. Invoked when 'encoding'
7122 * has been changed while starting up. Use the UCS-2 command line arguments
7123 * and convert them to 'encoding'.
7124 */
7125 void
7126fix_arg_enc(void)
7127{
7128 int i;
7129 int idx;
7130 char_u *str;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007131 int *fnum_list;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007132
Bram Moolenaar0f873732019-12-05 20:28:46 +01007133 // Safety checks:
7134 // - if argument count differs between the wide and non-wide argument
7135 // list, something must be wrong.
7136 // - the file name arguments must have been located.
7137 // - the length of the argument list wasn't changed by the user.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007138 if (global_argc != nArgsW
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007139 || ArglistW == NULL
7140 || used_file_indexes == NULL
7141 || used_file_count == 0
7142 || used_alist_count != GARGCOUNT)
7143 return;
7144
Bram Moolenaar0f873732019-12-05 20:28:46 +01007145 // Remember the buffer numbers for the arguments.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007146 fnum_list = ALLOC_MULT(int, GARGCOUNT);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007147 if (fnum_list == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007148 return; // out of memory
Bram Moolenaar86b68352004-12-27 21:59:20 +00007149 for (i = 0; i < GARGCOUNT; ++i)
7150 fnum_list[i] = GARGLIST[i].ae_fnum;
7151
Bram Moolenaar0f873732019-12-05 20:28:46 +01007152 // Clear the argument list. Make room for the new arguments.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007153 alist_clear(&global_alist);
7154 if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007155 return; // out of memory
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007156
7157 for (i = 0; i < used_file_count; ++i)
7158 {
7159 idx = used_file_indexes[i];
Bram Moolenaar36f692d2008-11-20 16:10:17 +00007160 str = utf16_to_enc(ArglistW[idx], NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007161 if (str != NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007162 {
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007163 int literal = used_file_literal;
7164
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007165#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +01007166 // When using diff mode may need to concatenate file name to
7167 // directory name. Just like it's done in main().
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007168 if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0
7169 && !mch_isdir(alist_name(&GARGLIST[0])))
7170 {
7171 char_u *r;
7172
7173 r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE);
7174 if (r != NULL)
7175 {
7176 vim_free(str);
7177 str = r;
7178 }
7179 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007180#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 // Re-use the old buffer by renaming it. When not using literal
7182 // names it's done by alist_expand() below.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007183 if (used_file_literal)
7184 buf_set_name(fnum_list[i], str);
7185
Bram Moolenaar0f873732019-12-05 20:28:46 +01007186 // Check backtick literal. backtick literal is already expanded in
7187 // main.c, so this part add str as literal.
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007188 if (literal == FALSE)
7189 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +02007190 size_t len = STRLEN(str);
7191
Bram Moolenaar39d21e32017-08-05 23:09:31 +02007192 if (len > 2 && *str == '`' && *(str + len - 1) == '`')
7193 literal = TRUE;
7194 }
7195 alist_add(&global_alist, str, literal ? 2 : 0);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007196 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007197 }
7198
7199 if (!used_file_literal)
7200 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007201 // Now expand wildcards in the arguments.
7202 // Temporarily add '(' and ')' to 'isfname'. These are valid
7203 // filename characters but are excluded from 'isfname' to make
7204 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
7205 // Also, unset wildignore to not be influenced by this option.
7206 // The arguments specified in command-line should be kept even if
7207 // encoding options were changed.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007208 do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007209 do_cmdline_cmd((char_u *)":let SaVe_WIG = &wig|set wig=");
Bram Moolenaar86b68352004-12-27 21:59:20 +00007210 alist_expand(fnum_list, used_alist_count);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007211 do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF");
Bram Moolenaar20586cb2018-03-08 22:03:14 +01007212 do_cmdline_cmd((char_u *)":let &wig = SaVe_WIG|unlet SaVe_WIG");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007213 }
7214
Bram Moolenaar0f873732019-12-05 20:28:46 +01007215 // If wildcard expansion failed, we are editing the first file of the
7216 // arglist and there is no file name: Edit the first argument now.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007217 if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL)
7218 {
7219 do_cmdline_cmd((char_u *)":rewind");
7220 if (GARGCOUNT == 1 && used_file_full_path)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007221 (void)vim_chdirfile(alist_name(&GARGLIST[0]), "drop");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007222 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007223
7224 set_alist_count();
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007225}
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007226
7227 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007228mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007229{
7230 char_u *envbuf;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007231 WCHAR *p;
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007232
Bram Moolenaar964b3742019-05-24 18:54:09 +02007233 envbuf = alloc(STRLEN(var) + STRLEN(value) + 2);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007234 if (envbuf == NULL)
7235 return -1;
7236
7237 sprintf((char *)envbuf, "%s=%s", var, value);
7238
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007239 p = enc_to_utf16(envbuf, NULL);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007240
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007241 vim_free(envbuf);
7242 if (p == NULL)
7243 return -1;
7244 _wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007245#ifdef libintl_wputenv
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007246 libintl_wputenv(p);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007247#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007248 // Unlike Un*x systems, we can free the string for _wputenv().
7249 vim_free(p);
Bram Moolenaar7c23d1d2017-02-01 13:14:16 +01007250
7251 return 0;
7252}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007253
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007254/*
7255 * Support for 256 colors and 24-bit colors was added in Windows 10
7256 * version 1703 (Creators update).
7257 */
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007258#define VTP_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 15063)
7259
7260/*
7261 * Support for pseudo-console (ConPTY) was added in windows 10
Bram Moolenaar57da6982019-09-13 22:30:11 +02007262 * version 1809 (October 2018 update).
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007263 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007264#define CONPTY_FIRST_SUPPORT_BUILD MAKE_VER(10, 0, 17763)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007265
7266/*
7267 * ConPTY differences between versions, need different logic.
7268 * version 1903 (May 2019 update).
7269 */
7270#define CONPTY_1903_BUILD MAKE_VER(10, 0, 18362)
7271
7272/*
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007273 * version 1909 (November 2019 update).
7274 */
7275#define CONPTY_1909_BUILD MAKE_VER(10, 0, 18363)
7276
7277/*
Bram Moolenaar57da6982019-09-13 22:30:11 +02007278 * Confirm until this version. Also the logic changes.
7279 * insider preview.
7280 */
Bram Moolenaar4c063dd2019-10-04 21:29:12 +02007281#define CONPTY_INSIDER_BUILD MAKE_VER(10, 0, 18995)
Bram Moolenaar57da6982019-09-13 22:30:11 +02007282
7283/*
7284 * Not stable now.
7285 */
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007286#define CONPTY_STABLE_BUILD MAKE_VER(10, 0, 32767) // T.B.D.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007287
7288 static void
7289vtp_flag_init(void)
7290{
7291 DWORD ver = get_build_number();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007292#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007293 DWORD mode;
7294 HANDLE out;
7295
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007296# ifdef VIMDLL
7297 if (!gui.in_use)
7298# endif
7299 {
7300 out = GetStdHandle(STD_OUTPUT_HANDLE);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007301
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007302 vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
7303 GetConsoleMode(out, &mode);
7304 mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
7305 if (SetConsoleMode(out, mode) == 0)
7306 vtp_working = 0;
7307 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007308#endif
7309
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007310 if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007311 conpty_working = 1;
7312 if (ver >= CONPTY_STABLE_BUILD)
7313 conpty_stable = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007314
Bram Moolenaar57da6982019-09-13 22:30:11 +02007315 if (ver <= CONPTY_INSIDER_BUILD)
7316 conpty_type = 3;
Bram Moolenaar36e7a822019-11-13 21:49:24 +01007317 if (ver <= CONPTY_1909_BUILD)
7318 conpty_type = 2;
Bram Moolenaar57da6982019-09-13 22:30:11 +02007319 if (ver <= CONPTY_1903_BUILD)
7320 conpty_type = 2;
7321 if (ver < CONPTY_FIRST_SUPPORT_BUILD)
7322 conpty_type = 1;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007323}
7324
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007325#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007326
7327 static void
7328vtp_init(void)
7329{
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007330 HMODULE hKerneldll;
7331 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007332# ifdef FEAT_TERMGUICOLORS
7333 COLORREF fg, bg;
7334# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007335
Bram Moolenaar0f873732019-12-05 20:28:46 +01007336 // Use functions supported from Vista
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007337 hKerneldll = GetModuleHandle("kernel32.dll");
7338 if (hKerneldll != NULL)
7339 {
7340 pGetConsoleScreenBufferInfoEx =
7341 (PfnGetConsoleScreenBufferInfoEx)GetProcAddress(
7342 hKerneldll, "GetConsoleScreenBufferInfoEx");
7343 pSetConsoleScreenBufferInfoEx =
7344 (PfnSetConsoleScreenBufferInfoEx)GetProcAddress(
7345 hKerneldll, "SetConsoleScreenBufferInfoEx");
7346 if (pGetConsoleScreenBufferInfoEx != NULL
7347 && pSetConsoleScreenBufferInfoEx != NULL)
7348 has_csbiex = TRUE;
7349 }
7350
7351 csbi.cbSize = sizeof(csbi);
7352 if (has_csbiex)
7353 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007354 save_console_bg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_bg];
7355 save_console_fg_rgb = (guicolor_T)csbi.ColorTable[g_color_index_fg];
Bram Moolenaardf543822020-01-30 11:53:59 +01007356 store_console_bg_rgb = save_console_bg_rgb;
7357 store_console_fg_rgb = save_console_fg_rgb;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007358
7359# ifdef FEAT_TERMGUICOLORS
7360 bg = (COLORREF)csbi.ColorTable[g_color_index_bg];
7361 fg = (COLORREF)csbi.ColorTable[g_color_index_fg];
7362 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7363 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7364 default_console_color_bg = bg;
7365 default_console_color_fg = fg;
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01007366# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007367
7368 set_console_color_rgb();
7369}
7370
7371 static void
7372vtp_exit(void)
7373{
Bram Moolenaardf543822020-01-30 11:53:59 +01007374 restore_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007375}
7376
7377 static int
7378vtp_printf(
7379 char *format,
7380 ...)
7381{
7382 char_u buf[100];
7383 va_list list;
7384 DWORD result;
7385
7386 va_start(list, format);
7387 vim_vsnprintf((char *)buf, 100, (char *)format, list);
7388 va_end(list);
7389 WriteConsoleA(g_hConOut, buf, (DWORD)STRLEN(buf), &result, NULL);
7390 return (int)result;
7391}
7392
7393 static void
7394vtp_sgr_bulk(
7395 int arg)
7396{
7397 int args[1];
7398
7399 args[0] = arg;
7400 vtp_sgr_bulks(1, args);
7401}
7402
7403 static void
7404vtp_sgr_bulks(
7405 int argc,
7406 int *args
7407)
7408{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007409 // 2('\033[') + 4('255.') * 16 + NUL
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007410 char_u buf[2 + (4 * 16) + 1];
7411 char_u *p;
7412 int i;
7413
7414 p = buf;
7415 *p++ = '\033';
7416 *p++ = '[';
7417
7418 for (i = 0; i < argc; ++i)
7419 {
7420 p += vim_snprintf((char *)p, 4, "%d", args[i] & 0xff);
7421 *p++ = ';';
7422 }
7423 p--;
7424 *p++ = 'm';
7425 *p = NUL;
7426 vtp_printf((char *)buf);
7427}
7428
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007429# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007430 static int
7431ctermtoxterm(
7432 int cterm)
7433{
Bram Moolenaar9894e392018-05-05 14:29:06 +02007434 char_u r, g, b, idx;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007435
7436 cterm_color2rgb(cterm, &r, &g, &b, &idx);
7437 return (((int)r << 16) | ((int)g << 8) | (int)b);
7438}
Bram Moolenaar8a938af2018-05-01 17:30:41 +02007439# endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007440
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007441 static void
7442set_console_color_rgb(void)
7443{
7444# ifdef FEAT_TERMGUICOLORS
7445 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
Bram Moolenaara050b942019-12-02 21:35:31 +01007446 guicolor_T fg, bg;
7447 int ctermfg, ctermbg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007448
7449 if (!USE_VTP)
7450 return;
7451
Bram Moolenaara050b942019-12-02 21:35:31 +01007452 get_default_console_color(&ctermfg, &ctermbg, &fg, &bg);
7453
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007454 fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
7455 bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);
7456
7457 csbi.cbSize = sizeof(csbi);
7458 if (has_csbiex)
7459 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7460
7461 csbi.cbSize = sizeof(csbi);
7462 csbi.srWindow.Right += 1;
7463 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007464 store_console_bg_rgb = csbi.ColorTable[g_color_index_bg];
7465 store_console_fg_rgb = csbi.ColorTable[g_color_index_fg];
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007466 csbi.ColorTable[g_color_index_bg] = (COLORREF)bg;
7467 csbi.ColorTable[g_color_index_fg] = (COLORREF)fg;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007468 if (has_csbiex)
7469 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7470# endif
7471}
7472
Bram Moolenaara050b942019-12-02 21:35:31 +01007473# if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
7474 void
7475get_default_console_color(
7476 int *cterm_fg,
7477 int *cterm_bg,
7478 guicolor_T *gui_fg,
7479 guicolor_T *gui_bg)
7480{
7481 int id;
7482 guicolor_T guifg = INVALCOLOR;
7483 guicolor_T guibg = INVALCOLOR;
7484 int ctermfg = 0;
7485 int ctermbg = 0;
7486
7487 id = syn_name2id((char_u *)"Normal");
7488 if (id > 0 && p_tgc)
7489 syn_id2colors(id, &guifg, &guibg);
7490 if (guifg == INVALCOLOR)
7491 {
7492 ctermfg = -1;
7493 if (id > 0)
7494 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7495 guifg = ctermfg != -1 ? ctermtoxterm(ctermfg)
7496 : default_console_color_fg;
7497 cterm_normal_fg_gui_color = guifg;
7498 ctermfg = ctermfg < 0 ? 0 : ctermfg;
7499 }
7500 if (guibg == INVALCOLOR)
7501 {
7502 ctermbg = -1;
7503 if (id > 0)
7504 syn_id2cterm_bg(id, &ctermfg, &ctermbg);
7505 guibg = ctermbg != -1 ? ctermtoxterm(ctermbg)
7506 : default_console_color_bg;
7507 cterm_normal_bg_gui_color = guibg;
7508 ctermbg = ctermbg < 0 ? 0 : ctermbg;
7509 }
7510
7511 *cterm_fg = ctermfg;
7512 *cterm_bg = ctermbg;
7513 *gui_fg = guifg;
7514 *gui_bg = guibg;
7515}
7516# endif
7517
Bram Moolenaardf543822020-01-30 11:53:59 +01007518/*
7519 * Set the console colors to the original colors or the last set colors.
7520 */
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007521 static void
7522reset_console_color_rgb(void)
7523{
7524# ifdef FEAT_TERMGUICOLORS
7525 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7526
7527 csbi.cbSize = sizeof(csbi);
7528 if (has_csbiex)
7529 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7530
7531 csbi.cbSize = sizeof(csbi);
7532 csbi.srWindow.Right += 1;
7533 csbi.srWindow.Bottom += 1;
Bram Moolenaardf543822020-01-30 11:53:59 +01007534 csbi.ColorTable[g_color_index_bg] = (COLORREF)store_console_bg_rgb;
7535 csbi.ColorTable[g_color_index_fg] = (COLORREF)store_console_fg_rgb;
7536 if (has_csbiex)
7537 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7538# endif
7539}
7540
7541/*
7542 * Set the console colors to the original colors.
7543 */
7544 static void
7545restore_console_color_rgb(void)
7546{
7547# ifdef FEAT_TERMGUICOLORS
7548 DYN_CONSOLE_SCREEN_BUFFER_INFOEX csbi;
7549
7550 csbi.cbSize = sizeof(csbi);
7551 if (has_csbiex)
7552 pGetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7553
7554 csbi.cbSize = sizeof(csbi);
7555 csbi.srWindow.Right += 1;
7556 csbi.srWindow.Bottom += 1;
Bram Moolenaarf6ceaf12018-08-30 17:47:05 +02007557 csbi.ColorTable[g_color_index_bg] = (COLORREF)save_console_bg_rgb;
7558 csbi.ColorTable[g_color_index_fg] = (COLORREF)save_console_fg_rgb;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007559 if (has_csbiex)
7560 pSetConsoleScreenBufferInfoEx(g_hConOut, &csbi);
7561# endif
7562}
7563
7564 void
7565control_console_color_rgb(void)
7566{
7567 if (USE_VTP)
7568 set_console_color_rgb();
7569 else
7570 reset_console_color_rgb();
7571}
7572
7573 int
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007574use_vtp(void)
7575{
7576 return USE_VTP;
7577}
7578
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007579 int
7580is_term_win32(void)
7581{
7582 return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
7583}
7584
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007585 int
7586has_vtp_working(void)
7587{
7588 return vtp_working;
7589}
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007590
Bram Moolenaar6902c0e2019-02-16 14:07:37 +01007591#endif
7592
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007593 int
7594has_conpty_working(void)
7595{
7596 return conpty_working;
7597}
7598
7599 int
Bram Moolenaar57da6982019-09-13 22:30:11 +02007600get_conpty_type(void)
7601{
7602 return conpty_type;
7603}
7604
7605 int
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007606is_conpty_stable(void)
7607{
7608 return conpty_stable;
7609}
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007610
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007611#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007612 void
7613resize_console_buf(void)
7614{
7615 CONSOLE_SCREEN_BUFFER_INFO csbi;
7616 COORD coord;
7617 SMALL_RECT newsize;
7618
7619 if (GetConsoleScreenBufferInfo(g_hConOut, &csbi))
7620 {
7621 coord.X = SRWIDTH(csbi.srWindow);
7622 coord.Y = SRHEIGHT(csbi.srWindow);
7623 SetConsoleScreenBufferSize(g_hConOut, coord);
7624
7625 newsize.Left = 0;
7626 newsize.Top = 0;
7627 newsize.Right = coord.X - 1;
7628 newsize.Bottom = coord.Y - 1;
7629 SetConsoleWindowInfo(g_hConOut, TRUE, &newsize);
7630
7631 SetConsoleScreenBufferSize(g_hConOut, coord);
7632 }
7633}
7634#endif